From 0a454e3c0628d963f1b60e872b9a149713ea884f Mon Sep 17 00:00:00 2001 From: Antonio Nuno Monteiro Date: Wed, 17 Mar 2021 16:22:41 -0700 Subject: [PATCH] Fix a dune crash when `subdir` is an absolute path (#4366) Fix a dune crash when `subdir` is an absolute path Signed-off-by: Antonio Nuno Monteiro --- CHANGES.md | 2 ++ src/dune_engine/sub_dirs.ml | 11 ++++++++--- .../test-cases/subdir-stanza.t/run.t | 16 ++++++++++++++++ 3 files changed, 26 insertions(+), 3 deletions(-) diff --git a/CHANGES.md b/CHANGES.md index e1c808c5f18..1d825b15f5e 100644 --- a/CHANGES.md +++ b/CHANGES.md @@ -94,6 +94,8 @@ Unreleased - Add support for sandboxing using hard links (#4360, @snowleopard) +- Fix dune crash when `subdir` is an absolute path (#4366, @anmonteiro) + 2.8.2 (21/01/2021) ------------------ diff --git a/src/dune_engine/sub_dirs.ml b/src/dune_engine/sub_dirs.ml index bad21bf0273..6bf90e04a1c 100644 --- a/src/dune_engine/sub_dirs.ml +++ b/src/dune_engine/sub_dirs.ml @@ -188,9 +188,14 @@ module Dir_map = struct let merge_all = List.fold_left ~f:merge ~init:empty end -let descedant_path = +let descendant_path = Dune_lang.Decoder.plain_string (fun ~loc fn -> - Path.Local.parse_string_exn ~loc fn |> Path.Local.explode) + if Filename.is_relative fn then + Path.Local.parse_string_exn ~loc fn |> Path.Local.explode + else + let msg = [ Pp.textf "invalid sub-directory path %S" fn ] in + let hints = [ Pp.textf "sub-directory path must be relative" ] in + User_error.raise ~loc ~hints msg) let strict_subdir field_name = let open Dune_lang.Decoder in @@ -263,7 +268,7 @@ let decode = in let rec subdir () = let* () = Dune_lang.Syntax.since Stanza.syntax (2, 5) in - let* subdir = descedant_path in + let* subdir = descendant_path in let+ node = fields (decode ~allow_ignored_subdirs:false) in Dir_map.make_at_path subdir node and decode ~allow_ignored_subdirs = diff --git a/test/blackbox-tests/test-cases/subdir-stanza.t/run.t b/test/blackbox-tests/test-cases/subdir-stanza.t/run.t index fb9a4e90a9a..b0413313b19 100644 --- a/test/blackbox-tests/test-cases/subdir-stanza.t/run.t +++ b/test/blackbox-tests/test-cases/subdir-stanza.t/run.t @@ -118,3 +118,19 @@ Include stanzas within subdir stanzas $ dune build --root . a/hello.txt $ cat _build/default/a/hello.txt Hello! + + + $ echo "(lang dune 2.5)" > dune-project + $ cat >dune < (rule (with-stdout-to foo.txt (echo "bar"))) + > (subdir /absolute/path/to/bar + > (rule (with-stdout-to foo.txt (echo "bar")))) + > EOF + $ dune build ./foo.txt ./bar/foo.txt + File "dune", line 2, characters 8-29: + 2 | (subdir /absolute/path/to/bar + ^^^^^^^^^^^^^^^^^^^^^ + Error: invalid sub-directory path "/absolute/path/to/bar" + Hint: sub-directory path must be relative + [1] +