Require opam-version 2.1 and greater at the start of the file #43
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
opam 2.1 does not define any new fields, so there is no need for any opam file to contain
opam-version: "2.1"
. Given this general quietness, there is one semantic change being made for the 2.1 and subsequent formats in order to allow the lexer and parser to evolve in future.opam-version
must appear first (apart from whitespace and comments) for "2.1" and later, or the file does not parse. This gives the possibility in the future that additional constructs can be added to the file format, while retaining compatibility with older lexers.This PR enforces two requirements on the whole-file lexing:
opam-version: "2.1"
must be the first non-whitespace/comment fieldopam-version: "2.1"
and greater, repeating theopam-version
field is a parsing errorIn both these cases, for maximum API compatibility,
Parsing.Parse_error
is raised rather than a new exception. API changes in the future will be needed to take advantage of this, but the key thing in this PR is to enforce the invariant so that this can be done when it's needed in future.Opam itself only uses
OpamParser.FullPos.string
,OpamParser.FullPos.channel
,OpamParser.FullPos.main
all of which obey this. The only additional function used isOpamParser.FullPos.value_from_string
- when lexing changes are introduced, functions such as that will need a parameter to specify the permitted lexing version.On top of this, there is then an additional future-proofing change to the library: if
opam-version
is greater than the library version, then the lexer and parser do a best-effort parse. In this case,Parse_error
is only raised ifopam-version
is not first (since it must be 2.2+) or if it is repeated before the first lexer/parser error. This allows opam (and any other tool) to display a superior "this is an opam 2.2 file, so was ignored" rather than a lexer/parser error message which is more likely to result in users reporting bugs (to pinned repositories, opam-repository maintainers, etc.).I added a few tests to ensure the
Parse_error
exceptions are raised. It could do with some more tests, although I have test_ed_ it! There is an additional slightly weird test which ensures thatOpamBaseParser.version
matches the build version.