You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
A panic occurs when an embedded/inline field is a type alias and Alias types are enabled in Go.
To reproduce on the main branch:
Apply the following example patch in controller-tools repository testdata:
diff --git a/pkg/crd/testdata/cronjob_types.go b/pkg/crd/testdata/cronjob_types.go
index e81a28ac..c6e16ab1 100644
--- a/pkg/crd/testdata/cronjob_types.go+++ b/pkg/crd/testdata/cronjob_types.go@@ -372,6 +372,9 @@ type CronJobSpec struct {
// This tests that selectable field.
SelectableFieldString string `json:"selectableFieldString,omitempty"`
++ // This tests that embedded struct, which is an alias type, is handled correctly.+ InlineAlias `json:",inline"`
}
type StringAlias = string
@@ -380,6 +383,14 @@ type StringAlias = string
// +kubebuilder:validation:MaxLength=255
type StringAliasWithValidation = string
+type InlineAlias = EmbeddedStruct++// EmbeddedStruct is for testing that embedded struct is handled correctly when it is used through an alias type.+type EmbeddedStruct struct {+ // FromEmbedded is a field from the embedded struct that was used through an alias type.+ FromEmbedded string `json:"fromEmbedded,omitempty"`+}+
type ContainsNestedMap struct {
InnerMap map[string]string `json:"innerMap,omitempty"`
}
Make sure Alias types are enabled, then run generate
$ go versiongo version go1.23.2 linux/amd64
$ cd pkg/crd/testdata
$ GODEBUG=gotypesalias=1 go generate
When running GODEBUG=gotypesalias=0 go generate or just go generate there will not be panic. I assume this is because of Go language level in go.mod.
FYI: Interestingly, I found the issue while running controller-gen in another project using go run sigs.k8s.io/controller-tools/cmd/controller-gen ... with go1.23.x. However, using go run sigs.k8s.io/controller-tools/cmd/[email protected] ... avoids the panic. We have pinned controller-tools v0.16.5 in the project's go.mod but it makes difference if the go run command has the version or not. When running go run -x, I noticed that gotypesalias=0 is set when specifying @v0.16.5, whereas without it, no flag is set, and compiler enables Alias types by default, presumably because project's language level in go.mod got set to Go 1.23 as a side effect after bumping some dependencies.
The text was updated successfully, but these errors were encountered:
A panic occurs when an embedded/inline field is a type alias and Alias types are enabled in Go.
To reproduce on the
main
branch:The output will be following
When running
GODEBUG=gotypesalias=0 go generate
or justgo generate
there will not be panic. I assume this is because of Go language level ingo.mod
.FYI: Interestingly, I found the issue while running
controller-gen
in another project usinggo run sigs.k8s.io/controller-tools/cmd/controller-gen ...
with go1.23.x. However, usinggo run sigs.k8s.io/controller-tools/cmd/[email protected] ...
avoids the panic. We have pinnedcontroller-tools
v0.16.5 in the project'sgo.mod
but it makes difference if thego run
command has the version or not. When runninggo run -x
, I noticed thatgotypesalias=0
is set when specifying@v0.16.5
, whereas without it, no flag is set, and compiler enables Alias types by default, presumably because project's language level ingo.mod
got set to Go 1.23 as a side effect after bumping some dependencies.The text was updated successfully, but these errors were encountered: