From 4fcce60e3a22ae299f7f31714427550842fa894a Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Daniel=20Mart=C3=AD?= Date: Sat, 31 Aug 2024 23:42:49 +0100 Subject: [PATCH] remove -mod and -modfile flags rather than setting them to zero Recently, a patch changed the argument `-mod=` to `-mod=readonly` as the former is not really a valid flag value, and broke with go.work. However, the latter seems to break our tests on Go 1.22.6 when listing all of runtimeLinknamed: panic: failed to load missing runtime-linknamed packages: golang.org/x/crypto@v0.16.1-0.20231129163542-152cdb1503eb: reading http://127.0.0.1:43357/mod/golang.org/x/crypto/@v/v0.16.1-0.20231129163542-152cdb1503eb.mod: 404 Not Found It seems like, somehow, listing std packages was trying to download x/crypto from GOPROXY - which is a local server with testdata/mod, and so it does not contain x/crypto. However, this is entirely wrong, as std vendors dependencies, including this very version of x/crypto. Reverting the change to `-mod=readonly` resolves this issue, which explains why we hadn't encountered this surprising GOPROXY error, but the revert would also break users of go.work files. Luckily, we have a better alternative: rather than trying to override the value of the flags by adding more arguments, delete them entirely. --- shared.go | 5 ++++- 1 file changed, 4 insertions(+), 1 deletion(-) diff --git a/shared.go b/shared.go index 2c7a34a..d80ce4b 100644 --- a/shared.go +++ b/shared.go @@ -14,6 +14,7 @@ import ( "os" "os/exec" "path/filepath" + "slices" "strings" "time" @@ -231,7 +232,9 @@ func appendListedPackages(packages []string, mainBuild bool) error { // However, when loading standard library packages, // using those flags would likely result in an error, // as the standard library uses its own Go module and vendoring. - args = append(args, "-mod=readonly", "-modfile=") + args = slices.DeleteFunc(args, func(arg string) bool { + return strings.HasPrefix(arg, "-mod=") || strings.HasPrefix(arg, "-modfile=") + }) } args = append(args, packages...)