Skip to content

List each search path entry once in mkProgramDb - #12551

Closed
ulysses4ever wants to merge 2 commits into
haskell:masterfrom
ulysses4ever:configure/no-duplicate-search-path
Closed

ulysses4ever wants to merge 2 commits into
haskell:masterfrom
ulysses4ever:configure/no-duplicate-search-path

Conversation

@ulysses4ever

Copy link
Copy Markdown
Collaborator

configure searches each directory of the program search path once again, and the programs it configures get a PATH that lists each directory once. For #12544. Below is the longer story from your friendly AI engine (Opus 5.5).


Since #9762 (Cabal 3.14), mkProgramDb puts the initial search path, which has the directories of the build-tool-depends executables, in front of the search path that prependProgramSearchPath made from it, so that path appears twice: [ProgramSearchPathDefault, ProgramSearchPathDefault] when there are no extra directories. This has two effects:

  • Every search for a program that is not installed goes through PATH twice. configure does several of those in every component, for the builtin programs that are not installed (Programs are configured needlessly often #12544).
  • Every configured program, ghc included, runs with a PATH that lists each directory twice. In cabal repl, getEnv "PATH" has 88 entries for a PATH of 44.

This keeps the first occurrence of each entry, so lookups find the same programs in the same order.

Reconfiguring the 41 components of a package of one-module sublibraries after a change to PATH (on Linux, GHC 9.10.3):

PATH of 44 entries PATH of 444 entries setup-config files, 444 entries
master 5.65 s 27.3–28.8 s, 19–23 GB allocated 51 MB
this PR 5.5 s 19.3–21.4 s, 13–17 GB allocated 34 MB

Testing

PathNoDuplicates puts a directory in front of PATH and checks that cabal repl sees it once; with master's cabal, it sees it twice. The tests about build tools and extra-prog-path pass locally: BuildToolDepends, BuildToolDependsExternal, BuildToolDependsInternalMissing, BuildTools/*, ExtraProgPath, ExtraProgPathGlobal, ExtraProgPathLocal, IgnoreBuildTools, MultiRepl/BuildToolDepends and PathRecomp.


QA Notes

Put a directory in front of PATH, start cabal repl in a package, and evaluate System.Environment.getEnv "PATH": the directory appears once. Since cabal 3.14 it has appeared twice (checked with 3.16.0.0).

🤖 Generated with Claude Code

https://claude.ai/code/session_0176vAefrw8jPKXdh9h1wKuj

mkProgramDb puts the initial search path, which has the directories of
the build-tool-depends executables, in front of the search path that
prependProgramSearchPath made from it, so since haskell#9762 that path appears
twice. Every search for a program that is not installed went through
PATH twice, and every configured program, ghc included, ran with a PATH
that listed each directory twice.

Keep the first occurrence of each entry, so that lookups find the same
programs in the same order. Found while looking into haskell#12544.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
Claude-Session: https://claude.ai/code/session_0176vAefrw8jPKXdh9h1wKuj
Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
Claude-Session: https://claude.ai/code/session_0176vAefrw8jPKXdh9h1wKuj
@sheaf

sheaf commented Nov 9, 2026

Copy link
Copy Markdown
Collaborator

nub seems to be a band-aid solution here. The real question is: why do these paths end up duplicated in the first place?

84c30c2 made it so that build-tool-depends are put first in the program search paths, but in a way which causes those search paths to be duplicated.

There are three components to the program search paths: build-tool-depends directories (B), then extra-prog-path (E) and then PATH (P). It seems to me the correct order is [B, E, P]: first build-tools explicitly solved for (this is what #9756 wants I think), and then extra-prog-path as a way to override the generic "fallback to PATH".

I think we should more deliberately pass these search paths in this order, both in-library and for the external Setup method, in a way where we don't create duplicates in the first place. I think the code today is so confused/confusing that it needs to be tidied up first.

@sheaf

sheaf commented Nov 9, 2026

Copy link
Copy Markdown
Collaborator

I'm working on another PR to solve this in a more principled way. Here's the note I am drafting to finally bring a bit of clarity to this area of the codebase:

{- Note [Program search paths]
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
Within Cabal, configuration (of a package or of a single component within a
package) produces a program search path (see 'progSearchPath'). It is used:

  1. When configuring a program, to find its location
     (see 'configureUnconfiguredProgram').
  2. When running a program: 'programOverrideEnv' modifies the environment a
     program runs in so that it gets a PATH that reflects the search path we
     used to locate the program when we configured it.

Program lookup proceeds as follows:

  O. Use the explicit override, if there was one (e.g. "--with-happy=<path>").

     The override is given by 'configProgramPaths' (one entry per --with-PROG flag)
     and recorded in the 'ProgramDb' by 'userSpecifyPaths'.

  C. If the program comes bundled with the compiler, then use the location of
     the compiler to find it (e.g. find `ghc-pkg` next to where we found `ghc`).
     See 'guessToolFromGhcPath'.

  If none of these cases above apply, we fall back to the program search path,
  which must respect the following order:

    B. Build tools specified in 'build-tool-depends'. This includes both
       internal and external build tools.
    E. Search paths given with --extra-prog-path, e.g. '--extra-prog-path=/opt/llvm/bin'.
    P. The PATH environment variable.

We arrange this as follows:

  1. Cabal puts internal build tools at the front, with 'addInternalBuildTools'.
     (This is a bit of a special case, as it happens when building, not at
     configure time.)
  2. The rest of B & E is seen by Cabal as a single list that it gets given
     (see 'configProgramPathExtra').

     It is the responsibility of callers (e.g. cabal-install) to construct this
     list in a way that respects the order above, for example by putting the
     directories of external build tools first in this list.
  3. Cabal falls back to P when lookup in 'configProgramPathExtra' fails.

B takes precedence over both E and P to ensure that build tools that were solved
for are used (e.g. to ensure the build tool has the exact version requested by
the user, as in #9756).
E takes precedence over P to allow users to customise program locations.
-}

@ulysses4ever

Copy link
Copy Markdown
Collaborator Author

@sheaf i tried to replace nub, take a look. But if you have a better idea Im happy to hold off with this one. There’s also #12552

@sheaf

sheaf commented Nov 9, 2026

Copy link
Copy Markdown
Collaborator

@sheaf i tried to replace nub, take a look. But if you have a better idea Im happy to hold off with this one. There’s also #12552

Did you mean to push? I don't see anything yet.

@sheaf

sheaf commented Nov 9, 2026

Copy link
Copy Markdown
Collaborator

I put up my attempt in #12556.

@ulysses4ever

Copy link
Copy Markdown
Collaborator Author

Closing in favour of #12556, which stops the search path from being duplicated in the first place (mkProgramDb no longer prepends the initial search path), and whose ProgramSearchPath test checks that ghc gets each directory once. The nub I mentioned replacing is in #12552 (one datadir variable per package, now without ordNub), which #12556 doesn't touch.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants