Skip to content

Packages are resolved w.r.t to project-file path when absolute, but not when relative #7695

Open

Description

@pranaysashank

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, ghc versions: 3.4, 8.10.4

Activity

  1. Mikolaj commented on Sep 30, 2021

    @Mikolaj
    Member

    I haven't looked in depth, but would this change the situation: #7581 or is it unrelated?

  2. pranaysashank commented on Sep 30, 2021

    @pranaysashank
    Author

    I looked through the pull request discussion and it looks like it's unrelated to this.

  3. Mikolaj commented on Sep 30, 2021

    @Mikolaj
    Member

    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 how distProjectRootDirectory is determined. Definitely worth answering, and if there is not an obvious answer I'm missing, investigating in codebase.

  4. fgaz commented on Sep 30, 2021

    @fgaz
    Member

    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 packages should 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:

    • The correct behaviour would be to fail in both cases (./tmp/foo would be the correct path)
    • The success in the first case is a bug in how relative paths are handled
  5. fgaz commented on Sep 30, 2021

    @fgaz
    Member

    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=PATH roots PATH to the first valid place while walking up from CWD. It may be that that first valid place is treated as project root, so packages is rooted at that place. In the OP example, this means

    • when --project-file ../cabal.project is used, the project is rooted in . since ./../cabal.project exists, so packages: foo refers to ./foo
    • when --project-file /home/pranaysashank/src/cabal.project is used, the project is rooted in / since //home/pranaysashank/src/cabal.project exists, so packages: foo refers to /foo, which does not exist
    • as a further example, if --project-file is not used, it would default to cabal.project, which means the project is rooted in .. since ../cabal.project exists, so packages: foo refers to ../foo and the build would fail

    Note that packages is still relative to the project root. The point is that --project-file changes 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

    • if yes, document it better
    • if not, document that this is a bug, fix it, and document the new behaviour

    edit: here's a place where the documentation conflicts with this behaviour:

    https://cabal.readthedocs.io/en/3.6/cabal-project.html#cmdoption-builddir

    [...]this directory is resolved relative to the root of the project (i.e., where the cabal.project file lives.)

  6. pranaysashank commented on Sep 30, 2021

    @pranaysashank
    Author

    as a further example, if --project-file is not used, it would default to cabal.project, which means the project is rooted in .. since ../cabal.project exists, so packages: foo refers to ../foo and the build would fail

    @fgaz Yes, this is what I get

    [pranaysashank@nixos:~/src/tmp]$ ls
    foo
    
    $ [pranaysashank@nixos:~/src/tmp]$ cabal build all
    When using configuration(s) from /home/pranaysashank/src/cabal.project, the following errors occurred:
    The package location 'foo' does not exist.
    
    [pranaysashank@nixos:~/src/tmp]$
    
  7. pranaysashank commented on Sep 30, 2021

    @pranaysashank
    Author

    I can understand that this behaviour makes sense in case we are in a subdirectory of a project and do a cabal build it 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.

  8. Mikolaj commented on Sep 30, 2021

    @Mikolaj
    Member

    @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.]

  9. pranaysashank commented on Sep 30, 2021

    @pranaysashank
    Author

    when --project-file /home/pranaysashank/src/cabal.project is used, the project is rooted in / since //home/pranaysashank/src/cabal.project exists, so packages: foo refers to /foo, which does not exist

    @fgaz I am not sure it's so simple since the following works

    $ cabal build all --project-file /home/pranaysashank/src/tmp/cabal.project
    

    Based on your comment it should have looked for foo in / and failed

  10. fgaz commented on Sep 30, 2021

    @fgaz
    Member

    @Mikolaj

    but it's yet clear how exactly to simplify

    simple: make the project root coincide to the location of the project file

    @pranaysashank

    cabal build all --project-file /home/pranaysashank/src/tmp/cabal.project

    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 worng

  11. pranaysashank commented on Sep 30, 2021

    @pranaysashank
    Author

    @fgaz No the project is in tmp, and foo is the cabal package inside tmp. So,

    $ pwd
    /home/pranaysashank/src/tmp
    $ ls
    foo cabal.project
    

    Note: I moved cabal.project here to tmp to test this. In the original post, cabal.project was in src directory (so outside the project)

  12. 10 remaining items

  13. cydparser commented on Apr 3, 2022

    @cydparser
    Collaborator

    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.

  14. cydparser commented on Sep 6, 2022

    @cydparser
    Collaborator

    I have opened a PR (#8454) that addresses this issue by:

    1. Deprecating the use of --project-file with an absolute path
    2. Adding a --project-dir flag
    3. Always resolving project-file relative to project dir

    Please feel free to discuss/bikeshed in the PR.

  15. Mikolaj commented on Oct 17, 2022

    @Mikolaj
    Member

    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.

  16. Mikolaj commented on Nov 23, 2022

    @Mikolaj
    Member

    A new proposal dropped at #8454. Could you kindly voice your opinion?

  17. pranaysashank commented on Mar 2, 2023

    @pranaysashank
    Author

    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

  18. Mikolaj commented on Mar 2, 2023

    @Mikolaj
    Member

    Thank you for the clarification. Let me reopen. If cabal 3.10, when it's released, fixes this, please somebody note it down here.

  19. reopened this on Mar 2, 2023
  20. gbaz commented on Mar 2, 2023

    @gbaz
    Collaborator

    The merged pr claims that it has a behavior of Always resolving project-file relative to project dir even though that isn't in the title. So I do think that it should resolve this.

  21. ulysses4ever commented on Mar 2, 2023

    @ulysses4ever
    Collaborator

    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 (#8454)

    @cydparser can you sched light on this?

  22. cydparser commented on Mar 2, 2023

    @cydparser
    Collaborator

    The expected behavior in this issue:

    Expect cabal to resolve the packages field relative to where I run cabal file or have a consistent behavior with both relative and absolute file paths.

    PR #8454 deprecates the use of --project-file with an absolute path (users will see a warning message directing them to use --project-dir). A later release should forbid absolute paths in --project-file without an explicit --project-dir. This would satisfy "or have a consistent behavior."

  23. pranaysashank commented on Mar 3, 2023

    @pranaysashank
    Author

    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:

    • Start from the current directory
    • Check if project file exists at the given relative path
    • If not, traverse to the parent directory, set it as the current directory and repeat step 2
    • If the file is found, set the current directory (the directory from where the relative path was resolved) as the project root.

    From one of the earlier comments in this discussion:

    Note that packages is still relative to the project root. The point is that --project-file changes the root in a really weird way.

    Please go through the example in the issue as that should clarify the what the actual bug is.

  24. added
    re: project-file
    re: project root
    on Jul 4, 2023
  25. self-assigned this
    on Dec 9, 2023
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

Labels

attention: pr-welcomecabal-install: otherdocumentationre: project rootre: project-filetype: bugtype: user-question

Type

No type

Projects

No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions