Skip to content

cabal run/list-bin does not default to exe component for package targets #7403

Closed

Description

@dminuoso

Describe the bug
When a package test2 is vendored, cabal run test2 or cabal list-bin test2 will not default to exe components.

$ cabal run test2
cabal: The run command is for running a single executable at once. The target
'test2' refers to the package test2-0.1.0.0 which includes the executable
'test2' and the test suite 'test2-test'.

With cabal test test2 cabal will default to the correct component, however.

To Reproduce
A testcase can be found at https://github.com/dminuoso/cabal-repro

Expected behavior
cabal should pick exe components for run or list-bin, test components for test, etc. This already happens for non-vendored packages (see related #7335 )

System information
Reproduceable with cabal-install 3.2.0.0 and 3.4.0.0

Activity

  1. dminuoso commented on May 21, 2021

    @dminuoso
    Author

    I just realized, that this probably applies not just to vendored packages, but rather when the target is a package in general.

    cabal build test2 also works and defaults to the exe component.

  2. changed the title [-]cabal-install does not default to the correct component for vendored packages[/-] [+]`cabal run/list-bin` does not default to exe component for package targets[/+] on May 21, 2021
  3. dminuoso commented on May 21, 2021

    @dminuoso
    Author

    Related #7326

  4. Mikolaj commented on May 21, 2021

    @Mikolaj
    Member

    I think the behaviour (and the code AFAICT) of test, run and list-bin is the same in this case. It so happens, though, that the name of the executable section in your .cabal file matches the name of the package, while the name of the test section doesn't. I think package name shadows the executable name and hence the message. Do you think this a wrong behaviour?

  5. dminuoso commented on May 25, 2021

    @dminuoso
    Author

    The diagnostic

    First, the diagnostic is not particularly helpful:

    [nix-shell:~/wobcom/projects/cabal-repro]$ cabal run test2
    Resolving dependencies...
    cabal: The run command is for running a single executable at once. The target
    'test2' refers to the package test2-0.1.0.0 which includes the executable
    'test2' and the test suite 'test2-test'.
    

    The user is not informed what the problem is, or how to resolve it. Is this an ambiguity between the executable test2 and the test suite test2-test? It says "is for running [...] [an] executable", and then it says it even found one. To me it begs the question "You found a single executable. What's the problem?".

    Vendoring packages

    Next, it is not un-common to have a repository structure like this

    repo
        | -- cabal.project
        | -- pkgA
        | -- pkgB
        \ -- pkgC
    

    Where cabal.project just vendors these three sub packages (maybe with a glob or explicitly)

    This is particularly useful, if some of these packages depend on each other so you don't have to maintain a cabal.project for each sub-package to vendor the rest. So to build pkgA, you'd sit at repo/ and then issue commands like repo/ $ cabal run pkgA. But now you are forced to use cabal run pkgA:exe:that-component, if you have multiple vendored packages with single executables, this is quite an annoyance. We experience this annoyance every day on one of our internal projects.

    What the user expects

    I expect run and list-bin to pick an executable component whenever they can, and test a test-suite whenever it can.

    Cabal even documents run to be intended for "single executables" and test for "test suites". Why not default to executable and test-suite components of the matching package target?

    Proposed changes

    • run <target> - If cabal-install matches <target> to be a package name, see if contains it a single executable component, and then just run that. If multiple candidates exist, issue an ambiguity error and ask the user to resolve by specifying <target>:exe:<component_name>. If no executable component exists, complain about that.
    • test - Similar to run except with test-suite components.
    • list-bin- Similar to run

    And all of these should work on whether it's another package, vendored or not.

  6. Mikolaj commented on May 25, 2021

    @Mikolaj
    Member

    Fine analysis. We'd need one more voice to finalize the specification but, in principle, the work could start already. @emilypi, @fgaz, others?

  7. dminuoso commented on May 25, 2021

    @dminuoso
    Author

    I provided a simple implementation in #7408. Judging from the implementation, cabal test <package> will always build and run all enabled test suites, so that one needs no further attention.

    If this is acceptable, I will change the documentation and then this can be merged.

  8. Mikolaj commented on May 26, 2021

    @Mikolaj
    Member

    Amazing. Let's move the discussion to the PR.

  9. fgaz commented on May 26, 2021

    @fgaz
    Member

    The specification looks fine to me! Running cabal run some-test would still work as this is only for package targets, right?

  10. dminuoso commented on May 27, 2021

    @dminuoso
    Author

    @fgaz Yes. The modification applies only when the target refers to a package.

  11. dminuoso commented on May 27, 2021

    @dminuoso
    Author

    One more thing I noticed. cabal list-bin without a target is not allowed, I think we should make it behave like cabal run, picking the package from the current directory as its target implicitly. Since this was not specified above, opinions? @Mikolaj @fgaz

  12. fgaz commented on May 27, 2021

    @fgaz
    Member

    I suspect that specific choice might have been deliberate, especially since list-bin is useful in scripts (=> it has to be robust => have the user be more explicit). @phadej may know more, as the author of the command

  13. dminuoso commented on May 28, 2021

    @dminuoso
    Author

    That argument makes sense, to warrant the user being explicit here.

    On the other hand, it'd be nice if
    cabal list-bin <whatever-goes-here>
    produced the path of the binary corresponding to
    cabal run <whatever-goes-here>

    But that wishful thinking wouldn't work with
    cabal build <whatever-goes-here>
    or
    cabal test <whatever-goes-here>
    Because these support multiple targets.

  14. Mikolaj commented on Jun 1, 2021

    @Mikolaj
    Member

    @phadej, could you weigh in? :)

  15. Mikolaj commented on Jun 8, 2021

    @Mikolaj
    Member

    Please see the PR for a continuation (conclusion?) of the discussion.

    Edit: Let's not, let's stick here as @dminuoso proposes.

  16. dminuoso commented on Jun 8, 2021

    @dminuoso
    Author

    So strange to have this discussion split in two streams.

    How about we resume the discussion of whether to make list-bin accept an implicit package target in this issue here, while the PR discussion stays about just what's already submitted?

    Here's my line of argumentation why I think list-bin should operate on an implicit package like run:

    All three commands run, test and list-bin work on a single executable. Your target specifier must somehow be resolvable to a single exact executable that you probably meant.

    So what component does a cabal list-bin user usually want? list-bin is a plumbing command with the primary purpose of being able to extract a build artifact, using something like cp $(cabal -v0 list-bin some-exe) destination, most likely in a build script/CI job. It seems obvious that the large portion of users wants to extract executable artifacts (as extracting test/benchmark artifacts seems less interesting to the average user).

    Now, there is the following scenarios to consider:

    1. The users cwd is inside a project pepper and wants to extract the single executable artifact mill. cabal list-bin would achieve the goal. Raised an error before, would select pepper:exe:mill now.
    2. The users cwd is outside a project and contains multiple cabal packages pepper and salt. cabal list-bin would not match a component. Raised an error before, and would raise it again.
    3. The users cwd is inside a project pepper and wants to extract the artifact from salt:exe:mill. cabal list-bin would do the unexpected thing. Raised an error before, would select pepper:exe:mill now (but not the intended salt:exe:mill)

    The change would not affect any existing CI jobs, since we don't introduce additional error paths, but we lift some. Any user who tried to use cabal list-bin would have changed to specify an explicit target. These we do not need to worry about. What about future users? Here only 3) is of concern, as 1) is desirable, and 2) does not change the behavior.

    Why is this the change in 3) unproblematic? For one, if your cwd is in a different package, cabal install and cabal test wouldn't have worked either, so the user would have already specified:

    cabal build salt:exe:mill
    cabal test salt:test:mill
    cp $(cabal -v0 list-bin salt:exe:mill)
    

    It's very unlikely any user would be specifying an exe/test component first, but be silent about what is passed to list-bin. And even if the user does, they would get rapid feedback that the wrong component was extracted.

    This is offset by the benefit of increased coherence between the commands. This leads to less mental overhead for the users, when they have to re-study the target selection algorithm for each command they want to use, or get confused why cabal build works, cabal run works, cabal test works, but cabal list-bin does not. I'm saying this as a user who struggled for nearly 2 years with why cabal <cmd> <target> would break and have differing behavior depending on the choice of <cmd>.

    How do you feel about this?

  17. Mikolaj commented on Jun 8, 2021

    @Mikolaj
    Member

    I'm swayed, but have no strong opinion,

  18. Mikolaj commented on Jun 12, 2021

    @Mikolaj
    Member

    @fgaz: would you weight in?

  19. fgaz commented on Jun 22, 2021

    @fgaz
    Member

    @dminuoso's argument is convincing, but it's about backward compatibility, and while we definitely have to consider that too, my specific concern was a bit different.

    I had in mind something like this:

    • the user writes in a script cabal list-bin p where p is a package with an exe a, so p refers to p:exe:a
      • (not possible currently)
    • the user adds a package q with an exe p and forgets to update the script
    • now the script points to q:exe:p!

    BUT this is a contrived example, and

    • the reason why this happens would be immediately apparent
    • run behaves the same anyway and is probably also used in scripts and such
    • as @dminuoso writes, consistency is good
    • we should probably let the user choose whether to be precise or not :)

    so unless someone else objects i'd be okay with it

  20. Mikolaj commented on Jul 16, 2021

    @Mikolaj
    Member

    @dminuoso: what's your take on that? Are we unblocked?

  21. added 2 commits that reference this issue on Nov 30, 2021
    a37fec9
    153709a
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Type

    No type

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions