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/goimports: better understanding of failure to update imports #28935

Closed
theckman opened this issue Nov 23, 2018 · 10 comments
Closed
Labels
FrozenDueToAge WaitingForInfo Issue is not actionable because of missing required information, which needs to be provided.
Milestone

Comments

@theckman
Copy link
Contributor

I'm using goimports with the go version go1.11.2 darwin/amd64 toolchain. I've hit a case where goimports isn't adding imports when I'd expect it to, and I'm trying to inspect why that is. I am not using modules, and the project is within my GOPATH. The import I'm trying to add is also within the GOPATH, and is actually within the same git repo.

Without the import added, the build failure (via go test .) is:

./case_test.go:13:8: undefined: mockaccounts

Once I manually add the import the tests pass just fine. I'm trying to understand what's causing goimports to be unsure what package to add. I've tried to look for any packages that have functions with similar methods, to no avail.

Does anyone know of a good way to figure out why goimports isn't adding my import.

@gopherbot gopherbot added this to the Unreleased milestone Nov 23, 2018
@myitcv
Copy link
Member

myitcv commented Nov 24, 2018

@theckman - do you think there is a bug here? If so, please can you complete the issue template with a small repro? The output of go env is particularly significant in this case.

Else if this is more of a general question, then please can we move this to another forum: https://github.com/golang/go/wiki/Questions?

Thanks

@myitcv myitcv added the WaitingForInfo Issue is not actionable because of missing required information, which needs to be provided. label Nov 24, 2018
@josharian
Copy link
Contributor

This could be read as a feature request for a verbose flag to goimports.

@heschi
Copy link
Contributor

heschi commented Nov 26, 2018

It already has one.

@theckman, please run goimports -v and attach the results.

@theckman
Copy link
Contributor Author

@myitcv it's an internal application, getting a repro ready would require a bit of work. I can maybe focus on that once I've some spare cycles. Sucks because I want to open source it eventually...

This is more of a feature request. I don't know if there's a bug present because goimports hasn't given me enough introspection ability. At face-value it's not behaving as expected.

@josharian there is one, but hopefully its output will be more meaningful to @heschik than it has been to me:

2018/11/26 11:11:59.230589 fixImports(filename="case_test.go"), abs="/Users/theckman/go/src/stash.corp.netflix.com/core/halp2/poller/cases/case_test.go", srcDir="/Users/theckman/go/src/stash.corp.netflix.com/core/halp2/poller/cases" ...
2018/11/26 11:11:59.231442 importPathToNameGoPathParse("stash.corp.netflix.com/core/halp2/test/mocks", srcDir="/Users/theckman/go/src/stash.corp.netflix.com/core/halp2/poller/cases") = "mocks", <nil>
2018/11/26 11:11:59.235594 scanning /Users/theckman/.gimme/versions/go1.11.2.darwin.amd64/src
2018/11/26 11:11:59.242998 scanned /Users/theckman/.gimme/versions/go1.11.2.darwin.amd64/src
2018/11/26 11:11:59.243031 scanning /Users/theckman/go/src
2018/11/26 11:11:59.243075 open /Users/theckman/go/src/.goimportsignore: no such file or directory
2018/11/26 11:11:59.243097 Error statting ignored directory: stat /Users/theckman/go/src/v: no such file or directory
2018/11/26 11:11:59.243114 Error statting ignored directory: stat /Users/theckman/go/src/mod: no such file or directory
2018/11/26 11:11:59.319635 scanned /Users/theckman/go/src

@heschi
Copy link
Contributor

heschi commented Nov 26, 2018

Judging from that output, it didn't find a directory on $GOPATH that contained the word "mockaccounts". What directory should it have found the package in?

@theckman
Copy link
Contributor Author

theckman commented Nov 26, 2018

@heschik it should be adding the import line "stash.corp.netflix.com/core/halp2/test/mocks/poller/accounts", which is currently located in $GOPATH/src/stash.corp.netflix.com/core/halp2/test/mocks/poller/accounts. Maybe it's an issue with the folder name not matching? 🤔

@theckman
Copy link
Contributor Author

Yep, that appears to be it. I don't recall ever having had an issue before with the package name not matching the folder name. Especially because I've used things like github.com/mitchellh/go-homedir.

Have I just been lucky?

@heschi
Copy link
Contributor

heschi commented Nov 26, 2018

It doesn't need to be an exact match but it does need to contain the package name. go-homedir contains homedir, but mocks/poller/accounts does not contain mockaccounts. (mock/accounts might work today but I wouldn't advise relying on it.)

This is WAI. It's not feasible to load the real package name from every directory in $GOPATH. I suppose we could add something to the log like "looking for directory named mockaccounts" but the real matching rules are pretty complicated.

@heschi heschi closed this as completed Nov 26, 2018
@theckman
Copy link
Contributor Author

theckman commented Nov 26, 2018

@heschik where is this documented? Could we re-open this issue and rename it to be an ask for docs? Separate issue?

@heschi
Copy link
Contributor

heschi commented Nov 26, 2018

I'm not aware of anything that would go into this much detail. File a new issue and ask bradfitz, I guess.

@golang golang locked and limited conversation to collaborators Nov 26, 2019
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
FrozenDueToAge WaitingForInfo Issue is not actionable because of missing required information, which needs to be provided.
Projects
None yet
Development

No branches or pull requests

5 participants