From ac35a4064328e235d002ae80d70a05931daffae9 Mon Sep 17 00:00:00 2001 From: Roger Peppe Date: Thu, 16 May 2024 11:16:05 +0100 Subject: [PATCH] cue/load: do not scan imports of non-CUE files MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit 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 Change-Id: I703b7c1f95be253070792d7744589474ce88ad7a Reviewed-on: https://review.gerrithub.io/c/cue-lang/cue/+/1194827 Reviewed-by: Daniel Martí TryBot-Result: CUEcueckoo --- cmd/cue/cmd/testdata/script/def_proto.txtar | 6 ++++++ cue/load/instances.go | 4 ++++ 2 files changed, 10 insertions(+) diff --git a/cmd/cue/cmd/testdata/script/def_proto.txtar b/cmd/cue/cmd/testdata/script/def_proto.txtar index 6c9ba6b29bc..0c3ca44f448 100644 --- a/cmd/cue/cmd/testdata/script/def_proto.txtar +++ b/cmd/cue/cmd/testdata/script/def_proto.txtar @@ -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 diff --git a/cue/load/instances.go b/cue/load/instances.go index 8e0382d4c86..be476186169 100644 --- a/cue/load/instances.go +++ b/cue/load/instances.go @@ -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)