Skip to content

Commit 5f27044

Browse files
committed
fix: support relative paths outside workspace in dune install --prefix
Signed-off-by: Sachin Beniwal <s474996633@gmail.com>
1 parent 36704d6 commit 5f27044

File tree

4 files changed

+41
-1
lines changed

4 files changed

+41
-1
lines changed

bin/install_uninstall.ml

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -34,7 +34,9 @@ let interpret_destdir ~destdir path =
3434
let get_dirs context ~prefix_from_command_line ~from_command_line =
3535
let open Fiber.O in
3636
let module Roots = Install.Roots in
37-
let prefix_from_command_line = Option.map ~f:Path.of_string prefix_from_command_line in
37+
let prefix_from_command_line =
38+
Option.map ~f:Path.of_string_allow_outside_workspace prefix_from_command_line
39+
in
3840
let+ roots =
3941
match prefix_from_command_line with
4042
| None -> Memo.run (Context.roots context)

otherlibs/stdune/src/path.ml

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -945,6 +945,15 @@ let of_filename_relative_to_initial_cwd fn =
945945
external_ (External.of_filename_relative_to_initial_cwd fn)
946946
;;
947947

948+
let of_string_allow_outside_workspace s =
949+
if Filename.is_relative s
950+
then (
951+
match Local.L.relative_result Local.root (explode_path s) with
952+
| Ok _ -> of_string s
953+
| Error `Outside_the_workspace -> of_filename_relative_to_initial_cwd s)
954+
else of_string s
955+
;;
956+
948957
let to_absolute_filename t = Outside_build_dir.to_absolute_filename (local_or_external t)
949958

950959
let external_of_local x ~root =

otherlibs/stdune/src/path.mli

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -249,6 +249,11 @@ val relative_to_source_in_build_or_external
249249
to the initial directory dune was launched in. *)
250250
val of_filename_relative_to_initial_cwd : string -> t
251251

252+
(** Like [of_string], but if the path is relative and would escape the workspace
253+
(e.g., [../foo]), it is treated as relative to the initial directory dune
254+
was launched in instead of raising an error. *)
255+
val of_string_allow_outside_workspace : string -> t
256+
252257
(** Convert a path to an absolute filename. Must be called after the workspace
253258
root has been set. [root] is the root directory of local paths *)
254259
val to_absolute_filename : t -> string
Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,24 @@
1+
Test that dune install accepts relative paths for --prefix that are outside the workspace.
2+
This is a regression test for issue #12241.
3+
4+
$ cat > dune-project <<EOF
5+
> (lang dune 3.6)
6+
> (package (name foo))
7+
> EOF
8+
9+
$ cat > dune <<EOF
10+
> (library
11+
> (name foo)
12+
> (public_name foo))
13+
> EOF
14+
15+
$ cat > foo.ml <<EOF
16+
> let x = 1
17+
> EOF
18+
19+
$ dune build @install
20+
21+
Test that relative path outside workspace works (previously failed with "path outside the workspace"):
22+
23+
$ dune install --prefix ../install-target --dry-run --display short 2>&1 | grep "Creating directory" | head -1
24+
Creating directory $TESTCASE_ROOT/../install-target/lib/foo

0 commit comments

Comments
 (0)