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/cmd/stringer: error when also having packages imported #62419

Closed
andreistan26 opened this issue Sep 1, 2023 · 5 comments
Closed

x/tools/cmd/stringer: error when also having packages imported #62419

andreistan26 opened this issue Sep 1, 2023 · 5 comments
Labels
Tools This label describes issues relating to any tools in the x/tools repository. WaitingForInfo Issue is not actionable because of missing required information, which needs to be provided.
Milestone

Comments

@andreistan26
Copy link

What version of Go are you using (go version)?

$ go version
go version go1.21.0 linux/amd64

Does this issue reproduce with the latest release?

Yes

What operating system and processor architecture are you using (go env)?

go env Output
$ go env
GO111MODULE=''
GOARCH='amd64'
GOBIN=''
GOCACHE='/home/andrei/.cache/go-build'
GOENV='/home/andrei/.config/go/env'
GOEXE=''
GOEXPERIMENT=''
GOFLAGS=''
GOHOSTARCH='amd64'
GOHOSTOS='linux'
GOINSECURE=''
GOMODCACHE='/home/andrei/Documents/Repositories/Go/pkg/mod'
GONOPROXY=''
GONOSUMDB=''
GOOS='linux'
GOPATH='/home/andrei/Documents/Repositories/Go'
GOPRIVATE=''
GOPROXY='https://proxy.golang.org,direct'
GOROOT='/usr/local/go'
GOSUMDB='sum.golang.org'
GOTMPDIR=''
GOTOOLCHAIN='auto'
GOTOOLDIR='/usr/local/go/pkg/tool/linux_amd64'
GOVCS=''
GOVERSION='go1.21.0'
GCCGO='gccgo'
GOAMD64='v1'
AR='ar'
CC='gcc'
CXX='g++'
CGO_ENABLED='1'
GOMOD='/home/andrei/Documents/Repositories/Go/src/github.com/andreistan26/golink/go.mod'
GOWORK=''
CGO_CFLAGS='-O2 -g'
CGO_CPPFLAGS=''
CGO_CXXFLAGS='-O2 -g'
CGO_FFLAGS='-O2 -g'
CGO_LDFLAGS='-O2 -g'
PKG_CONFIG='pkg-config'
GOGCCFLAGS='-fPIC -m64 -pthread -Wl,--no-gc-sections -fmessage-length=0 -ffile-prefix-map=/tmp/go-build946024150=/tmp/go-build -gno-record-gcc-switches'
GOROOT/bin/go version: go version go1.21.0 linux/amd64
GOROOT/bin/go tool compile -V: compile version go1.21.0
uname -sr: Linux 5.17.0-1019-oem
Distributor ID: Ubuntu
Description:    Ubuntu 22.04.2 LTS
Release:        22.04
Codename:       jammy
lldb --version: lldb version 14.0.0
gdb --version: GNU gdb (Ubuntu 12.1-0ubuntu1~22.04) 12.1

What did you do?

Tried using stringer in order to generate String() methods for my constant types.

What did you expect to see?

An output file with the generated code, like i do when not importing any packages.

// Code generated by "stringer -type ET -output test_string.go"; DO NOT EDIT.                     
                                                                                                  
package test                                                                                      
                                                                                                  
import "strconv"                                                                                  
                                                                                                  
func _() {                                                                                        
    // An "invalid array index" compiler error signifies that the constant values have changed.   
    // Re-run the stringer command to generate them again.                                        
    var x [1]struct{}                                                                             
    _ = x[ET_NONE-0]                                                                              
    _ = x[ET_REL-1]                                                                               
    _ = x[ET_EXEC-2]                                                                              
    _ = x[ET_DYN-3]                                                                               
    _ = x[ET_CORE-4]                                                                              
    _ = x[ET_LOOS-65024]                                                                          
    _ = x[ET_HIOS-65279]                                                                          
    _ = x[ET_LOPROC-65280]                                                                        
    _ = x[ET_HIPROC-65535]                                                                        
}                                                                                                 
                                                                                                  
const (                                                                                           
    _ET_name_0 = "ET_NONEET_RELET_EXECET_DYNET_CORE"                                              
    _ET_name_1 = "ET_LOOS"                                                                        
    _ET_name_2 = "ET_HIOSET_LOPROC"                                                               
    _ET_name_3 = "ET_HIPROC"                                                                      
)                                                                                                 
                                                                                                  
var (                                                                                             
    _ET_index_0 = [...]uint8{0, 7, 13, 20, 26, 33}                                                
    _ET_index_2 = [...]uint8{0, 7, 16}                                                            
)                                                                                                 
                                                                                                  
func (i ET) String() string {                                                                     
    switch {                                                                                      
    case i <= 4:                                                                                  
        return _ET_name_0[_ET_index_0[i]:_ET_index_0[i+1]]                                        
    case i == 65024:                                                                              
        return _ET_name_1                                                                         
    case 65279 <= i && i <= 65280:                                                                
        i -= 65279                                                                                
        return _ET_name_2[_ET_index_2[i]:_ET_index_2[i+1]]                                        
    case i == 65535:                                                                              
        return _ET_name_3                                                                         
    default:                                                                                      
        return "ET(" + strconv.FormatInt(int64(i), 10) + ")"                                      
    }                                                                                             
}                                                                                                 

What did you see instead?

When importing a package in the targeted go source file I get an error of the type:


stringer: internal error: package "<insert-package-name>" without types was imported from "github.com/andreistan26/golink/pkg/elf/test" 

After removing the imports the tool works as expected.

Source Code:

package test                                                       
                                                                   
import(                                                            
    "encoding/binary"                                              
)                                                                  
                                                                   
//go:generate stringer -type ET -output test_string.go             
                                                                   
type ET uint32                                                     
                                                                   
const (                                                            
    ET_NONE ET = iota                                              
                                                                   
    ET_REL                                                         
                                                                   
    ET_EXEC                                                        
                                                                   
    ET_DYN                                                         
                                                                   
    ET_CORE                                                        
                                                                   
    ET_LOOS   ET = 0xFE00                                          
    ET_HIOS   ET = 0xFEFF                                          
    ET_LOPROC ET = 0xFF00                                          
    ET_HIPROC ET = 0xFFFF                                          
)                                                                  
                                                                   
func DecodeET(dump []byte) ET {                                    
    return ET(binary.LittleEndian.Uint16(dump[0:2]))               
}                                                                  
@gopherbot gopherbot added the Tools This label describes issues relating to any tools in the x/tools repository. label Sep 1, 2023
@gopherbot gopherbot added this to the Unreleased milestone Sep 1, 2023
@seankhliao
Copy link
Member

what version of stringer did you use?

@andreistan26
Copy link
Author

I'm using latest v0.12.0

@cherrymui
Copy link
Member

Could you share the full input and the exact commands to reproduce the issue? I tried to put your code in the "Source Code" section to a package and run go generate and it generates the file as expected. Thanks.

@cherrymui cherrymui added the WaitingForInfo Issue is not actionable because of missing required information, which needs to be provided. label Sep 5, 2023
@andreistan26
Copy link
Author

On my machine I've tried running the posted source code with a dummy main function and it still gave the following error after running go generate:

stringer: internal error: package "encoding/binary" without types was imported from "github.com/andreistan26/test"

@seankhliao seankhliao added WaitingForInfo Issue is not actionable because of missing required information, which needs to be provided. and removed WaitingForInfo Issue is not actionable because of missing required information, which needs to be provided. labels Oct 7, 2023
@gopherbot
Copy link

Timed out in state WaitingForInfo. Closing.

(I am just a bot, though. Please speak up if this is a mistake or you have the requested information.)

@gopherbot gopherbot closed this as not planned Won't fix, can't repro, duplicate, stale Nov 7, 2023
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Tools This label describes issues relating to any tools in the x/tools repository. WaitingForInfo Issue is not actionable because of missing required information, which needs to be provided.
Projects
None yet
Development

No branches or pull requests

4 participants