Recent

Author Topic: PSA: Silent conflict between package name and unit name  (Read 905 times)

gcscaglia

  • Newbie
  • Posts: 2
PSA: Silent conflict between package name and unit name
« on: November 24, 2021, 05:52:27 pm »
This either a PSA or a bug report; I'm not sure which tbh.

Suppose you have the following file structure:

Code: [Select]
Pkg/
  MyCode.lpk
  Units/
    MyCode.pas

This will lead to confusing "Identifier not found" error messages. Worse still, the IDE will be able to find and even point you to those identifiers declarations, but the compiler will still complain they do not exists.

This behavior arises because, "under" the hood, an empty unit "Pkg/MyCode.pas" (note the path and extension) is created to pull in all units from the MyCode package. Since both "Pkg/MyCode.pas" and "Pkg/Units/MyCode.pas" have the same unit identifier and are compiled into the same output directory (under Pkg/lib/), the compiler picks up the empty unit (which is compiled last).

I do realize this is an unfortunate set of coincidences, but it surely made me waste a good hour figuring it out. Would it be possible to at least add an warning should this situation occur? Or, ideally, remove the need for the empty unit in the first place?

wp

  • Hero Member
  • *****
  • Posts: 9025
Re: PSA: Silent conflict between package name and unit name
« Reply #1 on: November 24, 2021, 06:04:12 pm »
This either a PSA or a bug report; I'm not sure which tbh.
PSA?

it surely made me waste a good hour figuring it out.
This made you learn an elemental rule: never name a unit like a package, or to put it the other way round: never name a package such that a collision with a "normal" unit can occur. That's why packages often have something like "pkg" or "laz" in their name.
Mainly Lazarus trunk / fpc 3.2.0 / all 32-bit on Win-10, but many more...

gcscaglia

  • Newbie
  • Posts: 2
Re: PSA: Silent conflict between package name and unit name
« Reply #2 on: November 24, 2021, 06:20:47 pm »
PSA?

Public Service Announcement. It is just an cheeky way to say it is an advice to others.

That's why packages often have something like "pkg" or "laz" in their name.

Well, while now my lesson is learned, I fear I will not be the last person to suffer from this. This is not intuitive at all (where does it says creating an package creates an unit with the same name?).

Anyway, the suggestion for improvement is made; at very least I hope this will save somebody the same hour I lost here.

*edit: fix typo
« Last Edit: November 24, 2021, 06:23:02 pm by gcscaglia »

MarkMLl

  • Hero Member
  • *****
  • Posts: 3524
Re: PSA: Silent conflict between package name and unit name
« Reply #3 on: November 24, 2021, 07:53:08 pm »
PSA?

Public Service Announcement. It is just an cheeky way to say it is an advice to others.

I'm used to that from CIX in the UK. Once you have a community of people that spans 30+ years there's /conventions/.

Quote
That's why packages often have something like "pkg" or "laz" in their name.

Well, while now my lesson is learned, I fear I will not be the last person to suffer from this. This is not intuitive at all (where does it says creating an package creates an unit with the same name?).

Anyway, the suggestion for improvement is made; at very least I hope this will save somebody the same hour I lost here.

I'm inclined to agree with you FWIW. If this /can/ be checked it /should/ be checked: if I wanted my software's robustness to be dependant on adherence to conventions I'd use C.

MarkMLl
MT+86 & Turbo Pascal v1 on CCP/M-86, multitasking with LAN & graphics in 128Kb.
Pet hate: people who boast about the size and sophistication of their computer.
GitHub repositories: https://github.com/MarkMLl?tab=repositories

 

TinyPortal © 2005-2018