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: 'go test' with a file argument in a module root uses the wrong import path #28011

Open
andig opened this Issue Oct 4, 2018 · 11 comments

Comments

Projects
None yet
4 participants
@andig

andig commented Oct 4, 2018

Please answer these questions before submitting your issue. Thanks!

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

go version go1.11 darwin/amd64

Does this issue reproduce with the latest release?

yes

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

GOARCH="amd64"
GOBIN=""
GOCACHE="/Users/andig/Library/Caches/go-build"
GOEXE=""
GOFLAGS=""
GOHOSTARCH="amd64"
GOHOSTOS="darwin"
GOOS="darwin"
GOPATH="/Users/andig/go"
GOPROXY=""
GORACE=""
GOROOT="/usr/local/Cellar/go/1.11/libexec"
GOTMPDIR=""
GOTOOLDIR="/usr/local/Cellar/go/1.11/libexec/pkg/tool/darwin_amd64"
GCCGO="gccgo"
CC="clang"
CXX="clang++"
CGO_ENABLED="1"
GOMOD="/Users/andig/htdocs/gosdm/go.mod"
CGO_CFLAGS="-g -O2"
CGO_CPPFLAGS=""
CGO_CXXFLAGS="-g -O2"
CGO_FFLAGS="-g -O2"
CGO_LDFLAGS="-g -O2"
PKG_CONFIG="pkg-config"
GOGCCFLAGS="-fPIC -m64 -pthread -fno-caret-diagnostics -Qunused-arguments -fmessage-length=0 -fdebug-prefix-map=/var/folders/73/89ycv7qn51j4kbm04jsz9b840000gn/T/go-build122974265=/tmp/go-build -gno-record-gcc-switches -fno-common"

What did you do?

I have script that imports from an internal package for testing:

import (
	"testing"
	"time"

	. "github.com/gonium/gosdm630/internal/meters"
)

What did you expect to see?

Since I can import internal packages otherwhere it would be nice to do so for testing.

What did you see instead?

❯ go test datagram_test.go
# command-line-arguments
datagram_test.go:8:2: use of internal package github.com/gonium/gosdm630/internal/meters not allowed
FAIL    command-line-arguments [setup failed]
@bcmills

This comment has been minimized.

Member

bcmills commented Oct 4, 2018

In module mode, we use the module path to determine internal-package visibility.
As of https://golang.org/cl/129798, that also applies to file arguments in module mode.

So whether the current behavior is correct depends on your module path.

  • What is the module path of the module containing the test (go list -m)?
  • What is the directory relative to the module root (echo "${PWD#$(dirname $(go env GOMOD))/}")?
@andig

This comment has been minimized.

andig commented Oct 4, 2018

Here we go:

~/htdocs/gosdm fix-tests
❯ go list -m
github.com/gonium/gosdm630

~/htdocs/gosdm fix-tests
❯ echo "${PWD#$(dirname $(go env GOMOD))/}"
/Users/andig/htdocs/gosdm

This might also be of interest:

~/htdocs/gosdm fix-tests 6s
❯ go test datagram_test.go
# command-line-arguments
datagram_test.go:8:2: use of internal package github.com/gonium/gosdm630/internal/meters not allowed
FAIL    command-line-arguments [setup failed]

But (not sure it does anything, but it doesn't complain about a noop at least):

❯ go test
PASS
ok      github.com/gonium/gosdm630      0.018s
@myitcv

This comment has been minimized.

Member

myitcv commented Nov 13, 2018

@andig - can you provide a link to a repro? Because I'm having trouble reproducing this. Indeed there is even a specific test

that appears to demonstrate the use of internal as working

@bcmills

This comment has been minimized.

Member

bcmills commented Nov 13, 2018

@myitcv note that go test and go test datagram_test.go likely compute different import paths. The explicit filename is probably important, and the relative file path within the module may also be important.

@bcmills

This comment has been minimized.

Member

bcmills commented Nov 13, 2018

@andig I note the following:

GOMOD=/Users/andig/htdocs/gosdm/go.mod
❯ go test
PASS
ok      github.com/gonium/gosdm630      0.018s

What is the module path declared in /Users/andig/htdocs/gosdm/go.mod, and what is the absolute path in which you ran go test datagram_test.go?

@andig

This comment has been minimized.

andig commented Nov 14, 2018

What is the module path declared in /Users/andig/htdocs/gosdm/go.mod, and what is the absolute path in which you ran go test datagram_test.go?

Here it is:

~/htdocs/gosdm
❯ pwd
/Users/andig/htdocs/gosdm

~/htdocs/gosdm
❯ cat go.mod
module github.com/gonium/gosdm630
require (
        github.com/aprice/embed v0.0.0-20171223211210-e0f543275bf966271e2c3b3ab70265f635f0394a
        github.com/eclipse/paho.mqtt.golang v1.1.1
        github.com/goburrow/modbus v0.1.0
        github.com/goburrow/serial v0.1.0 // indirect
        github.com/gorilla/context v1.1.1 // indirect
        github.com/gorilla/handlers v1.4.0
        github.com/gorilla/mux v1.6.2
        github.com/gorilla/websocket v1.4.0
        github.com/spf13/pflag v1.0.3 // indirect
        github.com/tdewolff/minify v2.3.5+incompatible // indirect
        github.com/tdewolff/parse v2.3.3+incompatible // indirect
        github.com/tdewolff/test v0.0.0-20171106182207-265427085153 // indirect
        golang.org/x/net v0.0.0-20181003013248-f5e5bdd77824 // indirect
        golang.org/x/tools v0.0.0-20181006002542-f60d9635b16a // indirect
        gopkg.in/urfave/cli.v1 v1.20.0
)

The good thing is I can still reproduce this and do further tests.

@myitcv

This comment has been minimized.

Member

myitcv commented Nov 14, 2018

note that go test and go test datagram_test.go likely compute different import paths

@bcmills - yes, I had totally missed that the the failing command is go test datagram_test.go as opposed to go test (quite how I'm not sure). Indeed I've never actually seen go test be supplied with a file before: TIL.

@andig

This comment has been minimized.

andig commented Nov 14, 2018

I'm happy with go test working without the specific file- shall we close this issue?

@myitcv

This comment has been minimized.

Member

myitcv commented Nov 14, 2018

@andig - let's wait for @bcmills to come back first.

@bcmills

This comment has been minimized.

Member

bcmills commented Nov 14, 2018

I'd like to at least figure out what's going on here, but it sounds like you have a workaround for now.

@bcmills bcmills modified the milestones: Go1.12, Go1.13 Nov 14, 2018

@bcmills bcmills removed the WaitingForInfo label Nov 14, 2018

@bcmills bcmills changed the title from cmd/go: modules: use of internal package not allowed when running test to cmd/go: 'go test' with a file argument in a module root uses the wrong import path Nov 19, 2018

@gopherbot

This comment has been minimized.

gopherbot commented Dec 10, 2018

Change https://golang.org/cl/153459 mentions this issue: cmd/go: fix 'go test' and 'go fmt' with files outside a module

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment