Skip to content

Commit

Permalink
Detect future syntax errors in the Syntax helper
Browse files Browse the repository at this point in the history
Repository parsing and linting use a slightly different parsing route
which wasn't checking for future syntax errors.

Co-authored-by: Raja Boujbel <raja.boujbel@ocamlpro.com>
  • Loading branch information
dra27 and rjbou committed Sep 18, 2024
1 parent 2a07dec commit a216344
Show file tree
Hide file tree
Showing 4 changed files with 16 additions and 7 deletions.
1 change: 1 addition & 0 deletions master_changes.md
Original file line number Diff line number Diff line change
Expand Up @@ -94,6 +94,7 @@ users)
## Opamfile
* Make all writes atomic [#5489 @kit-ty-kate]
* Propagate future opamfile parse errors correctly [#6199 @dra27]
* Ensure future syntax errors are only reported when the syntax version is greater than the client, not the format library [#6199 @dra27 - fix #6188]

## External dependencies
* Always pass --no-version-check and --no-write-registry to Cygwin setup [#6046 @dra27]
Expand Down
14 changes: 11 additions & 3 deletions src/format/opamFile.ml
Original file line number Diff line number Diff line change
Expand Up @@ -874,9 +874,17 @@ module Syntax = struct
let filename = OpamFilename.to_string filename in
lexbuf.Lexing.lex_curr_p <- { lexbuf.Lexing.lex_curr_p with
Lexing.pos_fname = filename };
try OpamParser.main OpamLexer.token lexbuf filename with
| OpamLexer.Error msg -> error msg
| Parsing.Parse_error -> error "Parse error"
match OpamParser.main OpamLexer.token lexbuf filename with
| {file_contents =
[{pelem = Variable({pelem = "opam-version"; _},
{pelem = String ver; _}); _ };
{pelem = Section {section_kind = {pelem = "#"; _}; _}; _}]; _}
when OpamVersion.(compare (nopatch (of_string ver))
(nopatch OpamVersion.current)) <= 0 ->
error "Parse error"
| opamfile -> opamfile
| exception OpamLexer.Error msg -> error msg
| exception Parsing.Parse_error -> error "Parse error"


let pp_channel filename ic oc =
Expand Down
4 changes: 2 additions & 2 deletions tests/reftests/lint.test
Original file line number Diff line number Diff line change
Expand Up @@ -1267,11 +1267,11 @@ ${BASEDIR}/future/pin-at-two-one.opam: Errors.
# Return code 1 #
### opam lint future/pin-at-two-two.opam
${BASEDIR}/future/pin-at-two-two.opam: Errors.
error 2: File format error: unsupported or missing file format version; should be 2.0 or older
error 2: File format error at line 11, column 0: Parse error
# Return code 1 #
### opam lint future/pin-at-two-three.opam
${BASEDIR}/future/pin-at-two-three.opam: Errors.
error 2: File format error: unsupported or missing file format version; should be 2.0 or older
error 2: File format error at line 11, column 0: Parse error
# Return code 1 #
### opam lint future/pin-at-future.opam
${BASEDIR}/future/pin-at-future.opam: Errors.
Expand Down
4 changes: 2 additions & 2 deletions tests/reftests/pin.test
Original file line number Diff line number Diff line change
Expand Up @@ -353,7 +353,7 @@ echo GARBAGE>>"$1"
Parse error [skipped]

[ERROR] Invalid opam file in pin-at-two-two source from file://${BASEDIR}/pin-at-two-two:
error 2: File format error: unsupported or missing file format version; should be 2.0 or older
error 2: File format error at line 11, column 0: Parse error
[ERROR] No package named pin-at-two-two found.
# Return code 5 #
### sh junk.sh pin-at-two-three/pin-at-two-three.opam
Expand All @@ -367,7 +367,7 @@ echo GARBAGE>>"$1"
Parse error [skipped]

[ERROR] Invalid opam file in pin-at-two-three source from file://${BASEDIR}/pin-at-two-three:
error 2: File format error: unsupported or missing file format version; should be 2.0 or older
error 2: File format error at line 11, column 0: Parse error
[ERROR] No package named pin-at-two-three found.
# Return code 5 #
### sh junk.sh pin-at-future/pin-at-future.opam
Expand Down

0 comments on commit a216344

Please sign in to comment.