Skip to content

Commit

Permalink
Warn when lang versions have an ignored suffix
Browse files Browse the repository at this point in the history
Syntax versions (the string after `lang dune` for example) are parsed
with `Scanf.sscanf`. This only parses the longest matching prefix and
ignored the rest of the string. So `lang dune 2.3.0` is silently
interpreted as `2.3` and so is `lang dune 2.4suffix`.

This PR adds a warning when there is a suffix. An error could break
existing projects.

The implementation adds a `%s` to match the suffix. This specifier stops
on whitespace characters, but these can not appear in valid atoms.

Signed-off-by: Etienne Millon <me@emillon.org>
  • Loading branch information
emillon committed Oct 27, 2021
1 parent d9675d9 commit 13fe495
Show file tree
Hide file tree
Showing 3 changed files with 43 additions and 2 deletions.
4 changes: 4 additions & 0 deletions CHANGES.md
Original file line number Diff line number Diff line change
@@ -1,6 +1,10 @@
Unreleased
----------

- Warn when lang versions have an ignored suffix. `(lang dune 2.3.4)` or `(lang
dune 2.3suffix)` were silently parsed as `2.3` and we know suggest to remove
the prefix. (#5040, @emillon)

- Allow users to specify dynamic dependencies in rules. For example `(deps
%{read:foo.gen})` (#4662, fixes #4089, @jeremiedimino)

Expand Down
10 changes: 8 additions & 2 deletions src/dune_lang/syntax.ml
Original file line number Diff line number Diff line change
Expand Up @@ -29,8 +29,14 @@ module Version = struct
let open Decoder in
raw >>| function
| Atom (loc, A s) -> (
match Scanf.sscanf s "%u.%u" (fun a b -> (a, b)) with
| Ok s -> s
match Scanf.sscanf s "%u.%u%s" (fun a b s -> ((a, b), s)) with
| Ok (v, "") -> v
| Ok (((a, b) as v), s) ->
User_warning.emit ~loc
[ Pp.textf "The %S part is ignored here." s
; Pp.textf "This version is parsed as just %d.%d." a b
];
v
| Error () ->
User_error.raise ~loc [ Pp.text "Atom of the form NNN.NNN expected" ])
| sexp -> User_error.raise ~loc:(Ast.loc sexp) [ Pp.text "Atom expected" ]
Expand Down
31 changes: 31 additions & 0 deletions test/blackbox-tests/test-cases/lang-dune-warning.t
Original file line number Diff line number Diff line change
@@ -0,0 +1,31 @@
If (lang dune) does not use a valid format, a warning is emitted:

$ cat > dune-project << EOF
> (lang dune 2.3.0)
> EOF

$ dune build
File "dune-project", line 1, characters 11-16:
1 | (lang dune 2.3.0)
^^^^^
Warning: The ".0" part is ignored here.
This version is parsed as just 2.3.

$ cat > dune-project << EOF
> (lang dune 2.4suffix)
> EOF

$ dune build
File "dune-project", line 1, characters 11-20:
1 | (lang dune 2.4suffix)
^^^^^^^^^
Warning: The "suffix" part is ignored here.
This version is parsed as just 2.4.

Of course if the version is valid, no warning is emitted:

$ cat > dune-project << EOF
> (lang dune 2.2)
> EOF

$ dune build

0 comments on commit 13fe495

Please sign in to comment.