Skip to content

Commit

Permalink
cue/load: treat stdlib-like import on command line as error
Browse files Browse the repository at this point in the history
It's not currently possible to name a stdlib package on the command
line, except for the edge case of when a directory of that name exists
in cue.mod/{pkg,gen,usr}.

Close off that edge case by making it an error to import such a package
as part of the command-line arguments. In the future, we might want to
explicitly allow this, but the current situation seems wrong.

This also fixes stdlib-imports in modules mode, because it prevents the
resolution logic from ever seeing a package with no module.

Fixes #2964

Signed-off-by: Roger Peppe <rogpeppe@gmail.com>
Change-Id: I22bbe764bb16fb2a992189981c9bb9d28fd3e78a
Dispatch-Trailer: {"type":"trybot","CL":1185363,"patchset":3,"ref":"refs/changes/63/1185363/3","targetBranch":"master"}
  • Loading branch information
rogpeppe authored and cueckoo committed Mar 20, 2024
1 parent f204294 commit f4cb1a6
Show file tree
Hide file tree
Showing 4 changed files with 18 additions and 12 deletions.
2 changes: 1 addition & 1 deletion cmd/cue/cmd/testdata/script/eval_loaderr.txtar
Original file line number Diff line number Diff line change
Expand Up @@ -3,4 +3,4 @@
cmp stderr expect-stderr

-- expect-stderr --
cannot find package "nonexisting"
"nonexisting" cannot be imported as CUE package
6 changes: 6 additions & 0 deletions cmd/cue/cmd/testdata/script/issue2964.txtar
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
env CUE_EXPERIMENT=modules
exec cue mod init test.example
! exec cue export anypath
cmp stderr want-stderr
-- want-stderr --
"anypath" cannot be imported as CUE package
7 changes: 5 additions & 2 deletions cue/load/import.go
Original file line number Diff line number Diff line change
Expand Up @@ -213,7 +213,7 @@ func (l *loader) _loadFunc(pos token.Pos, path string) *build.Instance {
return l.cfg.newErrInstance(errors.Newf(pos, "relative import paths not allowed (%q)", path))
}

if isStdlibImport(path) {
if isStdlibPackage(path) {
// It looks like a builtin.
return nil
}
Expand Down Expand Up @@ -326,6 +326,9 @@ func (l *loader) absDirFromImportPath(pos token.Pos, p importPath) (absDir, name
if l.cfg.ModuleRoot == "" {
return "", "", errors.Newf(pos, "cannot import %q (root undefined)", p)
}
if isStdlibPackage(string(p)) {
return "", "", errors.Newf(pos, "%q cannot be imported as CUE package", p)
}
origp := p
// Extract the package name.
parts := module.ParseImportPath(string(p))
Expand Down Expand Up @@ -407,5 +410,5 @@ func absPathForSourceLoc(loc module.SourceLoc) (string, error) {
// isStdlibPackage reports whether pkgPath looks like
// an import from the standard library.
func isStdlibPackage(pkgPath string) bool {
return strings.IndexByte(strings.Split(path, "/")[0], '.') == -1
return strings.IndexByte(strings.Split(pkgPath, "/")[0], '.') == -1
}
15 changes: 6 additions & 9 deletions cue/load/loader_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -222,24 +222,21 @@ display:foo.com/bad-identifier`,
}, {
cfg: dirCfg,
args: []string{"nonexisting"},
want: `err: cannot find package "nonexisting"
want: `err: "nonexisting" cannot be imported as CUE package
path: nonexisting
module: mod.test/test
root: $CWD/testdata/testmod
dir: $CWD/testdata/testmod/cue.mod/gen/nonexisting
dir: ""
display:nonexisting`,
}, {
// TODO this should not work, as "strconv" is a standard library
// import and should always be treated as such.
cfg: dirCfg,
args: []string{"strconv"},
want: `path: strconv
want: `err: "strconv" cannot be imported as CUE package
path: strconv
module: mod.test/test
root: $CWD/testdata/testmod
dir: $CWD/testdata/testmod/cue.mod/gen/strconv
display:strconv
files:
$CWD/testdata/testmod/cue.mod/pkg/strconv/strconv.cue`,
dir: ""
display:strconv`,
}, {
cfg: dirCfg,
args: []string{"./empty"},
Expand Down

0 comments on commit f4cb1a6

Please sign in to comment.