Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 3 additions & 0 deletions doc/changes/fixed/12794.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
- Improve error message when invalid version strings are used in `dune-project`
files. Non-ASCII characters and malformed versions now show a helpful hint
with an example of the correct format. (#12794, fixes #12751, @benodiwal)
21 changes: 17 additions & 4 deletions src/dune_sexp/versioned_file.ml
Original file line number Diff line number Diff line change
Expand Up @@ -57,11 +57,24 @@ struct

let parse first_line : Instance.t =
let { First_line.lang = name_loc, name; version = ver_loc, ver } = first_line in
let ver_atom =
match Atom.parse ver with
| Some atom -> atom
| None ->
let hints =
match Table.find langs name with
| None -> []
| Some lang ->
let latest = Syntax.greatest_supported_version_exn lang.syntax in
[ Pp.textf "lang dune %s" (Syntax.Version.to_string latest) ]
in
User_error.raise
~loc:ver_loc
~hints
[ Pp.text "Invalid version. Version must be two numbers separated by a dot." ]
in
let dune_lang_ver =
Decoder.parse
Syntax.Version.decode
Univ_map.empty
(Atom (ver_loc, Atom.of_string ver))
Decoder.parse Syntax.Version.decode Univ_map.empty (Atom (ver_loc, ver_atom))
in
match Table.find langs name with
| None ->
Expand Down
47 changes: 41 additions & 6 deletions test/blackbox-tests/test-cases/lang-invalid-version.t
Original file line number Diff line number Diff line change
Expand Up @@ -5,27 +5,62 @@ such situations provide a clear error.
> cat > dune-project <<EOF
> (lang dune $1)
> EOF
> dune build 2>&1 | grep "Internal error"
> dune build
> }

Invalid version number:

$ test_invalid_version "Ali"
File "dune-project", line 1, characters 11-14:
1 | (lang dune Ali)
^^^
Error: Atom of the form NNN.NNN expected
Comment thread
Alizter marked this conversation as resolved.
[1]

Test with various non-ASCII characters:

CR-someday benodiwal: The version_loc is greedy and captures the closing
parenthesis.

$ test_invalid_version "è"
Internal error, please report upstream including the contents of _build/log.
File "dune-project", line 1, characters 11-13:
1 | (lang dune è)
^^
Comment thread
Alizter marked this conversation as resolved.
Error: Invalid version. Version must be two numbers separated by a dot.
Hint: lang dune 3.21
[1]

$ test_invalid_version "π3.14"
Internal error, please report upstream including the contents of _build/log.
File "dune-project", line 1, characters 11-17:
1 | (lang dune π3.14)
^^^^^^
Error: Invalid version. Version must be two numbers separated by a dot.
Hint: lang dune 3.21
[1]

$ test_invalid_version "α"
Internal error, please report upstream including the contents of _build/log.
File "dune-project", line 1, characters 11-13:
1 | (lang dune α)
^^
Error: Invalid version. Version must be two numbers separated by a dot.
Hint: lang dune 3.21
[1]

$ test_invalid_version "😀"
Internal error, please report upstream including the contents of _build/log.
File "dune-project", line 1, characters 11-15:
1 | (lang dune 😀)
^^^^
Error: Invalid version. Version must be two numbers separated by a dot.
Hint: lang dune 3.21
[1]

CR-someday benodiwal: Unicode string lengths are miscomputed in location
excerpts for East Asian characters.

$ test_invalid_version "中3.16文"
Internal error, please report upstream including the contents of _build/log.
File "dune-project", line 1, characters 11-21:
1 | (lang dune 中3.16文)
^^^^^^^^^^
Error: Invalid version. Version must be two numbers separated by a dot.
Hint: lang dune 3.21
[1]
Loading