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: build fails if filename is provided #26798

Closed
allenpetersen opened this issue Aug 3, 2018 · 8 comments
Closed

cmd/go: build fails if filename is provided #26798

allenpetersen opened this issue Aug 3, 2018 · 8 comments

Comments

@allenpetersen
Copy link

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

go1.11beta3

Does this issue reproduce with the latest release?

Doesn't repro on go1.10 or go1.11beta2

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

GOARCH="amd64"
GOOS="darwin"

What did you do?

Made single main.go hello world app with one line go.mod file
Ran go build -o bin/mod-test main.go

What did you expect to see?

Successful build

What did you see instead?

Build error:
build main: cannot find module for path main

If I run the build without the filename
go build -o bin/mod-test it works fine

This is a change from previous versions of Go.

@robpike
Copy link
Contributor

robpike commented Aug 3, 2018

The go build command's arguments are interpreted only as packages, not files. You are asking to build the package "main.go", and there is no such package. See the output of "go help build", which begins:

usage: go build [-o output] [-i] [build flags] [packages]

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

@robpike robpike closed this as completed Aug 3, 2018
@allenpetersen
Copy link
Author

Our Makefiles have been building using the above syntax for years. I'll fix them up to be more correct.

This change in behavior, even though more correct, might want to be called out in the release notes. There may be other teams using go build incorrectly as well.

@allenpetersen
Copy link
Author

allenpetersen commented Aug 3, 2018

It also fails when using a package name

go build main
go: import "main": cannot find module providing package main

@allenpetersen
Copy link
Author

allenpetersen commented Aug 4, 2018

CC @bcmills

This works differently in module mode vs GOPATH mode

root@3a67ccb3e0d7:/go/src/mod-test# ls
main.go
root@3a67ccb3e0d7:/go/src/mod-test# cat main.go
package main

import "fmt"

func main() {
	fmt.Println("Hello World.")
}
root@3a67ccb3e0d7:/go/src/mod-test# go build main.go       ## this works
root@3a67ccb3e0d7:/go/src/mod-test# go build main          ## fails
can't load package: package main: cannot find package "main" in any of:
	/usr/local/go/src/main (from $GOROOT)
	/go/src/main (from $GOPATH)

@robpike
Copy link
Contributor

robpike commented Aug 4, 2018

Not sure quite what's happening here, but it sounds like there has been a change in behavior. Re-opening.

@robpike robpike reopened this Aug 4, 2018
@robpike robpike added this to the Go1.11 milestone Aug 4, 2018
@surajbarkale
Copy link

Similarly executing go run main.go in a module directory results in errors:

$ go run main.go
build main: cannot find module for path main
$ go run main
go: import "main": cannot find module providing package main

However go run . works as expected.

@seanpfeifer
Copy link

seanpfeifer commented Aug 4, 2018

This is a departure from previous documented behavior if we cannot specify a list of .go files to "go build".

The output of "go help build" continues:

If the arguments to build are a list of .go files, 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') ...

@myitcv
Copy link
Member

myitcv commented Aug 6, 2018

This has the same underlying cause as #26722; so closing as a duplicate.

I've confirmed that the fix in https://go-review.googlesource.com/c/go/+/127796 for that issue fixes this case:

cd `mktemp -d`
export GOPATH=$(mktemp -d)
mkdir hello
cd hello
go mod init example.com/hello
cat <<EOD > main.go

package main

import "fmt"

func main() {
        fmt.Println("==========")
}
EOD
go build main.go
./main

gives:

==========

whereas on tip (5a720d2) this fails.

@myitcv myitcv closed this as completed Aug 6, 2018
mizzy added a commit to aktsk/nolmandy that referenced this issue Aug 22, 2018
With go 1.10.3 and vgo devel +1b870077c8, this error occurs:
build main: cannot find module for path main

Ref golang/go#26798
mizzy added a commit to aktsk/kalvados that referenced this issue Aug 27, 2018
With go 1.10.3 and vgo devel +1b870077c8, this error occurs:
build main: cannot find module for path main

Ref golang/go#26798
@golang golang locked and limited conversation to collaborators Aug 6, 2019
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

6 participants