skip unnecessary "refusing to list" test errors

The added test case reproduces the failure if we uncomment the added
"continue" line in processImportCfg:

	# test/bar/exporttest [test/bar/exporttest.test]
	panic: refusing to list non-dependency package: test/bar/exporttest

	goroutine 1 [running]:
	mvdan.cc/garble.processImportCfg({0xc000166780?, 0xc0001f4a70?, 0x2?})
		/home/mvdan/src/garble/main.go:983 +0x58b
	mvdan.cc/garble.transformCompile({0xc000124020?, 0x11?, 0x12?})
		/home/mvdan/src/garble/main.go:736 +0x338

It seems like a quirk of cmd/go that it includes a redundant packagefile
line in this particular edge case, but it's generally harmless for "go
build". For "garble build" it's also harmless in principle, but in
practice we had sanity checks that got upset by the unexpected line.

For now, notice the edge case and ignore it.

Fixes #522.
pull/529/head
Daniel Martí 3 years ago committed by lu4p
parent e3a59eae07
commit 79b6e4db3d

@ -972,6 +972,14 @@ func processImportCfg(flags []string) (newImportCfg string, _ error) {
impPath, pkgfile := pair[0], pair[1]
lpkg, err := listPackage(impPath)
if err != nil {
// TODO: it's unclear why an importcfg can include an import path
// that's not a dependency in an edge case with "go test ./...".
// See exporttest/*.go in testdata/scripts/test.txt.
// For now, spot the pattern and avoid the unnecessary error;
// the dependency is unused, so the packagefile line is redundant.
if strings.HasSuffix(curPkg.ImportPath, ".test]") && strings.HasPrefix(curPkg.ImportPath, impPath) {
continue
}
panic(err) // shouldn't happen
}
if lpkg.Name != "main" {

@ -14,9 +14,18 @@ stdout 'package bar_test, func name:'
[short] stop # no need to verify this with -short
# also check that many packages test fine, including a main package and multiple
# test packages
garble test -v . ./somemain ./sometest
# Also check that many packages test fine, including a main package and multiple
# test packages.
#
# Exporttest is special, as it:
#
# * uses an external test package
# * exports unexported APIs via export_test.go
# * is imported by another package, also tested via "./..."
#
# The combination of those used to result in "refusing to list non-dependency
# package" errors, which we've currently worked around.
garble test -v ./...
stdout 'ok\s+test/bar\s'
stdout 'PASS.*TestFoo'
stdout 'PASS.*TestSeparateFoo'
@ -26,6 +35,7 @@ stdout 'package bar_test, func name:'
! stdout 'OriginalFuncName'
stdout '\?\s+test/bar/somemain\s'
stdout 'ok\s+test/bar/sometest\s'
stdout 'ok\s+test/bar/exporttest\s'
# verify that non-build flags are kept
garble test -withflag -v
@ -47,6 +57,8 @@ package bar
import "runtime"
import _ "test/bar/exporttest"
func LocalFoo() string { return "Foo" }
var ImportedVar = "imported var value"
@ -115,3 +127,17 @@ package sometest
import "testing"
func TestFoo(t *testing.T) {}
-- exporttest/foo.go --
package exporttest
type foo int
-- exporttest/export_test.go --
package exporttest
type Foo = foo
-- exporttest/foo_test.go --
package exporttest_test
import "testing"
func TestFoo(t *testing.T) {}

Loading…
Cancel
Save