-
-
Notifications
You must be signed in to change notification settings - Fork 369
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
Relative filepath in Template Haskell different in HLS from Stack #481
Comments
I'd be willing to try and fix this myself if someone could point me to where in the HLS code this might be fixed. |
I'd almost forgotten about this, but we had this issue too. We solved it in a truly appalling fashion with some symlinks so that we could use the path "from" the project root and have it work in both settings. I would love a fix for this! |
Making symlinks for all our projects in the root would create more headaches than it would solve. (I've tried after reading your / @michaelpj 's comment) |
@Vlix thank for you report. Just in case, have you tried to workaround the issue using f.e. cpp conditions? There is a new feature in hie-bios that might help to do it: you can add an alternative |
I guess it would be related with |
@jneira I've tried it out, and I guess it could work, except setting flags via a To be precise, I've added the following to a copy of
And then in the cradle:
(And I've seen HLS use the And in the
and then in the code {-# LANGUAGE CPP #-}
...
val :: Value
val = $$(decodeFile
#if USEIDE
"package1/config/value.yaml"
#else
"config/value.yaml"
#endif
) So |
Hi, i've tested it and you are right, it does not work cause hls via hie-bios uses I would say that it is a |
@Vlix hi! i am trying to reproduce the error. With only one package setup i cant reproduce it, but i infer from Nvm, i've reproduced it simply creating a subpackage
I think this is a |
@Vlix well
import System.Directory
import Data.Yaml (Value)
import Data.Yaml.TH (decodeFile)
import Language.Haskell.TH
import Language.Haskell.TH.Syntax
val :: Value
val = $$(do
file <- runIO $ do
exist <- doesFileExist "config/value.yaml"
return $ if exist then "config/value.yaml"
else "subpackage/config/value.yaml"
decodeFile file :: Q (TExp Value)
) Ugly but well,
|
Opened issue upstream |
Thank you for the effort! That type of work-around should be doable. |
EDIT: don't use the following function, just use Ok, I've made a helper function that just tries a couple of given filepaths: import Language.Haskell.TH
import System.Directory (doesFileExist)
import System.FilePath ((</>))
withFirstFile :: [FilePath] -> (FilePath -> Q a) -> Q a
withFirstFile [] _ = fail "No valid filepaths found"
withFirstFile (fp:fps) q = do
b <- runIO $ doesFileExist fp
if b
then q fp
else withFirstFile fps q And this does work around the issue, and HLS can go on and process the rest of the files... but then I ran into a new problem on our production code. I'll try to narrow it down more at a later time (it's not because of TH, that's for sure, those were in the half I kept that didn't segfault) I also can't share logs etc. because this is production code 😞 |
Wow, sad to read that. Could you open a new issue if it is a different problem not related with th relative filepaths? |
Not sure if useful at this point, but I have a pretty simple repro here https://github.com/AlistairB/morpheus-repro/tree/hls-th-path-repro |
@michaelpj Would the code snippet in my previous comment maybe help get rid of all the symlinks in your project? Or would that just make it more complicated? |
@Vlix thanks, I'll give that a try! |
the stack issue tracking the resolution has been closed with a pr so hopefully the next stack version will include it 🎉 |
This is needed for both `cabal build hledger-web` and `haskell-language-server` to succeed. This was already done in the heldger-cli package for example but not in the heldger-web package. In essence, hls and stack/cabal uses different roots when embedding files in template haskell in sub-packages. Reference: haskell/haskell-language-server#481 (comment)
This is needed for both `cabal build hledger-web` and `haskell-language-server` to succeed. This was already done in the heldger-cli package for example but not in the heldger-web package. In essence, hls and stack/cabal uses different roots when embedding files in template haskell in sub-packages. Reference: haskell/haskell-language-server#481 (comment)
This is needed for both `cabal build hledger-web` and `haskell-language-server` to succeed. This was already done in the heldger-cli package for example but not in the heldger-web package. In essence, hls and stack/cabal uses different roots when embedding files in template haskell in sub-packages. Reference: haskell/haskell-language-server#481 (comment)
This is needed for both `cabal build hledger-web` and `haskell-language-server` to succeed. This was already done in the heldger-cli package for example but not in the heldger-web package. In essence, hls and stack/cabal uses different roots when embedding files in template haskell in sub-packages. Reference: haskell/haskell-language-server#481 (comment)
Would be great to have some solution for this. Also willing to put in effort to help fix it, if someone can point me in the right direction. |
Do the workarounds posted in this thread not work for you? |
This commit works around the following HLS bug: haskell/haskell-language-server#481 That issue is actually related to stack, but seems like the cabal integration is suffering from the same in our case. There are quite a few relative-path related issue open HLS anyway. This commit uses the `makeRelativeToProject` function from file-embed in order to resolve the relative paths in a way that's independent of the build system, working around this long-standing HLS bug.
This PR adds a `hie.yaml` to the project and also works around the following HLS bug: haskell/haskell-language-server#481 That issue is actually related to stack, but seems like the cabal integration is suffering from the same in our case. There are quite a few relative-path related issues open in HLS anyway. This commit uses the `makeRelativeToProject` function from file-embed ([As suggested by phadej under another issue](commercialhaskell/stack#5421 (comment))) in order to resolve the relative paths in a way that's independent of the build system, working around this long-standing HLS bug. * Add a hie.yaml to the project * Work around HLS bug while embedding files This commit works around the following HLS bug: haskell/haskell-language-server#481 That issue is actually related to stack, but seems like the cabal integration is suffering from the same in our case. There are quite a few relative-path related issue open HLS anyway. This commit uses the `makeRelativeToProject` function from file-embed in order to resolve the relative paths in a way that's independent of the build system, working around this long-standing HLS bug.
I am still observing this under hls-2.0.0.0 with a multi-component project. The |
@goertzenator Using |
GHC 9.4 added support for multiple home units (c.f. #3738), and together with it a mechanism by which a tool like HLS can tell GHC where the "package root" is: a The docs claim that HLS sets the option, which I believe is misspelled (it's actually Then there's a TH version of makeRelativeToProject which is supposed to be a drop-in replacement for the function from So in a sense the ball is currently in HLS's court. |
We set the working directory appropriately so that TH code can access it and read files relative to the root of the current component. Fixes #481
Fix in #3891 |
We set the working directory appropriately so that TH code can access it and read files relative to the root of the current component. Fixes #481
We set the working directory appropriately so that TH code can access it and read files relative to the root of the current component. Fixes #481
Subject of the issue
When having sub-libraries in a stack project, relative filepaths used in Template Haskell expressions are parsed as if they were run from the root directory of the project, but stack itself parses it relatively from the sub-library's root (which is one directory deeper).
For example:
Given the TH is used in
./lib/src/Lib.hs
with the stack.yaml at./stack.yaml
and the lib.cabal at./lib/lib.cabal
, HLS will want the relative path to be"lib/config/value.yaml"
, where stack itself wants it to be"config/value.yml"
Your environment
I tried this on VS Code with just the
Haskell
plugin installedSteps to reproduce
Use this repo
Expected behaviour
I expect HLS to just compile the package
Actual behaviour
HLS gives an error that the file (
{root}/lib/config/value.yaml
) can't be found with relative path"config/value.yaml"
, even though it compiles fine withstack build
. It will build with"lib/config/value.yaml"
, but thenstack build
fails to compile.Include debug information
Debug output:
Paste the logs from the lsp-client, e.g. for VS Code
LSP logs:
The text was updated successfully, but these errors were encountered: