Skip to content

Commit 85298bb

Browse files
Improve implementation of external to source mapping
Signed-off-by: Marek Kubica <[email protected]>
1 parent 33b63e2 commit 85298bb

File tree

1 file changed

+24
-26
lines changed

1 file changed

+24
-26
lines changed

src/dune_pkg/workspace.ml

Lines changed: 24 additions & 26 deletions
Original file line numberDiff line numberDiff line change
@@ -72,35 +72,33 @@ module Repository = struct
7272
end
7373

7474
let dev_tool_path_to_source_dir path =
75-
let of_ =
76-
Path.Build.relative Path.Build.root ".dev-tools.locks"
77-
|> Path.build
78-
|> Path.to_absolute_filename
79-
|> Path.External.of_string
75+
let lock_dir_location =
76+
Path.Build.relative Path.Build.root ".dev-tools.locks" |> Path.build
8077
in
81-
let in_dev_tool_dir = Path.External.is_descendant ~of_ path in
82-
match in_dev_tool_dir with
78+
let absolute = Path.to_absolute_filename lock_dir_location in
79+
match Path.External.is_descendant ~of_:(Path.External.of_string absolute) path with
8380
| false ->
8481
Code_error.raise
85-
"External lock dir path is unsupported"
86-
[ "dir", Path.External.to_dyn path ]
82+
"External path is not pointing to lock dir location"
83+
[ "external", Path.External.to_dyn path
84+
; "dev tool lock dir location", Path.to_dyn lock_dir_location
85+
]
8786
| true ->
88-
let prefix = Path.External.to_string of_ in
8987
let as_string = Path.External.to_string path in
90-
let relative =
91-
String.sub
92-
as_string
93-
~pos:(String.length prefix)
94-
~len:(String.length as_string - String.length prefix)
95-
in
96-
let exploded = String.split ~on:'/' relative in
97-
(match exploded with
98-
| "" :: dev_tool_name :: components ->
99-
Path.Source.L.relative
100-
Path.Source.root
101-
([ "_build"; ".dev-tools.locks"; dev_tool_name ] @ components)
102-
| components ->
103-
Code_error.raise
104-
"Unexpected external path"
105-
[ "dir", Path.External.to_dyn path; "components", Dyn.(list string) components ])
88+
(match String.drop_prefix ~prefix:absolute as_string with
89+
| None ->
90+
(* we checked for descendants before, thus it has to match the prefix *)
91+
assert false
92+
| Some suffix ->
93+
(match String.split ~on:'/' suffix with
94+
| "" :: dev_tool_name :: components ->
95+
Path.Source.L.relative
96+
Path.Source.root
97+
([ "_build"; ".dev-tools.locks"; dev_tool_name ] @ components)
98+
| components ->
99+
Code_error.raise
100+
"Unexpected external path"
101+
[ "dir", Path.External.to_dyn path
102+
; "components", Dyn.(list string) components
103+
]))
106104
;;

0 commit comments

Comments
 (0)