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

Closed
nemith opened this issue Mar 15, 2021 · 9 comments
Closed
Labels
FrozenDueToAge help wanted modules NeedsFix The path to resolution is known, but the work has not been done.
Milestone

Comments

@nemith
Copy link
Contributor

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
Member

cc @bcmills @jayconrod @matloob

@cherrymui cherrymui added the NeedsDecision Feedback is required from experts, contributors, and/or the community before a change can be made. label Mar 15, 2021
@cherrymui cherrymui added this to the Backlog milestone Mar 15, 2021
@bcmills
Copy link
Contributor

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 bcmills added modules NeedsFix The path to resolution is known, but the work has not been done. help wanted labels Mar 15, 2021
@gopherbot gopherbot removed the NeedsDecision Feedback is required from experts, contributors, and/or the community before a change can be made. label Mar 15, 2021
@bcmills
Copy link
Contributor

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 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 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
Contributor

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

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.

@gopherbot
Copy link

Change https://golang.org/cl/320310 mentions this issue: cmd/go: align checks of module path when creating go.mod file

@iwdgo
Copy link
Contributor

iwdgo commented May 16, 2021

With the suggested CL, space in a module name is rejected as expected.

>goissue mod init "bad example"
go: malformed import path "bad example": invalid char ' '

@golang golang locked and limited conversation to collaborators May 24, 2022
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
FrozenDueToAge help wanted modules NeedsFix The path to resolution is known, but the work has not been done.
Projects
None yet
Development

No branches or pull requests

7 participants