Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Make ghc-pkg discoverable from ghc executable #7390

Open
bgamari opened this issue May 11, 2021 · 9 comments
Open

Make ghc-pkg discoverable from ghc executable #7390

bgamari opened this issue May 11, 2021 · 9 comments

Comments

@bgamari
Copy link
Contributor

bgamari commented May 11, 2021

Describe the bug
Currently there is no robust way to locate the ghc-pkg executable corresponding to a particular compiler. For this reason, Cabal currently implements a variety of heuristics to do the same. However, these can end up failing, in particular when the executable name is version-suffixed.

@hasufell and I discussed this some months ago after a ghc-up user encountered this problem, which resulted in GHC #18807. To address this I proposed that ghc --info should report the path to the corresponding ghc-pkg executable. This solves the problem nicely since Cabal already must query --info when configuring a package.

I have put together a very straightforward GHC implementation as !4214. I have also written a quick patch implementing the Cabal support.

My sense is that this is the right solution to the problem. However, I'm also happy to hear alternatives. If there are no objections, I will backport to 8.10.5, 9.0.2, and 9.2.1 such that future users won't encounter this issue in the future.

@phadej
Copy link
Collaborator

phadej commented May 11, 2021

Does this supersede/obsoletes #6648 or are both valuable?

@hasufell
Copy link
Member

hasufell commented May 11, 2021

I'll try the patch out tomorrow in conjunction with the GHC patch.

I also noticed that we could investigate another workaround in the following function

-- | Given something like /usr/local/bin/ghc-6.6.1(.exe) we try and find a
-- corresponding ghc-pkg, we try looking for both a versioned and unversioned
-- ghc-pkg in the same dir, that is:
--
-- > /usr/local/bin/ghc-pkg-ghc-6.6.1(.exe)
-- > /usr/local/bin/ghc-pkg-6.6.1(.exe)
-- > /usr/local/bin/ghc-pkg(.exe)
--
guessGhcPkgFromGhcPath :: ConfiguredProgram
-> Verbosity -> ProgramSearchPath
-> IO (Maybe (FilePath, [FilePath]))
guessGhcPkgFromGhcPath = guessToolFromGhcPath ghcPkgProgram

What if we canonicalize first and then effectively follow the symlink if it is one... that would make us usually end up in the internal ghc directory, which should always work. No?

The advantage of this would be that it's a backwards compatible fix that doesn't need a GHC patch.

@rvl do you know if this could break a nix use case?

@phadej
Copy link
Collaborator

phadej commented May 11, 2021

@hasufell, that would work. On my machine

Prelude System.Directory> canonicalizePath "/opt/ghc/bin/ghc"
"/opt/ghc/8.6.5/bin/ghc-8.6.5"

Prelude System.Directory> canonicalizePath "/opt/ghc/bin/ghc"
"/opt/ghc/8.6.5/bin/ghc-8.6.5"

I think both ghc --info and your canonicalizePath proposal will break nixpkgs if they (re)wrap ghc-pkg and rely on $PATH, yet if they pass --with-hc-pkg explicitly, then it shouldn't break.

@bgamari
Copy link
Contributor Author

bgamari commented May 11, 2021

Does this supersede/obsoletes #6648 or are both valuable?

I think they are essentially duplicates. I just didn't find #6648 until I had already opened this one.

hasufell added a commit to hasufell/cabal that referenced this issue May 12, 2021
Motivation
----------
Often times, the user facing `ghc` binary is
symlinked by other forces, such as the package manager,
tooling like ghcup etc. As such, the naming convention
(version suffix in particular) may not align with the
assumptions made in Cabal and it may find an incorrect ghc-pkg.

See:
  - haskell#7390
  - https://gitlab.haskell.org/ghc/ghc/-/issues/18807
  - https://gitlab.haskell.org/haskell/ghcup-hs/-/issues/73

Solution
--------
Guessing the ghc-pkg path is already a hack and will be solved
more appropriately in the future, see
  - https://gitlab.haskell.org/ghc/ghc/-/merge_requests/4214
  - https://gitlab.haskell.org/ghc/ghc/-/snippets/2710
These patches will solve the issue for future GHC versions.

As such, this patch provides a workaround for
older, already existing GHC versions by first always
following the symbolic link of the ghc binary (if it is one)
and prefering its target directory as the guess lookup
location.

Rationale
---------
The canonicalized path of the ghc binary usually points to the
bin/ directory unpacked from a bindist, which is less likely to be
tampered with by distributions and tools. As such, prefering the
canoncialized path should get us more robust results.
hasufell added a commit to hasufell/cabal that referenced this issue May 12, 2021
Motivation
----------
Often times, the user facing `ghc` binary is
symlinked by other forces, such as the package manager,
tooling like ghcup etc. As such, the naming convention
(version suffix in particular) may not align with the
assumptions made in Cabal and it may find an incorrect ghc-pkg.

See:
  - haskell#7390
  - https://gitlab.haskell.org/ghc/ghc/-/issues/18807
  - https://gitlab.haskell.org/haskell/ghcup-hs/-/issues/73

Solution
--------
Guessing the ghc-pkg path is already a hack and will be solved
more appropriately in the future, see
  - https://gitlab.haskell.org/ghc/ghc/-/merge_requests/4214
  - https://gitlab.haskell.org/ghc/ghc/-/snippets/2710
These patches will solve the issue for future GHC versions.

As such, this patch provides a workaround for
older, already existing GHC versions by first always
following the symbolic link of the ghc binary (if it is one)
and prefering its target directory as the guess lookup
location.

Rationale
---------
The canonicalized path of the ghc binary usually points to the
bin/ directory unpacked from a bindist, which is less likely to be
tampered with by distributions and tools. As such, prefering the
canoncialized path should get us more robust results.
ulysses4ever pushed a commit to ulysses4ever/cabal that referenced this issue May 26, 2021
Motivation
----------
Often times, the user facing `ghc` binary is
symlinked by other forces, such as the package manager,
tooling like ghcup etc. As such, the naming convention
(version suffix in particular) may not align with the
assumptions made in Cabal and it may find an incorrect ghc-pkg.

See:
  - haskell#7390
  - https://gitlab.haskell.org/ghc/ghc/-/issues/18807
  - https://gitlab.haskell.org/haskell/ghcup-hs/-/issues/73

Solution
--------
Guessing the ghc-pkg path is already a hack and will be solved
more appropriately in the future, see
  - https://gitlab.haskell.org/ghc/ghc/-/merge_requests/4214
  - https://gitlab.haskell.org/ghc/ghc/-/snippets/2710
These patches will solve the issue for future GHC versions.

As such, this patch provides a workaround for
older, already existing GHC versions by first always
following the symbolic link of the ghc binary (if it is one)
and prefering its target directory as the guess lookup
location.

Rationale
---------
The canonicalized path of the ghc binary usually points to the
bin/ directory unpacked from a bindist, which is less likely to be
tampered with by distributions and tools. As such, prefering the
canoncialized path should get us more robust results.
mergify bot pushed a commit that referenced this issue Aug 11, 2021
Motivation
----------
Often times, the user facing `ghc` binary is
symlinked by other forces, such as the package manager,
tooling like ghcup etc. As such, the naming convention
(version suffix in particular) may not align with the
assumptions made in Cabal and it may find an incorrect ghc-pkg.

See:
  - #7390
  - https://gitlab.haskell.org/ghc/ghc/-/issues/18807
  - https://gitlab.haskell.org/haskell/ghcup-hs/-/issues/73

Solution
--------
Guessing the ghc-pkg path is already a hack and will be solved
more appropriately in the future, see
  - https://gitlab.haskell.org/ghc/ghc/-/merge_requests/4214
  - https://gitlab.haskell.org/ghc/ghc/-/snippets/2710
These patches will solve the issue for future GHC versions.

As such, this patch provides a workaround for
older, already existing GHC versions by first always
following the symbolic link of the ghc binary (if it is one)
and prefering its target directory as the guess lookup
location.

Rationale
---------
The canonicalized path of the ghc binary usually points to the
bin/ directory unpacked from a bindist, which is less likely to be
tampered with by distributions and tools. As such, prefering the
canoncialized path should get us more robust results.

(cherry picked from commit f78d2aa)
hasufell added a commit to hasufell/cabal-extras that referenced this issue Oct 1, 2021
@Mikolaj
Copy link
Member

Mikolaj commented Aug 23, 2023

How can we help to move this forward? @bgamari, are your patches alive?

@andreabedini
Copy link
Collaborator

Just go out of curiosity, in what cases ghc-pkg path is not the the same as ghc plus -pkg?

@phadej
Copy link
Collaborator

phadej commented Aug 24, 2023

Just go out of curiosity, in what cases ghc-pkg path is not the the same as ghc plus -pkg?

  • ghc-9.6.2 and ghc-pkg-9.6.2
  • ghc-9.8-alpha1 and ghc-pkg-9.8-alpha1

Yes, one could look for suffix etc, but that is a hack (heuristic in OP) which has shown to be unreliable.

@phadej
Copy link
Collaborator

phadej commented Aug 24, 2023

FWIW, ghc should also make other stuff discoverable through --info, e.g. the haddock. (It's very hard to build docs with not bundled haddock, I doubt non-haddock-developers are actually ever doing so).

@andreabedini
Copy link
Collaborator

Thank you @phadej

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

No branches or pull requests

6 participants