Skip to content

Conversation

@nojb
Copy link
Collaborator

@nojb nojb commented Dec 6, 2025

This PR is an attempt to define a (files) stanza following discussion in #11819. The syntax (and semantics) are rather close to the existing (dirs) stanza. See the doc and the example for more.

Only the dune-file-level stanza is introduced here. Later, we could allow specifying it at the level of dune-project and/or dune-workspace to provide project- or workspace-wide defaults.

I am not very familiar with this part of the code (the source tree scanning logic), so a review by someone knowledgeable would be appreciated.

@gasche: could you try this branch with your use case and report back if it works as expected? You will need to add something like (files :standard \ *.cm* *.o *.a) to the Dune files of the compiler tree. You will also need to bump the (lang dune ...) version in dune-project to 3.21 (I tried to do it quickly, but it needs some other adaptations and I ran out of time to investigate that!).

@gasche
Copy link
Member

gasche commented Dec 6, 2025

Thanks a lot!

I tried to do what you suggested, and then I realized that you were hoping that I would know how to update the dune build system for the compiler from dune-lang 1.10 to 3.20, before I could test this. Nope, nope! It takes way more expertise with dune than I have to know how to do this. (It requires using foreign_stubs.)

But then I just took your PR and changed the line Dune_lang.Syntax.since Stanza.syntax (3, 21) to use (1, 10) instead. And it just works! I had to apply the following diff to get the OCaml compiler to build from an already-built tree (after make -j has run).

diff --git i/asmcomp/dune w/asmcomp/dune
index fb3da1060d6..1a869af39f3 100644
--- i/asmcomp/dune
+++ w/asmcomp/dune
@@ -12,6 +12,8 @@
 ;*                                                                        *
 ;**************************************************************************
 
+(files :standard \ emit.ml)
+
 (rule
  (targets arch.ml CSE.ml proc.ml reload.ml scheduling.ml selection.ml
           stackframe.ml)
diff --git i/otherlibs/str/dune w/otherlibs/str/dune
index 0c96b6385a1..94db27e32ee 100644
--- i/otherlibs/str/dune
+++ w/otherlibs/str/dune
@@ -12,6 +12,8 @@
 ;*                                                                        *
 ;**************************************************************************
 
+(files :standard \ *.cm* *.o *.a)
+
 (library
  (name str)
  (modes byte)
diff --git i/otherlibs/unix/dune w/otherlibs/unix/dune
index bc2c4709616..22fdb95f0ba 100644
--- i/otherlibs/unix/dune
+++ w/otherlibs/unix/dune
@@ -12,6 +12,8 @@
 ;*                                                                        *
 ;**************************************************************************
 
+(files :standard \ *.cm* *.o *.a)
+
 (library
  (name unix)
  (wrapped false)
diff --git i/stdlib/dune w/stdlib/dune
index 1e530bc83eb..667821f1614 100644
--- i/stdlib/dune
+++ w/stdlib/dune
@@ -12,6 +12,8 @@
 ;*                                                                        *
 ;**************************************************************************
 
+(files :standard \ *.cm* *.o *.a)
+
 (library
  (name stdlib)
  (libraries dune_support)

Pretty simple! In particular, it is interesting that I did not have to change most of the compiler source directories (like parsing/, typing/ etc.). I am not sure why, but my best guess is that this comes from the copy_files# in the toplevel dune repository, that would somehow have the effect of making dune ignore the rest of those directories and in particular the build artifacts they contain. (This also suggests a way to possibly do without this feature at all to build the compiler with build artifacts lying around, by systematically using these copy_files# directives for all source directories.)

In summary: I had to use a hack, and then it works very well, but maybe a Dune-and-compiler expert could do even better.

(It is a hack to claim that a new feature is available with old dune languages, but it is a harmless hack, right? The guarantees that people want to have is that old dune files keep working with newer Dune versions, and silently adding new stanzas to old dune-language versions preserves this. Removing stanzas/features or changing the build semantics of existing features would be evil, but this is not.)

@Alizter
Copy link
Collaborator

Alizter commented Dec 6, 2025

@gasche I have a patch updating the compiler to 3.19. Feel free to take over: ocaml/ocaml#14408.

nojb added 2 commits December 7, 2025 09:50
Signed-off-by: Nicolás Ojeda Bär <n.oje.bar@gmail.com>
Signed-off-by: Nicolás Ojeda Bär <n.oje.bar@gmail.com>
nojb added 2 commits December 7, 2025 10:07
Signed-off-by: Nicolás Ojeda Bär <n.oje.bar@gmail.com>
Signed-off-by: Nicolás Ojeda Bär <n.oje.bar@gmail.com>
@nojb
Copy link
Collaborator Author

nojb commented Dec 7, 2025

@Alizter I see you put yourself as a reviewer. I will wait for your approval before merging. Thanks!

@nojb nojb merged commit f181cca into ocaml:main Dec 8, 2025
29 checks passed
@nojb nojb deleted the files_stanza branch December 8, 2025 09:33
davesnx added a commit to davesnx/dune that referenced this pull request Dec 8, 2025
…without-system

* 'main' of github.com:/ocaml/dune: (30 commits)
  Add (files) stanza (ocaml#12879)
  Make sure to use string equality in parsing hot path (ocaml#12874)
  Hoist up [Dune_sexp.Decoder.sum] uses (ocaml#12876)
  Delay ocaml-index dependencies (ocaml#12881)
  Simplify Lib.requires (ocaml#12880)
  Lib: delay applying modules (ocaml#12884)
  Annotate some ignored parameters in lib.ml (ocaml#12883)
  feat: expand variables in `(promote (into ..))` (ocaml#12832)
  dune-binaries.t: nix ci (ocaml#12875)
  fix: greedy version location in lang declarations (ocaml#12869)
  Reproduction case for ocaml#6220. (ocaml#6221)
  Defunctionalize the dune sexp decoder (ocaml#12768)
  disable flake (ocaml#12873)
  refactor: [instantiate] takes a regular library (ocaml#12849)
  Respect potentially existing lock dir when running format rules (ocaml#12847)
  fix: ascii start
  chore: updated CR someday
  chore: added entry for CHANGES.md
  fix: else claude message
  tests: promoted non-ascii-characters.t to new logic
  ...
@gasche
Copy link
Member

gasche commented Dec 8, 2025

Thanks everyone (in particular @nojb for writing the PR an @Alizter for fixing the OCaml dune build system) for your help on this issue. I think that this will be notably helpful to compiler developers.

shonfeder added a commit to shonfeder/opam-repository that referenced this pull request Dec 15, 2025
CHANGES:

### Fixed

- Fix `include_subdirs qualified` incorrectly picking the furthest module
  instead of the closest when resolving module name ambiguities. (ocaml/dune#12587,
  @ElectreAAS and @Alizter)

- Fix: include the module alias in the transitive dependency closure with
  `(include_subdirs qualified)`. (ocaml/dune#12299, @anmonteiro)

- Improve error messages for invalid version formats containing non-ASCII
  characters. Previously, non-ASCII characters in version strings (e.g., `(lang
  dune è)` or `(using menhir π3.14)`) would fail with a generic "Invalid file"
  error. Now they display a clear message: "Invalid atom: contains non-ASCII
  character(s). Atoms must only contain ASCII characters." The fix is
  implemented at the lexer level, providing consistent error handling across all
  s-expression parsing. (ocaml/dune#12844, fixes ocaml/dune#12836, @benodiwal)

- Pass private modules with -H when this is available (ocaml/dune#12666, @rgrinberg)

- Allow multiple modules in `(modules_flags ...)`, in `coq.theory` (ocaml/dune#12733, @rlepigre)

- Improve error message for invalid version formats in both `(lang dune ...)` and
  `(using extension ...)` declarations. Changes "Atom of the form NNN.NNN expected"
  to "Invalid version. Version must be two numbers separated by a dot." (ocaml/dune#12833, @benodiwal)

- Fix crash when running `dune build @check` on a library with virtual modules.
  (ocaml/dune#12644, fixes ocaml/dune#12636, @Alizter)

- Provide a more informative error message when `(pkg enabled)` is put in
  `dune-project` instead of `dune-workspace`. (ocaml/dune#12802, fixes ocaml/dune#12801,
  @benodiwal)

- Improve error message when invalid version strings are used in `dune-project`
  files. Non-ASCII characters and malformed versions now show a helpful hint
  with an example of the correct format. (ocaml/dune#12794, fixes ocaml/dune#12751, @benodiwal)

- Stop hiding the `root_module` from the include path (ocaml/dune#12239, @rgrinberg)

- Allow `$ dune init` to work on absolute paths (ocaml/dune#12601, fixes ocaml/dune#7806,
  @rgrinberg)

- `(include_subdirs qualified)`: Add missing alias dependency to module group.
  (ocaml/dune#12530, @anmonteiro)

- Add Melange compilation to the `@all` alias in libraries (ocaml/dune#12628,
  @anmonteiro)

- Fix greedy version location in lang declarations. Previously, error locations for
  invalid lang versions would span multiple bytes for multi-byte UTF-8 characters,
  causing carets to appear misaligned and seemingly include the closing
  parenthesis. Now, error locations for ASCII strings show the full length (e.g.,
  "Ali" shows `^^^`), while non-ASCII strings show only the first byte (e.g., "è"
  shows `^`) to avoid multi-byte character display issues. (ocaml/dune#12869, fixes ocaml/dune#12806,
  @benodiwal)

- melange support: don't emit empty JavaScript modules for generated module
  aliases. (ocaml/dune#12464, @anmonteiro)

### Added

- (Experimental): Introduce the `library_parameter` stanza. It allows users to
  declare a parameter when using the OxCaml compiler.
  (ocaml/dune#11963, implements ocaml/dune#12084, @maiste)

- Added the ability to scroll horizontally in TUI. (ocaml/dune#12386, @Alizter)

- Feature: Include shell command that was executed when a cram test has
  occurred in the error message (ocaml/dune#12307, @rgrinberg)

-  support expanding variables in `(promote (into ..))` (ocaml/dune#12832, fixes ocaml/dune#12742,
   @anmonteiro)

- Add support for `%{cmt:...}` and `%{cmti:...}` variables to reference
  compiled annotation files (.cmt and .cmti) containing typed abstract syntax
  trees with location and type information. (ocaml/dune#12634, grants ocaml/dune#12633, @Alizter)

- Add `$ dune describe tests` to describe the tests in the workspace
  (@Gromototo, ocaml/dune#12545, fixes ocaml/dune#12030)

- Add `argv`, the process environment, and the dune version to the config event
  in the trace (ocaml/dune#12909, @rgrinberg)

- Allow `dune runtest` to properly run while a watch mode server is running.
  (ocaml/dune#12473, grants ocaml/dune#8114, @gridbugs and @ElectreAAS)

- Use copy-on-write (COW) when copying files on filesystems that support it
  (Btrfs, ZFS, XFS, etc), under Linux. (ocaml/dune#12074, fixes ocaml/dune#12071, @nojb)

- Add support for Tangled ATproto-based code repositories (ocaml/dune#12197, @avsm)

- Add support for instantiating OxCaml parameterised libraries.
  (ocaml/dune#12561, @art-w)

- Add a `(conflict_markers error|ignore)` option to the cram stanza. When
  `(conflict_markers error)` is set, the cram test will fail in the presence of
  conflict markers. Git, diff3 and jujutsu conflict markers are detected.
  (ocaml/dune#12538, ocaml/dune#12617, ocaml/dune#12655, fixes ocaml/dune#12512, @rgrinberg, @Alizter)

- Introduce a `%{ppx:lib1+..+libn}` stanza to make it possible to refer to ppx
  executables built by dune. This is useful for writing tests (ocaml/dune#12711,
  @rgrinberg)

- Introduce a `(dir ..)` field on packages defined in the `dune-project`. This
  field allows to associate a directory with a particular package. This makes
  dune automatically filter out all stanzas in this directory and its
  descendants with `--only-packages`. All users are recommended to switch to
  using this field. (ocaml/dune#12614, fixes ocaml/dune#3255, @rgrinberg)

- Add support for `DUNE_ROOT` environment variable, similar to the existing
  `--root` CLI parameter. (fixes ocaml/dune#12399 @sir4ur0n)

- Introduce an `unused-libs` alias to detect unused libraries.
  (ocaml/dune#12623, fixes ocaml/dune#650, @rgrinberg)

- Add `--files` flag to `dune describe opam-files` to print only the names of
  the opam files line by line. (ocaml/dune#9793, @reynir and @Alizter)

- `dune exec` now accepts absolute paths inside the workspace.
  (ocaml/dune#12094, @Alizter)

- Add `coqdoc_header` and `coqdoc_footer` fields to the `coq` field of the
  `env` stanza, and to the `coq.theory` stanza, allowing to configure a
  custom header or footer respectively in the HTML output of `coqdoc`.
  (ocaml/dune#11131, @rlepigre)

- Allow `dune fmt` to properly run while a watch mode server is running.
  Note that the `--preview` flag is not supported in this mode.
  (ocaml/dune#12064, @ElectreAAS)

- Support for generating `_CoqProject` files for `coq.theory` stanzas.
  (ocaml/dune#11752, @rlepigre)

- Added `(files)` stanza, similar to `(dirs)` to control which files are visible
  to Dune on a per-directory basis. (ocaml/dune#12879, @nojb)
- Add support for %{ocaml-config:ox} (ocaml/dune#12236, @jonludlam)

- Introduce `dune promotion show` command to display the contents of corrected
  files that are ready for promotion. This allows users to preview changes
  before running `dune promote`. The command accepts file arguments to show
  specific files, or displays all promotable files when called without
  arguments. (ocaml/dune#12669, fixes ocaml/dune#3883, @MixiMaxiMouse)
- New `(lang rocq)` build mode for Rocq 9.0 and later. This new mode
  is very similar to the existing `(lang coq)`, except that it doesn't
  need the `coq*` compatibility wrappers. As of today `(lang rocq)`
  doesn't support yet composed builds with Rocq itself, this will be
  added later.  `(lang coq)` is deprecated, development is frozen, and
  will be removed at some point in the future. (ocaml/dune#12035, @ejgallego,
  @Lysxia, fixes ocaml/dune#11572)

### Changed

- Don't run `ocamldep` to compute false dependencies on the `root_module`
  (ocaml/dune#12227, @rgrinberg)

- `dune format-dune-file` now uses the syntax version of the Dune project that
  contains the file being formatted (if any) instead of using the latest version
  available, which remains the default if there is no Dune project in scope.
  (ocaml/dune#11865, @nojb)

- Persistent DB and process events have been slightly modified. Persistent
  DB events have more concise names and job events always include full
  information. (ocaml/dune#12867, @rgrinberg)

- Removed the `--trace-extended` flag. Its functionality is always enabled when
  tracing is active (ocaml/dune#12908, @rgrinberg)

- The `test/dune` file generated by `dune init proj` now depends on the project library. (ocaml/dune#12791, @shonfeder)

- Starting with version 3.21 of the Dune language, Dune no longer changes the
  default set of compiler warnings. For users that would like to keep the old
  behaviour, the variable `%{dune-warnings}` can be used in an `(env)` stanza in
  a top-level Dune file: `(env (dev (flags :standard %{dune-warnings})))`.
  (ocaml/dune#12766, @nojb)
- Fix: stop generating `cmt` files for cinaps binaries (ocaml/dune#12530, @rgrinberg)
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