You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
{{ message }}
Repository navigation
cmd/go: runtime/debug build information not populated for test binaries when package name is not 'main' #33976
Reacted by Alexey Palazhchenko, Adrien Petel, Pascal S. de Kloe, dozn, James Munnelly, Erik Sipsma, Radek Simko, olegshmuelov, Leo Di Donato, David de Regt and 14 more
I ended up on this issue while investigating how to access (non Go) files located in a sub-directory of my Go module. The access should happen from tests located in sub-sub-package.
We have it working with a relative path (e.g. ../../../folder/file) but it would be beneficial to have a less brittle way of accessing these files (especially for the case of refactoring).
So I tried the same as @bcmills is describing in this issue and I wanted to access info.Main.Path with the end goal of being able to access my files with info.Main.Path + "/folder/file".
Questions:
Is my assumption correct, that info.Main.Path would contain the basepath for the module, if the buildinfo would be available in test builds?
Why is the the buildinfo currently not available in test builds?
Just as a note, using go list does work to get details about Go modules. E.g.:
Note that running go list in the working directory of a test will tell you about the module that contains the test itself, not the main module in which the test was built.
Just as a note, using go list does work to get details about Go modules. E.g.:
Note that running go list in the working directory of a test will tell you about the module that contains the test itself, not the main module in which the test was built.
That is not clear to me. Is this not the same in the case of tests? My understanding is, that go test builds for every package a test binary and executes it. The guarantee is, that the current working directory is actually the directory that contains the test. So therefore, the module that contains the test and the module the test was built for is the same (or is there something special going on in regards to modules when building tests?).
This is why go test -o test ./... does not work (error message: cannot use -o flag with multiple packages).
The guarantee is, that the current working directory is actually the directory that contains the test.
Yes.
So therefore, the module that contains the test and the module the test was built for is the same (or is there something special going on in regards to modules when building tests?).
The module that contains the test is the module containing the working directory, yes.
However, that directory does not indicate the version of that module, and the versions reported by go list -m all will not be accurate: those versions are determined by the “main module” (in the sense of https://tip.golang.org/cmd/go/#hdr-The_main_module_and_the_build_list), not “the module containing package main”. (See #33975.)
However, that directory does not indicate the version of that module, and the versions reported by go list -m all will not be accurate: those versions are determined by the “main module” (in the sense of https://tip.golang.org/cmd/go/#hdr-The_main_module_and_the_build_list), not “the module containing package main”. (See #33975.)
Thanks for your detailed explanation. This comment made it clear to me with the provided example. For my use case this does not matter, because the "main module" and "the module containing package main" are the same.
I am exploring adding some logic and maybe a feature or two to github.com/go-stack/stack that take advantage of build information, but was surprised to discover that it didn't work with go test. I am glad to see it has already been reported, but sad that it hasn't been fixed yet.
I can probably get my ideas to work with a different testing strategy, but the new features would have to come with a big caveat about not working in tests since github.com/go-stack/stack is often used by logging code and that in turn is often relied upon to help diagnose test failures.
Has anyone investigated the level of effort to fix this? I am willing to help given some suggestions about where to start digging into the issue.
I also have found that when the package is named main the BuildInfo.Path and .Main fields are populated but BuildInfo.Deps field is nil even when the package under test depends on code from other modules.
Reacted by Anmol Sethi, James Pirruccello, Kris Rousey, Tyrone Villaluna and Stefan VanBuren
I ran into this myself when I was working on a problem that required me to take advantage of Go's ability to create per-package test binaries to distribute tests to specific runtime/parallelize.
The resulting test binaries don't have any buildinfo, so it was not possible to interrogate the binaries themselves for their details either at runtime or externally with go version -m.
Context:
I had hoped to leverage the details to select certain packages to select packages of tests that needed to be executed with a different system user, root, but I didn't want to run the whole test suite as it took awhile and made re-combining test reports a wee bit ambiguous. The data would have been useful to know what the fully qualified path of the packgae originally came from. Between that and a precompiled test.pkg binary doesn't support go test's -json I think I just simply ran everything twice.
This also caused #69203. If we had populated build info on all test binaries, they'd have the default godebug incorporated from the build info into the action id.
Activity
breml commentedon Dec 19, 2019
I ended up on this issue while investigating how to access (non Go) files located in a sub-directory of my Go module. The access should happen from tests located in sub-sub-package.
We have it working with a relative path (e.g.
../../../folder/file) but it would be beneficial to have a less brittle way of accessing these files (especially for the case of refactoring).So I tried the same as @bcmills is describing in this issue and I wanted to access
info.Main.Pathwith the end goal of being able to access my files withinfo.Main.Path + "/folder/file".Questions:
info.Main.Pathwould contain the basepath for the module, if the buildinfo would be available in test builds?breml commentedon Dec 19, 2019
Just as a note, using
go listdoes work to get details about Go modules. E.g.:bcmills commentedon Dec 19, 2019
info.Main.Pathin general contains a module path, not a filesystem path.That is the bug described in this issue.
bcmills commentedon Dec 19, 2019
Note that running
go listin the working directory of a test will tell you about the module that contains the test itself, not the main module in which the test was built.breml commentedon Dec 19, 2019
OK, understood. So this would not have helped me anyway.
I was under the impression, that the buildinfo has been left out intentionally and not that it is a bug. Good to hear, that it is considered a bug.
breml commentedon Dec 19, 2019
That is not clear to me. Is this not the same in the case of tests? My understanding is, that
go testbuilds for every package a test binary and executes it. The guarantee is, that the current working directory is actually the directory that contains the test. So therefore, the module that contains the test and the module the test was built for is the same (or is there something special going on in regards to modules when building tests?).This is why
go test -o test ./...does not work (error message:cannot use -o flag with multiple packages).bcmills commentedon Dec 19, 2019
Yes.
The module that contains the test is the module containing the working directory, yes.
However, that directory does not indicate the version of that module, and the versions reported by
go list -m allwill not be accurate: those versions are determined by the “main module” (in the sense of https://tip.golang.org/cmd/go/#hdr-The_main_module_and_the_build_list), not “the module containing package main”. (See #33975.)breml commentedon Dec 19, 2019
Thanks for your detailed explanation. This comment made it clear to me with the provided example. For my use case this does not matter, because the "main module" and "the module containing package main" are the same.
ChrisHines commentedon Feb 18, 2021
I am exploring adding some logic and maybe a feature or two to
github.com/go-stack/stackthat take advantage of build information, but was surprised to discover that it didn't work withgo test. I am glad to see it has already been reported, but sad that it hasn't been fixed yet.I can probably get my ideas to work with a different testing strategy, but the new features would have to come with a big caveat about not working in tests since
github.com/go-stack/stackis often used by logging code and that in turn is often relied upon to help diagnose test failures.Has anyone investigated the level of effort to fix this? I am willing to help given some suggestions about where to start digging into the issue.
ChrisHines commentedon Feb 18, 2021
I also have found that when the package is named
maintheBuildInfo.Pathand.Mainfields are populated butBuildInfo.Depsfield isnileven when the package under test depends on code from other modules.8 remaining items
matloob commentedon Jul 1, 2024
cc @samthanawalla
dekimsey commentedon Sep 13, 2024
I ran into this myself when I was working on a problem that required me to take advantage of Go's ability to create per-package test binaries to distribute tests to specific runtime/parallelize.
The resulting test binaries don't have any buildinfo, so it was not possible to interrogate the binaries themselves for their details either at runtime or externally with
go version -m.Context:
I had hoped to leverage the details to select certain packages to select packages of tests that needed to be executed with a different system user, root, but I didn't want to run the whole test suite as it took awhile and made re-combining test reports a wee bit ambiguous. The data would have been useful to know what the fully qualified path of the packgae originally came from. Between that and a precompiled test.pkg binary doesn't support go test's
-jsonI think I just simply ran everything twice.matloob commentedon Sep 13, 2024
This also caused #69203. If we had populated build info on all test binaries, they'd have the default godebug incorporated from the build info into the action id.
gopherbot commentedon Sep 13, 2024
Change https://go.dev/cl/613096 mentions this issue:
cmd/go: populate build info for test mainscmd/go: populate build info for test mains
gopherbot commentedon Sep 13, 2024
Change https://go.dev/cl/613275 mentions this issue:
internal/upload: update test assertions to account for test build infointernal/upload: update test assertions to account for test build info