From e4e3d117728c4ce5a4991d0b124c1ea1add3d8db Mon Sep 17 00:00:00 2001 From: Marc Vertes Date: Mon, 24 Oct 2022 15:48:04 +0200 Subject: [PATCH] interp: fix the logic to skip source files based on OS or CPU arch For example, on architecture GOARCH=amd64, a file named `foobar_amd64.go` would be skipped instead of being read and parsed. The function `skipFile` is fixed and missing tests are added. --- interp/build.go | 18 +++++++++++++++--- interp/build_test.go | 10 +++++++++- 2 files changed, 24 insertions(+), 4 deletions(-) diff --git a/interp/build.go b/interp/build.go index 1e641b776..85c1ac908 100644 --- a/interp/build.go +++ b/interp/build.go @@ -147,10 +147,22 @@ func skipFile(ctx *build.Context, p string, skipTest bool) bool { } a := strings.Split(p[i+1:], "_") last := len(a) - 1 - if last1 := last - 1; last1 >= 0 && a[last1] == ctx.GOOS && a[last] == ctx.GOARCH { - return false + if last-1 >= 0 { + switch x, y := a[last-1], a[last]; { + case x == ctx.GOOS: + if knownArch[y] { + return y != ctx.GOARCH + } + return false + case knownOs[x] && knownArch[y]: + return true + case knownArch[y] && y != ctx.GOARCH: + return true + default: + return false + } } - if s := a[last]; s != ctx.GOOS && s != ctx.GOARCH && knownOs[s] || knownArch[s] { + if x := a[last]; knownOs[x] && x != ctx.GOOS || knownArch[x] && x != ctx.GOARCH { return true } return false diff --git a/interp/build_test.go b/interp/build_test.go index 8c89d9ac9..5706cc996 100644 --- a/interp/build_test.go +++ b/interp/build_test.go @@ -51,7 +51,7 @@ func TestBuildTag(t *testing.T) { } } -func TestBuildFile(t *testing.T) { +func TestSkipFile(t *testing.T) { // Assume a specific OS, arch and go pattern no matter the real underlying system ctx := build.Context{ GOARCH: "amd64", @@ -65,10 +65,18 @@ func TestBuildFile(t *testing.T) { {"bar_linux.go", false}, {"bar_maix.go", false}, {"bar_mlinux.go", false}, + {"bar_aix_foo.go", false}, + {"bar_linux_foo.go", false}, + {"bar_foo_amd64.go", false}, + {"bar_foo_arm.go", true}, + {"bar_aix_s390x.go", true}, {"bar_aix_amd64.go", true}, {"bar_linux_arm.go", true}, + + {"bar_amd64.go", false}, + {"bar_arm.go", true}, } for _, test := range tests {