diff --git a/src/cmd/go/internal/load/pkg.go b/src/cmd/go/internal/load/pkg.go index a83cc9a812b674..ccc66560dc3b1a 100644 --- a/src/cmd/go/internal/load/pkg.go +++ b/src/cmd/go/internal/load/pkg.go @@ -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) diff --git a/src/cmd/go/testdata/script/build_relative_import_not_supported.txt b/src/cmd/go/testdata/script/build_relative_import_not_supported.txt new file mode 100644 index 00000000000000..2e85b53ed3d082 --- /dev/null +++ b/src/cmd/go/testdata/script/build_relative_import_not_supported.txt @@ -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() {} diff --git a/src/cmd/go/testdata/script/build_relative_import_not_supported_in_go_path_mode.txt b/src/cmd/go/testdata/script/build_relative_import_not_supported_in_go_path_mode.txt new file mode 100644 index 00000000000000..4c21f1c9c2e1f4 --- /dev/null +++ b/src/cmd/go/testdata/script/build_relative_import_not_supported_in_go_path_mode.txt @@ -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 _ "." \ No newline at end of file