From 3c3f1cf6c42767091a736414359d0d3d6a47baf1 Mon Sep 17 00:00:00 2001 From: motemen Date: Wed, 21 Feb 2018 13:56:10 +0900 Subject: [PATCH 1/2] cmd/go: fix formatting of file paths under cwd The output of go with -x flag is formatted in a manner that file paths under current directory are modified to start with a dot (.), but when the directory path ends with a slash (/), the formatting goes wrong. Fixes 23982. --- src/cmd/go/internal/work/exec.go | 6 +++++- 1 file changed, 5 insertions(+), 1 deletion(-) diff --git a/src/cmd/go/internal/work/exec.go b/src/cmd/go/internal/work/exec.go index ab216e748f0c11..a9f622c7936573 100644 --- a/src/cmd/go/internal/work/exec.go +++ b/src/cmd/go/internal/work/exec.go @@ -1364,7 +1364,11 @@ func mayberemovefile(s string) { func (b *Builder) fmtcmd(dir string, format string, args ...interface{}) string { cmd := fmt.Sprintf(format, args...) if dir != "" && dir != "/" { - cmd = strings.Replace(" "+cmd, " "+dir, " .", -1)[1:] + dot := " ." + if dir[len(dir)-1] == '/' { + dot += "/" + } + cmd = strings.Replace(" "+cmd, " "+dir, dot, -1)[1:] if b.scriptDir != dir { b.scriptDir = dir cmd = "cd " + dir + "\n" + cmd From 1493f38bafdf2c40f16392b794fd1a12eb12a151 Mon Sep 17 00:00:00 2001 From: motemen Date: Wed, 21 Feb 2018 17:37:37 +0900 Subject: [PATCH 2/2] add test, use filepath.Separator --- src/cmd/go/go_test.go | 8 ++++++++ src/cmd/go/internal/work/exec.go | 4 ++-- 2 files changed, 10 insertions(+), 2 deletions(-) diff --git a/src/cmd/go/go_test.go b/src/cmd/go/go_test.go index 773b8240d27578..8f5a1f6c71384f 100644 --- a/src/cmd/go/go_test.go +++ b/src/cmd/go/go_test.go @@ -5921,3 +5921,11 @@ echo $* >>`+tg.path("pkg-config.out")) t.Errorf("got %q want %q", out, want) } } + +// Issue 23982 +func TestFilepathUnderCwdFormat(t *testing.T) { + tg := testgo(t) + defer tg.cleanup() + tg.run("test", "-x", "-cover", "log") + tg.grepStderrNot(`\.log\.cover\.go`, "-x output should contain correctly formatted filepath under cwd") +} diff --git a/src/cmd/go/internal/work/exec.go b/src/cmd/go/internal/work/exec.go index a9f622c7936573..e61d6d5cc4dc30 100644 --- a/src/cmd/go/internal/work/exec.go +++ b/src/cmd/go/internal/work/exec.go @@ -1365,8 +1365,8 @@ func (b *Builder) fmtcmd(dir string, format string, args ...interface{}) string cmd := fmt.Sprintf(format, args...) if dir != "" && dir != "/" { dot := " ." - if dir[len(dir)-1] == '/' { - dot += "/" + if dir[len(dir)-1] == filepath.Separator { + dot += string(filepath.Separator) } cmd = strings.Replace(" "+cmd, " "+dir, dot, -1)[1:] if b.scriptDir != dir {