Skip to content
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: nil pointer reference panic when using "get go" #2046

Closed
LeonardAukea opened this issue Oct 26, 2022 · 7 comments
Closed

cmd/cue: nil pointer reference panic when using "get go" #2046

LeonardAukea opened this issue Oct 26, 2022 · 7 comments
Labels
get go issues related to cue get go NeedsFix panic

Comments

@LeonardAukea
Copy link

What version of CUE are you using (cue version)?

$ cue version
cue version v0.5.0-alpha.1

       -compiler gc
        -ldflags -s -w
     CGO_ENABLED 0
          GOARCH amd64
            GOOS darwin
         GOAMD64 v1

Does this issue reproduce with the latest release?

What did you do?

I ran the following commands:

go get istio.io/api/networking/v1alpha3
go: downloading istio.io/api v0.0.0-20221025180617-53adcb30720d
go: downloading google.golang.org/protobuf v1.28.1
go: downloading google.golang.org/genproto v0.0.0-20221018160656-63c7b68cfc55
go: upgraded google.golang.org/genproto v0.0.0-20220624142145-8cd45d7dbd1f => v0.0.0-20221018160656-63c7b68cfc55
go: added istio.io/api v0.0.0-20221025180617-53adcb30720d

cue get go istio.io/api/networking/v1alpha3

Which returned:

What did you expect to see?

Some confirmation from the cli that the cue get go loaded the definitions successfully, or at least not return an error.

What did you see instead?

panic: runtime error: invalid memory address or nil pointer dereference [recovered]
        panic: runtime error: invalid memory address or nil pointer dereference
[signal SIGSEGV: segmentation violation code=0x1 addr=0x20 pc=0x16c5ed9]

goroutine 1 [running]:
cuelang.org/go/cmd/cue/cmd.recoverError(0xc008e07e90)
        /home/runner/go/pkg/mod/cuelang.org/go@v0.5.0-alpha.1/cmd/cue/cmd/root.go:232 +0x7b
panic({0x1768dc0, 0x1d9dc70})
        /opt/hostedtoolcache/go/1.18.1/x64/src/runtime/panic.go:838 +0x207
cuelang.org/go/cmd/cue/cmd.(*extractor).extractPkg(0xc00a2ac3f0, {0xc00003a0c4, 0x2a}, 0x0)
        /home/runner/go/pkg/mod/cuelang.org/go@v0.5.0-alpha.1/cmd/cue/cmd/get_go.go:438 +0x79
cuelang.org/go/cmd/cue/cmd.(*extractor).extractPkg(0xc00a2ac3f0, {0xc00003a0c4, 0x2a}, 0xc000565380)
        /home/runner/go/pkg/mod/cuelang.org/go@v0.5.0-alpha.1/cmd/cue/cmd/get_go.go:554 +0xb3a
cuelang.org/go/cmd/cue/cmd.extract(0xc000325680, {0xc0002c76a0, 0x1, 0x1})
        /home/runner/go/pkg/mod/cuelang.org/go@v0.5.0-alpha.1/cmd/cue/cmd/get_go.go:417 +0x3dc
cuelang.org/go/cmd/cue/cmd.mkRunE.func1(0xc000337b80?, {0xc0002c76a0?, 0x1?, 0x1?})
        /home/runner/go/pkg/mod/cuelang.org/go@v0.5.0-alpha.1/cmd/cue/cmd/root.go:48 +0x4d
github.com/spf13/cobra.(*Command).execute(0xc000337b80, {0xc0002c7670, 0x1, 0x1})
        /home/runner/go/pkg/mod/github.com/spf13/cobra@v1.4.0/command.go:856 +0x67c
github.com/spf13/cobra.(*Command).ExecuteC(0xc000336500)
        /home/runner/go/pkg/mod/github.com/spf13/cobra@v1.4.0/command.go:974 +0x3b4
github.com/spf13/cobra.(*Command).Execute(...)
        /home/runner/go/pkg/mod/github.com/spf13/cobra@v1.4.0/command.go:902
cuelang.org/go/cmd/cue/cmd.(*Command).Run(0xc000325680, {0x3?, 0x3?})
        /home/runner/go/pkg/mod/cuelang.org/go@v0.5.0-alpha.1/cmd/cue/cmd/root.go:217 +0x6b
cuelang.org/go/cmd/cue/cmd.mainErr({0x1954ed8, 0xc0000be008}, {0xc0000bc050?, 0x60?, 0x1dc1f60?})
        /home/runner/go/pkg/mod/cuelang.org/go@v0.5.0-alpha.1/cmd/cue/cmd/root.go:154 +0x55
cuelang.org/go/cmd/cue/cmd.Main()
        /home/runner/go/pkg/mod/cuelang.org/go@v0.5.0-alpha.1/cmd/cue/cmd/root.go:136 +0x85
main.main()
        /home/runner/go/pkg/mod/cuelang.org/go@v0.5.0-alpha.1/cmd/cue/main.go:24 +0x19

They seem to have been loaded in cue.mod either way.

cue.mod/gen/istio.io/api/networking
└── v1alpha3
    ├── destination_rule.pb_go_gen.cue
    ├── destination_rule_deepcopy.gen_go_gen.cue
    ├── destination_rule_json.gen_go_gen.cue
    ├── envoy_filter.pb_go_gen.cue
    ├── envoy_filter_deepcopy.gen_go_gen.cue
    ├── envoy_filter_json.gen_go_gen.cue
    ├── gateway.pb_go_gen.cue
    ├── gateway_deepcopy.gen_go_gen.cue
    ├── gateway_json.gen_go_gen.cue
    ├── service_entry.pb_go_gen.cue
    ├── service_entry_deepcopy.gen_go_gen.cue
    ├── service_entry_json.gen_go_gen.cue
    ├── sidecar.pb_go_gen.cue
    ├── sidecar_deepcopy.gen_go_gen.cue
    ├── sidecar_json.gen_go_gen.cue
    ├── virtual_service.pb_go_gen.cue
    ├── virtual_service_deepcopy.gen_go_gen.cue
    ├── virtual_service_json.gen_go_gen.cue
    ├── workload_entry.pb_go_gen.cue
    ├── workload_entry_deepcopy.gen_go_gen.cue
    ├── workload_entry_json.gen_go_gen.cue
    ├── workload_group.pb_go_gen.cue
    ├── workload_group_deepcopy.gen_go_gen.cue
    └── workload_group_json.gen_go_gen.cue
@LeonardAukea LeonardAukea added NeedsInvestigation Triage Requires triage/attention labels Oct 26, 2022
@LeonardAukea
Copy link
Author

I've probably done something I don't fully understand. but maybe this is also useful:

 cue ls
import failed: import failed: cannot find package "google.golang.org/protobuf/types/known/durationpb":
    ./k8s_defs.cue:6:2
    ./cue.mod/gen/istio.io/api/networking/v1alpha3/virtual_service.pb_go_gen.cue:7:8

@LeonardAukea LeonardAukea changed the title Invalid memory address cue get go Invalid memory address cue get go istio.io/api/networking/v1alpha3 Oct 26, 2022
@wolfmah
Copy link

wolfmah commented Dec 3, 2022

I stumbled upon the same problem this morning.

I found that all the other APIs from Istio are ok, only the networking one is giving the stack trace, on both API versions.

Those are all ok:

go get istio.io/api/extensions/v1alpha1
cue get go istio.io/api/extensions/v1alpha1

go get istio.io/api/security/v1beta1
cue get go istio.io/api/security/v1beta1

go get istio.io/api/telemetry/v1alpha1
cue get go istio.io/api/telemetry/v1alpha1

Those are giving the same stack trace as mentioned previously:

go get istio.io/api/networking/v1alpha3
cue get go istio.io/api/networking/v1alpha3

go get istio.io/api/networking/v1beta1
cue get go istio.io/api/networking/v1beta1

@mvdan mvdan changed the title Invalid memory address cue get go istio.io/api/networking/v1alpha3 cmd/cue: nil pointer reference panic when using "get go" Feb 8, 2023
@kortschak
Copy link

I am seeing the same issue when importing stdlib packages. Digging in to it, it was failing because "io/fs" was not in the p.Import map by the time we get to here, so p is nil in the e.extractPkg call.

@seh
Copy link
Contributor

seh commented Apr 18, 2023

I'm able to reproduce what I think is the same problem using CUE version 0.5.0.

Run these commands:

  1. go get -d github.com/open-policy-agent/gatekeeper@6e84f384ba67c8866d7db70cb471604982bf7406
  2. cue get go github.com/open-policy-agent/gatekeeper/apis/mutations/v1

That yields the following failure.

"cue get go" output
panic: runtime error: invalid memory address or nil pointer dereference [recovered]
	panic: runtime error: invalid memory address or nil pointer dereference
[signal SIGSEGV: segmentation violation code=0x1 addr=0x20 pc=0x16ff0f9]

goroutine 1 [running]:
cuelang.org/go/cmd/cue/cmd.recoverError(0xc03143de90)
	cuelang.org/go/cmd/cue/cmd/root.go:261 +0x7b
panic({0x17aeca0, 0x1e09ee0})
	runtime/panic.go:884 +0x213
cuelang.org/go/cmd/cue/cmd.(*extractor).extractPkg(0xc0513fd7a0, {0xc000040064, 0x1d}, 0x0)
	cuelang.org/go/cmd/cue/cmd/get_go.go:438 +0x79
cuelang.org/go/cmd/cue/cmd.(*extractor).extractPkg(0xc0513fd7a0, {0xc000040064, 0x1d}, 0xc000263500)
	cuelang.org/go/cmd/cue/cmd/get_go.go:554 +0xb1a
cuelang.org/go/cmd/cue/cmd.(*extractor).extractPkg(0xc0513fd7a0, {0xc000040064, 0x1d}, 0xc00071b500)
	cuelang.org/go/cmd/cue/cmd/get_go.go:554 +0xb1a
cuelang.org/go/cmd/cue/cmd.(*extractor).extractPkg(0xc0513fd7a0, {0xc000040064, 0x1d}, 0xc000a97980)
	cuelang.org/go/cmd/cue/cmd/get_go.go:554 +0xb1a
cuelang.org/go/cmd/cue/cmd.(*extractor).extractPkg(0xc0513fd7a0, {0xc000040064, 0x1d}, 0xc000cda600)
	cuelang.org/go/cmd/cue/cmd/get_go.go:554 +0xb1a
cuelang.org/go/cmd/cue/cmd.(*extractor).extractPkg(0xc0513fd7a0, {0xc000040064, 0x1d}, 0xc000cde180)
	cuelang.org/go/cmd/cue/cmd/get_go.go:554 +0xb1a
cuelang.org/go/cmd/cue/cmd.(*extractor).extractPkg(0xc0513fd7a0, {0xc000040064, 0x1d}, 0xc000cdf500)
	cuelang.org/go/cmd/cue/cmd/get_go.go:554 +0xb1a
cuelang.org/go/cmd/cue/cmd.extract(0xc0002dced0, {0xc00039c050, 0x1, 0x1})
	cuelang.org/go/cmd/cue/cmd/get_go.go:417 +0x3e5
cuelang.org/go/cmd/cue/cmd.mkRunE.func1(0xc00038e280?, {0xc00039c050, 0x1, 0x1})
	cuelang.org/go/cmd/cue/cmd/root.go:72 +0xb3
github.com/spf13/cobra.(*Command).execute(0xc00038e280, {0xc00039c020, 0x1, 0x1})
	github.com/spf13/cobra@v1.4.0/command.go:856 +0x67c
github.com/spf13/cobra.(*Command).ExecuteC(0xc0002f6a00)
	github.com/spf13/cobra@v1.4.0/command.go:974 +0x3bd
github.com/spf13/cobra.(*Command).Execute(...)
	github.com/spf13/cobra@v1.4.0/command.go:902
cuelang.org/go/cmd/cue/cmd.(*Command).Run(0xc0002dced0, {0x3?, 0x3?})
	cuelang.org/go/cmd/cue/cmd/root.go:246 +0x6b
cuelang.org/go/cmd/cue/cmd.mainErr({0x1996288, 0xc000122008}, {0xc000120050?, 0x2940a68?, 0x60?})
	cuelang.org/go/cmd/cue/cmd/root.go:183 +0x55
cuelang.org/go/cmd/cue/cmd.Main()
	cuelang.org/go/cmd/cue/cmd/root.go:165 +0x7c
main.main()
	cuelang.org/go/cmd/cue/main.go:24 +0x19

That command does produce a bunch of CUE files, but fails before it can produce all of them. Unfortunately, it also produces files under the cue.mod/gen/{context,internal,io,net,syscall} directories too, recalling #1453.

@seh
Copy link
Contributor

seh commented Apr 19, 2023

Unfortunately, it also produces files under the cue.mod/gen/{context,internal,io,net,syscall} directories too, recalling #1453.

For this case, here's a workaround that I had forgotten about:

cue get go github.com/open-policy-agent/gatekeeper/apis/mutations/v1 --exclude='Matchable$'

That eliminates the dependency on controller-runtime's client package.

@iNoahNothing
Copy link

Super hackish but I was workaround this by manually generating the Istio schema from the protobuf at https://github.com/istio/api.

I took advantage of the fact that Istio already uses cue to generate their CRDs and OpenAPI docs from the protobufs and added a statement to https://github.com/istio/tools/tree/master/cmd/cue-gen to print the cue definitions once they have been loaded into the script.

This is not a robust solution but got me the schema output I was looking for.

@uhthomas
Copy link
Contributor

uhthomas commented Dec 4, 2023

It looks like there may be a few conflated issues here.

It seems the panic for github.com/open-policy-agent/gatekeeper/apis/mutations/v1 is because cue is mixing import paths and package paths golang.org/x/net/http2/hpack and vendor/golang.org/x/net/http2/hpack.

The panic for istio.io/api/networking/v1alpha3 is because of #1607, where cue thinks the imported package is google.golang.org/protobuf/types/known/durationpb when in reality it's github.com/golang/protobuf/ptypes/duration.

https://github.com/istio/api/blob/342d60d7bee6bdaa15490ac1120e8e8c648abbb8/networking/v1beta1/virtual_service.pb.go

@mvdan mvdan removed the Triage Requires triage/attention label Mar 17, 2024
cueckoo pushed a commit that referenced this issue Mar 17, 2024
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 <thomas@6f.io>
cueckoo pushed a commit that referenced this issue Mar 17, 2024
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 <thomas@6f.io>
Co-authored-by: Daniel Martí <mvdan@mvdan.cc>
Signed-off-by: Daniel Martí <mvdan@mvdan.cc>
@mvdan mvdan moved this to v0.8.1 in Release v0.8 Apr 2, 2024
cueckoo pushed a commit that referenced this issue Apr 3, 2024
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 <thomas@6f.io>
Co-authored-by: Daniel Martí <mvdan@mvdan.cc>
Signed-off-by: Daniel Martí <mvdan@mvdan.cc>
Reviewed-on: https://review.gerrithub.io/c/cue-lang/cue/+/1173100
Reviewed-by: Paul Jolly <paul@myitcv.io>
Unity-Result: CUE porcuepine <cue.porcuepine@gmail.com>
TryBot-Result: CUEcueckoo <cueckoo@gmail.com>
(cherry picked from commit b648cf4)
cueckoo pushed a commit that referenced this issue Apr 3, 2024
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 <thomas@6f.io>
Co-authored-by: Daniel Martí <mvdan@mvdan.cc>
Signed-off-by: Daniel Martí <mvdan@mvdan.cc>
Reviewed-on: https://review.gerrithub.io/c/cue-lang/cue/+/1173100
Reviewed-by: Paul Jolly <paul@myitcv.io>
Unity-Result: CUE porcuepine <cue.porcuepine@gmail.com>
TryBot-Result: CUEcueckoo <cueckoo@gmail.com>
(cherry picked from commit b648cf4)
Reviewed-on: https://review.gerrithub.io/c/cue-lang/cue/+/1190977
TryBot-Result: CUEcueckoo <cueckoo@cuelang.org>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
get go issues related to cue get go NeedsFix panic
Projects
No open projects
Status: v0.8.1
Development

No branches or pull requests

8 participants