Repository navigation
Open
Labels
attention: pr-welcomecabal-install: otherdocumentationre: project rootConcerning what cabal considers the root of the projectre: project-fileConcerning cabal.project filestype: bugtype: user-question
Description
Describe the bug
When a project-file is passed as an absolute path that is not in the current directory. cabal seems to be resolving the packages location relative to where the file is located. This doesn't happen if the file path is relative.
Is this a bug or correct behaviour?
To Reproduce
Steps to reproduce the behavior:
[nix-shell:~/src/tmp]$ mkdir foo
[nix-shell:~/src/tmp]$ cd foo/
[nix-shell:~/src/tmp/foo]$ cabal init
Guessing dependencies...
Generating LICENSE...
Warning: unknown license type, you must put a copy in LICENSE yourself.
Generating CHANGELOG.md...
Generating app/Main.hs...
Generating foo.cabal...
Warning: no synopsis given. You should edit the .cabal file and add one.
You may want to edit the .cabal file and add a Description field.
[nix-shell:~/src/tmp/foo]$ cd ..
[nix-shell:~/src/tmp]$ echo "packages: foo" > ../cabal.project
[nix-shell:~/src/tmp]$ cabal v2-clean; cabal v2-build all --project-file ../cabal.project
Resolving dependencies...
Build profile: -w ghc-8.10.4 -O1
In order, the following will be built (use -v for more details):
- foo-0.1.0.0 (exe:foo) (first run)
Configuring executable 'foo' for foo-0.1.0.0..
Preprocessing executable 'foo' for foo-0.1.0.0..
Building executable 'foo' for foo-0.1.0.0..
[1 of 1] Compiling Main ( app/Main.hs, /home/pranaysashank/src/tmp/dist-newstyle/build/x86_64-linux/ghc-8.10.4/foo-0.1.0.0/x/foo/build/foo/foo-tmp/Main.o )
Linking /home/pranaysashank/src/tmp/dist-newstyle/build/x86_64-linux/ghc-8.10.4/foo-0.1.0.0/x/foo/build/foo/foo ...
[nix-shell:~/src/tmp]$ cabal v2-clean; cabal v2-build all --project-file /home/pranaysashank/src/cabal.project
When using configuration(s) from /home/pranaysashank/src/cabal.project, the following errors occurred:
The package location 'foo' does not exist.
[nix-shell:~/src/tmp]$
Expected behavior
Expect cabal to resolve the packages field relative to where I run cabal file or have a consistent behaviour with both relative and absolute file paths.
System information
- Operating system: nixos - 21.05
cabal,ghcversions: 3.4, 8.10.4
Metadata
Metadata
Assignees
Labels
attention: pr-welcomecabal-install: otherdocumentationre: project rootConcerning what cabal considers the root of the projectre: project-fileConcerning cabal.project filestype: bugtype: user-question
Activity
Mikolaj commentedon Sep 30, 2021
I haven't looked in depth, but would this change the situation: #7581 or is it unrelated?
pranaysashank commentedon Sep 30, 2021
I looked through the pull request discussion and it looks like it's unrelated to this.
Mikolaj commentedon Sep 30, 2021
OK, I'm puzzled why it doesn't work in the second case. I did a quick code dive, found
BadLocNonexistantFile, but then got stuck figuring out howdistProjectRootDirectoryis determined. Definitely worth answering, and if there is not an obvious answer I'm missing, investigating in codebase.fgaz commentedon Sep 30, 2021
Where relative paths are rooted is not clarified in the docs as far as i can see, and that should be fixed.
I always assumed that
packagesshould be relative to the project root directory, not to cwd. If it wasn't, the project would change depending on cwd, which doesn't sound like a good feature. If my assumption is correct:./tmp/foowould be the correct path)fgaz commentedon Sep 30, 2021
Okay, I have an idea why this is happening, and my experiments confirm this. From https://cabal.readthedocs.io/en/3.6/cabal-project.html#cmdoption-0 i see that
--project-file=PATHrootsPATHto the first valid place while walking up from CWD. It may be that that first valid place is treated as project root, sopackagesis rooted at that place. In the OP example, this means--project-file ../cabal.projectis used, the project is rooted in.since./../cabal.projectexists, sopackages: foorefers to./foo--project-file /home/pranaysashank/src/cabal.projectis used, the project is rooted in/since//home/pranaysashank/src/cabal.projectexists, sopackages: foorefers to/foo, which does not exist--project-fileis not used, it would default tocabal.project, which means the project is rooted in..since../cabal.projectexists, sopackages: foorefers to../fooand the build would failNote that
packagesis still relative to the project root. The point is that--project-filechanges the root in a really weird way.This does not sound user friendly at all but at least it is consistent and predictable.
We should decide whether this is acceptable and
edit: here's a place where the documentation conflicts with this behaviour:
https://cabal.readthedocs.io/en/3.6/cabal-project.html#cmdoption-builddir
pranaysashank commentedon Sep 30, 2021
@fgaz Yes, this is what I get
pranaysashank commentedon Sep 30, 2021
I can understand that this behaviour makes sense in case we are in a subdirectory of a project and do a
cabal buildit resolves all the packages correctly at the top level dir where cabal.project is present.I am not sure if the same behaviour makes sense when we explicitly pass a project-file though.
Mikolaj commentedon Sep 30, 2021
@pranaysashank: well put. @fgaz and I think it's worth a simplification. Let me mark it so. [Edit: but it's yet clear how exactly to simplify.]
pranaysashank commentedon Sep 30, 2021
@fgaz I am not sure it's so simple since the following works
Based on your comment it should have looked for foo in / and failed
fgaz commentedon Sep 30, 2021
@Mikolaj
simple: make the project root coincide to the location of the project file
@pranaysashank
isn't cabal.project in
~/src, and not in~/src/tmp? It could also be that absolute path are always resolved absolutely and not rooted anywhere, but that path is still worngpranaysashank commentedon Sep 30, 2021
@fgaz No the project is in
tmp, andfoois the cabal package insidetmp. So,Note: I moved cabal.project here to
tmpto test this. In the original post,cabal.projectwas insrcdirectory (so outside the project)10 remaining items
cydparser commentedon Apr 3, 2022
I was looking at implementing #6051 and noticed the difference between absolute and relative --project-file paths while reading the code. For my use case, the relative behavior is ideal, since I would like to store the Nix project files in a nix directory (rather than pollute the root directory). I think there are two separate things here: 1) specifying the project file(s) 2) specifying the project root (currently only inferred). I suspect that the main use case for using an absolute path with --project-file is to specify the project root. Perhaps there should be a separate command for both.
cydparser commentedon Sep 6, 2022
I have opened a PR (#8454) that addresses this issue by:
--project-filewith an absolute path--project-dirflagPlease feel free to discuss/bikeshed in the PR.
Mikolaj commentedon Oct 17, 2022
Could I ask for help reviewing the PR fixing this issue (#8454)? Even a cursory look and a comment whether this might fix your use case (bonus points for checking out and confirming) would be very much appreciated.
Mikolaj commentedon Nov 23, 2022
A new proposal dropped at #8454. Could you kindly voice your opinion?
pranaysashank commentedon Mar 2, 2023
I'd just like to point out that the bug this issue discovered happens when we pass a relative path which I don't think is fixed by the merged PR
Mikolaj commentedon Mar 2, 2023
Thank you for the clarification. Let me reopen. If cabal 3.10, when it's released, fixes this, please somebody note it down here.
gbaz commentedon Mar 2, 2023
The merged pr claims that it has a behavior of
Always resolving project-file relative to project direven though that isn't in the title. So I do think that it should resolve this.ulysses4ever commentedon Mar 2, 2023
@cydparser can you sched light on this?
cydparser commentedon Mar 2, 2023
The expected behavior in this issue:
PR #8454 deprecates the use of
--project-filewith an absolute path (users will see a warning message directing them to use--project-dir). A later release should forbid absolute paths in--project-filewithout an explicit--project-dir. This would satisfy "or have a consistent behavior."pranaysashank commentedon Mar 3, 2023
There was no issue with using absolute paths in project-file. There is a bug in the behaviour of relative paths, here is the current behaviour when you pass a relative file path:
From one of the earlier comments in this discussion:
Please go through the example in the issue as that should clarify the what the actual bug is.