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

Support TemplateHaskell #1121

Closed
ndmitchell opened this issue Sep 11, 2019 · 25 comments · Fixed by haskell/ghcide#222
Closed

Support TemplateHaskell #1121

ndmitchell opened this issue Sep 11, 2019 · 25 comments · Fixed by haskell/ghcide#222

Comments

@ndmitchell
Copy link
Collaborator

Trying to synthesise one ticket that describes all the TH work. My understanding of the current state is:

  • If you have TH or ANN pragmas, and they all refer to code that is imported by packages or in this module, it works.
  • If you refer to other modules in this package it breaks, perhaps with haskell/ghcide#25.

The solution is likely to make sure we have compiled the dependencies to bytecode and send them at the linker before type checking a module that uses TH or ANN.

However, we don't have tests for the current state, and it works by luck rather than deliberately.

@cocreature
Copy link
Contributor

I have also seen some issue around async exceptions and TH which seemed to make GHC very unhappy (I don’t recall the precise error message).

@DavidEichmann
Copy link

I've noticed a huge slowdown when TH is enabled. After some searching I've noticed the folks at hie-engine ran into what looks like the same issue (HIE github issue), which was fixed in Infinisil/all-hies
by dynamically linking HIE (infinisil/all-hies#3). That PR says dynamic linking may not be necessary in for ghc versions with a fix to this GHC issue.

@pwm
Copy link

pwm commented Oct 4, 2019

Hi, anyone is in a position to estimate how much work this would entail? Since my team started using ghcide we can’t imagine going back to the stone age ever. You guys did an amazing job! On our end TH support seems to be the last big issue that’s in the way of full adoption.

@ndmitchell
Copy link
Collaborator Author

My guess is you need to:

  • Figure out which modules require TH, nice and easy.
  • For those that do, first build all the dependencies to object code (moderate amount of work, probably < 100 lines, fits fairly well with the graph approach).
  • Then send that object code to the linker (dunno how hard this is, might have problems with state management).

So probably not obviously hard, but I'd almost be surprised if something didn't get in your way.

@domenkozar
Copy link
Contributor

@DavidEichmann in case you were using ghcide-nix, that has been fixed now.

@serras
Copy link
Contributor

serras commented Dec 4, 2019

I would like to tackle this issue, since out team uses TH in quite some places.
Is there any further information that I can read to work on this?

@ndmitchell
Copy link
Collaborator Author

None that I'm aware of @serras - but best of luck!

@serras
Copy link
Contributor

serras commented Dec 5, 2019

This seems to be related https://gitlab.haskell.org/ghc/ghc/issues/8025, at least it ends up with the same error as haskell/ghcide#212

@serras
Copy link
Contributor

serras commented Dec 5, 2019

I have started working on the issue at serras/ghcide@90c784b in a quite crude way, asking to compile all dependencies even if the file does not use Template Haskell.
Now I get errors which look the ones in haskell/ghcide#212, even though now I am loading some bytecode:

File:     ./src/B.hs
Range:    1:0-100001:0
Source:   typecheck
Severity: DsError
Message:
  Program error:
  ByteCodeLink.lookupCE
  During interactive linking, GHCi couldn't find the following symbol:
  A_a_closure
  This may be due to you not asking GHCi to load extra object files,
  archives or DLLs needed by your current session. Restart GHCi, specifying
  the missing library using the -L/path/to/object/dir and -lmissinglibname
  flags, or simply by naming the relevant files on the GHCi command line.
  Alternatively, this link failure might indicate a bug in GHCi.
  If you suspect the latter, please send a bug report to:
  glasgow-haskell-bugs@haskell.org

Is there any way I can check which rules are firing? At least I can be sure that the bytecode for A.hs is generated before continuing my investigation.

@cocreature
Copy link
Contributor

You can use shake profiling to figure out the rules, see https://github.com/digital-asset/ghcide/blob/master/src/Development/IDE/Types/Options.hs#L44 (I think this is not exposed via the CLI so you’ll have to set it in the code somewhere) but that’s more useful for benchmarking than for debugging imho. I would probably just use some printf statements in the rules.

@merijn
Copy link

merijn commented Dec 10, 2019

I was testing out this PR and run into similar errors about a missing _closure despite said close being in the relevant library being loaded when I inspect it via nm. Is there a way I can see the ghci configuration ghcide is using to load things?

@domenkozar
Copy link
Contributor

domenkozar commented Dec 11, 2019

I'm seeing the following using these changes:

2019-12-11-112742_600x282_escrotum

https://github.com/hercules-ci/hercules-ci-agent

@merijn
Copy link

merijn commented Dec 11, 2019

Yeah, I'm seeing similar errors, and I have some suspicions but I'm not really sure enough how to debug ghcide to investigate further. Essentially, I seem to have both a static library (missing some symbols) and a dynamic library (not missing said symbols) in my dist-newstyle and ghcide seems to try to use the static one, but I'm not really sure why.

@merijn
Copy link

merijn commented Dec 11, 2019

I'm trying to trick it into loading the dynamic library to see if that works, but that's not working :\

@cocreature
Copy link
Contributor

We don’t do anything explicit atm wtr to linking external libraries in our code. I was hoping that this just magically works™ if we setup the GHC API with the right flags but it looks like we need to figure out the right incantation.

@merijn
Copy link

merijn commented Dec 11, 2019

I've been trying to find the right incantation with explicit -l/-L flags in hie.yaml, but so far no luck, but I've also going about it a bit blind since I don't actually know how ghcide sets up it's GHC session

@ndmitchell
Copy link
Collaborator Author

ndmitchell commented Dec 12, 2019

Ghcide uses hie-bios to set up its session, which essentially dumps command line flags into a GHC session. If flags work in ghci, my experience is it usually works in ghcide too.

@merijn
Copy link

merijn commented Dec 12, 2019

@ndmitchell I wish :)

ghci seems perfectly happy to work with just .ghc.environment, but I'm seeing all sorts of weird missing symbol errors in ghcide that I can't quite explain. I don't mind digging a bit deeper trying to debug them, but I'm rather unsure/clueless on where to start.

@serras
Copy link
Contributor

serras commented Dec 12, 2019

@merijn you can have a look at hie-bios documentation and try to turn your current .ghc.environment configuration into a hie.yaml file.

@ndmitchell
Copy link
Collaborator Author

@merijn yeah, .ghc.environment is entirely ignored by everything else, so you'll have to replicate that on the command line. FWIW, I turn off the GHC environment files because they cause lots of confusing behaviour with tools working differently in different places.

@merijn
Copy link

merijn commented Dec 12, 2019

I was under the impression that ghcide used them to find dependencies? Because that seems to work fine in most of my simple-ish projects. I do have a hie.yaml to resolve the look source paths, but I'm always a bit mystified what else has to go in there.

@ndmitchell
Copy link
Collaborator Author

@merijn nope, ghcide doesn't look at those files at all. It only uses cabal/stack as configured by hie-bios. In many cases cabal/stack will know the same information as those environment files.

@merijn
Copy link

merijn commented Dec 13, 2019

Ah, so maybe that's where things go wrong, because in other projects I haven't had to use a hie.yaml to set things up! I'll start fiddling with hie-bios

@cocreature
Copy link
Contributor

As far as I know, we do pick up .ghc.environment files. The GHC API behaves like GHC itself in that it picks them up implicitly by default.

@ndmitchell
Copy link
Collaborator Author

API's being influenced by random environment files scares me. The more I see about these environment files, the less I like them!

@pepeiborra pepeiborra transferred this issue from haskell/ghcide Jan 1, 2021
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging a pull request may close this issue.

7 participants