Skip to content

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

Closed
@dmitshur

Description

@dmitshur

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

Metadata

Metadata

Assignees

No one assigned

    Labels

    FrozenDueToAgeNeedsFixThe path to resolution is known, but the work has not been done.modules

    Type

    No type

    Projects

    No projects

    Milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions