From 62ddf7d0c5a2a876933b2e52479712270191b92f Mon Sep 17 00:00:00 2001 From: Liberatys Date: Wed, 24 Apr 2019 14:32:21 +0000 Subject: [PATCH] cmd/go: derive executable name from package path in 'go run' Change name of temporary executable on go run . to directory name. Fixes #31571 Change-Id: I0a0ce74154e76205bb43805c95bd7fb8fd2dfd01 GitHub-Last-Rev: e0964983e18a1d45b55f7098c7489059708c7e5e GitHub-Pull-Request: golang/go#31614 Reviewed-on: https://go-review.googlesource.com/c/go/+/173297 Run-TryBot: Jay Conrod TryBot-Result: Gobot Gobot Reviewed-by: Jay Conrod --- src/cmd/go/internal/run/run.go | 34 +++++++------ .../script/run_set_executable_name.txt | 48 +++++++++++++++++++ 2 files changed, 68 insertions(+), 14 deletions(-) create mode 100644 src/cmd/go/testdata/script/run_set_executable_name.txt diff --git a/src/cmd/go/internal/run/run.go b/src/cmd/go/internal/run/run.go index 8b3006bf2c..71da5adc93 100644 --- a/src/cmd/go/internal/run/run.go +++ b/src/cmd/go/internal/run/run.go @@ -8,6 +8,7 @@ package run import ( "fmt" "os" + "path" "strings" "cmd/go/internal/base" @@ -94,10 +95,10 @@ func runRun(cmd *base.Command, args []string) { base.Fatalf("go run: no go files listed") } cmdArgs := args[i:] - if p.Error != nil { base.Fatalf("%s", p.Error) } + p.Internal.OmitDebug = true if len(p.DepsErrors) > 0 { // Since these are errors in dependencies, @@ -117,21 +118,26 @@ func runRun(cmd *base.Command, args []string) { base.Fatalf("go run: cannot run non-main package") } p.Target = "" // must build - not up to date - var src string - if len(p.GoFiles) > 0 { - src = p.GoFiles[0] - } else if len(p.CgoFiles) > 0 { - src = p.CgoFiles[0] - } else { - // this case could only happen if the provided source uses cgo - // while cgo is disabled. - hint := "" - if !cfg.BuildContext.CgoEnabled { - hint = " (cgo is disabled)" + if p.Internal.CmdlineFiles { + //set executable name if go file is given as cmd-argument + var src string + if len(p.GoFiles) > 0 { + src = p.GoFiles[0] + } else if len(p.CgoFiles) > 0 { + src = p.CgoFiles[0] + } else { + // this case could only happen if the provided source uses cgo + // while cgo is disabled. + hint := "" + if !cfg.BuildContext.CgoEnabled { + hint = " (cgo is disabled)" + } + base.Fatalf("go run: no suitable source files%s", hint) } - base.Fatalf("go run: no suitable source files%s", hint) + p.Internal.ExeName = src[:len(src)-len(".go")] + } else { + p.Internal.ExeName = path.Base(p.ImportPath) } - p.Internal.ExeName = src[:len(src)-len(".go")] // name temporary executable for first go file a1 := b.LinkAction(work.ModeBuild, work.ModeBuild, p) a := &work.Action{Mode: "go run", Func: buildRunProgram, Args: cmdArgs, Deps: []*work.Action{a1}} b.Do(a) diff --git a/src/cmd/go/testdata/script/run_set_executable_name.txt b/src/cmd/go/testdata/script/run_set_executable_name.txt new file mode 100644 index 0000000000..e12aed0f00 --- /dev/null +++ b/src/cmd/go/testdata/script/run_set_executable_name.txt @@ -0,0 +1,48 @@ +env GO111MODULE=on +# Check for correct naming of temporary executable + +#Test for single file specified +cd x/y/z +go run foo.go +stderr 'foo' + +#Test for current directory +go run . +stderr 'z' + +#Test for set path +go run m/x/y/z/ +stderr 'z' + +-- m/x/y/z/foo.go -- +package main +import( + "os" + "path/filepath" +) +func main() { + println(filepath.Base(os.Args[0])) +} + +-- x/y/z/foo.go -- +package main +import( + "os" + "path/filepath" +) +func main() { + println(filepath.Base(os.Args[0])) +} + +-- x/y/z/foo.go -- +package main +import( + "os" + "path/filepath" +) +func main() { + println(filepath.Base(os.Args[0])) +} + +-- go.mod -- +module m \ No newline at end of file -- 2.48.1