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: go build output creates unexpected file when directory of same name exists #34535

Closed
13rac1 opened this issue Sep 25, 2019 · 2 comments
Closed

Comments

@13rac1
Copy link

@13rac1 13rac1 commented Sep 25, 2019

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

$ go version
go version go1.13 linux/amd64

Does this issue reproduce with the latest release?

Yes

What operating system and processor architecture are you using (go env)?

go env Output
$ go env
GO111MODULE=""
GOARCH="amd64"
GOBIN=""
GOCACHE="/home/b/.cache/go-build"
GOENV="/home/b/.config/go/env"
GOEXE=""
GOFLAGS=""
GOHOSTARCH="amd64"
GOHOSTOS="linux"
GONOPROXY=""
GONOSUMDB=""
GOOS="linux"
GOPATH="/go"
GOPRIVATE=""
GOPROXY="https://proxy.golang.org,direct"
GOROOT="/usr/local/go"
GOSUMDB="sum.golang.org"
GOTMPDIR=""
GOTOOLDIR="/usr/local/go/pkg/tool/linux_amd64"
GCCGO="gccgo"
AR="ar"
CC="gcc"
CXX="g++"
CGO_ENABLED="1"
GOMOD=""
CGO_CFLAGS="-g -O2"
CGO_CPPFLAGS=""
CGO_CXXFLAGS="-g -O2"
CGO_FFLAGS="-g -O2"
CGO_LDFLAGS="-g -O2"
PKG_CONFIG="pkg-config"
GOGCCFLAGS="-fPIC -m64 -pthread -fno-caret-diagnostics -Qunused-arguments -fmessage-length=0 -fdebug-prefix-map=/tmp/go-build792560259=/tmp/go-build -gno-record-gcc-switches"

What did you do?

  1. Created a basic main.go
  2. Built the main.go specifying an output with the same name as an existing directory.
$ docker run --rm -it golang:1.13-alpine3.10 sh
/go # cd
~ # vi main.go 
~ # cat main.go 
package main

import "fmt"

func main() {
	fmt.Println("Hello")
}
~ # mkdir test
~ # go build -o test main.go 
~ # ls -l test/
total 1964
-rwxr-xr-x    1 root     root       2008649 Sep 25 18:45 command-line-arguments

What did you expect to see?

An error message saying the file cannot be created.

$ mkdir test
$ go build -o test main.go
go build _/root: open /root/test: file exists

What did you see instead?

A file created with the surprising name /root/test/command-line-arguments.

This is a minor strange issue. Seemed best to document, rather than ignore.

#31296 is related.

@jayconrod

This comment has been minimized.

Copy link
Contributor

@jayconrod jayconrod commented Sep 25, 2019

The intended behavior is documented in go help build:

Build compiles the packages named by the import paths,
along with their dependencies, but it does not install the results.

If the arguments to build are a list of .go files from a single directory,
build treats them as a list of source files specifying a single package.

When compiling a single main package, build writes
the resulting executable to an output file named after
the first source file ('go build ed.go rx.go' writes 'ed' or 'ed.exe')
or the source code directory ('go build unix/sam' writes 'sam' or 'sam.exe').
The '.exe' suffix is added when writing a Windows executable.

When compiling multiple packages or a single non-main package,
build compiles the packages but discards the resulting object,
serving only as a check that the packages can be built.

When compiling packages, build ignores files that end in '_test.go'.

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.

There is a bug here, which is that the output file is named test/command-line-arguments. It should be named test/main instead.

@gopherbot

This comment has been minimized.

Copy link

@gopherbot gopherbot commented Sep 26, 2019

Change https://golang.org/cl/197544 mentions this issue: cmd/go: set expected filename when building a local package with -o is pointing to a folder

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
3 participants
You can’t perform that action at this time.