Skip to content

pkg: load dune projects and allow in and out dependencies - #16995

Draft
Alizter wants to merge 6 commits into
ocaml:mainfrom
Alizter:prototype/flatten-dune-v2
Draft

Alizter wants to merge 6 commits into
ocaml:mainfrom
Alizter:prototype/flatten-dune-v2

Conversation

@Alizter

@Alizter Alizter commented Nov 11, 2026 •

Copy link
Copy Markdown
Collaborator

Here is the second public sprint of "flattening dune".

Description

Load dune projects from locked packages directly (#9983) and allow locked packages to depend on workspace packages (#8652).

Technical Overview

(Disclaimer: This is my rough intention with these changes, it might not be faithfully reflected in the changes yet)

All (non-empty) locked packages have their sources "mounted" directly in _build/default/.pkg. Then a heuristic, based on dune-project and it's (package) stanzas, is used to classify a given package as "native" or "opaque".

Native packages are dune projects, and the sources _build/default/.pkg/foo are directly fetched/prepared and loaded as usual dune projects are, acting like a monorepo build. As a result they inherit all the useful compositionality that dune projects already had, except for a few differences. Aliases don't enter mounts, cram tests are not loaded, formatting is not checked etc.

Opaque packages fallback to the translated build recipe found in the lock directory entry, and are built using the old package rules. They produce a _build/default/.pkg/bar/_install directory target with the install layout. They partake in the composition of binaries, libraries and packages, whilst trying to be as lazy as possible. Allowing for a seamless integration in the workspace.

Many rules needed to be adapted to accept a more general notion of Source_tree that allows for build-backed paths. Much of dune lang had to be adpated to be agnostic to source or build paths.

Mounted packages are not allowed to register promotions of any kind.

TODO: lots of little details that need expansion

Remaining design

This is enough for dune to be able to be built with dune pkg, with a few tweaks in the workspace and dependency packages.

There is still some LLMisms and cruft that needs to cut from the PR.

@Alizter Alizter added slop Needs to be desloppified. package management Dune's package management — `(pkg)` stanza, lockdirs, `dune pkg` commands labels Nov 11, 2026
Comment thread src/dune_engine/build_system.ml Resolved
Comment thread src/dune_engine/rule.mli Resolved
@rgrinberg

Copy link
Copy Markdown
Member

I find it difficult to understand why we would ever need a "hidden" stanza. Everything that a stanza can accomplish is easier accomplished by just doing the thing your hidden stanza intended to trigger directly. It seems like this is just an LLM artifact that should be easily simplified away.

Comment thread opam/dune.opam Resolved
@Alizter

Alizter commented Nov 11, 2026

Copy link
Copy Markdown
Collaborator Author

Summarising the feedback from the dev meeting 11-11-2026:

The PR looks unlikely to be able to be broken into smaller meaningful chunks. The focus should instead be on making sure all the LLMisms are cut and we have a solid feature, however big it needs to be.

The direction with an opam stanza is a red herring and should be cut, the composable opam rules backing them are the important parts.

Promotion registration should be handled from the rules rather than the engine trying to guess it. There ideally should be no engine changes overall.

@rgrinberg

Copy link
Copy Markdown
Member

I wouldn't say that we are looking to minimize changes to the engine, but we only want to add what we need and articulate why we need it.

The changes to the engine in this PR are mostly not particularly well motivated. As far as I can tell, all the fundamental levers in the engine are already in place, so we just need to use them correctly. Maybe the only thing that is missing is being able to work with empty and anonymous contexts directly, but I don't really see a clean API in this PR that does that.

@Alizter

Alizter commented Nov 11, 2026

Copy link
Copy Markdown
Collaborator Author

@rgrinberg What is an empty or anonymous context? (And how does it relate?)

@rgrinberg

Copy link
Copy Markdown
Member

It is anonymous because its name does not really matter. It is not available for the user to refer to. It is empty because there are no copying rules in it by default.

@Alizter

Alizter commented Nov 11, 2026

Copy link
Copy Markdown
Collaborator Author

Is this assuming that package builds occur in separate contexts? Because that's not what is happening here although I had tried this design in the past. All the package builds occur in the same single context as if they were in a monorepo. The only difference being that Source_tree.t can be backed by arbitrary build rules, in this case something copying and/or patching from _fetch.

@rgrinberg

Copy link
Copy Markdown
Member

No, improved the API could help existing code. It does not require anything from this PR.

I haven't yet looked at the source tree changes here.

Alizter added a commit that referenced this pull request Nov 24, 2026
Record the duplicate install-cookie entries produced when an install
action copies a file also declared by its `.install` manifest. Cover the
bin section and confirm a dependent package can still read the installed
file; installation behavior is unchanged.

Found when working on #16995
@Alizter
Alizter force-pushed the prototype/flatten-dune-v2 branch 3 times, most recently from d80639a to abe4616 Compare November 26, 2026 20:03
@Alizter

Alizter commented Nov 26, 2026 •

Copy link
Copy Markdown
Collaborator Author

I've pushed the result of the last two week sprint. I managed to cut out a lot of useless changes, but there is still a lot of curft to be removed. I've updated the PR description with some technical overview of the changes too.

On my machine null builds without dune pkg are around 1.5s, with dune pkg 5.3s.

PLAN.md contains insights on the remaining tasks that need to be done and information about some design decisions that have been made. Eventually this will morph into a design document, but for now is an agent coordination board.

@Alizter
Alizter force-pushed the prototype/flatten-dune-v2 branch 2 times, most recently from 500f14b to 1ba40f0 Compare November 26, 2026 22:20
With a relocatable compiler, launchers such as ocaml and ocamlmklib need
the runtime installed next to them. A rule running one depends only on the
tool, so a sandboxed rule cannot find the runtime.

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

A context with a locked compiler provides a dependency on the lock
directory's (ocaml ..) package and its closure. Rules that run a compiler
tool add it, and so does a user action whose program is installed by one
of those packages, so a sandboxed rule finds the whole installation, the
runtime of a relocatable compiler's launchers for instance.

Signed-off-by: Ali Caglayan <[email protected]>
A package's (dir ..) must name a directory inside the project that
declares the package.

Signed-off-by: Ali Caglayan <[email protected]>
deps-bin-pform.t's "A name installed by two workspace packages" covers the
same setup and also checks the PATH route.

Signed-off-by: Ali Caglayan <[email protected]>
@Alizter
Alizter force-pushed the prototype/flatten-dune-v2 branch 3 times, most recently from b601a4b to e4f04b9 Compare November 27, 2026 00:07
@Alizter Alizter changed the title prototype: building dune with dune pkg pkg: load dune projects and allow in and out dependencies Nov 27, 2026
@Alizter
Alizter force-pushed the prototype/flatten-dune-v2 branch from e4f04b9 to 211643f Compare November 27, 2026 09:12
; promotion : bool
(** Whether the diffs this action runs may register promotions: its
diff actions and the sandbox's corrections. When [false] a
mismatch still fails. *)

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

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

This field should not be needed. When setting up the rules, we should modify the actions so that they produce the promotions. This might involve removing a certain flag that allows actions to produce corrections. It might also involve creating a new action like diff that doesn't create promotions on failure. But it should not be done at this layer.

Expands [targets] with the set of files discovered in directory targets. *)
val move_targets_to_build_dir
: t
-> promotion:bool

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

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

Same as the other comment. We should setup the rules in such a way so that the engine doesn't look for promotions in the first place

This branch has not been deployed

No deployments
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 slop Needs to be desloppified.

Projects

None yet

Development

Successfully merging this pull request may close these issues.

Build Dune Packages Overlapping Dependencies in Lock Directory ("in and out problem")

2 participants