From e58863b69b2d950110226a3bedd9f898007707a7 Mon Sep 17 00:00:00 2001 From: Sachin Beniwal Date: Thu, 18 Dec 2025 21:04:27 +0530 Subject: [PATCH 1/2] fix: support relative paths outside workspace in dune install --prefix Signed-off-by: Sachin Beniwal --- bin/install_uninstall.ml | 4 +++- otherlibs/stdune/src/path.ml | 9 +++++++ otherlibs/stdune/src/path.mli | 5 ++++ .../test-cases/install-relative-prefix.t | 24 +++++++++++++++++++ 4 files changed, 41 insertions(+), 1 deletion(-) create mode 100644 test/blackbox-tests/test-cases/install-relative-prefix.t diff --git a/bin/install_uninstall.ml b/bin/install_uninstall.ml index d7b2414df98..ef328aa7ff5 100644 --- a/bin/install_uninstall.ml +++ b/bin/install_uninstall.ml @@ -34,7 +34,9 @@ let interpret_destdir ~destdir path = let get_dirs context ~prefix_from_command_line ~from_command_line = let open Fiber.O in let module Roots = Install.Roots in - let prefix_from_command_line = Option.map ~f:Path.of_string prefix_from_command_line in + let prefix_from_command_line = + Option.map ~f:Path.of_string_allow_outside_workspace prefix_from_command_line + in let+ roots = match prefix_from_command_line with | None -> Memo.run (Context.roots context) diff --git a/otherlibs/stdune/src/path.ml b/otherlibs/stdune/src/path.ml index 66a354910e8..75e2f8c3ca8 100644 --- a/otherlibs/stdune/src/path.ml +++ b/otherlibs/stdune/src/path.ml @@ -945,6 +945,15 @@ let of_filename_relative_to_initial_cwd fn = external_ (External.of_filename_relative_to_initial_cwd fn) ;; +let of_string_allow_outside_workspace s = + if Filename.is_relative s + then ( + match Local.L.relative_result Local.root (explode_path s) with + | Ok _ -> of_string s + | Error `Outside_the_workspace -> of_filename_relative_to_initial_cwd s) + else of_string s +;; + let to_absolute_filename t = Outside_build_dir.to_absolute_filename (local_or_external t) let external_of_local x ~root = diff --git a/otherlibs/stdune/src/path.mli b/otherlibs/stdune/src/path.mli index 02b5f0cfe78..835aeac842f 100644 --- a/otherlibs/stdune/src/path.mli +++ b/otherlibs/stdune/src/path.mli @@ -249,6 +249,11 @@ val relative_to_source_in_build_or_external to the initial directory dune was launched in. *) val of_filename_relative_to_initial_cwd : string -> t +(** Like [of_string], but if the path is relative and would escape the workspace + (e.g., [../foo]), it is treated as relative to the initial directory dune + was launched in instead of raising an error. *) +val of_string_allow_outside_workspace : string -> t + (** Convert a path to an absolute filename. Must be called after the workspace root has been set. [root] is the root directory of local paths *) val to_absolute_filename : t -> string diff --git a/test/blackbox-tests/test-cases/install-relative-prefix.t b/test/blackbox-tests/test-cases/install-relative-prefix.t new file mode 100644 index 00000000000..0bb8e5abd14 --- /dev/null +++ b/test/blackbox-tests/test-cases/install-relative-prefix.t @@ -0,0 +1,24 @@ +Test that dune install accepts relative paths for --prefix that are outside the workspace. +This is a regression test for issue #12241. + + $ cat > dune-project < (lang dune 3.6) + > (package (name foo)) + > EOF + + $ cat > dune < (library + > (name foo) + > (public_name foo)) + > EOF + + $ cat > foo.ml < let x = 1 + > EOF + + $ dune build @install + +Test that relative path outside workspace works (previously failed with "path outside the workspace"): + + $ dune install --prefix ../install-target --dry-run --display short 2>&1 | grep "Creating directory" | head -1 + Creating directory $TESTCASE_ROOT/../install-target/lib/foo From 7ce835b40ad35ac62b2983fb795c3d5ae9e60360 Mon Sep 17 00:00:00 2001 From: Sachin Beniwal Date: Sun, 28 Dec 2025 05:32:57 +0530 Subject: [PATCH 2/2] chore: added CHANGES entry Signed-off-by: Sachin Beniwal --- doc/changes/fixed/12993.md | 2 ++ 1 file changed, 2 insertions(+) create mode 100644 doc/changes/fixed/12993.md diff --git a/doc/changes/fixed/12993.md b/doc/changes/fixed/12993.md new file mode 100644 index 00000000000..ca57b8195f4 --- /dev/null +++ b/doc/changes/fixed/12993.md @@ -0,0 +1,2 @@ +- Fix `dune install --prefix` failing with relative paths outside the workspace + like `../foo` (#12993, fixes #12241, @benodiwal)