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

cabal wrapper uses always global ghc in PATH #194

Closed
jneira opened this issue Jun 10, 2020 · 17 comments · Fixed by #282
Closed

cabal wrapper uses always global ghc in PATH #194

jneira opened this issue Jun 10, 2020 · 17 comments · Fixed by #282
Labels
can-workaround type: bug Something isn't working

Comments

@jneira
Copy link
Member

jneira commented Jun 10, 2020

PS D:\dev\ws\haskell\ghcide> ghc --version
The Glorious Glasgow Haskell Compilation System, version 8.8.3
PS D:\dev\ws\haskell\ghcide> ghcide --version
ghcide version: 0.2.0 (GHC: 8.6.5) (PATH: D:\bin\ghcide.exe)
PS D:\dev\ws\haskell\ghcide> cat .\cabal.project
packages: .

with-compiler: ghc-8.6.5
PS D:\dev\ws\haskell\ghcide> cabal clean
PS D:\dev\ws\haskell\ghcide> ghcide
ghcide version: 0.2.0 (GHC: 8.6.5) (PATH: D:\bin\ghcide.exe)
Ghcide setup tester in D:\dev\ws\haskell\ghcide.
Report bugs at https://github.com/digital-asset/ghcide/issues

Step 1/4: Finding files to test in D:\dev\ws\haskell\ghcide
Found 63 files

Step 2/4: Looking for hie.yaml files that control setup
Found 3 cradles

Step 3/4: Initializing the IDE

Step 4/4: Type checking the files
[INFO] Consulting the cradle for "D:\\dev\\ws\\haskell\\ghcide\\src\\Development\\IDE\\GHC\\WithDynFlags.hs"
> Resolving dependencies...
> Build profile: -w ghc-8.8.3 -O1
> In order, the following will be built (use -v for more details):
>  - ghcide-0.2.0 (lib) (first run)
> Configuring library for ghcide-0.2.0..
> Preprocessing library for ghcide-0.2.0..
File:     D:\dev\ws\haskell\ghcide\src\Development\IDE\GHC\WithDynFlags.hs
Hidden:   no
Range:    1:0-100001:0
Source:   compiler
Severity: DsError
Message:
  <command line>: cannot satisfy -package-id aeson-1.5.1.0-1e42a0cdbd4b065e4d6f1c804d63f35706230918:
  aeson-1.5.1.0-1e42a0cdbd4b065e4d6f1c804d63f35706230918 is unusable due to missing dependencies:
  attoparsec-0.13.2.4-04e8dfec95c5fe0d96babd7d76d0463f6955b1db base-4.13.0.0
  base-compat-b_-0.11.1-1ae618f5c56712e963c7c424698817f24ad5c97a bytestring-0.10.10.0
PS D:\dev\ws\haskell\ghcide> ghc --version
The Glorious Glasgow Haskell Compilation System, version 8.8.3
PS D:\dev\ws\haskell\ghcide> cat .\cabal.project
packages: .

with-compiler: ghc-8.6.5
PS D:\dev\ws\haskell\ghcide> cabal exec ghc -- --version
Resolving dependencies...
The Glorious Glasgow Haskell Compilation System, version 8.6.5
PS D:\dev\ws\haskell\ghcide> cabal exec ghc --with-compiler=ghc-8.8.3 -- --version
Resolving dependencies...
The Glorious Glasgow Haskell Compilation System, version 8.8.3
PS D:\dev\ws\haskell\ghcide>
  • so it is ignoring the original ghc used in by hie-bios (ghcPath here )

Possible solutions:

  • Use an intermmediate env var (HIE_BIOS_WRAPPER_GHC?) to inform the wrapper about the real ghc
  • Pass a new argument to the wrapper --hie-bios-ghc and use it, removing it before calling the real ghc
@jneira jneira changed the title windows cabal wrapper uses global ghc in PATH cabal wrapper uses always global ghc in PATH Jun 10, 2020
@jneira
Copy link
Member Author

jneira commented Jun 10, 2020

Another test, using directly the wrapper:

PS D:\dev\ws\haskell\ghcide> cat .\cabal.project
packages: .
with-compiler: ghc-8.6.5
PS D:\dev\ws\haskell\ghcide> cabal exec ghc -- --version
Resolving dependencies...
The Glorious Glasgow Haskell Compilation System, version 8.6.5
PS D:\dev\ws\haskell\ghcide> cabal --with-compiler=C:\Users\atrey\AppData\Local\hie-bios\wrapper-3496bab802428a4b095415cdb49e49be.exe exec ghc -- --version
Resolving dependencies...
The Glorious Glasgow Haskell Compilation System, version 8.8.3

@fendor
Copy link
Collaborator

fendor commented Jun 11, 2020

I can confirm the behaviour on linux, too.
The hypothesis seems to be correct!

Possible fixes:

  • Find the appropriate GHC version in Cradle.hs (via cabal-install-parsers?) and pass it via an environment variable to the wrapper.
  • Wait for cabal show-build-info to land (show-build-info (exe:cabal part plus tests) cabal#6241) since it will fix all our problems.
    • Document why it doesn't work and how to work around it.

Unless a lot of users complain about this issue, I am in favour of the latter, as it will be just a few months and solve the issue for good.
Any work-around will be code cruft very soon.

@jneira
Copy link
Member Author

jneira commented Jun 11, 2020

Fair enough, moreover, cabal action uses global ghc in two points:

  • the wrapper
  • when compiling the wrapper itself, it usually does not throw any error cause the wrapper is dead simple code but it would throw it if you are using cabal but you dont have ghc in global path (nix?)

@fendor
Copy link
Collaborator

fendor commented Jun 12, 2020

I think this issue is related: #148

@jneira
Copy link
Member Author

jneira commented Jul 30, 2021

A nice workaround if you use ghcup was mentioned by @hasufell in irc: open a terminal in the project dir root and do PATH=$(ghcup whereis -d ghc <ghc-ver>):$PATH && code .

@jneira jneira added can-workaround type: bug Something isn't working labels Jul 30, 2021
@hasufell
Copy link
Member

Wait for cabal show-build-info to land (show-build-info (exe:cabal part plus tests) haskell/cabal#6241) since it will fix all our problems.

That was one year ago...

@fendor
Copy link
Collaborator

fendor commented Jul 30, 2021

Back then it seemed like a reasonable expectation that it was going to be merged soon

@hasufell
Copy link
Member

Back then it seemed like a reasonable expectation that it was going to be merged soon

The dream
NicePng_crying-meme-png_1801723

@jneira
Copy link
Member Author

jneira commented Jul 30, 2021

A nice workaround if you use ghcup was mentioned by @hasufell in irc: open a terminal in the project dir root and do PATH=$(ghcup whereis -d ghc <ghc-ver>):$PATH && code .

if you don't have ghcup you can do simply PATH=$(cabal exec which -- ghc):$PATH && code .

@jneira
Copy link
Member Author

jneira commented Jul 30, 2021

Back then it seemed like a reasonable expectation that it was going to be merged soon

The dream

third time is the right time

@hasufell
Copy link
Member

hasufell commented Jul 30, 2021

A nice workaround if you use ghcup was mentioned by @hasufell in irc: open a terminal in the project dir root and do PATH=$(ghcup whereis -d ghc ):$PATH && code .

Here's a bash function for your .bashrc:

with_ghc() {
  local np=$(ghcup --offline whereis -d ghc $1 || { ghcup --cache install ghc $1 && ghcup whereis -d ghc $1 ;})
  if [ -e "${np}" ] ; then
    shift
    PATH="$np:$PATH" "$@"
  else
    >&2 echo "Cannot find or install GHC version $1"
    return 1
  fi
}

And for your fish config:

function with_ghc
  set --local np (ghcup --offline whereis -d ghc $argv[1] ; or begin ghcup --cache install ghc $argv[1] ; and ghcup whereis -d ghc $argv[1] ; end)
  if test -e "$np"
    PATH="$np:$PATH" $argv[2..-1]
  else
    echo "Cannot find or install GHC version $argv[1]" 1>&2
    return 1
  end
end

use:

with_ghc 8.10.5 sh -c 'echo $PATH'
with_ghc 8.10.5 cabal build
with_ghc 8.10.5 code .

Edit: I feel I just reimplemented stack.

@hasufell
Copy link
Member

hasufell commented Sep 9, 2021

Since the cabal patch is merged, I guess this can move forward

@fendor
Copy link
Collaborator

fendor commented Sep 9, 2021

Yep, but not quite ideal yet, as we still have no quick way of querying for the project ghc. Currently, it requires to build at least one cabal component.

@hasufell
Copy link
Member

hasufell commented Sep 9, 2021

Currently, it requires to build at least one cabal component.

Well, that means if I run cabal build before starting the language server, it should work?

@fendor
Copy link
Collaborator

fendor commented Sep 9, 2021

if you launch the correct language server (e.g. 8.6.5 for projects that use 8.6.5) then it will work (assuming MR #301) even without running cabal build beforehand. But hls-wrapper will still pick up the wrong ghc version and launch the wrong language server.

@fendor
Copy link
Collaborator

fendor commented Sep 9, 2021

BTW, MR #301 is not correct currently, as it does not use the latest json lib decoder/encoder for build-info.

@jneira
Copy link
Member Author

jneira commented Nov 9, 2021

The last promising hack to get the path to ghc could be:

cabal exec ghc -- -package-env=- -e 'System.Environment.getExecutablePath'

ugly but seems to work for now.

-package-env=- is needed to bypass the GHC_ENVIRONMENT value set by cabal to pass the packagedb stack to ghc as it makes the one liner fail withou a full build, which creates effectively that packagedb stack

The error without that option and without the costly full build is:

ghc: can't find a package database at /home/hugin/Documents/haskell/hie-bios/dist-newstyle/packagedb/ghc-8.10.4

Talking with @fendor it seems i can be used in a more direct way than #282 so maybe it could be used in another smaller pr if i understood it correctly

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
can-workaround type: bug Something isn't working
Projects
None yet
Development

Successfully merging a pull request may close this issue.

3 participants