this repo has no description
0
fork

Configure Feed

Select the types of activity you want to include in your feed.

encoding/protobuf: cope with major version suffixes in module paths

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>

+19 -4
+18 -3
encoding/protobuf/protobuf.go
··· 22 22 // # Package Paths 23 23 // 24 24 // If a .proto file contains a go_package directive, it will be used as the 25 - // destination package fo the generated .cue files. A common use case is to 25 + // destination package for the generated .cue files. A common use case is to 26 26 // generate the CUE in the same directory as the .proto definition. If a 27 27 // destination package is not within the current CUE module, it will be written 28 28 // relative to the pkg directory. 29 29 // 30 30 // If a .proto file does not specify go_package, it will convert a proto package 31 31 // "google.parent.sub" to the import path "googleapis.com/google/parent/sub". 32 - // It is safe to mix package with and without a go_package within the same 32 + // It is safe to mix packages with and without a go_package within the same 33 33 // project. 34 34 // 35 35 // # Type Mappings ··· 99 99 "cuelang.org/go/cue/parser" 100 100 "cuelang.org/go/cue/token" 101 101 "cuelang.org/go/internal" 102 + "cuelang.org/go/mod/module" 102 103 103 104 // Generated protobuf CUE may use builtins. Ensure that these can always be 104 105 // found, even if the user does not use cue/load or another package that ··· 181 182 // it will be observable by the Err method fo the Extractor. It is safe, 182 183 // however, to only check errors after building the output. 183 184 func NewExtractor(c *Config) *Extractor { 185 + var modulePath string 186 + // We don't want to consider the module's major version as 187 + // part of the path when checking to see a protobuf package 188 + // declares itself as part of that module. 189 + // TODO(rogpeppe) the Go package path might itself include a major 190 + // version, so we should probably consider that too. 191 + if c.Module != "" { 192 + var ok bool 193 + modulePath, _, ok = module.SplitPathVersion(c.Module) 194 + if !ok { 195 + modulePath = c.Module 196 + 197 + } 198 + } 184 199 cwd, _ := os.Getwd() 185 200 b := &Extractor{ 186 201 root: c.Root, 187 202 cwd: cwd, 188 203 paths: c.Paths, 189 204 pkgName: c.PkgName, 190 - module: c.Module, 205 + module: modulePath, 191 206 enumMode: c.EnumMode, 192 207 fileCache: map[string]result{}, 193 208 imports: map[string]*build.Instance{},
+1 -1
encoding/protobuf/protobuf_test.go
··· 78 78 root := filepath.Join(cwd, "testdata/istio.io/api") 79 79 c := &Config{ 80 80 Root: root, 81 - Module: "istio.io/api", 81 + Module: "istio.io/api@v0", 82 82 Paths: []string{ 83 83 root, 84 84 filepath.Join(cwd, "testdata"),