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

Bugfix: don't mangle GHC options that begin with "-i" (e.g. "-instantiated-with") #157

Closed
wants to merge 2 commits into from

Conversation

acerempel
Copy link

The 'fixImportDirs' function inadvertently catches GHC options that begin with "-i", such as "-instantiated-with" and "-ignore-package", and changes them to e.g. "-i/Users/foo/stantiated-with". This patch fixes that.

This bit me when I was trying to use ghcide with a Cabal package that has two module signatures: BaseMonad.hsig and List.hsig. I would get this error:

target ‘BaseMonad=<BaseMonad>,List=<List>’ is not a module name or a source file

This error occurred because Cabal was trying to pass instantiated-with BaseMonad=<BaseMonad>,List=<List> to GHC, but fixImportDirs had turned that into -i/Users/parsonyorick/…/nstantiated-with BaseMonad=<BaseMonad>,List=<List>, which GHC parsed as two commandline arguments.

The 'fixImportDirs' function is supposed to target the "-i" option,
but inadvertently also gets options such as "-instantiated-with"
and "-ignore-package" that begin with "-i" (and changes them to
e.g. "-i/Users/foo/stantiated-with"). This patch fixes that.
@acerempel acerempel changed the title fixImportDirs: ignore GHC options that begin with the string "-i" Bugfix: don't mangle GHC options that begin with "-i" (e.g. "-instantiated-with") Feb 28, 2020
Copy link
Collaborator

@fendor fendor left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Thank you for catching this!
Is there a reason why you are matching by prefix instead of elem?

@acerempel
Copy link
Author

I use isPrefixOf in order to also catch options of the form -instantiated-with=… (in addition to -instantiated-with followed by an argument). Does that make sense?

That deserves a comment, so I'll add one.

Also refactor a little to place the comment in such a way that it
doesnt' harm readability.
@fendor
Copy link
Collaborator

fendor commented Feb 28, 2020

Do you think there is a more general solution? This requires that we update hie-bios for every ghc version that adds a new flag beginning with -i.

@acerempel
Copy link
Author

Sure, I suppose so! I guess I imagined that ghc's flags don't change that often.

One way to solve this more generally would be to do this fixing of import paths after GHC parses its command line arguments—i.e. modifying the DynFlags—rather than operating on the raw list of strings.

Another way would be to check, for each argument that looks like -idir, whether dir is actually a directory, and modify the argument only in that case. (This works as long as no one has an hs-source-dirs: nstantiated-with or similar, which would be very strange!)

Upon further thought, I like the first option better. GHC already knows how to parse its own command line arguments – no need for us to try to do it ourselves. It looks like we modify the DynFlags in initSession anyway, so moving the modification of the import dirs over there shouldn't be hard.

If that sounds good to you, I'm happy to implement it.

@fendor
Copy link
Collaborator

fendor commented Mar 2, 2020

The problem with the first approach is that previously, it was possible to pass around the raw compilation flags and they could be easily used. We would lose this property if the paths to -i are relative.
Moreover, I fear that think that we can not easily know the root of the project, in initSession. I would have to verify, but I think that the flags are relative to the root of a cradle and cradles can be nested, thus getCurrentDirectory would be wrong, it would require that we pass in the Cradle to initSession.

I thought about the second approach as well, but I feel it is a bit too brittle. Unless the tools guarantee that the paths exist relative to the working directory.

@mpickering Do you have a preference or idea how to properly handle this?

@acerempel
Copy link
Author

acerempel commented Mar 2, 2020 via email

@mpickering
Copy link
Collaborator

The way to fix this probably is to allow each Cradle to specify the directory the flags are relative to and then make the source directories can be made relative to this after the flags have been parsed. At least the cabal cradle also has issues with Template Haskell splices using relative paths which this approach would fix.

@fendor
Copy link
Collaborator

fendor commented Apr 13, 2020

@parsonyorick Sorry for not returning earlier to this issue. I have a prototype of the suggested implementation at #166 which ought to solve the problem by normalising the filepaths after GHC flag parsing

@acerempel
Copy link
Author

Great, the approach at #166 makes complete sense to me—that will do the trick.

@fendor
Copy link
Collaborator

fendor commented Apr 25, 2020

Closed in favour of #166

@fendor fendor closed this Apr 25, 2020
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 this pull request may close these issues.

None yet

3 participants