Skip to content

Commit

Permalink
internal/mod/modimports: use cue/parser.ImportsOnly
Browse files Browse the repository at this point in the history
Invalid syntax after import declarations should be handled consistently
no matter whether a file started with a package clause or not.
If for any reason cueimports.Read thinks there is invalid syntax
and reads the entire source, such as when a package clause is missing,
stop cue/parser from parsing the entire file as well.

Not only is parsing the entire file in that case wasteful,
but it also leads to errors which aren't particularly useful
when all we care about here is listing imported packages.

While here, remove the use of ParseComments; none of the modimports
users need to parse comments at the top of the file.
This is a remnant from Go, where comments at the top of the file
are important as they may contain build tag directives.

Signed-off-by: Daniel Martí <mvdan@mvdan.cc>
Change-Id: I35eeade6481b74da7f04240f9ef85da338491f7e
Reviewed-on: https://review.gerrithub.io/c/cue-lang/cue/+/1194961
TryBot-Result: CUEcueckoo <cueckoo@cuelang.org>
Unity-Result: CUE porcuepine <cue.porcuepine@gmail.com>
Reviewed-by: Roger Peppe <rogpeppe@gmail.com>
  • Loading branch information
mvdan committed May 20, 2024
1 parent c0d6193 commit 7d93bcc
Show file tree
Hide file tree
Showing 2 changed files with 4 additions and 6 deletions.
2 changes: 1 addition & 1 deletion internal/mod/modimports/modimports.go
Original file line number Diff line number Diff line change
Expand Up @@ -154,7 +154,7 @@ func yieldPackageFile(fsys fs.FS, fpath, pkgQualifier string, yield func(ModuleF
if err != nil {
return yield(pf, err)
}
syntax, err := parser.ParseFile(fpath, data, parser.ParseComments)
syntax, err := parser.ParseFile(fpath, data, parser.ImportsOnly)
if err != nil {
return yield(pf, err)
}
Expand Down
8 changes: 3 additions & 5 deletions internal/mod/modimports/testdata/parseerror.txtar
Original file line number Diff line number Diff line change
@@ -1,12 +1,12 @@
-- want --
bad_after_import.cue: error: expected ')', found newline
bad_after_import.cue "dep3"
bad_after_package.cue
bad_after_package_import.cue "dep4"
bad_imports.cue: error: string literal not terminated
bad_lone.cue: error: expected ')', found newline
bad_lone.cue
bad_package_imports.cue: error: string literal not terminated
-- want-imports --
error: cannot read "bad_after_import.cue": expected ')', found newline
error: cannot read "bad_imports.cue": string literal not terminated
-- bad_imports.cue --
// Invalid import syntax without a package clause.

Expand All @@ -21,7 +21,6 @@ import "dep2

-- bad_lone.cue --
// Invalid syntax without a package clause, assumed to be after imports.
// TODO(mvdan): just like bad_after_package.cue, this shouldn't fail.

(unterminated

Expand All @@ -34,7 +33,6 @@ package p

-- bad_after_import.cue --
// Invalid syntax after some imports without a package clause.
// TODO(mvdan): just like bad_after_package_import.cue, this shouldn't fail.

import "dep3"

Expand Down

0 comments on commit 7d93bcc

Please sign in to comment.