diff --git a/src/cmd/go/internal/run/run.go b/src/cmd/go/internal/run/run.go index 0029ee1541f63..7ebaf707c189b 100644 --- a/src/cmd/go/internal/run/run.go +++ b/src/cmd/go/internal/run/run.go @@ -8,7 +8,7 @@ package run import ( "fmt" "os" - "path/filepath" + "path" "strings" "cmd/go/internal/base" @@ -23,7 +23,7 @@ var CmdRun = &base.Command{ Short: "compile and run Go program", Long: ` Run compiles and runs the named main Go package. -Typically the package is specified as a list of .go source files from a single directory, +Typically the package is specified as a list of .go source files, but it may also be an import path, file system path, or pattern matching a single known package, as in 'go run .' or 'go run my/cmd'. @@ -95,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, @@ -118,25 +118,25 @@ 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")] // set name of first go file as temporary executable name - if len(args) == 1 && args[0] == "." { - p.Internal.ExeName = p.Dir // set name of directory as temporary executable name - } else if len(args) == 1 && filepath.Ext(args[0]) == "" { - p.Internal.ExeName = p.Dir // set name of directoy as temporary executable name if args[0] is a directory. + p.Internal.ExeName = src[:len(src)-len(".go")] + } else { + p.Internal.ExeName = path.Base(p.ImportPath) } a1 := b.LinkAction(work.ModeBuild, work.ModeBuild, p) a := &work.Action{Mode: "go run", Func: buildRunProgram, Args: cmdArgs, Deps: []*work.Action{a1}} diff --git a/src/cmd/go/testdata/script/run_set_executable_name.txt b/src/cmd/go/testdata/script/run_set_executable_name.txt index 5bad3b3e43615..61e7d4324d36a 100644 --- a/src/cmd/go/testdata/script/run_set_executable_name.txt +++ b/src/cmd/go/testdata/script/run_set_executable_name.txt @@ -16,30 +16,30 @@ stderr 'z' -- m/x/y/z/foo.go -- package main import( - "os" + "os" "path/filepath" ) -func main() { - println(filepath.Base(os.Args[0])) +func main() { + println(filepath.Base(os.Args[0])) } -- m/x/y/z/foo.go -- package main import( - "os" + "os" "path/filepath" ) -func main() { - println(filepath.Base(os.Args[0])) +func main() { + println(filepath.Base(os.Args[0])) } -- m/x/y/z/foo.go -- package main import( - "os" + "os" "path/filepath" ) -func main() { - println(filepath.Base(os.Args[0])) +func main() { + println(filepath.Base(os.Args[0])) }