diff --git a/doc/changes/fixed/14668.md b/doc/changes/fixed/14668.md new file mode 100644 index 00000000000..fdd35939054 --- /dev/null +++ b/doc/changes/fixed/14668.md @@ -0,0 +1,4 @@ +- Reword the error raised when `%{target}` is used in a rule that omits the + `(target ...)` (or `(targets ...)`) field, so it points at the missing + field instead of mentioning "inferred rules". (#14668, fixes #12439, + @Alizter) diff --git a/src/dune_rules/expander.ml b/src/dune_rules/expander.ml index 6aaacba2849..ae84bd20231 100644 --- a/src/dune_rules/expander.ml +++ b/src/dune_rules/expander.ml @@ -273,7 +273,8 @@ let[@inline never] invalid_use_of_target_variable User_error.raise ~loc:source.loc [ Pp.textf - "You cannot use %s with inferred rules." + "You cannot use %s unless the rule has a (target ...) or (targets ...) \ + field." (Dune_lang.Template.Pform.describe source) ] | Static { targets = _; multiplicity } -> diff --git a/test/blackbox-tests/test-cases/rule-target-inferrence/target-pform-without-target-field.t b/test/blackbox-tests/test-cases/rule-target-inferrence/target-pform-without-target-field.t new file mode 100644 index 00000000000..13d10b392a6 --- /dev/null +++ b/test/blackbox-tests/test-cases/rule-target-inferrence/target-pform-without-target-field.t @@ -0,0 +1,18 @@ +When a rule uses %{target} but omits the (target ...) field, dune should +produce a clear error message pointing the user at the missing field. See +https://github.com/ocaml/dune/issues/12439. + + $ make_dune_project 3.24 + $ cat > dune < (rule + > (action + > (write-file %{target} hello))) + > EOF + + $ dune build + File "dune", line 3, characters 14-23: + 3 | (write-file %{target} hello))) + ^^^^^^^^^ + Error: You cannot use %{target} unless the rule has a (target ...) or + (targets ...) field. + [1]