Skip to content

Commit

Permalink
cue/load: fix loading when Config.Package does not match default package
Browse files Browse the repository at this point in the history
When a caller of `cue/load.Instances`, specifies an explicit package
to load by setting `Config.Package`, it should be essentially the
same as specifying an explicit package qualifier in all package arguments
that don't already have one. That's not the case currently. This change
fixes that.

Fixes #3213.

Signed-off-by: Roger Peppe <rogpeppe@gmail.com>
Change-Id: I740a16911340e083776303fb0e454b4533e66f2f
Reviewed-on: https://review.gerrithub.io/c/cue-lang/cue/+/1196178
Reviewed-by: Daniel Martí <mvdan@mvdan.cc>
TryBot-Result: CUEcueckoo <cueckoo@cuelang.org>
Unity-Result: CUE porcuepine <cue.porcuepine@gmail.com>
Reviewed-on: https://review.gerrithub.io/c/cue-lang/cue/+/1196232
Reviewed-by: Paul Jolly <paul@myitcv.io>
  • Loading branch information
rogpeppe authored and myitcv committed Jun 12, 2024
1 parent ee98525 commit fc6d421
Show file tree
Hide file tree
Showing 2 changed files with 35 additions and 13 deletions.
20 changes: 19 additions & 1 deletion cue/load/instances.go
Original file line number Diff line number Diff line change
Expand Up @@ -58,7 +58,6 @@ func Instances(args []string, c *Config) []*build.Instance {
if len(args) == 0 {
args = []string{"."}
}

// TODO: This requires packages to be placed before files. At some point this
// could be relaxed.
i := 0
Expand All @@ -75,6 +74,25 @@ func Instances(args []string, c *Config) []*build.Instance {
return []*build.Instance{c.newErrInstance(err)}
}
}
if c.Package != "" && c.Package != "_" && c.Package != "*" {
// The caller has specified an explicit package to load.
// This is essentially the same as passing an explicit package
// qualifier to all package arguments that don't already have
// one. We add that qualifier here so that there's a distinction
// between package paths specified as arguments, which
// have the qualifier added, and package paths that are dependencies
// of those, which don't.
pkgArgs1 := make([]string, 0, len(pkgArgs))
for _, p := range pkgArgs {
if ip := module.ParseImportPath(p); !ip.ExplicitQualifier {
ip.Qualifier = c.Package
p = ip.String()
}
pkgArgs1 = append(pkgArgs1, p)
}
pkgArgs = pkgArgs1
}

synCache := newSyntaxCache(c)
tg := newTagger(c)
// Pass all arguments that look like packages to loadPackages
Expand Down
28 changes: 16 additions & 12 deletions cue/load/loader_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -433,40 +433,44 @@ display:""`}, {
Package: "main",
},
args: []string{"."},
want: `err: found packages "main" (file.cue) and "test_package" (file_appengine.cue) in "multi"
path: ""
want: `path: mod.test/test/multi@v0:main
module: mod.test/test@v0
root: $CWD/testdata/testmod
dir: ""
display:""`}, {
dir: $CWD/testdata/testmod/multi
display:.
files:
$CWD/testdata/testmod/multi/file.cue`}, {
name: "ExplicitPackageWithUnqualifiedImportPath#2",
// This test replicates the failure reported in https://cuelang.org/issue/3213
cfg: &Config{
Dir: filepath.Join(testdataDir, "multi2"),
Package: "other",
},
args: []string{"."},
want: `err: mod.test/test/multi2@v0:other: import failed: no dependency found for package "mod.test/test/sub":
$CWD/testdata/testmod/multi2/other.cue:3:8
path: mod.test/test/multi2@v0:other
want: `path: mod.test/test/multi2@v0:other
module: mod.test/test@v0
root: $CWD/testdata/testmod
dir: $CWD/testdata/testmod/multi2
display:.
files:
$CWD/testdata/testmod/multi2/other.cue`}, {
$CWD/testdata/testmod/multi2/other.cue
imports:
mod.test/test/sub: $CWD/testdata/testmod/sub/sub.cue`}, {
name: "ExplicitPackageWithUnqualifiedImportPath#3",
cfg: &Config{
Dir: filepath.Join(testdataDir, "multi3"),
Package: "other",
},
args: []string{"."},
want: `err: found packages "multi3" (multi3.cue) and "other" (other.cue) in "multi3"
path: ""
want: `path: mod.test/test/multi3@v0:other
module: mod.test/test@v0
root: $CWD/testdata/testmod
dir: ""
display:""`}}
dir: $CWD/testdata/testmod/multi3
display:.
files:
$CWD/testdata/testmod/multi3/other.cue
imports:
mod.test/test/sub: $CWD/testdata/testmod/sub/sub.cue`}}
tdtest.Run(t, testCases, func(t *tdtest.T, tc *loadTest) {
pkgs := Instances(tc.args, tc.cfg)

Expand Down

0 comments on commit fc6d421

Please sign in to comment.