diff options
author | Sam Xie <xsambundy@gmail.com> | 2020-09-17 02:59:28 +0000 |
---|---|---|
committer | Jay Conrod <jayconrod@google.com> | 2020-09-17 15:51:14 +0000 |
commit | 07d5eb075b6f270ae4443e9689821d2e403b72b5 (patch) | |
tree | e069baec85a37a64d3d731a99d1c53c3cbd08c1c /src/cmd/go/internal/work/build.go | |
parent | 0f7ac9b4f5f6bc20344feb8a2c32b8126df80baa (diff) | |
download | go-07d5eb075b6f270ae4443e9689821d2e403b72b5.tar.gz go-07d5eb075b6f270ae4443e9689821d2e403b72b5.zip |
cmd/go: allow output in non-existent directory
When 'go build' is given an output path with -o, if the output path
ends with a path separator, always treat it as a directory.
Fixes #41313
Change-Id: I9a9c25448abfcd6297ad973f5ed2025b2568a4a7
GitHub-Last-Rev: 20a19bd63a2779a2c94b0efdf86146ffd551293c
GitHub-Pull-Request: golang/go#41314
Reviewed-on: https://go-review.googlesource.com/c/go/+/253821
Run-TryBot: Jay Conrod <jayconrod@google.com>
Run-TryBot: Bryan C. Mills <bcmills@google.com>
TryBot-Result: Go Bot <gobot@golang.org>
Reviewed-by: Jay Conrod <jayconrod@google.com>
Trust: Bryan C. Mills <bcmills@google.com>
Trust: Jay Conrod <jayconrod@google.com>
Diffstat (limited to 'src/cmd/go/internal/work/build.go')
-rw-r--r-- | src/cmd/go/internal/work/build.go | 12 |
1 files changed, 8 insertions, 4 deletions
diff --git a/src/cmd/go/internal/work/build.go b/src/cmd/go/internal/work/build.go index 990e5d9ecd..86423f118c 100644 --- a/src/cmd/go/internal/work/build.go +++ b/src/cmd/go/internal/work/build.go @@ -53,8 +53,9 @@ serving only as a check that the packages can be built. The -o flag forces build to write the resulting executable or object to the named output file or directory, instead of the default behavior described -in the last two paragraphs. If the named output is a directory that exists, -then any resulting executables will be written to that directory. +in the last two paragraphs. If the named output is an existing directory or +ends with a slash or backslash, then any resulting executables +will be written to that directory. The -i flag installs the packages that are dependencies of the target. @@ -387,10 +388,13 @@ func runBuild(ctx context.Context, cmd *base.Command, args []string) { } if cfg.BuildO != "" { - // If the -o name exists and is a directory, then + // If the -o name exists and is a directory or + // ends with a slash or backslash, then // write all main packages to that directory. // Otherwise require only a single package be built. - if fi, err := os.Stat(cfg.BuildO); err == nil && fi.IsDir() { + if fi, err := os.Stat(cfg.BuildO); (err == nil && fi.IsDir()) || + strings.HasSuffix(cfg.BuildO, "/") || + strings.HasSuffix(cfg.BuildO, string(os.PathSeparator)) { if !explicitO { base.Fatalf("go build: build output %q already exists and is a directory", cfg.BuildO) } |