-
Notifications
You must be signed in to change notification settings - Fork 17.9k
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/vuln: govulncheck reports "undeclared name" for global variable declared in _test.go file #55016
Comments
govulncheck is failing due to a type checking problem while loading. What you are describing should be a type checking error for compiling the package
There are actually up to 4 ways a directory can define Go packages once you take into account testing and binaries. Your definitions appear to be split over 2 of these. If the
My general advice for you though (that does not improve govulncheck) is to make sure |
Thanks for the quick reply! The test package is never compiled outside of
Are you able to repro the error using the repo/directory in the initial report (https://github.com/derat/bugs/tree/main/golang/govulncheck-global-in-test-file)?
If I add an additional reference to a variable that isn't declared anywhere, then
|
I was. Apologies for the reading comprehension issues on my side. For reference for others here are the relevant files.
On this
If this is the only action it is not surprising that Right now govulncheck is run with If we turn packages.Config.Tests on we will inspect the package as both The code in question is being written in a way that it does not typecheck under all usages by not considering all of the ways a test package can be loaded (See https://pkg.go.dev/cmd/go#hdr-Test_packages). The simplest solution for users is to write code that conforms to all of the ways a package can be loaded. (In this case, the
I am somewhat tempted to just consider this as "Unfortunate" and close. The only candidate alternative I can see is to change |
Thanks for the investigation! For whatever it's worth, I'm glad that |
Change https://go.dev/cl/436835 mentions this issue: |
I am going to close as Unfortunate for now. We will keep an eye out for this impacting others. If this is impacting you too, please let us know. I'll try to add some internal documentation about what is going on in the meantime. |
What version of Go are you using (
go version
)?Does this issue reproduce at the latest version of golang.org/x/vuln?
Yes (I ran
go install golang.org/x/vuln/cmd/govulncheck@latest
a few minutes ago).What operating system and processor architecture are you using (
go env
)?go env
OutputWhat did you do?
I ran
govulncheck ./...
on a more-complicated version of the code in https://github.com/derat/bugs/tree/main/golang/govulncheck-global-in-test-file.What did you expect to see?
I expected
govulncheck
to run successfully and maybe print some vulnerabilities.What did you see instead?
A function in
test/lib.go
references a global variable declared intest/foo_test.go
. The function is only called byfoo_test.go
.In the real-life scenario where I saw this, the
test
package contains tests that exercise code across a multi-language project.foo_test.go
contains the actual tests and initializes global variables needed for testing, whilelib.go
and other files contain helper functions that are called byfoo_test.go
and that reference the global variables.I'm aware that this setup is strange (or wrong?), so this might be working as intended:
lib_test.go
; the shorter names were used to make it clear that they don't contain the actual tests._test
-suffixed file.I'm just filing an issue since
go vet
seems to be able to handle this scenario without printing anundeclared name
error:The text was updated successfully, but these errors were encountered: