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

x/tools/stringer: Generate platform specific code #53753

Closed
holyhope opened this issue Jul 8, 2022 · 2 comments
Closed

x/tools/stringer: Generate platform specific code #53753

holyhope opened this issue Jul 8, 2022 · 2 comments
Labels
FrozenDueToAge Tools This label describes issues relating to any tools in the x/tools repository.
Milestone

Comments

@holyhope
Copy link

holyhope commented Jul 8, 2022

stringer version

v0.1.11

What did you do?

  1. I define a enumeration in a platform specific go file: https://go.dev/play/p/1cKkWu9ngsD
    //go:build darwin && !linux && !freebsd && !netbsd && !openbsd && !windows && !js
    // +build darwin,!linux,!freebsd,!netbsd,!openbsd,!windows,!js
    
    package mypackage
    
    //go:generate stringer -type=MyEnum
    type MyEnum uint32
    
    const (
    	DarwinValue1Enum MyEnum = iota
    	DarwinValue2Enum
    )
  2. I run stringer -type=MyEnum to generate a new file myenum_string.go: https://go.dev/play/p/eR6E7A9A7aU

What did you expect to see?

I expect to get a valid code for all platform. Which here means the MyEnum.String() should only exists on darwin platform.

What did you see instead?

The new generated file is only valid when compiling for darwin, and not for other GOOS:
GOOS=linux go vet ./...

./myenum_string.go:11:8: undefined: DarwinValue1Enum
./myenum_string.go:12:8: undefined: DarwinValue2Enum
./myenum_string.go:19:9: undefined: MyEnum
./myenum_string.go:20:10: undefined: MyEnum

Suggestions

I suggest to either:

  1. stringer copies the header from the current file so it inject automatically the right //go:build comments.
  2. Add --platform flag as a list of string so stringer automatically adds the header.
  3. Add --template flag as a path to a template, so stringer use it to generate go files (and I can add my own //go:build comment).
@gopherbot gopherbot added the Tools This label describes issues relating to any tools in the x/tools repository. label Jul 8, 2022
@gopherbot gopherbot added this to the Unreleased milestone Jul 8, 2022
@holyhope
Copy link
Author

holyhope commented Jul 8, 2022

Note: I looked for this kind of issue, but I may missed it. If so, I am sorry for the duplication;;;

@seankhliao
Copy link
Member

Duplicate of #47179

@seankhliao seankhliao marked this as a duplicate of #47179 Jul 8, 2022
@seankhliao seankhliao closed this as not planned Won't fix, can't repro, duplicate, stale Jul 8, 2022
@golang golang locked and limited conversation to collaborators Jul 8, 2023
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
FrozenDueToAge Tools This label describes issues relating to any tools in the x/tools repository.
Projects
None yet
Development

No branches or pull requests

3 participants