cue/load: fix module loading bug
Package loading failed when module was not
set programmatically.
Most changes are unnecessary to fix this, but
make the code a bit more defensive w.r.t.
potential crashes.
Closes #66.
Change-Id: Ie512a940ff9b8a0618d550edd5d1d9246f54948c
Reviewed-on: https://cue-review.googlesource.com/c/cue/+/2700
Reviewed-by: Marcel van Lohuizen <mpvl@golang.org>
diff --git a/cue/build/context.go b/cue/build/context.go
index 3c20f1d..fc3a4c4 100644
--- a/cue/build/context.go
+++ b/cue/build/context.go
@@ -42,6 +42,9 @@
// NewInstance creates an instance for this Context.
func (c *Context) NewInstance(dir string, f LoadFunc) *Instance {
+ if c == nil {
+ c = &Context{}
+ }
if f == nil {
f = c.loader
}
diff --git a/cue/load/config.go b/cue/load/config.go
index da4be24..2852270 100644
--- a/cue/load/config.go
+++ b/cue/load/config.go
@@ -209,10 +209,10 @@
if prefix.IsValid() {
name, err := prefix.String()
if err != nil {
- return nil, err
+ return &c, err
}
- if c.Module == "" || c.Module != name {
- return nil, errors.Newf(prefix.Pos(), "inconsistent modules: got %q, want %q", name, c.Module)
+ if c.Module != "" && c.Module != name {
+ return &c, errors.Newf(prefix.Pos(), "inconsistent modules: got %q, want %q", name, c.Module)
}
c.Module = name
}
diff --git a/cue/load/loader.go b/cue/load/loader.go
index 7dd8e90..06165d6 100644
--- a/cue/load/loader.go
+++ b/cue/load/loader.go
@@ -39,11 +39,11 @@
if c == nil {
c = &Config{}
}
-
- c, err := c.complete()
+ newC, err := c.complete()
if err != nil {
return []*build.Instance{c.newErrInstance(nil, "", err)}
}
+ c = newC
l := c.loader
diff --git a/cue/load/loader_test.go b/cue/load/loader_test.go
index 18f709e..949c3e9 100644
--- a/cue/load/loader_test.go
+++ b/cue/load/loader_test.go
@@ -43,10 +43,14 @@
err string
}{{
args: nil,
- want: "test: test.cue (1 files)",
+ want: `
+test: test.cue (1 files)
+ sub: sub/sub.cue (1 files)`,
}, {
args: args("."),
- want: "test: test.cue (1 files)",
+ want: `
+test: test.cue (1 files)
+ sub: sub/sub.cue (1 files)`,
}, {
args: args("./other/..."),
want: `
@@ -64,7 +68,9 @@
file: other/file/file.cue (1 files)`,
}, {
args: args("./hello"),
- want: "test: test.cue hello/test.cue (2 files)",
+ want: `
+test: test.cue hello/test.cue (2 files)
+ sub: sub/sub.cue (1 files)`,
}, {
args: args("./anon.cue", "./other/anon.cue"),
want: ": ./anon.cue ./other/anon.cue (2 files)",
@@ -84,7 +90,8 @@
args: args("./imports"),
want: `
imports: imports/imports.cue (1 files)
- catch: pkg/acme.com/catch/catch.cue (1 files)`,
+ catch: pkg/acme.com/catch/catch.cue (1 files)
+ helper: pkg/acme.com/helper/helper.cue (1 files)`,
err: ``,
}}
for i, tc := range testCases {
diff --git a/cue/load/testdata/cue.mod b/cue/load/testdata/cue.mod
index b86175d..b71591c 100644
--- a/cue/load/testdata/cue.mod
+++ b/cue/load/testdata/cue.mod
@@ -12,3 +12,4 @@
// See the License for the specific language governing permissions and
// limitations under the License.
+module: "example.org/test"
\ No newline at end of file
diff --git a/cue/load/testdata/pkg/acme.com/catch/catch.cue b/cue/load/testdata/pkg/acme.com/catch/catch.cue
index 7b952fe..0b9beae 100644
--- a/cue/load/testdata/pkg/acme.com/catch/catch.cue
+++ b/cue/load/testdata/pkg/acme.com/catch/catch.cue
@@ -1,3 +1,5 @@
package catch
-Method: "tnt" | "catapult" | "net"
+import "acme.com/helper"
+
+Method: "tnt" | "catapult" | "net" | helper.Gotcha
diff --git a/cue/load/testdata/pkg/acme.com/helper/helper.cue b/cue/load/testdata/pkg/acme.com/helper/helper.cue
new file mode 100644
index 0000000..bdd38a7
--- /dev/null
+++ b/cue/load/testdata/pkg/acme.com/helper/helper.cue
@@ -0,0 +1,3 @@
+package helper
+
+Gotcha: "gotcha"
diff --git a/cue/load/testdata/sub/sub.cue b/cue/load/testdata/sub/sub.cue
new file mode 100644
index 0000000..04b66b7
--- /dev/null
+++ b/cue/load/testdata/sub/sub.cue
@@ -0,0 +1,17 @@
+// Copyright 2019 CUE Authors
+//
+// Licensed under the Apache License, Version 2.0 (the "License");
+// you may not use this file except in compliance with the License.
+// You may obtain a copy of the License at
+//
+// http://www.apache.org/licenses/LICENSE-2.0
+//
+// Unless required by applicable law or agreed to in writing, software
+// distributed under the License is distributed on an "AS IS" BASIS,
+// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+// See the License for the specific language governing permissions and
+// limitations under the License.
+
+package sub
+
+User: "sub"
diff --git a/cue/load/testdata/test.cue b/cue/load/testdata/test.cue
index 761254b..8829606 100644
--- a/cue/load/testdata/test.cue
+++ b/cue/load/testdata/test.cue
@@ -1 +1,5 @@
-package test
\ No newline at end of file
+package test
+
+import "example.org/test/sub"
+
+out: "Hello \(sub.User)!"