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 install should warn or error if none of its arguments have install targets #58666

Open
zamicol opened this issue Feb 23, 2023 · 6 comments
Labels
GoCommand cmd/go NeedsFix The path to resolution is known, but the work has not been done.
Milestone

Comments

@zamicol
Copy link
Contributor

zamicol commented Feb 23, 2023

(I'm on go version go1.19.5 linux/amd64)

go test reports a warning if there's nothing to do:

$ go test -run test_that_doesn't_exist
testing: warning: no tests to run

The commands go test, go run, go build, and go install will also warn if there are no Go files in the given directory.

$ go test
no Go files in /directory

(In the case of go run, the warning is go: no go files listed)

However, unlike go test and go run, go build and go install will not warn when a Go file is present in the current directory but no binary is produced.

For example, in a directory with file helloWorld.go with the contents of the following:

package hello

import "fmt"

func talk() {
    fmt.Println("Hello World!")
}

go run warns

$ go run helloWorld.go 
package command-line-arguments is not a main package

go test warns

$ go test
?   	/helloWorldDir	[no test files]
$ go test helloWorld.go 
?   	command-line-arguments	[no test files]

But go install and go build reports nothing.

$ go install helloWorld.go
$
$ go build helloWorld.go
$

And simply doing go install and go build also reports nothing.

$ go install
$
$ go build
$

What I would expect to see is the same warning as go run

$ go install
package is not a main package
$ go build
package is not a main package
@thanm
Copy link
Contributor

thanm commented Feb 23, 2023

The fact that the Go command prints nothing when you run "go build helloWorld.go" doesn't mean that nothing's happening, it just means that there was nothing to report at the end of the build action. Try running "go build -x" to see what's happening.

In addition, developers run "go build" on non-main packages all the time without any expectation that a binary is going to be produced-- they just want to see if the code still compiles after a change to the source.

People also run builds of non-main packages to gather more info. For example in your hello package perhaps you might want to do a build with "-gcflags=-m" to see what decisions are made by the compiler regarding inlining or escape analysis.

@zamicol
Copy link
Contributor Author

zamicol commented Feb 23, 2023

Perhaps go install should be considered independently. What use is go install on packages without func main()?

@ianlancetaylor
Copy link
Contributor

When using GOPATH mode, go install will install a non-main package .a file in $GOPATH/src. Also, before 1.20, go install of a standard library package would install it in $GOROOT/src. That said, it's true that not many people use GOPATH mode any more, and presumably people are starting to use 1.20. A warning from go install for a non-main package might make sense now.

CC @bcmills

@ianlancetaylor ianlancetaylor changed the title go install and go build: Commands should warn if no binary is produced. cmd/go: go install should warn for a non-main package Feb 23, 2023
@ianlancetaylor ianlancetaylor added NeedsInvestigation Someone must examine and confirm this is a valid issue and not a duplicate of an existing one. GoCommand cmd/go labels Feb 23, 2023
@ianlancetaylor ianlancetaylor added this to the Backlog milestone Feb 23, 2023
@bcmills bcmills changed the title cmd/go: go install should warn for a non-main package cmd/go: go install should warn for a non-main package in module mode Feb 24, 2023
@bcmills
Copy link
Member

bcmills commented Feb 24, 2023

go install cmd is useful even though that pattern matches a mix of main and non-main packages. I think the same is true for other patterns.

On the other hand, I think it would make sense for go install to error out completely (not just warn!) if it doesn't actually have anything to install — that is: if the arguments don't match any main packages and the remaining arguments don't have a (GOPATH or GODEBUG=installgoroot=all) install target.

@bcmills bcmills added the NeedsFix The path to resolution is known, but the work has not been done. label Feb 24, 2023
@gopherbot gopherbot removed the NeedsInvestigation Someone must examine and confirm this is a valid issue and not a duplicate of an existing one. label Feb 24, 2023
@bcmills bcmills changed the title cmd/go: go install should warn for a non-main package in module mode cmd/go: go install should warn or error if none of its arguments have install targets Feb 24, 2023
@anthoturc
Copy link

@bcmills would you recommend this as a good first issue for someone looking to contribute?

@bcmills
Copy link
Member

bcmills commented Sep 25, 2023

@anthoturc, I'm honestly not sure. 😅

But if you want to take a look, the entry point for the go install command is the function runInstall in src/cmd/go/internal/work/build.go.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
GoCommand cmd/go NeedsFix The path to resolution is known, but the work has not been done.
Projects
None yet
Development

No branches or pull requests

6 participants