Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

cmd/go: inconsistent result when go build output is a non-existent directory #41313

Closed
XSAM opened this issue Sep 10, 2020 · 2 comments
Closed

cmd/go: inconsistent result when go build output is a non-existent directory #41313

XSAM opened this issue Sep 10, 2020 · 2 comments

Comments

@XSAM
Copy link
Contributor

@XSAM XSAM commented Sep 10, 2020

What version of Go are you using (go version)?

go1.15 darwin/amd64

Does this issue reproduce with the latest release?

Yes

What did you do?

Project Hierarchy
.
├── go.mod
└── main.go

The first time to build, go complain about an existent directory that is actually created by go itself and not exists before the build.

$ go build -o bin/ main.go
go build command-line-arguments: build output "bin/" already exists and is a directory
Project Hierarchy
.
├── bin
├── go.mod
└── main.go

The second time to build. Since the directory already exists, go works fine at this time.

$ go build -o bin/ main.go
Project Hierarchy
.
├── bin
│   └── main
├── go.mod
└── main.go

What did you expect to see?

An idempotent result from go build.

  1. Keep complaining about the directory
$ go build -o bin/ main.go
go build command-line-arguments: build output "bin/" already exists and is a directory
$ go build -o bin/ main.go
go build command-line-arguments: build output "bin/" already exists and is a directory
  1. Allow go build output is an inexistent directory

I prefer the second solution since it's more convenient for users.

Pros:

  • No need to create a directory by users.
  • Easy to write a common build command in scripts. (Putting binary in a directory seems like a common convention to build)

Before: go build -o bin/foo . go build -o bin/bar . (We need to change the output name)
After: go build -o bin/ .

  • Consistency. go build -o bin/foo . also creates the directory for users.
XSAM added a commit to XSAM/go that referenced this issue Sep 10, 2020
`go build` will complain that output is a inexistent directory, but also creates that directory. It makes a inconsistent result since building will pass in the next time. By checking output with directory convention, allow building output is not a exists directory, we can fix the issue and bring more conveniences for users.

Fixes golang#41313
XSAM added a commit to XSAM/go that referenced this issue Sep 10, 2020
`go build` will complain that output is an inexistent directory, but also creates that directory. It makes an inconsistent result since the building will pass next time. By checking output with directory convention, allow building output is not a exists directory, we can fix the issue and bring more conveniences for users.

Fixes golang#41313
XSAM added a commit to XSAM/go that referenced this issue Sep 10, 2020
`go build` will complain that output is an inexistent directory, but also creates that directory. It makes an inconsistent result since the building will pass next time. By checking output with directory convention, allow building output is not a exists directory, we can fix the issue and bring more conveniences for users.

Fixes golang#41313
@gopherbot
Copy link

@gopherbot gopherbot commented Sep 10, 2020

Change https://golang.org/cl/253821 mentions this issue: cmd/go/internal/work: allow building output is an inexistent directory

@XSAM XSAM changed the title cmd/go: fix inconsistent result when go build output path is not a exists directory cmd/go: inconsistent result when go build output path is not a exists directory Sep 10, 2020
@andybons andybons added this to the Unplanned milestone Sep 10, 2020
@andybons
Copy link
Member

@andybons andybons commented Sep 10, 2020

@XSAM XSAM changed the title cmd/go: inconsistent result when go build output path is not a exists directory cmd/go: inconsistent result when go build output is a non-existent directory Sep 14, 2020
XSAM added a commit to XSAM/go that referenced this issue Sep 14, 2020
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 golang#41313
XSAM added a commit to XSAM/go that referenced this issue Sep 17, 2020
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 golang#41313
@gopherbot gopherbot closed this in 07d5eb0 Sep 17, 2020
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Linked pull requests

Successfully merging a pull request may close this issue.

3 participants
You can’t perform that action at this time.