1
0
mirror of https://github.com/golang/go synced 2024-11-14 07:00:21 -07:00

cmd/go: don't crash when running "go version" in deleted directory

If the go command is executed on Linux in a deleted directory,
it fails. This behavior is reasonable for commands which depend on
the CWD, but it's unexpected for commands like `go version`.
This change delays initialization of a global CWD variable.

Fixed #34499

Change-Id: I7302fb84a3b7f5f149a123d277abd5b9b5bc95b2
Reviewed-on: https://go-review.googlesource.com/c/go/+/268261
Reviewed-by: Bryan C. Mills <bcmills@google.com>
Trust: Bryan C. Mills <bcmills@google.com>
Trust: Ian Lance Taylor <iant@golang.org>
Run-TryBot: Bryan C. Mills <bcmills@google.com>
TryBot-Result: Go Bot <gobot@golang.org>
This commit is contained in:
Philipp Sauter 2020-11-08 22:12:38 +01:00 committed by Bryan C. Mills
parent bb5e45219a
commit 4df662fb37
19 changed files with 93 additions and 57 deletions

View File

@ -2832,3 +2832,25 @@ func TestCoverpkgTestOnly(t *testing.T) {
tg.grepStderrNot("no packages being tested depend on matches", "bad match message") tg.grepStderrNot("no packages being tested depend on matches", "bad match message")
tg.grepStdout("coverage: 100", "no coverage") tg.grepStdout("coverage: 100", "no coverage")
} }
// Regression test for golang.org/issue/34499: version command should not crash
// when executed in a deleted directory on Linux.
func TestExecInDeletedDir(t *testing.T) {
// The crash has only been reproduced on Linux.
if runtime.GOOS == "windows" || runtime.GOOS == "plan9" {
t.Skip()
}
tg := testgo(t)
defer tg.cleanup()
wd, err := os.Getwd()
tg.check(err)
tg.makeTempdir()
tg.check(os.Chdir(tg.tempdir))
defer func() { tg.check(os.Chdir(wd)) }()
tg.check(os.Remove(tg.tempdir))
// `go version` should not fail
tg.run("version")
}

View File

@ -8,21 +8,27 @@ import (
"os" "os"
"path/filepath" "path/filepath"
"strings" "strings"
"sync"
) )
func getwd() string { var cwd string
wd, err := os.Getwd() var cwdOnce sync.Once
// Cwd returns the current working directory at the time of the first call.
func Cwd() string {
cwdOnce.Do(func() {
var err error
cwd, err = os.Getwd()
if err != nil { if err != nil {
Fatalf("cannot determine current directory: %v", err) Fatalf("cannot determine current directory: %v", err)
} }
return wd })
return cwd
} }
var Cwd = getwd()
// ShortPath returns an absolute or relative name for path, whatever is shorter. // ShortPath returns an absolute or relative name for path, whatever is shorter.
func ShortPath(path string) string { func ShortPath(path string) string {
if rel, err := filepath.Rel(Cwd, path); err == nil && len(rel) < len(path) { if rel, err := filepath.Rel(Cwd(), path); err == nil && len(rel) < len(path) {
return rel return rel
} }
return path return path
@ -33,7 +39,7 @@ func ShortPath(path string) string {
func RelPaths(paths []string) []string { func RelPaths(paths []string) []string {
var out []string var out []string
for _, p := range paths { for _, p := range paths {
rel, err := filepath.Rel(Cwd, p) rel, err := filepath.Rel(Cwd(), p)
if err == nil && len(rel) < len(p) { if err == nil && len(rel) < len(p) {
p = rel p = rel
} }

View File

@ -200,7 +200,7 @@ func runEnv(ctx context.Context, cmd *base.Command, args []string) {
env := cfg.CmdEnv env := cfg.CmdEnv
env = append(env, ExtraEnvVars()...) env = append(env, ExtraEnvVars()...)
if err := fsys.Init(base.Cwd); err != nil { if err := fsys.Init(base.Cwd()); err != nil {
base.Fatalf("go: %v", err) base.Fatalf("go: %v", err)
} }

View File

@ -44,7 +44,7 @@ func (n *node) isDeleted() bool {
// TODO(matloob): encapsulate these in an io/fs-like interface // TODO(matloob): encapsulate these in an io/fs-like interface
var overlay map[string]*node // path -> file or directory node var overlay map[string]*node // path -> file or directory node
var cwd string // copy of base.Cwd to avoid dependency var cwd string // copy of base.Cwd() to avoid dependency
// Canonicalize a path for looking it up in the overlay. // Canonicalize a path for looking it up in the overlay.
// Important: filepath.Join(cwd, path) doesn't always produce // Important: filepath.Join(cwd, path) doesn't always produce

View File

@ -258,7 +258,7 @@ func download(arg string, parent *load.Package, stk *load.ImportStack, mode int)
load1 := func(path string, mode int) *load.Package { load1 := func(path string, mode int) *load.Package {
if parent == nil { if parent == nil {
mode := 0 // don't do module or vendor resolution mode := 0 // don't do module or vendor resolution
return load.LoadImport(context.TODO(), load.PackageOpts{}, path, base.Cwd, nil, stk, nil, mode) return load.LoadImport(context.TODO(), load.PackageOpts{}, path, base.Cwd(), nil, stk, nil, mode)
} }
return load.LoadImport(context.TODO(), load.PackageOpts{}, path, parent.Dir, parent, stk, nil, mode|load.ResolveModule) return load.LoadImport(context.TODO(), load.PackageOpts{}, path, parent.Dir, parent, stk, nil, mode|load.ResolveModule)
} }

View File

@ -34,7 +34,7 @@ type ppfValue struct {
// Set is called each time the flag is encountered on the command line. // Set is called each time the flag is encountered on the command line.
func (f *PerPackageFlag) Set(v string) error { func (f *PerPackageFlag) Set(v string) error {
return f.set(v, base.Cwd) return f.set(v, base.Cwd())
} }
// set is the implementation of Set, taking a cwd (current working directory) for easier testing. // set is the implementation of Set, taking a cwd (current working directory) for easier testing.

View File

@ -603,7 +603,7 @@ func ReloadPackageNoFlags(arg string, stk *ImportStack) *Package {
}) })
packageDataCache.Delete(p.ImportPath) packageDataCache.Delete(p.ImportPath)
} }
return LoadImport(context.TODO(), PackageOpts{}, arg, base.Cwd, nil, stk, nil, 0) return LoadImport(context.TODO(), PackageOpts{}, arg, base.Cwd(), nil, stk, nil, 0)
} }
// dirToImportPath returns the pseudo-import path we use for a package // dirToImportPath returns the pseudo-import path we use for a package
@ -991,7 +991,7 @@ func (pre *preload) preloadMatches(ctx context.Context, opts PackageOpts, matche
case pre.sema <- struct{}{}: case pre.sema <- struct{}{}:
go func(pkg string) { go func(pkg string) {
mode := 0 // don't use vendoring or module import resolution mode := 0 // don't use vendoring or module import resolution
bp, loaded, err := loadPackageData(ctx, pkg, "", base.Cwd, "", false, mode) bp, loaded, err := loadPackageData(ctx, pkg, "", base.Cwd(), "", false, mode)
<-pre.sema <-pre.sema
if bp != nil && loaded && err == nil && !opts.IgnoreImports { if bp != nil && loaded && err == nil && !opts.IgnoreImports {
pre.preloadImports(ctx, opts, bp.Imports, bp) pre.preloadImports(ctx, opts, bp.Imports, bp)
@ -2456,7 +2456,7 @@ func PackagesAndErrors(ctx context.Context, opts PackageOpts, patterns []string)
if pkg == "" { if pkg == "" {
panic(fmt.Sprintf("ImportPaths returned empty package for pattern %s", m.Pattern())) panic(fmt.Sprintf("ImportPaths returned empty package for pattern %s", m.Pattern()))
} }
p := loadImport(ctx, opts, pre, pkg, base.Cwd, nil, &stk, nil, 0) p := loadImport(ctx, opts, pre, pkg, base.Cwd(), nil, &stk, nil, 0)
p.Match = append(p.Match, m.Pattern()) p.Match = append(p.Match, m.Pattern())
p.Internal.CmdlinePkg = true p.Internal.CmdlinePkg = true
if m.IsLiteral() { if m.IsLiteral() {
@ -2670,7 +2670,7 @@ func GoFilesPackage(ctx context.Context, opts PackageOpts, gofiles []string) *Pa
var err error var err error
if dir == "" { if dir == "" {
dir = base.Cwd dir = base.Cwd()
} }
dir, err = filepath.Abs(dir) dir, err = filepath.Abs(dir)
if err != nil { if err != nil {

View File

@ -135,7 +135,7 @@ func Init() {
return return
} }
if err := fsys.Init(base.Cwd); err != nil { if err := fsys.Init(base.Cwd()); err != nil {
base.Fatalf("go: %v", err) base.Fatalf("go: %v", err)
} }
@ -179,7 +179,7 @@ func Init() {
} }
modRoot = "" modRoot = ""
} else { } else {
modRoot = findModuleRoot(base.Cwd) modRoot = findModuleRoot(base.Cwd())
if modRoot == "" { if modRoot == "" {
if cfg.ModFile != "" { if cfg.ModFile != "" {
base.Fatalf("go: cannot find main module, but -modfile was set.\n\t-modfile cannot be used to set the module root directory.") base.Fatalf("go: cannot find main module, but -modfile was set.\n\t-modfile cannot be used to set the module root directory.")
@ -276,7 +276,7 @@ func WillBeEnabled() bool {
return false return false
} }
if modRoot := findModuleRoot(base.Cwd); modRoot == "" { if modRoot := findModuleRoot(base.Cwd()); modRoot == "" {
// GO111MODULE is 'auto', and we can't find a module root. // GO111MODULE is 'auto', and we can't find a module root.
// Stay in GOPATH mode. // Stay in GOPATH mode.
return false return false
@ -335,8 +335,8 @@ func die() {
if cfg.Getenv("GO111MODULE") == "off" { if cfg.Getenv("GO111MODULE") == "off" {
base.Fatalf("go: modules disabled by GO111MODULE=off; see 'go help modules'") base.Fatalf("go: modules disabled by GO111MODULE=off; see 'go help modules'")
} }
if dir, name := findAltConfig(base.Cwd); dir != "" { if dir, name := findAltConfig(base.Cwd()); dir != "" {
rel, err := filepath.Rel(base.Cwd, dir) rel, err := filepath.Rel(base.Cwd(), dir)
if err != nil { if err != nil {
rel = dir rel = dir
} }
@ -479,7 +479,7 @@ func loadModFile(ctx context.Context) (rs *Requirements, needCommit bool) {
// exactly the same as in the legacy configuration (for example, we can't get // exactly the same as in the legacy configuration (for example, we can't get
// packages at multiple versions from the same module). // packages at multiple versions from the same module).
func CreateModFile(ctx context.Context, modPath string) { func CreateModFile(ctx context.Context, modPath string) {
modRoot = base.Cwd modRoot = base.Cwd()
Init() Init()
modFilePath := ModFilePath() modFilePath := ModFilePath()
if _, err := fsys.Stat(modFilePath); err == nil { if _, err := fsys.Stat(modFilePath); err == nil {
@ -646,7 +646,7 @@ func initTarget(m module.Version) {
Target = m Target = m
targetPrefix = m.Path targetPrefix = m.Path
if rel := search.InDir(base.Cwd, cfg.GOROOTsrc); rel != "" { if rel := search.InDir(base.Cwd(), cfg.GOROOTsrc); rel != "" {
targetInGorootSrc = true targetInGorootSrc = true
if m.Path == "std" { if m.Path == "std" {
// The "std" module in GOROOT/src is the Go standard library. Unlike other // The "std" module in GOROOT/src is the Go standard library. Unlike other

View File

@ -407,7 +407,7 @@ func matchLocalDirs(ctx context.Context, m *search.Match, rs *Requirements) {
dir := filepath.Dir(filepath.Clean(m.Pattern()[:i+3])) dir := filepath.Dir(filepath.Clean(m.Pattern()[:i+3]))
absDir := dir absDir := dir
if !filepath.IsAbs(dir) { if !filepath.IsAbs(dir) {
absDir = filepath.Join(base.Cwd, dir) absDir = filepath.Join(base.Cwd(), dir)
} }
if search.InDir(absDir, cfg.GOROOTsrc) == "" && search.InDir(absDir, ModRoot()) == "" && pathInModuleCache(ctx, absDir, rs) == "" { if search.InDir(absDir, cfg.GOROOTsrc) == "" && search.InDir(absDir, ModRoot()) == "" && pathInModuleCache(ctx, absDir, rs) == "" {
m.Dirs = []string{} m.Dirs = []string{}
@ -425,7 +425,7 @@ func resolveLocalPackage(ctx context.Context, dir string, rs *Requirements) (str
if filepath.IsAbs(dir) { if filepath.IsAbs(dir) {
absDir = filepath.Clean(dir) absDir = filepath.Clean(dir)
} else { } else {
absDir = filepath.Join(base.Cwd, dir) absDir = filepath.Join(base.Cwd(), dir)
} }
bp, err := cfg.BuildContext.ImportDir(absDir, 0) bp, err := cfg.BuildContext.ImportDir(absDir, 0)
@ -632,7 +632,7 @@ func DirImportPath(ctx context.Context, dir string) string {
LoadModFile(ctx) // Sets targetPrefix. LoadModFile(ctx) // Sets targetPrefix.
if !filepath.IsAbs(dir) { if !filepath.IsAbs(dir) {
dir = filepath.Join(base.Cwd, dir) dir = filepath.Join(base.Cwd(), dir)
} else { } else {
dir = filepath.Clean(dir) dir = filepath.Clean(dir)
} }

View File

@ -445,7 +445,7 @@ func ImportPathsQuiet(patterns []string) []*Match {
for i, dir := range m.Dirs { for i, dir := range m.Dirs {
absDir := dir absDir := dir
if !filepath.IsAbs(dir) { if !filepath.IsAbs(dir) {
absDir = filepath.Join(base.Cwd, dir) absDir = filepath.Join(base.Cwd(), dir)
} }
if bp, _ := cfg.BuildContext.ImportDir(absDir, build.FindOnly); bp.ImportPath != "" && bp.ImportPath != "." { if bp, _ := cfg.BuildContext.ImportDir(absDir, build.FindOnly); bp.ImportPath != "" && bp.ImportPath != "." {
m.Pkgs[i] = bp.ImportPath m.Pkgs[i] = bp.ImportPath

View File

@ -26,8 +26,8 @@ func initCoverProfile() {
if testCoverProfile == "" || testC { if testCoverProfile == "" || testC {
return return
} }
if !filepath.IsAbs(testCoverProfile) && testOutputDir != "" { if !filepath.IsAbs(testCoverProfile) {
testCoverProfile = filepath.Join(testOutputDir, testCoverProfile) testCoverProfile = filepath.Join(testOutputDir.getAbs(), testCoverProfile)
} }
// No mutex - caller's responsibility to call with no racing goroutines. // No mutex - caller's responsibility to call with no racing goroutines.

View File

@ -486,7 +486,7 @@ var (
testJSON bool // -json flag testJSON bool // -json flag
testList string // -list flag testList string // -list flag
testO string // -o flag testO string // -o flag
testOutputDir = base.Cwd // -outputdir flag testOutputDir outputdirFlag // -outputdir flag
testShuffle shuffleFlag // -shuffle flag testShuffle shuffleFlag // -shuffle flag
testTimeout time.Duration // -timeout flag testTimeout time.Duration // -timeout flag
testV bool // -v flag testV bool // -v flag
@ -710,7 +710,7 @@ func runTest(ctx context.Context, cmd *base.Command, args []string) {
match := make([]func(*load.Package) bool, len(testCoverPaths)) match := make([]func(*load.Package) bool, len(testCoverPaths))
matched := make([]bool, len(testCoverPaths)) matched := make([]bool, len(testCoverPaths))
for i := range testCoverPaths { for i := range testCoverPaths {
match[i] = load.MatchPackage(testCoverPaths[i], base.Cwd) match[i] = load.MatchPackage(testCoverPaths[i], base.Cwd())
} }
// Select for coverage all dependencies matching the testCoverPaths patterns. // Select for coverage all dependencies matching the testCoverPaths patterns.
@ -945,11 +945,11 @@ func builderTest(b *work.Builder, ctx context.Context, pkgOpts load.PackageOpts,
var installAction, cleanAction *work.Action var installAction, cleanAction *work.Action
if testC || testNeedBinary() { if testC || testNeedBinary() {
// -c or profiling flag: create action to copy binary to ./test.out. // -c or profiling flag: create action to copy binary to ./test.out.
target := filepath.Join(base.Cwd, testBinary+cfg.ExeSuffix) target := filepath.Join(base.Cwd(), testBinary+cfg.ExeSuffix)
if testO != "" { if testO != "" {
target = testO target = testO
if !filepath.IsAbs(target) { if !filepath.IsAbs(target) {
target = filepath.Join(base.Cwd, target) target = filepath.Join(base.Cwd(), target)
} }
} }
if target == os.DevNull { if target == os.DevNull {

View File

@ -62,7 +62,7 @@ func init() {
cf.String("memprofilerate", "", "") cf.String("memprofilerate", "", "")
cf.StringVar(&testMutexProfile, "mutexprofile", "", "") cf.StringVar(&testMutexProfile, "mutexprofile", "", "")
cf.String("mutexprofilefraction", "", "") cf.String("mutexprofilefraction", "", "")
cf.Var(outputdirFlag{&testOutputDir}, "outputdir", "") cf.Var(&testOutputDir, "outputdir", "")
cf.Int("parallel", 0, "") cf.Int("parallel", 0, "")
cf.String("run", "", "") cf.String("run", "", "")
cf.Bool("short", false, "") cf.Bool("short", false, "")
@ -71,7 +71,7 @@ func init() {
cf.BoolVar(&testV, "v", false, "") cf.BoolVar(&testV, "v", false, "")
cf.Var(&testShuffle, "shuffle", "") cf.Var(&testShuffle, "shuffle", "")
for name, _ := range passFlagToTest { for name := range passFlagToTest {
cf.Var(cf.Lookup(name).Value, "test."+name, "") cf.Var(cf.Lookup(name).Value, "test."+name, "")
} }
} }
@ -128,19 +128,26 @@ func (f stringFlag) Set(value string) error {
// outputdirFlag implements the -outputdir flag. // outputdirFlag implements the -outputdir flag.
// It interprets an empty value as the working directory of the 'go' command. // It interprets an empty value as the working directory of the 'go' command.
type outputdirFlag struct { type outputdirFlag struct {
resolved *string abs string
} }
func (f outputdirFlag) String() string { return *f.resolved } func (f *outputdirFlag) String() string {
func (f outputdirFlag) Set(value string) (err error) { return f.abs
}
func (f *outputdirFlag) Set(value string) (err error) {
if value == "" { if value == "" {
// The empty string implies the working directory of the 'go' command. f.abs = ""
*f.resolved = base.Cwd
} else { } else {
*f.resolved, err = filepath.Abs(value) f.abs, err = filepath.Abs(value)
} }
return err return err
} }
func (f *outputdirFlag) getAbs() string {
if f.abs == "" {
return base.Cwd()
}
return f.abs
}
// vetFlag implements the special parsing logic for the -vet flag: // vetFlag implements the special parsing logic for the -vet flag:
// a comma-separated list, with a distinguished value "off" and // a comma-separated list, with a distinguished value "off" and
@ -404,7 +411,7 @@ func testFlags(args []string) (packageNames, passToTest []string) {
// command. Set it explicitly if it is needed due to some other flag that // command. Set it explicitly if it is needed due to some other flag that
// requests output. // requests output.
if testProfile() != "" && !outputDirSet { if testProfile() != "" && !outputDirSet {
injectedFlags = append(injectedFlags, "-test.outputdir="+testOutputDir) injectedFlags = append(injectedFlags, "-test.outputdir="+testOutputDir.getAbs())
} }
// If the user is explicitly passing -help or -h, show output // If the user is explicitly passing -help or -h, show output

View File

@ -344,7 +344,7 @@ func readpkglist(shlibpath string) (pkgs []*load.Package) {
if strings.HasPrefix(t, "pkgpath ") { if strings.HasPrefix(t, "pkgpath ") {
t = strings.TrimPrefix(t, "pkgpath ") t = strings.TrimPrefix(t, "pkgpath ")
t = strings.TrimSuffix(t, ";") t = strings.TrimSuffix(t, ";")
pkgs = append(pkgs, load.LoadImportWithFlags(t, base.Cwd, nil, &stk, nil, 0)) pkgs = append(pkgs, load.LoadImportWithFlags(t, base.Cwd(), nil, &stk, nil, 0))
} }
} }
} else { } else {
@ -355,7 +355,7 @@ func readpkglist(shlibpath string) (pkgs []*load.Package) {
scanner := bufio.NewScanner(bytes.NewBuffer(pkglistbytes)) scanner := bufio.NewScanner(bytes.NewBuffer(pkglistbytes))
for scanner.Scan() { for scanner.Scan() {
t := scanner.Text() t := scanner.Text()
pkgs = append(pkgs, load.LoadImportWithFlags(t, base.Cwd, nil, &stk, nil, 0)) pkgs = append(pkgs, load.LoadImportWithFlags(t, base.Cwd(), nil, &stk, nil, 0))
} }
} }
return return
@ -776,7 +776,7 @@ func (b *Builder) linkSharedAction(mode, depMode BuildMode, shlib string, a1 *Ac
} }
} }
var stk load.ImportStack var stk load.ImportStack
p := load.LoadImportWithFlags(pkg, base.Cwd, nil, &stk, nil, 0) p := load.LoadImportWithFlags(pkg, base.Cwd(), nil, &stk, nil, 0)
if p.Error != nil { if p.Error != nil {
base.Fatalf("load %s: %v", pkg, p.Error) base.Fatalf("load %s: %v", pkg, p.Error)
} }

View File

@ -173,10 +173,11 @@ func TestSharedLibName(t *testing.T) {
if err != nil { if err != nil {
t.Fatal(err) t.Fatal(err)
} }
cwd := base.Cwd()
oldGopath := cfg.BuildContext.GOPATH oldGopath := cfg.BuildContext.GOPATH
defer func() { defer func() {
cfg.BuildContext.GOPATH = oldGopath cfg.BuildContext.GOPATH = oldGopath
os.Chdir(base.Cwd) os.Chdir(cwd)
err := os.RemoveAll(tmpGopath) err := os.RemoveAll(tmpGopath)
if err != nil { if err != nil {
t.Error(err) t.Error(err)

View File

@ -2377,7 +2377,7 @@ func (b *Builder) gccld(a *Action, p *load.Package, objdir, outfile string, flag
cmdargs := []interface{}{cmd, "-o", outfile, objs, flags} cmdargs := []interface{}{cmd, "-o", outfile, objs, flags}
dir := p.Dir dir := p.Dir
out, err := b.runOut(a, base.Cwd, b.cCompilerEnv(), cmdargs...) out, err := b.runOut(a, base.Cwd(), b.cCompilerEnv(), cmdargs...)
if len(out) > 0 { if len(out) > 0 {
// Filter out useless linker warnings caused by bugs outside Go. // Filter out useless linker warnings caused by bugs outside Go.
@ -2991,7 +2991,7 @@ func (b *Builder) dynimport(a *Action, p *load.Package, objdir, importGo, cgoExe
if p.Standard && p.ImportPath == "runtime/cgo" { if p.Standard && p.ImportPath == "runtime/cgo" {
cgoflags = []string{"-dynlinker"} // record path to dynamic linker cgoflags = []string{"-dynlinker"} // record path to dynamic linker
} }
return b.run(a, base.Cwd, p.ImportPath, b.cCompilerEnv(), cfg.BuildToolexec, cgoExe, "-dynpackage", p.Name, "-dynimport", dynobj, "-dynout", importGo, cgoflags) return b.run(a, base.Cwd(), p.ImportPath, b.cCompilerEnv(), cfg.BuildToolexec, cgoExe, "-dynpackage", p.Name, "-dynimport", dynobj, "-dynout", importGo, cgoflags)
} }
// Run SWIG on all SWIG input files. // Run SWIG on all SWIG input files.

View File

@ -197,7 +197,7 @@ func (gcToolchain) gc(b *Builder, a *Action, archive string, importcfg, embedcfg
args = append(args, f) args = append(args, f)
} }
output, err = b.runOut(a, base.Cwd, nil, args...) output, err = b.runOut(a, base.Cwd(), nil, args...)
return ofile, output, err return ofile, output, err
} }

View File

@ -102,7 +102,7 @@ func (tools gccgoToolchain) gc(b *Builder, a *Action, archive string, importcfg,
if b.gccSupportsFlag(args[:1], "-ffile-prefix-map=a=b") { if b.gccSupportsFlag(args[:1], "-ffile-prefix-map=a=b") {
if cfg.BuildTrimpath { if cfg.BuildTrimpath {
args = append(args, "-ffile-prefix-map="+base.Cwd+"=.") args = append(args, "-ffile-prefix-map="+base.Cwd()+"=.")
args = append(args, "-ffile-prefix-map="+b.WorkDir+"=/tmp/go-build") args = append(args, "-ffile-prefix-map="+b.WorkDir+"=/tmp/go-build")
} }
if fsys.OverlayFile != "" { if fsys.OverlayFile != "" {
@ -114,9 +114,9 @@ func (tools gccgoToolchain) gc(b *Builder, a *Action, archive string, importcfg,
} }
toPath := absPath toPath := absPath
// gccgo only applies the last matching rule, so also handle the case where // gccgo only applies the last matching rule, so also handle the case where
// BuildTrimpath is true and the path is relative to base.Cwd. // BuildTrimpath is true and the path is relative to base.Cwd().
if cfg.BuildTrimpath && str.HasFilePathPrefix(toPath, base.Cwd) { if cfg.BuildTrimpath && str.HasFilePathPrefix(toPath, base.Cwd()) {
toPath = "." + toPath[len(base.Cwd):] toPath = "." + toPath[len(base.Cwd()):]
} }
args = append(args, "-ffile-prefix-map="+overlayPath+"="+toPath) args = append(args, "-ffile-prefix-map="+overlayPath+"="+toPath)
} }
@ -572,7 +572,7 @@ func (tools gccgoToolchain) cc(b *Builder, a *Action, ofile, cfile string) error
} }
defs = tools.maybePIC(defs) defs = tools.maybePIC(defs)
if b.gccSupportsFlag(compiler, "-ffile-prefix-map=a=b") { if b.gccSupportsFlag(compiler, "-ffile-prefix-map=a=b") {
defs = append(defs, "-ffile-prefix-map="+base.Cwd+"=.") defs = append(defs, "-ffile-prefix-map="+base.Cwd()+"=.")
defs = append(defs, "-ffile-prefix-map="+b.WorkDir+"=/tmp/go-build") defs = append(defs, "-ffile-prefix-map="+b.WorkDir+"=/tmp/go-build")
} else if b.gccSupportsFlag(compiler, "-fdebug-prefix-map=a=b") { } else if b.gccSupportsFlag(compiler, "-fdebug-prefix-map=a=b") {
defs = append(defs, "-fdebug-prefix-map="+b.WorkDir+"=/tmp/go-build") defs = append(defs, "-fdebug-prefix-map="+b.WorkDir+"=/tmp/go-build")

View File

@ -23,7 +23,7 @@ func BuildInit() {
modload.Init() modload.Init()
instrumentInit() instrumentInit()
buildModeInit() buildModeInit()
if err := fsys.Init(base.Cwd); err != nil { if err := fsys.Init(base.Cwd()); err != nil {
base.Fatalf("go: %v", err) base.Fatalf("go: %v", err)
} }