Skip to content

Commit

Permalink
cue/load: do not scan imports of non-CUE files
Browse files Browse the repository at this point in the history
The recent fix to do better with imports in command-line-specified files
(https://cuelang.org/cl/1194765) changed cue/load to scan imports before
loading files. It scans imports in all files including non-CUE files.
This turns out to be a problem because the code to get the CUE syntax in
turn invokes the internal/encoding.Decoder logic which requires a fully
populated encoding.Config struct in order to do its job properly.
Specifically, the encoding.Config created by cue/load does not have the
protobuf import path required when decoding protobuf files, so this was
failing.

Work around this by scanning imports of CUE files only, mirroring the
same logic in cue/load.matchFile.

In the future, we will probably have to rework this, as it's entirely
possible that non-CUE specified on the command line might end up
producing a CUE representation that imports external CUE packages, but
for now this should be sufficient.

Signed-off-by: Roger Peppe <rogpeppe@gmail.com>
Change-Id: I703b7c1f95be253070792d7744589474ce88ad7a
Reviewed-on: https://review.gerrithub.io/c/cue-lang/cue/+/1194827
Reviewed-by: Daniel Martí <mvdan@mvdan.cc>
TryBot-Result: CUEcueckoo <cueckoo@cuelang.org>
  • Loading branch information
rogpeppe committed May 16, 2024
1 parent f513a17 commit ac35a40
Show file tree
Hide file tree
Showing 2 changed files with 10 additions and 0 deletions.
6 changes: 6 additions & 0 deletions cmd/cue/cmd/testdata/script/def_proto.txtar
Original file line number Diff line number Diff line change
@@ -1,3 +1,9 @@
env CUE_EXPERIMENT=modules=0
exec cue def policy.proto -p api -I include
cmp stdout expect-stdout

env CUE_EXPERIMENT=modules
env CUE_CACHE_DIR=$WORK/tmp/cache
exec cue def policy.proto -p api -I include
cmp stdout expect-stdout

Expand Down
4 changes: 4 additions & 0 deletions cue/load/instances.go
Original file line number Diff line number Diff line change
Expand Up @@ -188,6 +188,10 @@ func loadPackages(ctx context.Context, cfg *Config, synCache *syntaxCache, extra
}
// Add any imports found in other files.
for _, f := range otherFiles {
if f.Encoding != build.CUE {
// not a CUE file; assume it has no imports for now.
continue
}
syntaxes, err := synCache.getSyntax(f)
if err != nil {
return nil, fmt.Errorf("cannot get syntax for %q: %v", f.Filename, err)
Expand Down

0 comments on commit ac35a40

Please sign in to comment.