diff --git a/cue/load/import.go b/cue/load/import.go index 5a46fc46f..4f21afae4 100644 --- a/cue/load/import.go +++ b/cue/load/import.go @@ -185,16 +185,7 @@ func (l *loader) importPkg(pos token.Pos, p *build.Instance) *build.Instance { return p } - for _, f := range p.CUEFiles { - if !ctxt.isAbsPath(f) { - f = ctxt.joinPath(cfg.ModuleRoot, f) - } - r, err := ctxt.openFile(f) - if err != nil { - p.ReportError(err) - } - _ = p.AddFile(f, r) - } + l.addFiles(cfg.ModuleRoot, p) p.Complete() return p } diff --git a/cue/load/loader.go b/cue/load/loader.go index 8d6b349c2..debd4ae05 100644 --- a/cue/load/loader.go +++ b/cue/load/loader.go @@ -152,17 +152,7 @@ func (l *loader) cueFilesPackage(files []string) *build.Instance { // bp.ImportPath = ModDirImportPath(dir) // } - for _, f := range pkg.CUEFiles { - fs := &l.cfg.fileSystem - if !fs.isAbsPath(f) { - f = fs.joinPath(cfg.Dir, f) - } - r, err := fs.openFile(f) - if err != nil { - pkg.ReportError(err) - } - _ = pkg.AddFile(f, r) - } + l.addFiles(cfg.Dir, pkg) pkg.Local = true l.stk.Push("user") @@ -176,6 +166,23 @@ func (l *loader) cueFilesPackage(files []string) *build.Instance { return pkg } +func (l *loader) addFiles(dir string, p *build.Instance) { + files := p.CUEFiles + fs := &l.cfg.fileSystem + + for _, f := range files { + if !fs.isAbsPath(f) { + f = fs.joinPath(dir, f) + } + r, err := fs.openFile(f) + if err != nil { + p.ReportError(err) + } + + _ = p.AddFile(f, r) + } +} + func cleanImport(path string) string { orig := path path = pathpkg.Clean(path) diff --git a/cue/load/loader_test.go b/cue/load/loader_test.go index a7aa8671b..c6de0ab6e 100644 --- a/cue/load/loader_test.go +++ b/cue/load/loader_test.go @@ -241,9 +241,6 @@ dir: $CWD/testdata/toolonly display:./toolonly`, }} for i, tc := range testCases { - // if i != 5 { - // continue - // } t.Run(strconv.Itoa(i)+"/"+strings.Join(tc.args, ":"), func(t *testing.T) { pkgs := Instances(tc.args, tc.cfg) @@ -261,7 +258,7 @@ display:./toolonly`, want := strings.TrimSpace(tc.want) want = strings.Replace(want, "\t", " ", -1) if got != want { - t.Errorf("\n%s", diff.Diff(got, want)) + t.Errorf("\n%s", diff.Diff(want, got)) t.Logf("\n%s", got) } })