From fc6d421baf9f14e34a2978694a85a21aef495c95 Mon Sep 17 00:00:00 2001 From: Roger Peppe Date: Tue, 11 Jun 2024 22:32:08 +0100 Subject: [PATCH] cue/load: fix loading when Config.Package does not match default package MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit 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 Change-Id: I740a16911340e083776303fb0e454b4533e66f2f Reviewed-on: https://review.gerrithub.io/c/cue-lang/cue/+/1196178 Reviewed-by: Daniel Martí TryBot-Result: CUEcueckoo Unity-Result: CUE porcuepine Reviewed-on: https://review.gerrithub.io/c/cue-lang/cue/+/1196232 Reviewed-by: Paul Jolly --- cue/load/instances.go | 20 +++++++++++++++++++- cue/load/loader_test.go | 28 ++++++++++++++++------------ 2 files changed, 35 insertions(+), 13 deletions(-) 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)