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

go/build: Import does not respect FindOnly flag in module mode #31603

Open
dmitshur opened this issue Apr 21, 2019 · 0 comments

Comments

@dmitshur
Copy link
Member

commented Apr 21, 2019

Issue #26504 was about making it possible for go/build's Import and Context.Import to find packages in module mode.

The build.FindOnly import mode is documented as:

// If FindOnly is set, Import stops after locating the directory
// that should contain the sources for a package. It does not
// read any files in the directory.

For example, consider the following Go program:

package main

import (
	"fmt"
	"go/build"
	"log"
	"os"
)

func main() {
	wd, err := os.Getwd()
	if err != nil {
		log.Fatalln(err)
	}
	p, err := build.Import("golang.org/x/exp/shiny/example/goban", wd, build.FindOnly)
	fmt.Printf("dir=%q err=%v\n", p.Dir, err)
}

It works as expected in GOPATH mode:

$ cd $(mktemp -d)
$ cat >main.go <<EOF
package main

import (
    "fmt"
    "go/build"
    "log"
    "os"
)

func main() {
    wd, err := os.Getwd()
    if err != nil {
        log.Fatalln(err)
    }
    p, err := build.Import("golang.org/x/exp/shiny/example/goban", wd, build.FindOnly)
    fmt.Printf("dir=%q err=%v\n", p.Dir, err)
}
EOF

# Run in GOPATH mode.
$ GO111MODULE=off go run .
dir="/Users/gopher/go/src/golang.org/x/exp/shiny/example/goban" err=<nil>

But build.FindOnly has no effect in module mode:

# Run in module mode.
$ go mod init m
go: creating new go.mod: module m
$ GO111MODULE=on go run .
dir="" err=go/build: importGo golang.org/x/exp/shiny/example/goban: exit status 1
can't load package: package golang.org/x/exp/shiny/example/goban: build constraints exclude all Go files in /Users/gopher/go/pkg/mod/golang.org/x/exp@v0.0.0-20190419195159-b8972e603456/shiny/example/goban

@dmitshur dmitshur added this to the Go1.13 milestone Apr 21, 2019

@dmitshur dmitshur changed the title go/build: Import does not respect build.FindOnly flag in module mode go/build: Import does not respect FindOnly flag in module mode Apr 21, 2019

@andybons andybons modified the milestones: Go1.13, Go1.14 Jul 8, 2019

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