From b57a20aed28d286054c67b8dfa1921e20e12d4b9 Mon Sep 17 00:00:00 2001 From: Roger Peppe Date: Wed, 15 May 2024 16:32:33 +0100 Subject: [PATCH] encoding/protobuf: cope with major version suffixes in module paths MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Currently the protobuf logic for determining if an import is inside the main module does not work if the module has a major version suffix. This change makes that work OK. It's not perfect, because we do not consider the major version that might be present in the Go import path, but it's certainly better than before. Signed-off-by: Roger Peppe Change-Id: I36fbc476ecdaff1fce01dd2e5095c77f455bbacf Reviewed-on: https://review.gerrithub.io/c/cue-lang/cue/+/1194791 Unity-Result: CUE porcuepine Reviewed-by: Daniel Martí TryBot-Result: CUEcueckoo --- encoding/protobuf/protobuf.go | 21 ++++++++++++++++++--- encoding/protobuf/protobuf_test.go | 2 +- 2 files changed, 19 insertions(+), 4 deletions(-) diff --git a/encoding/protobuf/protobuf.go b/encoding/protobuf/protobuf.go index af7958246d1..efb7924e80e 100644 --- a/encoding/protobuf/protobuf.go +++ b/encoding/protobuf/protobuf.go @@ -22,14 +22,14 @@ // # Package Paths // // If a .proto file contains a go_package directive, it will be used as the -// destination package fo the generated .cue files. A common use case is to +// destination package for the generated .cue files. A common use case is to // generate the CUE in the same directory as the .proto definition. If a // destination package is not within the current CUE module, it will be written // relative to the pkg directory. // // If a .proto file does not specify go_package, it will convert a proto package // "google.parent.sub" to the import path "googleapis.com/google/parent/sub". -// It is safe to mix package with and without a go_package within the same +// It is safe to mix packages with and without a go_package within the same // project. // // # Type Mappings @@ -99,6 +99,7 @@ import ( "cuelang.org/go/cue/parser" "cuelang.org/go/cue/token" "cuelang.org/go/internal" + "cuelang.org/go/mod/module" // Generated protobuf CUE may use builtins. Ensure that these can always be // found, even if the user does not use cue/load or another package that @@ -181,13 +182,27 @@ type result struct { // it will be observable by the Err method fo the Extractor. It is safe, // however, to only check errors after building the output. func NewExtractor(c *Config) *Extractor { + var modulePath string + // We don't want to consider the module's major version as + // part of the path when checking to see a protobuf package + // declares itself as part of that module. + // TODO(rogpeppe) the Go package path might itself include a major + // version, so we should probably consider that too. + if c.Module != "" { + var ok bool + modulePath, _, ok = module.SplitPathVersion(c.Module) + if !ok { + modulePath = c.Module + + } + } cwd, _ := os.Getwd() b := &Extractor{ root: c.Root, cwd: cwd, paths: c.Paths, pkgName: c.PkgName, - module: c.Module, + module: modulePath, enumMode: c.EnumMode, fileCache: map[string]result{}, imports: map[string]*build.Instance{}, diff --git a/encoding/protobuf/protobuf_test.go b/encoding/protobuf/protobuf_test.go index e5b1ce22ee3..3d1a5303cc1 100644 --- a/encoding/protobuf/protobuf_test.go +++ b/encoding/protobuf/protobuf_test.go @@ -78,7 +78,7 @@ func TestBuild(t *testing.T) { root := filepath.Join(cwd, "testdata/istio.io/api") c := &Config{ Root: root, - Module: "istio.io/api", + Module: "istio.io/api@v0", Paths: []string{ root, filepath.Join(cwd, "testdata"),