Skip to content

Commit

Permalink
mod/modfile: check canonical language version
Browse files Browse the repository at this point in the history
There are a couple of ways that non-canonical version
could find its way into a parser module file. This
CL makes sure that a version is always present and canonical
as long as the `language` field is present.

Signed-off-by: Roger Peppe <rogpeppe@gmail.com>
Change-Id: I39663d73bf2d73146d9d0533bfa5cd81611ff974
Dispatch-Trailer: {"type":"trybot","CL":1178014,"patchset":1,"ref":"refs/changes/14/1178014/1","targetBranch":"master"}
  • Loading branch information
rogpeppe authored and cueckoo committed Mar 12, 2024
1 parent 708885c commit 2749703
Show file tree
Hide file tree
Showing 2 changed files with 20 additions and 1 deletion.
6 changes: 5 additions & 1 deletion mod/modfile/modfile.go
Original file line number Diff line number Diff line change
Expand Up @@ -207,9 +207,13 @@ func parse(modfile []byte, filename string, strict bool) (*File, error) {
mf.Module += "@v0"
}
if mf.Language != nil {
if vers := mf.Language.Version; vers != "" && !semver.IsValid(vers) {
vers := mf.Language.Version
if !semver.IsValid(vers) {
return nil, fmt.Errorf("language version %q in %s is not well formed", vers, filename)
}
if semver.Canonical(vers) != vers {
return nil, fmt.Errorf("language version %v in %s is not canonical", vers, filename)
}
}
var versions []module.Version
// The main module is always the default for its own major version.
Expand Down
15 changes: 15 additions & 0 deletions mod/modfile/modfile_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -122,6 +122,21 @@ module: "foo.com/bar@v0"
language: version: "vblah"
module: "foo.com/bar@v0"`,
wantError: `language version "vblah" in module.cue is not well formed`,
}, {
testName: "EmptyLanguageVersion",
parse: Parse,
data: `
language: {}
module: "foo.com/bar@v0"`,
wantError: `language version "" in module.cue is not well formed`,
}, {
testName: "NonCanonicalLanguageVersion",
parse: Parse,
data: `
module: "foo.com/bar@v0"
language: version: "v0.8"
`,
wantError: `language version v0.8 in module.cue is not canonical`,
}, {
testName: "InvalidDepVersion",
parse: Parse,
Expand Down

0 comments on commit 2749703

Please sign in to comment.