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
Labels
FrozenDueToAge NeedsInvestigation Someone must examine and confirm this is a valid issue and not a duplicate of an existing one.
Milestone

Comments

@XSAM
Copy link
Contributor

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

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 the NeedsInvestigation Someone must examine and confirm this is a valid issue and not a duplicate of an existing one. label Sep 10, 2020
@andybons andybons added this to the Unplanned milestone Sep 10, 2020
@andybons
Copy link
Member

@bcmills @matloob @jayconrod

@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
sergystepanov added a commit to giongto35/cloud-game that referenced this issue Aug 27, 2021
If you specify just an output path without a filename when building the app, it will say that "build output dir/ already exists and is a directory" then it'll crash the build process.
This is considered as a bug (golang/go#41313) and was fixed in Go 1.16. The fix is for older versions.
sergystepanov added a commit to giongto35/cloud-game that referenced this issue Aug 27, 2021
If you specify just an output path without a filename when building the app, it will say that "build output dir/ already exists and is a directory" then it'll crash the build process.
This is considered as a bug (golang/go#41313) and was fixed in Go 1.16. The fix is for older versions.
@golang golang locked and limited conversation to collaborators Sep 17, 2021
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
FrozenDueToAge NeedsInvestigation Someone must examine and confirm this is a valid issue and not a duplicate of an existing one.
Projects
None yet
Development

Successfully merging a pull request may close this issue.

3 participants