Skip to content

Commit

Permalink
encoding/protobuf: cope with major version suffixes in module paths
Browse files Browse the repository at this point in the history
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 <rogpeppe@gmail.com>
Change-Id: I36fbc476ecdaff1fce01dd2e5095c77f455bbacf
Reviewed-on: https://review.gerrithub.io/c/cue-lang/cue/+/1194791
Unity-Result: CUE porcuepine <cue.porcuepine@gmail.com>
Reviewed-by: Daniel Martí <mvdan@mvdan.cc>
TryBot-Result: CUEcueckoo <cueckoo@cuelang.org>
  • Loading branch information
rogpeppe committed May 16, 2024
1 parent ac35a40 commit b57a20a
Show file tree
Hide file tree
Showing 2 changed files with 19 additions and 4 deletions.
21 changes: 18 additions & 3 deletions encoding/protobuf/protobuf.go
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -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
Expand Down Expand Up @@ -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{},
Expand Down
2 changes: 1 addition & 1 deletion encoding/protobuf/protobuf_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -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"),
Expand Down

0 comments on commit b57a20a

Please sign in to comment.