dune icon indicating copy to clipboard operation
dune copied to clipboard

Adding constraint to `ocamlformat` developer tool fails

Open Sudha247 opened this issue 1 month ago • 1 comments

Expected Behavior

Adding constraints to developer tools in general, and ocamlformat in particular lets you install the version of ocamlformat per the constraints (as long as it exists)

Actual Behavior

Adding constraints to the path of ocamlformat leads it the path being treated as an external path outside _build, but in reality it is inside _build.

Internal error, please report upstream including the contents of _build/log.
Description:
  ("as_outside_build_dir_exn",
   { path =
       In_build_dir ".dev-tools.locks/ocamlformat/ocaml-compiler-libs.files"
   })
Raised at Stdune__Code_error.raise in file "stdune__Code_error.ml", line 11,
  characters 30-62
Called from Dune_rules__Lock_rules.scan_lock_directory.scan in file
  "dune_rules__Lock_rules.ml", line 431, characters 25-60
Called from Fiber__Core.apply2 in file "fiber__Core.ml", line 93, characters
  6-11
-> required by ("gen-rules", In_build_dir "_private/default/.lock")
-> required by ("load-dir", In_build_dir "_private/default/.lock")
-> required by
   ("build-file", In_build_dir "_private/default/.lock/dune.lock")
-> required by ("<unnamed>", ())
-> required by
   ("gen-rules", In_build_dir "_private/default/.dev-tool/ocamlformat")
-> required by
   ("gen-rules", In_build_dir "_private/default/.dev-tool/ocamlformat/target")
-> required by
   ("gen-rules",
    In_build_dir "_private/default/.dev-tool/ocamlformat/target/bin")
-> required by
   ("load-dir",
    In_build_dir "_private/default/.dev-tool/ocamlformat/target/bin")
-> required by
   ("build-file",
    In_build_dir
      "_private/default/.dev-tool/ocamlformat/target/bin/ocamlformat")
-> required by ("toplevel", ())

I must not crash.  Uncertainty is the mind-killer. Exceptions are the
little-death that brings total obliteration.  I will fully express my cases. 
Execution will pass over me and through me.  And when it has gone past, I
will unwind the stack along its path.  Where the cases are handled there will
be nothing.  Only I will remain.

Reproduction

  • PR with a reproducing test:
$ cat > dune <<'EOF'
(executable
 (public_name main))
EOF

$ cat > dune-workspace <<'EOF'
(lang dune 3.21)

(pkg enabled)

(repository
 (name oxcaml)
 (url git+file:///home/sudha/ocaml/work/oxcaml/oxcaml-opam-repository))

(pin
 (name ocamlbuild)
 (url "git+https://github.com/Sudha247/ocamlbuild#oxcaml+dune")
 (package
  (name ocamlbuild)
  (version 0.15.0+ox)))

(lock_dir
 (path "dune.lock")
 (repositories overlay oxcaml upstream))

(lock_dir
 (path "_build/.dev-tools.locks/ocaml-lsp-server")
 (pins ocamlbuild)
 (constraints
  (ocaml-lsp-server (= 1.19.0+ox))
  (ocaml-variants (= 5.2.0+ox)))
 (repositories :standard oxcaml))
EOF

$ cat > dune-project <<'EOF'
(lang dune 3.21)

(package
 (name hello-oxcaml)
 (depends
  ocaml
  (ocaml-variants
   (= 5.2.0+ox))))
EOF

$ cat > main.ml <<'EOF'
let () =
  (* The `local_` keyword requires OxCaml *)
  let local_ i = 42 in
  let j = i + 1 in
  Printf.printf "%d\n" j
EOF

$ dune tools install ocamlformat

Specifications

  • Version of dune (output of dune --version): "Nightly build 2025-12-04T02:46:51Z, git revision b25137a5321432edb6a36a19c1c5d5484bab5252"
  • Version of ocaml (output of ocamlc --version): 5.2.0+ox
  • Operating system (distribution and version): Ubuntu 20.02

Sudha247 avatar Dec 05 '25 11:12 Sudha247

Is this a duplicate (or at least a special case of) https://github.com/ocaml/dune/issues/12777 ?

shonfeder avatar Dec 11 '25 06:12 shonfeder

It is definitely related to #12777. However, this one is a bug report of an existing failure, and #12777 is a feature enhancement request for specifying constraints.

Sudha247 avatar Dec 15 '25 07:12 Sudha247

The same behaviour happens when adding constraints to other dev tools such as ocamllsp. As the error message indicates, I suspect it has something to do with the path, _build/.dev-tools.locks/ocaml-lsp-server, being treated as an external path when it is actually inside the _build directory.

Sudha247 avatar Dec 22 '25 10:12 Sudha247

After digging into this a bit and trying to reproduce this with a cram test, I think this could be a special case of https://github.com/ocaml/dune/issues/12851. The failure happens specifically for .patch files within .dev-tools.locks/. I will try to dig further.

Sudha247 avatar Dec 22 '25 12:12 Sudha247