diff --git a/cue/load/instances.go b/cue/load/instances.go index d68ae5bcc09..9bf0a49f20e 100644 --- a/cue/load/instances.go +++ b/cue/load/instances.go @@ -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 @@ -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 diff --git a/cue/load/loader_test.go b/cue/load/loader_test.go index 82db78bd6bb..2a43c47c5ae 100644 --- a/cue/load/loader_test.go +++ b/cue/load/loader_test.go @@ -433,12 +433,13 @@ 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{ @@ -446,27 +447,30 @@ display:""`}, { 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)