cue icon indicating copy to clipboard operation
cue copied to clipboard

cmd/cue exp gengotypes package override broken when cue source has imports

Open trumant opened this issue 10 months ago • 1 comments

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

$ cue version
cue version v0.13.0-alpha.4

go version go1.24.2
      -buildmode exe
       -compiler gc
       -trimpath true
     CGO_ENABLED 0
          GOARCH arm64
            GOOS darwin
         GOARM64 v8.0
cue.lang.version v0.13.0

Does this issue reproduce with the latest stable release?

Yes, I can repro with v0.12.1

What did you do?

Here's the testscript txtar

cue exp gengotypes foo.cue
-- cue_types_gen.go --
// Code generated by "cue exp gengotypes"; DO NOT EDIT.

package security_insights_spec

type URL string

type Email string

type Date string
-- foo.cue --
package security_insights_spec
@go("si")
import (
  "time"
)



#URL: =~"^https?://[^\\s]+$"
#Email: =~"^[A-Za-z0-9._%+-]+@[A-Za-z0-9.-]+\\.[A-Za-z]{2,}$"
#Date: time.Format("2006-01-02")

What did you expect to see?

-- cue_types_gen.go --
// Code generated by "cue exp gengotypes"; DO NOT EDIT.

package si

type URL string

type Email string

type Date string

What did you see instead?

-- cue_types_gen.go --
// Code generated by "cue exp gengotypes"; DO NOT EDIT.

package security_insights_spec

type URL string

type Email string

type Date string

trumant avatar Apr 27 '25 13:04 trumant

Thanks for filing this bug! I believe it will work for you if you move the attribute after the imports, as otherwise it's not attached to the top-level struct value.

cue exp gengotypes should do a better job about warning you when @go attributes are not being used though, as that is surprisingly easy to do by accident.

mvdan avatar May 01 '25 17:05 mvdan