]> Cypherpunks repositories - gostls13.git/commitdiff
cmd/go: do not pass a local prefix to the compiler in module mode
authorBryan C. Mills <bcmills@google.com>
Tue, 28 Sep 2021 15:52:45 +0000 (11:52 -0400)
committerBryan C. Mills <bcmills@google.com>
Wed, 6 Oct 2021 16:46:09 +0000 (16:46 +0000)
In GOPATH mode, source files may import other packages using relative
(“local”) paths. In module mode, relative imports are never allowed:
import paths must always be fully specified.

When local imports are allowed, we pass a local-import prefix to the
compiler using the '-D' flag. That could theoretically change the
compiler's output, so it must be included in the cache key even when
-trimpath is set. (TODO: when -trimpath is set, the local-import
prefix ought to be trimmed anyway, so it still shouldn't matter.)

However, when local imports are disallowed, we should not pass the
local-import prefix and it should not affect cmd/go's cache key or the
final build ID of any artifact.

For #48557

Change-Id: I2d627d67d13e5da2cac6d411cd4e2d87e510876c
Reviewed-on: https://go-review.googlesource.com/c/go/+/352810
Trust: Bryan C. Mills <bcmills@google.com>
Run-TryBot: Bryan C. Mills <bcmills@google.com>
TryBot-Result: Go Bot <gobot@golang.org>
Reviewed-by: Jay Conrod <jayconrod@google.com>
src/cmd/go/internal/load/pkg.go
src/cmd/go/internal/work/gc.go
src/cmd/go/testdata/script/mod_build_trimpath_issue48557.txt [new file with mode: 0644]

index 8a5a1a5fe299983bbfa12ddc3c5d11da7e818a8a..a3a8de81c92621d4d69262750857dd50e5b467c1 100644 (file)
@@ -1678,9 +1678,10 @@ func (p *Package) DefaultExecName() string {
 func (p *Package) load(ctx context.Context, opts PackageOpts, path string, stk *ImportStack, importPos []token.Position, bp *build.Package, err error) {
        p.copyBuild(opts, bp)
 
-       // The localPrefix is the path we interpret ./ imports relative to.
+       // The localPrefix is the path we interpret ./ imports relative to,
+       // if we support them at all (not in module mode!).
        // Synthesized main packages sometimes override this.
-       if p.Internal.Local {
+       if p.Internal.Local && !cfg.ModulesEnabled {
                p.Internal.LocalPrefix = dirToImportPath(p.Dir)
        }
 
@@ -2703,7 +2704,9 @@ func GoFilesPackage(ctx context.Context, opts PackageOpts, gofiles []string) *Pa
        pkg.Internal.Local = true
        pkg.Internal.CmdlineFiles = true
        pkg.load(ctx, opts, "command-line-arguments", &stk, nil, bp, err)
-       pkg.Internal.LocalPrefix = dirToImportPath(dir)
+       if !cfg.ModulesEnabled {
+               pkg.Internal.LocalPrefix = dirToImportPath(dir)
+       }
        pkg.ImportPath = "command-line-arguments"
        pkg.Target = ""
        pkg.Match = gofiles
index 85f661bf3111dc393a42e6b9dbc58d8bf42da9eb..3eb9b35f40828f9010cfb63ca0f6aa3ab5079531 100644 (file)
@@ -165,8 +165,9 @@ func (gcToolchain) gc(b *Builder, a *Action, archive string, importcfg, embedcfg
        }
 
        args := []interface{}{cfg.BuildToolexec, base.Tool("compile"), "-o", ofile, "-trimpath", a.trimpath(), defaultGcFlags, gcflags}
-       if p.Internal.LocalPrefix != "" {
-               // Workaround #43883.
+       if p.Internal.LocalPrefix == "" {
+               args = append(args, "-nolocalimports")
+       } else {
                args = append(args, "-D", p.Internal.LocalPrefix)
        }
        if importcfg != nil {
diff --git a/src/cmd/go/testdata/script/mod_build_trimpath_issue48557.txt b/src/cmd/go/testdata/script/mod_build_trimpath_issue48557.txt
new file mode 100644 (file)
index 0000000..859eafc
--- /dev/null
@@ -0,0 +1,52 @@
+# Regression test for issue #48557.
+# Since builds in module mode do not support relative imports at all, the build
+# ID for (and other contents of) a binary built with -trimpath in module mode
+# should not depend on its working directory, even if the binary is specified as
+# a list of relative source files.
+
+[short] skip  # links and runs binaries
+
+env GOFLAGS=-trimpath
+env GOCACHE=$WORK/gocache
+
+
+# When we build a binary in module mode with -trimpath, the -D flag (for the
+# "local import prefix") should not be passed to it.
+
+cd $WORK/tmp/foo
+go build -x -o a.exe main.go
+stderr ${/}compile$GOEXE.*' -nolocalimports'
+! stderr ${/}compile$GOEXE.*' -D[ =]'
+
+go tool buildid a.exe
+cp stdout ../foo-buildid.txt
+go version a.exe
+cp stdout ../foo-version.txt
+cd ..
+
+
+# On the second build — in a different directory but with -trimpath — the
+# compiler should not be invoked, since the cache key should be identical.
+# Only the linker and buildid tool should be needed.
+
+mkdir bar
+cp foo/main.go bar/main.go
+cd bar
+go build -x -o a.exe main.go
+! stderr ${/}compile$GOEXE
+
+go tool buildid a.exe
+cp stdout ../bar-buildid.txt
+go version a.exe
+cp stdout ../bar-version.txt
+cd ..
+
+cmp bar-buildid.txt foo-buildid.txt
+cmp bar-version.txt foo-version.txt
+cmp bar/a.exe foo/a.exe
+
+
+-- $WORK/tmp/foo/main.go --
+package main
+
+func main() {}