Prefer canonicalized path when guessing tools from GHC path
commitf78d2aaaa76b527f4f03032f256cf183c4ef8f5d
authorJulian Ospald <hasufell@posteo.de>
Wed, 12 May 2021 08:12:24 +0000 (12 10:12 +0200)
committerJulian Ospald <hasufell@posteo.de>
Wed, 12 May 2021 10:36:10 +0000 (12 12:36 +0200)
tree473c3c50ce300725727ed2f1def05839821d6a60
parent4f8aeb2c8a0a3638e1af887dc869a17e291c8329
Prefer canonicalized path when guessing tools from GHC path

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:
  - https://github.com/haskell/cabal/issues/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.
Cabal/src/Distribution/Simple/GHC.hs
cabal-testsuite/PackageTests/GhcPkgGuess/SameDirectory/setup.cabal.out
cabal-testsuite/PackageTests/GhcPkgGuess/SameDirectory/setup.out
cabal-testsuite/PackageTests/GhcPkgGuess/SameDirectoryGhcVersion/setup.cabal.out
cabal-testsuite/PackageTests/GhcPkgGuess/SameDirectoryGhcVersion/setup.out
cabal-testsuite/PackageTests/GhcPkgGuess/SameDirectoryVersion/setup.cabal.out
cabal-testsuite/PackageTests/GhcPkgGuess/SameDirectoryVersion/setup.out