Commit 62ddf7d0 authored by Liberatys's avatar Liberatys Committed by Jay Conrod

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 <jayconrod@google.com>
TryBot-Result: Gobot Gobot <gobot@golang.org>
Reviewed-by: default avatarJay Conrod <jayconrod@google.com>
parent 888bac14
...@@ -8,6 +8,7 @@ package run ...@@ -8,6 +8,7 @@ package run
import ( import (
"fmt" "fmt"
"os" "os"
"path"
"strings" "strings"
"cmd/go/internal/base" "cmd/go/internal/base"
...@@ -94,10 +95,10 @@ func runRun(cmd *base.Command, args []string) { ...@@ -94,10 +95,10 @@ func runRun(cmd *base.Command, args []string) {
base.Fatalf("go run: no go files listed") base.Fatalf("go run: no go files listed")
} }
cmdArgs := args[i:] cmdArgs := args[i:]
if p.Error != nil { if p.Error != nil {
base.Fatalf("%s", p.Error) base.Fatalf("%s", p.Error)
} }
p.Internal.OmitDebug = true p.Internal.OmitDebug = true
if len(p.DepsErrors) > 0 { if len(p.DepsErrors) > 0 {
// Since these are errors in dependencies, // Since these are errors in dependencies,
...@@ -117,21 +118,26 @@ func runRun(cmd *base.Command, args []string) { ...@@ -117,21 +118,26 @@ func runRun(cmd *base.Command, args []string) {
base.Fatalf("go run: cannot run non-main package") base.Fatalf("go run: cannot run non-main package")
} }
p.Target = "" // must build - not up to date p.Target = "" // must build - not up to date
var src string if p.Internal.CmdlineFiles {
if len(p.GoFiles) > 0 { //set executable name if go file is given as cmd-argument
src = p.GoFiles[0] var src string
} else if len(p.CgoFiles) > 0 { if len(p.GoFiles) > 0 {
src = p.CgoFiles[0] src = p.GoFiles[0]
} else { } else if len(p.CgoFiles) > 0 {
// this case could only happen if the provided source uses cgo src = p.CgoFiles[0]
// while cgo is disabled. } else {
hint := "" // this case could only happen if the provided source uses cgo
if !cfg.BuildContext.CgoEnabled { // while cgo is disabled.
hint = " (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) a1 := b.LinkAction(work.ModeBuild, work.ModeBuild, p)
a := &work.Action{Mode: "go run", Func: buildRunProgram, Args: cmdArgs, Deps: []*work.Action{a1}} a := &work.Action{Mode: "go run", Func: buildRunProgram, Args: cmdArgs, Deps: []*work.Action{a1}}
b.Do(a) b.Do(a)
......
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
Markdown is supported
0%
or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment