-
Notifications
You must be signed in to change notification settings - Fork 294
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
cmd/cue: get go panics extracting package for type alias #1607
Comments
The issue seems to be that the package import is not looked up in the package in which it is important, but rather always the top-level package. This is why this only fails with two layers of imports. |
More generally, the issue is that It does handle nested imports just fine. In my example above replacing the alias with a type declaration Life permitting I will attempt a PR this weekend. |
Correct, it indeed only occurs with type aliases. The simplest and arguably best thing to do is to preserve the indirection of type aliases. I looked at the underlying |
That weekend really got away from me 😆. Confirmed that this is still an issue in v0.4.3, and would (of course) welcome anyone to contribute a fix. |
I just ran into this as well and had a look at how to fix this. The repro as given above can be fixed by scanning through all imported packages imports, but that actually doesn't work 100% for all cases. After implementing this (very rudimentary), the repro ran through fine, but I tried to A solution to this is to instead of going through the package's imported packages, go through all packages that are known. diff --git a/cmd/cue/cmd/get_go.go b/cmd/cue/cmd/get_go.go
index b67bdf4e..20a62fb9 100644
--- a/cmd/cue/cmd/get_go.go
+++ b/cmd/cue/cmd/get_go.go
@@ -580,7 +580,16 @@ func (e *extractor) extractPkg(root string, p *packages.Package) error {
for path := range e.usedPkgs {
if !e.done[path] {
e.done[path] = true
- p := p.Imports[path]
+ for _, pkg := range e.pkgs {
+ if pkg.PkgPath == path {
+ p = pkg
+ break
+ }
+ }
+
+ if p == nil {
+ return fmt.Errorf("no package %v found", path)
+ }
if err := e.extractPkg(root, p); err != nil {
return err
} |
Most Go APIs like go/types.Package.Path and go/packages.Package.PkgPath use full canonical package paths, whereas go/packages.Package.Imports uses source import paths as they appear in the import declarations. We mixed the two, using paths from go/types to index into Imports, resulting in nil packages in the edge cases when they are different. One such instance is vendored packages, whose full path may be "vendor/foo/bar" when they are simply imported as "foo/bar". This also fixes a similar panic with some type aliases, in particular when an alias points to an indirectly imported package, as such a package would be missing from the Imports map. The two added testscripts reproduced both panics without the fix, and now work as expected with the fix. Fixes #1607. Fixes #2046. Change-Id: I51df3465da76b39f031855982b48552604817f80 Signed-off-by: Thomas Way <[email protected]>
Most Go APIs like go/types.Package.Path and go/packages.Package.PkgPath use full canonical package paths, whereas go/packages.Package.Imports uses source import paths as they appear in the import declarations. We mixed the two, using paths from go/types to index into Imports, resulting in nil packages in the edge cases when they are different. One such instance is vendored packages, whose full path may be "vendor/foo/bar" when they are simply imported as "foo/bar". This also fixes a similar panic with some type aliases, in particular when an alias points to an indirectly imported package, as such a package would be missing from the Imports map. The two added testscripts reproduced both panics without the fix, and now work as expected with the fix. Fixes #1607. Fixes #2046. Change-Id: I51df3465da76b39f031855982b48552604817f80 Signed-off-by: Thomas Way <[email protected]> Co-authored-by: Daniel Martí <[email protected]> Signed-off-by: Daniel Martí <[email protected]>
Most Go APIs like go/types.Package.Path and go/packages.Package.PkgPath use full canonical package paths, whereas go/packages.Package.Imports uses source import paths as they appear in the import declarations. We mixed the two, using paths from go/types to index into Imports, resulting in nil packages in the edge cases when they are different. One such instance is vendored packages, whose full path may be "vendor/foo/bar" when they are simply imported as "foo/bar". This also fixes a similar panic with some type aliases, in particular when an alias points to an indirectly imported package, as such a package would be missing from the Imports map. The two added testscripts reproduced both panics without the fix, and now work as expected with the fix. Fixes #1607. Fixes #2046. Change-Id: I51df3465da76b39f031855982b48552604817f80 Signed-off-by: Thomas Way <[email protected]> Co-authored-by: Daniel Martí <[email protected]> Signed-off-by: Daniel Martí <[email protected]> Reviewed-on: https://review.gerrithub.io/c/cue-lang/cue/+/1173100 Reviewed-by: Paul Jolly <[email protected]> Unity-Result: CUE porcuepine <[email protected]> TryBot-Result: CUEcueckoo <[email protected]> (cherry picked from commit b648cf4)
Most Go APIs like go/types.Package.Path and go/packages.Package.PkgPath use full canonical package paths, whereas go/packages.Package.Imports uses source import paths as they appear in the import declarations. We mixed the two, using paths from go/types to index into Imports, resulting in nil packages in the edge cases when they are different. One such instance is vendored packages, whose full path may be "vendor/foo/bar" when they are simply imported as "foo/bar". This also fixes a similar panic with some type aliases, in particular when an alias points to an indirectly imported package, as such a package would be missing from the Imports map. The two added testscripts reproduced both panics without the fix, and now work as expected with the fix. Fixes #1607. Fixes #2046. Change-Id: I51df3465da76b39f031855982b48552604817f80 Signed-off-by: Thomas Way <[email protected]> Co-authored-by: Daniel Martí <[email protected]> Signed-off-by: Daniel Martí <[email protected]> Reviewed-on: https://review.gerrithub.io/c/cue-lang/cue/+/1173100 Reviewed-by: Paul Jolly <[email protected]> Unity-Result: CUE porcuepine <[email protected]> TryBot-Result: CUEcueckoo <[email protected]> (cherry picked from commit b648cf4) Reviewed-on: https://review.gerrithub.io/c/cue-lang/cue/+/1190977 TryBot-Result: CUEcueckoo <[email protected]>
What version of CUE are you using (
cue version
)?Does this issue reproduce with the latest release?
Y
What did you do?
I was experimenting with importing go type definitions instead of the protobuf definitions they are generated from, and hit a panic.
I ran again in a debugger to see what I was doing wrong, and kind of suspect it could be a more general problem when we extract package A, where package A imports package B, then package A uses an aliased type from package C that was declared in package B.
Minimal go project to reproduce this:
What did you expect to see?
Passing test.
What did you see instead?
The text was updated successfully, but these errors were encountered: