diff --git a/doc/changes/10923.md b/doc/changes/10923.md new file mode 100644 index 00000000000..53c916fae11 --- /dev/null +++ b/doc/changes/10923.md @@ -0,0 +1,3 @@ +- Fix the file referred to in the error/warning message displayed due to the + dune configuration version not supporting a particular configuration + stanza in use. (#10923, @H-ANSEN) diff --git a/src/dune_config_file/dune_config_file.ml b/src/dune_config_file/dune_config_file.ml index 7d97cfdf90c..6d4e8958b73 100644 --- a/src/dune_config_file/dune_config_file.ml +++ b/src/dune_config_file/dune_config_file.ml @@ -367,8 +367,10 @@ module Dune_config = struct let decode_generic ~min_dune_version = let check min_ver = - let ver = Dune_lang.Syntax.Version.max min_ver min_dune_version in - Dune_lang.Syntax.since Stanza.syntax ver + let module S = Dune_lang.Syntax in + let ver = S.Version.max min_ver min_dune_version in + let* loc, what = S.desc () in + S.since_fmt ~fmt:(S.Error_msg.since_config ~what) Stanza.syntax ver loc in let field_o n v d = field_o n (check v >>> d) in let+ display = field_o "display" (1, 0) (enum Display.all) diff --git a/src/dune_sexp/syntax.ml b/src/dune_sexp/syntax.ml index 3e6d43457ee..c2108c0b214 100644 --- a/src/dune_sexp/syntax.ml +++ b/src/dune_sexp/syntax.ml @@ -250,18 +250,22 @@ module Key = struct end module Error_msg = struct - let since t ver ~what = - let lang_or_using = if t.name = "dune" then "lang" else "using" in + let fmt_error_msg t ver ~what ~file = + let lang_or_using name = if name = "dune" then "lang" else "using" in Printf.sprintf - "%s is only available since version %s of %s. Please update your dune-project file \ - to have (%s %s %s)." + "%s is only available since version %s of %s. Please update your %s file to have \ + (%s %s %s)." what (Version.to_string ver) t.desc - lang_or_using + file + (lang_or_using t.name) t.name (Version.to_string ver) ;; + + let since t ver ~what = fmt_error_msg t ver ~what ~file:"dune-project" + let since_config t ver ~what = fmt_error_msg t ver ~what ~file:"dune config" end module Error = struct @@ -520,16 +524,20 @@ let renamed_in t ver ~to_ = Error.renamed_in loc t ver ~what ~to_ ;; -let since ?what ?(fatal = true) t ver = +let since_fmt ?(fatal = true) ~fmt t ver loc = let open Version.Infix in let* current_ver = get_exn t in if current_ver >= ver then return () - else - let* loc, what_ctx = desc () in - let what = Option.value what ~default:what_ctx in + else ( if fatal - then Error.since loc t ver ~what - else User_warning.emit ~loc [ Pp.text (Error_msg.since t ver ~what) ]; - return () + then User_error.raise ~loc [ Pp.text (fmt t ver) ] + else User_warning.emit ~loc [ Pp.text (fmt t ver) ]; + return ()) +;; + +let since ?what ?(fatal = true) t ver = + let* loc, what_ctx = desc () in + let what = Option.value what ~default:what_ctx in + since_fmt ~fatal ~fmt:(Error_msg.since ~what) t ver loc ;; diff --git a/src/dune_sexp/syntax.mli b/src/dune_sexp/syntax.mli index f4fd657aa50..0352e34108b 100644 --- a/src/dune_sexp/syntax.mli +++ b/src/dune_sexp/syntax.mli @@ -29,7 +29,14 @@ end type t module Error_msg : sig + (** [since t ver what] formats an error string indicating that the syntax + [t] described by [what] is only available since [ver] of the dune-project + file. *) val since : t -> Version.t -> what:string -> string + + (** Like [since] but formats an error message relating to the dune config file + rather than the dune-project file. *) + val since_config : t -> Version.t -> what:string -> string end module Error : sig @@ -71,6 +78,9 @@ val create (** Return the name of the syntax. *) val name : t -> string +(** Indicate the location and kind of value being parsed *) +val desc : unit -> (Loc.t * string, 'a) Decoder.parser + (** Check that the given version is supported and raise otherwise. *) val check_supported : dune_lang_ver:Version.t -> t -> Loc.t * Version.t -> unit @@ -98,6 +108,16 @@ val renamed_in : t -> Version.t -> to_:string -> (unit, _) Decoder.parser [fatal] defaults to true. [what] allows customizing the error message. *) val since : ?what:string -> ?fatal:bool -> t -> Version.t -> (unit, _) Decoder.parser +(** Like [since] but accepts a function [fmt] allowing custom formatting of the + entire error/warning message. See [Error_msg] for format functions. *) +val since_fmt + : ?fatal:bool + -> fmt:(t -> Version.t -> string) + -> t + -> Version.t + -> Loc.t + -> (unit, 'a) Decoder.parser + (** {2 Low-level functions} *) module Key : sig diff --git a/test/blackbox-tests/test-cases/config-project-defaults.t b/test/blackbox-tests/test-cases/config-project-defaults.t index 4bfd71e62d3..7186ee51147 100644 --- a/test/blackbox-tests/test-cases/config-project-defaults.t +++ b/test/blackbox-tests/test-cases/config-project-defaults.t @@ -39,7 +39,7 @@ Change the version of the config file to one which does not support the 4 | (maintainers MaintainerTest) 5 | (license MIT)) Error: 'project_defaults' is only available since version 3.17 of the dune - language. Please update your dune-project file to have (lang dune 3.17). + language. Please update your dune config file to have (lang dune 3.17). [1] $ sed -i -e '1s|.*|(lang dune 3.17)|' dune-config diff --git a/test/blackbox-tests/test-cases/config/config-in-workspace-file.t b/test/blackbox-tests/test-cases/config/config-in-workspace-file.t index 5dddf2a7324..2d4c97593c0 100644 --- a/test/blackbox-tests/test-cases/config/config-in-workspace-file.t +++ b/test/blackbox-tests/test-cases/config/config-in-workspace-file.t @@ -19,7 +19,7 @@ Setting such options is not supported with older Dune: 2 | (display short) ^^^^^^^^^^^^^^^ Error: 'display' is only available since version 3.0 of the dune language. - Please update your dune-project file to have (lang dune 3.0). + Please update your dune config file to have (lang dune 3.0). [1] But is supported with Dune >= 3.0.0: diff --git a/test/blackbox-tests/test-cases/config/config-version.t b/test/blackbox-tests/test-cases/config/config-version.t new file mode 100644 index 00000000000..2c935354de0 --- /dev/null +++ b/test/blackbox-tests/test-cases/config/config-version.t @@ -0,0 +1,49 @@ +Tests verifying that the config file presents accurate error or warning +messages when a stanza is used with a version of dune which does not support +such a stanza. + +Create a config file to use in the following tests. We will initialize the +config with a version and a stanza incompatable with that version. + + $ export DUNE_CACHE_ROOT=$PWD/dune-cache + $ touch dune-config + $ cat >dune-config < (lang dune 1.0) + > (cache enabled) + > EOF + +Attempt to initialize a new project while an invaild stanza due to versioning +exists in the config. + + $ dune init proj test --config-file=dune-config + File "$TESTCASE_ROOT/dune-config", line 2, characters 0-15: + 2 | (cache enabled) + ^^^^^^^^^^^^^^^ + Error: 'cache' is only available since version 2.0 of the dune language. + Please update your dune config file to have (lang dune 2.0). + [1] + +Update the dune configuration with a version that would support the +'(cache enabled)' stanza and attempt a successful project initialization. + + $ cat >dune-config < (lang dune 2.0) + > (cache enabled) + > EOF + + $ dune init proj test_vaild --config-file=dune-config + Entering directory 'test_vaild' + Success: initialized project component named test_vaild + Leaving directory 'test_vaild' + +Append an invaild stanza to the config file and attempt project initialzation. + + $ echo "(cache-check-probability 0.5)" >> dune-config + $ dune init proj test --config-file=dune-config + File "$TESTCASE_ROOT/dune-config", line 3, characters 0-29: + 3 | (cache-check-probability 0.5) + ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ + Error: 'cache-check-probability' is only available since version 2.7 of the + dune language. Please update your dune config file to have (lang dune 2.7). + [1] +