Skip to content

Looking up paths to executables with dune which #11905

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

Open
wants to merge 1 commit into
base: main
Choose a base branch
from

Conversation

gridbugs
Copy link
Collaborator

@gridbugs gridbugs commented Jun 13, 2025

The "dune exec" command has three different ways of resolving the names of executables to paths to executables:

  • public executables defined in the current project
  • executables in the "bin" directories of dependencies
  • executables in directories listed in $PATH

This can lead to unexpected shadowing, especially in the case of executables from dependecies, as users may not be aware that one of the packages in their project's dependency cone defines an executable with the same name of an executable that's also insalled system-wide.

Short of fixing the problem for now, this change introduces a tool for helping investigate specifically which executable will be run by "dune exec". This adds a command "dune which" which prints the path to the executable.

@gridbugs
Copy link
Collaborator Author

Here's an example session using this command:

37 s@u3 ~/src/llama (main=)
+ λ dune exec --which ocamlbuild
_build/_private/default/.pkg/ocamlbuild/target/bin/ocamlbuild

38 s@u3 ~/src/llama (main=)
+ λ dune exec --which ocamlc
/home/s/.cache/dune/toolchains/ocaml-compiler.5.3.0-a3dd2a36d935a7ada272441c8543eba1/target/bin/ocamlc

39 s@u3 ~/src/llama (main=)
+ λ dune exec --which cat
/nix/store/87fck6hm17chxjq7badb11mq036zbyv9-coreutils-9.7/bin/cat

40 s@u3 ~/src/llama (main=)
+ λ dune exec --which foo
_build/install/default/bin/foo

@Alizter
Copy link
Collaborator

Alizter commented Jun 13, 2025

Is this essentially dune exec -- which %{bin:foo}? I'm still for including such a feature, since not every system has which.

@gridbugs
Copy link
Collaborator Author

It is, yes, but I bet a lot of people don't know you can expand pforms like that (for example I did not know this).

Copy link
Collaborator

@Leonidas-from-XIV Leonidas-from-XIV left a comment

Choose a reason for hiding this comment

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

I think it is a very cool feature. In fact, I would actually suggest making a dune which top level command, as dune exec --which is a flag I will immediately forget and it is fairly obscure and does not actually execute the command.

@gridbugs gridbugs changed the title Looking up paths to executables with dune exec Looking up paths to executables with dune which Jun 17, 2025
@gridbugs
Copy link
Collaborator Author

Updated this so that the command is now dune which <name>.

@gridbugs gridbugs force-pushed the dune-exec-which branch 2 times, most recently from f59a49b to 985c96c Compare June 17, 2025 06:52
Copy link
Collaborator

@Leonidas-from-XIV Leonidas-from-XIV left a comment

Choose a reason for hiding this comment

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

Except for some minor cleanup I think this is nearly done. I really like the cram test!

@rgrinberg
Copy link
Member

Maybe this should go under describe/show? Don't think this command is going to be used enough to live at the top level tbh.

@gridbugs
Copy link
Collaborator Author

Why is the expected frequency with which a command will be used a useful metric for deciding whether to put it at the top level or not? I think it belongs at the top level because I expect this command to be used by people debugging a situation where dune ran a command they didn't expect, and for this use case it's important that the command be easy to discover.

The "dune exec" command has three different ways of resolving the
names of executables to paths to executables:
 - public executables defined in the current project
 - executables in the "bin" directories of dependencies
 - executables in directories listed in $PATH

This can lead to unexpected shadowing, especially in the case of
executables from dependecies, as users may not be aware that one of
the packages in their project's dependency cone defines an executable
with the same name of an executable that's also insalled system-wide.

Short of fixing the problem for now, this change introduces a tool for
helping investigate specifically which executable will be run by "dune
exec". This adds a command "dune which" which prints the path to the
executable.

Signed-off-by: Stephen Sherratt <[email protected]>
@rgrinberg
Copy link
Member

There are other commands under $ dune describe that are no less useful for debugging, shall we promote them all to the top level as well?

The point of having a hierarchical command line is to make the commands more discoverable. If putting things under sub-commands made the CLI less discoverable, why did we introduce the hierarchy in the first place? Dune is not like git where sub-commands are their own binaries, so it was done for purely organizational purposes.

IMO, the underlying issue is that the "discoverability" of one command comes at the expense of other commands. So yes, you have made $ dune which more discoverable, but at the cost of making the user slightly more likely gloss over more useful sub-commands.

Now it could be that I'm underestimating the utility of this command, and it is going to be more useful than I'm imagning - I'll leave the decision where to put it up to you. But I do want to clarify that:

Why is the expected frequency with which a command will be used a useful metric for deciding whether to put it at the top level or not?

Is the principle we should be following. We hide scarcely used commands, and promote the ones that are often used. We group related sub-commands to avoid repeating prefixes. We optimize for the common case.

$ chmod a+x bin/baz
$ export PATH=$PWD/bin:$PATH

$ dune which baz
Copy link
Member

Choose a reason for hiding this comment

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

Could you add a test for dune which %{..}? I think it should support this.

@Leonidas-from-XIV
Copy link
Collaborator

For what it's worth, something like dune [describe/show] location would also sound fine with me and dune [describe/show] is reasonably discoverable as it has existed for a while. My concern was that dune exec --option-that-does-not-exec is quite hard to notice and also remember, so I suggested dune which to reuse the familiar term from POSIX.

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.

4 participants