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: can't compile files with relative imports under testdata directory using modules #33163

Closed
natefinch opened this issue Jul 18, 2019 · 4 comments

Comments

@natefinch
Copy link
Contributor

@natefinch natefinch commented Jul 18, 2019

What did you do?

OK so here's an edge case for you...

I have ~/test/testdata/foo/bar

in bar I have

package bar

var Bar string

in foo I have

package main

import "./bar"

func main() {
    _ = bar.Bar
}

This code will compile if I run it with GO111MODULE=off, but will fail if I compile with GO111MODULE=on with this error message:

$ go build
build foo: cannot find module for path _/Users/finchnat/test/testdata/foo/bar

Does this issue reproduce with the latest release (go1.12.7)?

Yes (note the below says 1.12.4 but I verified with .7 as well).

System details

go version go1.12.4 darwin/amd64
GOARCH="amd64"
GOBIN=""
GOCACHE="/Users/finchnat/Library/Caches/go-build"
GOEXE=""
GOFLAGS=""
GOHOSTARCH="amd64"
GOHOSTOS="darwin"
GOOS="darwin"
GOPATH="/Users/finchnat"
GOPROXY=""
GORACE=""
GOROOT="/Users/finchnat/sdk/go1.12.4"
GOTMPDIR=""
GOTOOLDIR="/Users/finchnat/sdk/go1.12.4/pkg/tool/darwin_amd64"
GCCGO="gccgo"
CC="clang"
CXX="clang++"
CGO_ENABLED="1"
GOMOD="/Users/finchnat/test/go.mod"
GOROOT/bin/go version: go version go1.12.4 darwin/amd64
GOROOT/bin/go tool compile -V: compile version go1.12.4
uname -v: Darwin Kernel Version 18.6.0: Thu Apr 25 23:16:27 PDT 2019; root:xnu-4903.261.4~2/RELEASE_X86_64
ProductName:	Mac OS X
ProductVersion:	10.14.5
BuildVersion:	18F132
lldb --version: lldb-1001.0.13.3
  Swift-5.0
@natefinch natefinch changed the title cmd/go: can't compile files with relative imports under testdata directory using modules cmd/go: can't compile files under testdata directory using modules Jul 18, 2019
@natefinch

This comment has been minimized.

Copy link
Contributor Author

@natefinch natefinch commented Jul 18, 2019

I just re-tested this with full import paths and it appears to behave the same (works with modules turned off, fails with that same error for the import path with modules turned on).

nevermind it is just relative imports... I had a typo with my full path import.

@natefinch

This comment has been minimized.

Copy link
Contributor Author

@natefinch natefinch commented Jul 18, 2019

BTW, the reason for compiling code under testdata is to have that code avoid go test ./... etc when working with the "real" code, but to have it available for my tests to compile. These are tests of github.com/magefile/mage which calls go build and so I have a lot of actual go files that I use for tests.

@natefinch natefinch changed the title cmd/go: can't compile files under testdata directory using modules cmd/go: can't compile files with relative imports under testdata directory using modules Jul 18, 2019
@FiloSottile

This comment has been minimized.

Copy link
Member

@FiloSottile FiloSottile commented Jul 18, 2019

I think relative imports just don't work with modules, regardless of testdata.

#26645 (comment)

To make your use case work in modules, it sounds like you can just put a go.mod in the testdata folder?

@natefinch

This comment has been minimized.

Copy link
Contributor Author

@natefinch natefinch commented Jul 18, 2019

I think you're right that it's just that relative import paths don't work with modules. I swear I'd tested it outside testdata and it worked fine, but I can't reproduce that now. I'm fine with relative paths going the way of the dodo, mostly just didn't realize this was a backwards incompatibility that was deemed ok.

@natefinch natefinch closed this Jul 18, 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.