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 help build incorrectly describes behaviour of flag -o #10865

Closed
okdave opened this issue May 15, 2015 · 4 comments
Closed

cmd/go: go help build incorrectly describes behaviour of flag -o #10865

okdave opened this issue May 15, 2015 · 4 comments
Milestone

Comments

@okdave
Copy link
Contributor

okdave commented May 15, 2015

The help for go build doesn't correctly describe what the effect of -o has on the output file (if any). It also seems internally inconsistent.

From experimenting on the command line -o /tmp/foo will produce an object file when building non-main packages iff the package is somewhere in goroot or a gopath. Specifically running go build -o /tmp/foo at the directory:

  1. $GOROOT/src/fmt – object file created
  2. $GOPATH/src/golang.org/x/net/context – object file created
  3. /tmp/my_binary (containing package main main.go) – binary file created
  4. /tmp/my_pkg (contain package foo foo.go) – no file created

Further, the output of go help build seems to contradict itself. It first states that the results will be discarded for anything other than a single package main. The next paragraph then describes what the output file name will be for non-main packages if no other name is specified.

When the command line specifies a single main package,
build writes the resulting executable to output.
Otherwise build compiles the packages but discards the results,
serving only as a check that the packages can be built.

The -o flag specifies the output file name. If not specified, the
output file name depends on the arguments and derives from the name
of the package, such as p.a for package p, unless p is 'main'. If
the package is main and file names are provided, the file name
derives from the first file name mentioned, such as f1 for 'go build
f1.go f2.go'; with no files provided ('go build'), the output file
name is the base name of the containing directory.

@ianlancetaylor ianlancetaylor added this to the Go1.5 milestone May 15, 2015
@robpike robpike changed the title go help build incorrectly describes behaviour of flag -o cmd/go: go help build incorrectly describes behaviour of flag -o Jun 1, 2015
@okdave
Copy link
Contributor Author

okdave commented Jun 2, 2015

I can tackle this, but I think both the docs and the build tool need tweaking. The behaviour should be:

  • if no -o flag
    • if a single main package is provided, derive the output name automatically
    • otherwise, build then discard the results
  • if -o flag
    • if a single package, write to the given file
    • otherwise, fail (report error)

@adg
Copy link
Contributor

adg commented Jun 2, 2015

What you describe is the way I assumed it worked. cc @davecheney @rsc

@okdave okdave self-assigned this Jun 9, 2015
@gopherbot
Copy link

CL https://golang.org/cl/11261 mentions this issue.

@gopherbot
Copy link

CL https://golang.org/cl/13024 mentions this issue.

@rsc rsc closed this as completed in 961f456 Aug 3, 2015
@golang golang locked and limited conversation to collaborators Aug 5, 2016
@rsc rsc unassigned okdave Jun 23, 2022
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Projects
None yet
Development

No branches or pull requests

4 participants