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: 'go mod init' should reject module paths that are not valid import paths #45025

Open
nemith opened this issue Mar 15, 2021 · 7 comments
Open

Comments

@nemith
Copy link
Contributor

@nemith nemith commented Mar 15, 2021

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

$ go version
go version go1.16.2 linux/amd64

Does this issue reproduce with the latest release?

Tested on Go 1.16.2 and on tip using gotip

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

go env Output
$ go env
GO111MODULE=""
GOARCH="amd64"
GOBIN=""
GOCACHE="/home/bbennett/.cache/go-build"
GOENV="/home/bbennett/.config/go/env"
GOEXE=""
GOFLAGS=""
GOHOSTARCH="amd64"
GOHOSTOS="linux"
GOINSECURE=""
GOMODCACHE="/home/bbennett/go/pkg/mod"
GONOPROXY=""
GONOSUMDB=""
GOOS="linux"
GOPATH="/home/bbennett/go"
GOPRIVATE=""
GOPROXY="https://proxy.golang.org,direct"
GOROOT="/opt/go"
GOSUMDB="sum.golang.org"
GOTMPDIR=""
GOTOOLDIR="/opt/go/pkg/tool/linux_amd64"
GOVCS=""
GOVERSION="go1.16.2"
GCCGO="gccgo"
AR="ar"
CC="gcc"
CXX="g++"
CGO_ENABLED="1"
GOMOD="/home/bbennett/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 -fmessage-length=0 -fdebug-prefix-map=/tmp/go-build45415822=/tmp/go-build -gno-record-gcc-switches"

What did you do?

$ go mod init "bad module"
$ cat go.mod
module "bad module"

go 1.16

What did you expect to see?

An error about the import path not being correct due to the space and not have the go.mod file created

What did you see instead?

go: creating new go.mod: module bad mod
go: to add module requirements and sums:
	go mod tidy

Also once trying to use the module then I get an error about the module name.

[bbennett@bbennett-fedora-PC1RSH91 tmp]$ go run .
malformed import path "bad mod": invalid char ' '
@cherrymui
Copy link
Contributor

@cherrymui cherrymui commented Mar 15, 2021

@cherrymui cherrymui added this to the Backlog milestone Mar 15, 2021
@bcmills
Copy link
Member

@bcmills bcmills commented Mar 15, 2021

(See also #36774, #44052, #31549.)

@bcmills bcmills changed the title cmd/go: 'go mod init' doesn't validate module path to be valid before creating go.mod cmd/go: 'go mod init' should reject module paths that are not valid import paths Mar 15, 2021
@bcmills
Copy link
Member

@bcmills bcmills commented Mar 15, 2021

We can't reject module paths that fail module.CheckPath, because they may be needed for users to migrate from GOPATH mode to modules (and can be used in conjunction with replace directives even if they can't be fetched from a proxy). However, we certainly should reject module paths that fail module.CheckImportPath.

I thought we had fixed this in CL 211597, but I guess we missed something there. 😞

CC @matloob @jayconrod

@nemith
Copy link
Contributor Author

@nemith nemith commented Mar 15, 2021

This may be as simple as adding ' '(space) to the one of the reject lists (it's really not a shell character or a fs character though). I wonder what other character may need to be blocked.

@oiooj
Copy link
Member

@oiooj oiooj commented Mar 17, 2021

Yes, this issue fixed in CL 211597 .

  $WORK/gopath/src/mod
  > go mod init "bad module"
  [stderr]
  go mod init: too many arguments
  [exit status 1]
  FAIL: testdata/script/mod_invalid_importpath.txt:5: unexpected command failure
@bcmills
Copy link
Member

@bcmills bcmills commented Mar 17, 2021

@oiooj, it's nice that go mod init now correctly rejects this path, but IMO too many arguments is a pretty confusing error message. I'd like to leave this issue open to at least improve that. 🙂

@jayconrod
Copy link
Contributor

@jayconrod jayconrod commented Mar 17, 2021

go mod init currently does not reject spaces.

$ go mod init "bad module"
go: creating new go.mod: module bad module
$ cat go.mod
module "bad module"

go 1.16

(I think testscript interprets " literally; that would explain the too many arguments error, which I agree should be clarified).

Now that we have separate validation for import paths, perhaps we should call module.CheckImportPath on top of the existing restrictions? It shouldn't be possible to create a module with go mod init if we can't import its packages.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Linked pull requests

Successfully merging a pull request may close this issue.

None yet
6 participants