Skip to content

go/internal/load: fix bad error message for local imports in module mode #47516

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

Closed
wants to merge 5 commits into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 3 additions & 1 deletion src/cmd/go/internal/load/pkg.go
Original file line number Diff line number Diff line change
Expand Up @@ -752,7 +752,9 @@ func loadImport(ctx context.Context, opts PackageOpts, pre *preload, path, srcDi
if p.Internal.Local && parent != nil && !parent.Internal.Local {
perr := *p
var err error
if path == "." {
if cfg.ModulesEnabled {
err = ImportErrorf(path, "local import %q not supported in module mode", path)
} else if path == "." {
err = ImportErrorf(path, "%s: cannot import current directory", path)
} else {
err = ImportErrorf(path, "local import %q in non-local package", path)
Expand Down
17 changes: 17 additions & 0 deletions src/cmd/go/testdata/script/build_relative_import_not_supported.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
! go build
stderr 'main.go:4:2: local import "./foo" not supported in module mode$'

-- foo/foo.go --
package foo
-- go.mod --
module example.com/m

go 1.17
-- main.go --
package main

import (
_ "./foo"
)

func main() {}
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
env GO111MODULE=off

# Control case: in GOPATH mode,
# a/a.go can import package "./b" using a relative path.
go build a/a.go
! stderr .

# package "a" itself cannot import "./b": "a" has a non-local
# import path, so its imports must also have non-local import paths.
! go build a
stderr '^a[/\\]a.go:3:8: local import "./b" in non-local package$'

# package "c" imports itself, which is not allowed.
! go build c
stderr '^c[/\\]c.go:3:8: .: cannot import current directory$'


-- a/a.go --
package a

import _ "./b"
-- a/b/b.go --
package b
-- c/c.go --
package c

import _ "."