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: support go build -o /dev/null #4851

Closed
dominikh opened this issue Feb 20, 2013 · 12 comments
Closed

cmd/go: support go build -o /dev/null #4851

dominikh opened this issue Feb 20, 2013 · 12 comments
Milestone

Comments

@dominikh
Copy link
Member

What steps will reproduce the problem?
If possible, include a link to a program on play.golang.org.
1. Run `go build -o /dev/null` in a main package that lives in GOPATH

What is the expected output?
No output, but the resulting binary should be discarded

What do you see instead?
go install foo: build output "/dev/null" already exists and is not an object
file

Which compiler are you using (5g, 6g, 8g, gccgo)?
6g

Which operating system are you using?
Linux 3.2.1-gentoo-r2

Which version are you using?  (run 'go version')
go version devel +e93de8482d59 Mon Feb 18 19:03:10 2013 -0800 linux/amd64

Please provide any additional information below.
When compiling a package that lives in GOPATH, go build will eventually call copyFile,
which checks that the output file is neither a directory nor a non-object file, to
prevent accidentally overwriting the wrong file. 

It would be useful if -o /dev/null would work, to discard the binary.
@rsc
Copy link
Contributor

rsc commented Mar 12, 2013

Comment 2:

[The time for maybe has passed.]

Labels changed: removed go1.1maybe.

@adg
Copy link
Contributor

adg commented Mar 22, 2013

Comment 3:

Labels changed: added priority-later, removed priority-triage.

Status changed to Thinking.

@rsc
Copy link
Contributor

rsc commented Jul 30, 2013

Comment 4:

Labels changed: added go1.2maybe.

@rsc
Copy link
Contributor

rsc commented Jul 30, 2013

Comment 5:

Labels changed: added feature.

@robpike
Copy link
Contributor

robpike commented Aug 19, 2013

Comment 6:

Too magic for my taste. Reopen if you can make a compelling argument.

Status changed to Fixed.

@ugorji
Copy link
Contributor

ugorji commented Oct 19, 2013

Comment 7:

This issue was closed as Fixed, but it was not fixed.
It is causing issues with tools that want to check if a file can be built successfully,
without actually creating a binary. flychecker for example is a very popular emacs tool
that does syntax checking for many languages. Its support for go uses the output of "go
build" to figure out where syntax errors exist and highlight them. 
flycheck/flycheck#107 
http://flycheck.github.io/
Also, I don't think it is "too magic". The concept of a null device is pretty standard,
and even supported in go as const os.DevNull (/dev/null for *nix, NUL for windows, etc).
The fix seems pretty straightforward. Only copy final target file if destination is not
os.DevNull. 
i.e. end (*builder).install method with:
    if a.target == os.DevNull {
        return nil
    }
    return b.copyFile(a, a.target, a1.target, perm)

@minux
Copy link
Member

minux commented Oct 20, 2013

Comment 8:

i suspect using "go build" just to test syntax is not correct as it invokes the
whole toolchain.
why not just let the go tool create a binary in $TMPDIR? and then delete it?
(please note that the Go tool is already creating the final binary, so i believe
we can't gain anything by output to /dev/null)

@dominikh
Copy link
Member Author

Comment 9:

re: "just testing syntax", saying that it does syntax checking was probably incorrect.
It does want to do a whole compile + link to catch the same errors that a `go build`
would report.
re: $TMPDIR, that's probably generally a better idea. It's not any less
platform-specific than /dev/null would be and doesn't require any changes. In a way,
using /dev/null would be "too magic". Go doesn't write to a device, it creates a file,
so handling /dev/null would be special and not the same meaning as the Unix meaning of
it.

@dominikh
Copy link
Member Author

Comment 10:

not any more platform-specific, of course.

@minux
Copy link
Member

minux commented Mar 1, 2014

Comment 11:

Note:
if you name every Go source file on the command line, "go build -o /dev/null" will work.
to support import paths, some go list trickery is required:
go build -o /dev/null $(go list -f $'{{range $f := .GoFiles}}{{$.Dir}}/{{$f}}\n{{end}}'
cmd/go)

@josharian
Copy link
Contributor

Comment 12:

Simple workaround (albeit a bit cutesy):
go build . errors

@gopherbot
Copy link

Comment 13 by quintus.public:

This would be nice to be able to throw out the resulting binary to simplify the last leg
of the test cycle. Especially with the fix mentioned in #8, it seems like an easy and
trivial change to make.

@rsc rsc added this to the Go1.2 milestone Apr 14, 2015
@rsc rsc removed the go1.2maybe label Apr 14, 2015
lcd047 added a commit to vim-syntastic/syntastic that referenced this issue Jun 3, 2015
Running "go build -o /dev/null" in a main package that lives in GOPATH
produces an error message:

build output "/dev/null" already exists and is not an object file

This issue won't be addressed on the Go side:

http://code.google.com/p/go/issues/detail?id=4851
golang/go#4851

Consequently, syntastic leaves this to the user to resolve.  Set
g:syntastic_go_go_build_args to something including "-o /some/dir"
to redirect the output of "go build".
robashton pushed a commit to robashton/syntastic that referenced this issue Jun 12, 2015
Running "go build -o /dev/null" in a main package that lives in GOPATH
produces an error message:

build output "/dev/null" already exists and is not an object file

This issue won't be addressed on the Go side:

http://code.google.com/p/go/issues/detail?id=4851
golang/go#4851

Consequently, syntastic leaves this to the user to resolve.  Set
g:syntastic_go_go_build_args to something including "-o /some/dir"
to redirect the output of "go build".
@golang golang locked and limited conversation to collaborators Jun 24, 2016
This issue was closed.
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

8 participants