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

Closed
dmitshur opened this issue Apr 21, 2019 · 1 comment
Closed

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

dmitshur opened this issue Apr 21, 2019 · 1 comment
Labels
FrozenDueToAge modules NeedsFix The path to resolution is known, but the work has not been done.
Milestone

Comments

@dmitshur
Copy link
Contributor

dmitshur 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 NeedsInvestigation Someone must examine and confirm this is a valid issue and not a duplicate of an existing one. modules labels Apr 21, 2019
@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
@rsc rsc modified the milestones: Go1.14, Backlog Oct 9, 2019
@gopherbot
Copy link

Change https://golang.org/cl/218817 mentions this issue: go/build: populate partial package information in importGo

@dmitshur dmitshur self-assigned this Feb 10, 2020
@dmitshur dmitshur added NeedsFix The path to resolution is known, but the work has not been done. and removed NeedsInvestigation Someone must examine and confirm this is a valid issue and not a duplicate of an existing one. labels Feb 10, 2020
@dmitshur dmitshur modified the milestones: Backlog, Go1.14 Feb 10, 2020
@golang golang locked and limited conversation to collaborators Feb 11, 2021
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
FrozenDueToAge modules NeedsFix The path to resolution is known, but the work has not been done.
Projects
None yet
Development

No branches or pull requests

4 participants