Skip to content

feat: bin-layout for %{bin:...} deps - #14432

Merged
Alizter merged 1 commit into
ocaml:mainfrom
Alizter:push-wrvoymzuvzsq
May 21, 2026
Merged

Alizter merged 1 commit into
ocaml:mainfrom
Alizter:push-wrvoymzuvzsq

Conversation

@Alizter

@Alizter Alizter commented May 5, 2026 •

Copy link
Copy Markdown
Collaborator

Resolve %{bin:...} to the build artifact path (Original_path) instead of the install staging area. When %{bin:...} appears in (deps ...), a bin-layout directory is created under _build/install/<context>/.binaries/<digest>/ with correctly-named symlinks, and added to PATH.

The legacy staging paths (_build/install/default/bin, lib, etc.) remain on the root env for now. They cannot be removed until (deps (package ...)) provides its own scoped layout (the install-layout PR #14373), since actions using (package ...) deps still rely on the staging area for PATH and OCAMLPATH visibility.

Env binaries (from (env (binaries ...))) are excluded from the bin-layout since they already use .bin/ on PATH.

Dep_conf_eval.unnamed and Dep_conf_eval.named now return a single Env.t Action_builder.t whose evaluation both registers deps and produces the env (previously a tuple with a separate unit Action_builder.t for deps registration). Build-tracking-only callers (lib_flags, simple_rules) discard the env via Action_builder.ignore.

(include FILE) inside (deps ...) propagates the recursive call's bin_env via a new Include_result variant on dep_evaluation_result; named_paths_builder folds the include bin_envs into the final bin_env via Env_path.extend_env_concat_path. Without this, the action's PATH would not get a bin-layout dir for include-d bin pforms.

The change is not gated by lang version: #3324 affects every lang version, so gating would leave the bug unfixed for pre-3.24 projects. The user-visible realignments are confined to in-_build paths and the migration cost is small enough to not warrant a per-project opt-in.

Known limitations (deferred to follow-up)

  • dune's sandbox does not relocate the PATH environment variable. The bin-layout dir on PATH points at the un-sandboxed _build/. This works locally because absolute paths are still readable from inside the sandbox, but would break under remote action execution. Tracked by test/blackbox-tests/test-cases/bin-pform/sandbox.t.
  • Bin_layout.Key.reverse_table grows unboundedly in long-running --watch sessions (one entry per unique sorted bin set). Pattern inherited from Ppx_driver.Key. Bounded by the number of distinct bin sets the workspace evaluates; unlikely to be a real problem.

@Alizter
Alizter force-pushed the push-wrvoymzuvzsq branch 4 times, most recently from 3dc397b to f5576c4 Compare May 6, 2026 00:01
@Alizter Alizter changed the title feat: resolve %{bin:...} to build artifact path feat: bin-layout for %{bin:...} deps May 6, 2026
@Alizter
Alizter force-pushed the push-wrvoymzuvzsq branch 2 times, most recently from 44701f1 to b9f314d Compare May 6, 2026 17:37
@Alizter Alizter self-assigned this May 6, 2026
@Alizter
Alizter force-pushed the push-wrvoymzuvzsq branch 2 times, most recently from 78a86fb to dea3003 Compare May 8, 2026 18:38
Alizter added a commit that referenced this pull request May 10, 2026
In this PR we organise all the tests about `%{bin:...}` and increase our
test coverage. This is in preparation of:

- #14432 
- #14373

Add `test/blackbox-tests/test-cases/bin-pform/` with focused tests for
the `%{bin:NAME}` pform.

New tests:

- `cram-path.t`            `%{bin:...}` in cram `(deps ...)`
- `env-binaries.t` `%{bin:...}` for `(env (binaries ...))` entries
- `include-deps.t`         `%{bin:...}` inside `(include ...)` deps
- `inline-tests.t`         `%{bin:...}` in `(inline_tests (deps ...))`
- `multiple-bins.t`        multiple `%{bin:...}` deps in a single rule
- `public-name.t`          `%{bin:...}` expansion for `(public_name)`
- `run-action.t` `%{bin:...}` in `(run ...)` and `PATH` visibility
- `run-literal-vs-pform.t` `(run NAME)` vs `(run %{bin:NAME})` record
different deps (build artifact vs install staging)
- `system.t`               `%{bin:...}` for a system binary on `PATH`
- `test-stanza.t`          `%{bin:...}` in a test stanza
- `missing-bin.t` `%{bin:...}` and `(run LITERAL)` where the name
doesn't resolve

Plus a cross-compilation analog of
`lib-pform-context-expansion-gh9199.t`
at `custom-cross-compilation/bin-pform-context-expansion.t`.

Renamed:

- `bin-eager-deps.t          -> bin-pform/lazy-failure.t`
- `adversarial-binary-name.t -> bin-pform/colon-in-name.t`
- `build-percent-form-bin.t  -> bin-pform/cli-build.t`

Split `missing-lib-bin-pform-errors-gh1541.t`: bin parts moved into
`bin-pform/missing-bin.t`, lib parts stay in
`missing-lib-pform-errors-gh1541.t`.

`setup-script.sh`: add env_added helper.
@Alizter
Alizter force-pushed the push-wrvoymzuvzsq branch 2 times, most recently from b499421 to d2626f9 Compare May 11, 2026 11:15
@Alizter Alizter added the package management Dune's package management — `(pkg)` stanza, lockdirs, `dune pkg` commands label May 11, 2026
@Alizter Alizter mentioned this pull request May 11, 2026
2 of 12 tasks
@Alizter
Alizter force-pushed the push-wrvoymzuvzsq branch 8 times, most recently from b85b32e to ff2547a Compare May 12, 2026 15:20
@Alizter
Alizter force-pushed the push-wrvoymzuvzsq branch 4 times, most recently from f120f61 to e6f9fdd Compare May 19, 2026 14:13
@Alizter
Alizter marked this pull request as ready for review May 21, 2026 22:01
Comment thread src/dune_rules/command.ml Resolved
Comment thread src/dune_rules/command.ml Resolved
Comment thread src/dune_rules/pp_spec_rules.ml Resolved
Resolve %{bin:...} to the build artifact path (Original_path) instead
of the install staging area. When %{bin:...} appears in (deps ...), a
bin-layout directory is created under
_build/install/<context>/.binaries/<digest>/ with correctly-named
symlinks, and added to PATH.

The legacy staging paths (_build/install/default/bin, lib, etc.) remain
on the root env for now. They cannot be removed until (deps (package ...))
provides its own scoped layout (the install-layout PR), since actions
using (package ...) deps still rely on the staging area for PATH and
OCAMLPATH visibility.

Env binaries (from (env (binaries ...))) are excluded from the
bin-layout since they already use .bin/ on PATH.

Dep_conf_eval.unnamed and Dep_conf_eval.named now return a single
Env.t Action_builder.t whose evaluation both registers deps and
produces the env (previously a tuple with a separate unit
Action_builder.t for deps registration). Build-tracking-only callers
(lib_flags, simple_rules) discard the env via Action_builder.ignore.

(include FILE) inside (deps ...) propagates the recursive call's
bin_env via a new [Include_result] variant on [dep_evaluation_result];
named_paths_builder folds the include bin_envs into the final bin_env
via Env_path.extend_env_concat_path. Without this, the action's PATH
would not get a bin-layout dir for include-d bin pforms.

Command.run, Command.run', and Command.run_dyn_prog now take an
optional ?env:Env.t Action_builder.t argument. When supplied, the env
is applied to the resulting Action.Full.t via Action.Full.add_env. This
lets callers thread a Dep_conf_eval-derived bin env directly through
the command pipeline instead of post-wrapping the result with
[map_build] + [add_env]. Used at the ppx driver invocation in
pp_spec_rules and at the ctypes type_gen/function_gen/build_c sites.

Env threading was missing at two sites and is fixed here:

- inline_tests: the partitions-discovery action runs the test exe with
  -list-partitions flags. It never received the deps env, so
  (inline_tests (deps %{bin:foo})) would not put foo on PATH at
  discovery time.

- ctypes: build_c_program (the C compiler invocation) and exe_link_only
  (the type_gen/function_gen linker) did not apply the action env from
  (ctypes (deps ...)), so %{bin:...} deps were invisible to those
  actions.

The change is not gated by lang version: ocaml#3324 affects every lang
version, so gating would leave the bug unfixed for pre-3.24 projects.
The user-visible realignments are confined to in-_build paths and the
migration cost is small enough to not warrant a per-project opt-in.

Fixes ocaml#3324 (dune build %{bin:foo} returns an error).

Known limitations (deferred to follow-up):

- dune's sandbox does not relocate the PATH environment variable.
  The bin-layout dir on PATH points at the un-sandboxed _build/.
  This works locally because absolute paths are still readable from
  inside the sandbox, but would break under remote action execution.
  Tracked by test/blackbox-tests/test-cases/bin-pform/sandbox.t.

- Bin_layout.Key.reverse_table grows unboundedly in long-running
  --watch sessions (one entry per unique sorted bin set). Pattern
  inherited from Ppx_driver.Key. Bounded by the number of distinct
  bin sets the workspace evaluates; unlikely to be a real problem.

- Several call sites carry a redundant Action_builder.ignore action_env
  next to the real env application; marked with CR-soon Alizter
  comments for follow-up cleanup.

Signed-off-by: Ali Caglayan <[email protected]>
@Alizter
Alizter force-pushed the push-wrvoymzuvzsq branch from ac0b3b5 to 7892b0d Compare May 21, 2026 23:13
@Alizter
Alizter merged commit a3325c0 into ocaml:main May 21, 2026
29 checks passed
@Alizter
Alizter deleted the push-wrvoymzuvzsq branch May 21, 2026 23:53
Alizter added a commit to Alizter/dune that referenced this pull request May 23, 2026
`(deps (package ...))` materializes a scoped install layout under
`_build/install/<context>/.packages/<digest>/` containing only the declared
package dependencies. This replaces the old alias-based mechanism where
`(deps (package foo))` populated the shared `_build/install/` staging area,
making every package in the workspace discoverable regardless of what was
declared.

The layout includes only immediate deps (no transitive closure). Actions
should declare what they need explicitly. Environment variables (PATH,
OCAMLPATH, CAML_LD_LIBRARY_PATH, OCAMLFIND_IGNORE_DUPS_IN,
OCAMLTOP_INCLUDE_PATH, MANPATH) are set from the layout directory and
consed onto the directory env via extend_action.

The layout only applies to workspace packages. Lock-dir packages and the
site/plugin system are unaffected.

See doc/dev/install-layouts.md for design details and known limitations.

Signed-off-by: Ali Caglayan <[email protected]>

---

Follow-up work (not blocking):

Pre-existing limitations preserved by this PR:

- Lock-dir packages: (deps (package lockdir-pkg)) builds the package
  but does not set up layout env vars. This was already broken before
  this PR (the old `package` function returned only the build action);
  this PR documents it as a known TODO rather than fixing or worsening
  it. dune exec lockdir-tool continues to work via bin/exec.ml's
  unconditional staging cons.

- Sandbox PATH non-relocation: layout dirs on PATH/OCAMLPATH point at
  un-sandboxed _build/. Works locally; would break remote execution.
  Same mechanism as the bin-layout limitation in ocaml#14432.

New limitations from this PR's scope:

- _root section collisions (lib_root, share_root, libexec_root) raise
  hard errors today. Opam-style override via dep DAG ordering is
  future work.

- Install_layout.Key.reverse_table, Bin_layout.Key.reverse_table, and
  Ppx_driver.Key.reverse_table are three copies of the same digest+
  reverse table pattern. Worth a Digest_key functor; non-blocking.

Test coverage gaps:

- Mixing workspace + lock-dir package deps in a single (deps ...).

CR-someday Alizter: factor the Context.for_host fallback used in
make_bin_env, combined_package_deps_builder and
super_context.ml:make_root_env into a single helper.

Signed-off-by: Ali Caglayan <[email protected]>
Alizter added a commit that referenced this pull request May 26, 2026
`(deps (package ...))` materializes a scoped install layout under
`_build/install/<context>/.packages/<digest>/` containing only the
declared package dependencies. This replaces the old alias-based
mechanism where `(deps (package foo))` populated the shared
`_build/install/` staging area, making every package in the workspace
discoverable regardless of what was declared.

The layout includes only immediate deps (no transitive closure). Actions
should declare what they need explicitly. Environment variables (`PATH`,
`OCAMLPATH`, `CAML_LD_LIBRARY_PATH`, `OCAMLFIND_IGNORE_DUPS_IN`,
`OCAMLTOP_INCLUDE_PATH`, `MANPATH`) are set from the layout directory
and consed onto the directory env via `extend_action`.

The layout only applies to workspace packages. Lock-dir packages and the
site/plugin system are unaffected.

See `doc/dev/install-layouts.md` for design details.

Related:

- #7908
- #8652
- #13307
- #13500
- Depends on #14432
- [x] changelog
- [x] documentation (`doc/dev/install-layouts.md`)

## Follow-up

- [ ] refactor the env helpers
- [ ] document env propagation in dune generally
- [ ] exhaustively test env propagation
- [ ] lock-dir packages classify as `Build` and don't set up layout env
vars
- [ ] sandbox PATH non-relocation for layout dirs
- [ ] extract a `Digest_key` functor to dedupe `Install_layout.Key` /
`Bin_layout.Key` / `Ppx_driver.Key`
- [ ] factor the `Context.for_host` fallback into a single helper
- [ ] missing test for mixed workspace + lock-dir package deps
- [ ] dep expander invoked twice on the `(deps (package ...))` path
raphael-proust pushed a commit to ocaml/opam-repository that referenced this pull request Jun 30, 2026
CHANGES:

### Fixed

- Fix promotion failure when a target changes from a directory to a file
  between builds, causing "Is a directory" errors.
  (ocaml/dune#14371, fixes ocaml/dune#5647, fixes ocaml/dune#6575, @Alizter)

- Make `dune build @doc` pick up an odoc installed via `dune tools install
  odoc` even without `DUNE_CONFIG__LOCK_DEV_TOOL=enabled`, mirroring how
  `dune fmt` consumes a locked ocamlformat (ocaml/dune#14426, fixes ocaml/dune#14235, @mt-caret)

- Reject `(package ...)` inside a named dependency binding
  (`(deps (:name (package foo)))`). Previously this was silently accepted but
  `%{name}` would resolve to an empty path list. (ocaml/dune#14499, @Alizter)

- Fix incorrect dependency in the `.cmxs` build for libraries with
  mode-dependent foreign stubs: the rule depended on the byte stubs archive
  instead of the native one, so parallel builds could fail to find
  `-l<lib>_stubs_native`. (ocaml/dune#14500, fixes ocaml/dune#12964, @Alizter)

- Fix `dune build` failing with "No rule found" when `lock_dir` paths in
  `dune-workspace` contain a subdirectory (e.g. `(path sub/dune.lock)`).
  (ocaml/dune#14524, fixes ocaml/dune#14523, @Alizter)

- Artifact substitution repairs executable bit if it's not set correctly in the workspace
  (ocaml/dune#14556, @rgrinberg)

- Ignore `EINVAL` when accepting sockets on MacOS (ocaml/dune#14612, fixes ocaml/dune#12660, @rgrinberg)

- Validate profile names. Profile names must be non-empty and can only contain
  letters, digits, `_` and `-`. The name `_` is reserved as a wildcard in
  `(env ...)` stanzas. (ocaml/dune#14657, fixes ocaml/dune#14630, @rlepigre)

### Added

- Add `%{pkg:<package>:<section>:<path>}` pform for resolving package install
  files. Works with workspace packages, lock-file packages, and installed
  packages. (ocaml/dune#14200, fixes ocaml/dune#14193, fixes ocaml/dune#3378, @Alizter)

- Enable the relocatable compiler by default for package management (ocaml/dune#14357,
  fixes ocaml/dune#14012, @Alizter)

- Add changed source files to the `build-start` trace event for watch-mode
  restarts, showing the paths that triggered the rebuild (ocaml/dune#14396, @rgrinberg)

- Add dune's rusage information in start/finish build trace events (ocaml/dune#14402, @rgrinberg)

- Add a status message for RPC clients that manage to connect (ocaml/dune#14424, @rgrinberg)

- Allow blang expressions in the `runtest_alias` field in the `cram` stanza (ocaml/dune#14425, @rgrinberg)

- Extend the stat based cache to cache the contents of directories and not just
  source files (ocaml/dune#14469, @rgrinberg).

- Show the number of connected RPC clients in the watch mode status line
  (ocaml/dune#14489, @rgrinberg)

- Promote directory targets from experimental to generally available in 3.24
  (ocaml/dune#14579, @rgrinberg)

### Changed

- Use `/` as directory separator when appending local paths to external paths,
  making path construction consistent across platforms. (ocaml/dune#14278, @Alizter)
- On Windows, normalise process paths to `\` before passing them to
  `CreateProcessW` so that programs which scan `argv[0]` (notably `cmd.exe`)
  do not misparse mixed separators. (ocaml/dune#14278, @Alizter)

- `%{bin:NAME}` now resolves to the build artifact path rather than
  the install staging path. Rules with `%{bin:NAME}` deps additionally
  get a per-rule `.binaries` directory prepended to the action's
  `PATH`, containing correctly-named symlinks for each declared bin
  pform dep. (ocaml/dune#14432, fixes ocaml/dune#3324, @Alizter)

- Replace the Rocq language field `(stdlib ...)` with the presence-only field
  `(no_corelib)` in Rocq language 0.14. (ocaml/dune#14452, fixes ocaml/dune#14358, @Durbatuluk1701)

- Throttle sandbox creation to 250 concurrent sandboxes. (ocaml/dune#14464, @rgrinberg)

- The package solver now uses cached revision store lookups for improved
  performance. (ocaml/dune#14494, fixes ocaml/dune#12637, @Alizter, @rgrinberg)

- Remove the deprecated `(lang coq)` Coq Build Language. Use the
  Rocq Build Language (`(using rocq <version>)`) instead. Projects that
  still declare `(using coq <version>)` now get an error pointing them
  at Rocq. (ocaml/dune#14525, fixes ocaml/dune#12788, @Alizter)
@Alizter Alizter added this to the 3.24.0 milestone Jul 8, 2026
craff pushed a commit to craff/opam-repository that referenced this pull request Jul 25, 2026
CHANGES:

### Fixed

- Fix promotion failure when a target changes from a directory to a file
  between builds, causing "Is a directory" errors.
  (ocaml/dune#14371, fixes ocaml/dune#5647, fixes ocaml/dune#6575, @Alizter)

- Make `dune build @doc` pick up an odoc installed via `dune tools install
  odoc` even without `DUNE_CONFIG__LOCK_DEV_TOOL=enabled`, mirroring how
  `dune fmt` consumes a locked ocamlformat (ocaml/dune#14426, fixes ocaml/dune#14235, @mt-caret)

- Reject `(package ...)` inside a named dependency binding
  (`(deps (:name (package foo)))`). Previously this was silently accepted but
  `%{name}` would resolve to an empty path list. (ocaml/dune#14499, @Alizter)

- Fix incorrect dependency in the `.cmxs` build for libraries with
  mode-dependent foreign stubs: the rule depended on the byte stubs archive
  instead of the native one, so parallel builds could fail to find
  `-l<lib>_stubs_native`. (ocaml/dune#14500, fixes ocaml/dune#12964, @Alizter)

- Fix `dune build` failing with "No rule found" when `lock_dir` paths in
  `dune-workspace` contain a subdirectory (e.g. `(path sub/dune.lock)`).
  (ocaml/dune#14524, fixes ocaml/dune#14523, @Alizter)

- Artifact substitution repairs executable bit if it's not set correctly in the workspace
  (ocaml/dune#14556, @rgrinberg)

- Ignore `EINVAL` when accepting sockets on MacOS (ocaml/dune#14612, fixes ocaml/dune#12660, @rgrinberg)

- Validate profile names. Profile names must be non-empty and can only contain
  letters, digits, `_` and `-`. The name `_` is reserved as a wildcard in
  `(env ...)` stanzas. (ocaml/dune#14657, fixes ocaml/dune#14630, @rlepigre)

### Added

- Add `%{pkg:<package>:<section>:<path>}` pform for resolving package install
  files. Works with workspace packages, lock-file packages, and installed
  packages. (ocaml/dune#14200, fixes ocaml/dune#14193, fixes ocaml/dune#3378, @Alizter)

- Enable the relocatable compiler by default for package management (ocaml/dune#14357,
  fixes ocaml/dune#14012, @Alizter)

- Add changed source files to the `build-start` trace event for watch-mode
  restarts, showing the paths that triggered the rebuild (ocaml/dune#14396, @rgrinberg)

- Add dune's rusage information in start/finish build trace events (ocaml/dune#14402, @rgrinberg)

- Add a status message for RPC clients that manage to connect (ocaml/dune#14424, @rgrinberg)

- Allow blang expressions in the `runtest_alias` field in the `cram` stanza (ocaml/dune#14425, @rgrinberg)

- Extend the stat based cache to cache the contents of directories and not just
  source files (ocaml/dune#14469, @rgrinberg).

- Show the number of connected RPC clients in the watch mode status line
  (ocaml/dune#14489, @rgrinberg)

- Promote directory targets from experimental to generally available in 3.24
  (ocaml/dune#14579, @rgrinberg)

### Changed

- Use `/` as directory separator when appending local paths to external paths,
  making path construction consistent across platforms. (ocaml/dune#14278, @Alizter)
- On Windows, normalise process paths to `\` before passing them to
  `CreateProcessW` so that programs which scan `argv[0]` (notably `cmd.exe`)
  do not misparse mixed separators. (ocaml/dune#14278, @Alizter)

- `%{bin:NAME}` now resolves to the build artifact path rather than
  the install staging path. Rules with `%{bin:NAME}` deps additionally
  get a per-rule `.binaries` directory prepended to the action's
  `PATH`, containing correctly-named symlinks for each declared bin
  pform dep. (ocaml/dune#14432, fixes ocaml/dune#3324, @Alizter)

- Replace the Rocq language field `(stdlib ...)` with the presence-only field
  `(no_corelib)` in Rocq language 0.14. (ocaml/dune#14452, fixes ocaml/dune#14358, @Durbatuluk1701)

- Throttle sandbox creation to 250 concurrent sandboxes. (ocaml/dune#14464, @rgrinberg)

- The package solver now uses cached revision store lookups for improved
  performance. (ocaml/dune#14494, fixes ocaml/dune#12637, @Alizter, @rgrinberg)

- Remove the deprecated `(lang coq)` Coq Build Language. Use the
  Rocq Build Language (`(using rocq <version>)`) instead. Projects that
  still declare `(using coq <version>)` now get an error pointing them
  at Rocq. (ocaml/dune#14525, fixes ocaml/dune#12788, @Alizter)
Gbury pushed a commit to Gbury/flambda-backend that referenced this pull request Nov 26, 2026
Dune 3.25.0 introduced a change that breaks Merlin’s test suite. For
`%{bin:...}` dependencies, the binary now only gets added to the PATH if
it is a direct dependency rather than indirect via an alias. (This
change was made in ocaml/dune#14432 and
discussed in ocaml/dune#16617.)

Merlin’s ocaml index tests fall victim to this. As a result of the
change, the tests now fall back to the `ocaml-index` on the user’s PATH
rather than using the one built from the repo. The fix was to inline an
alias (that conveniently was only used once) to that the dependency is
now direct.
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

package management Dune's package management — `(pkg)` stanza, lockdirs, `dune pkg` commands

Projects

None yet

Development

Successfully merging this pull request may close these issues.

dune build %{bin:foo} returns an error

2 participants