Skip to content

Commit 32b73ae

Browse files
iwdgoJay Conrod
authored and
Jay Conrod
committed
cmd/go: align checks of module path during initialization.
Fixes #45025. Change-Id: I70c2b745f764484e4b3a2824adc470f168fb2c50 Reviewed-on: https://go-review.googlesource.com/c/go/+/320310 Trust: Jay Conrod <[email protected]> Trust: Bryan C. Mills <[email protected]> Run-TryBot: Jay Conrod <[email protected]> Reviewed-by: Bryan C. Mills <[email protected]> Reviewed-by: Jay Conrod <[email protected]> TryBot-Result: Go Bot <[email protected]>
1 parent 15d9d4a commit 32b73ae

File tree

3 files changed

+19
-60
lines changed

3 files changed

+19
-60
lines changed

src/cmd/go/internal/modload/init.go

+15-53
Original file line numberDiff line numberDiff line change
@@ -432,7 +432,10 @@ func loadModFile(ctx context.Context) (rs *Requirements, needCommit bool) {
432432
initTarget(f.Module.Mod)
433433
index = indexModFile(data, f, fixed)
434434

435-
if err := checkModulePathLax(f.Module.Mod.Path); err != nil {
435+
if err := module.CheckImportPath(f.Module.Mod.Path); err != nil {
436+
if pathErr, ok := err.(*module.InvalidPathError); ok {
437+
pathErr.Kind = "module"
438+
}
436439
base.Fatalf("go: %v", err)
437440
}
438441

@@ -492,7 +495,15 @@ func CreateModFile(ctx context.Context, modPath string) {
492495
if err != nil {
493496
base.Fatalf("go: %v", err)
494497
}
495-
} else if err := checkModulePathLax(modPath); err != nil {
498+
} else if err := module.CheckImportPath(modPath); err != nil {
499+
if pathErr, ok := err.(*module.InvalidPathError); ok {
500+
pathErr.Kind = "module"
501+
// Same as build.IsLocalPath()
502+
if pathErr.Path == "." || pathErr.Path == ".." ||
503+
strings.HasPrefix(pathErr.Path, "./") || strings.HasPrefix(pathErr.Path, "../") {
504+
pathErr.Err = errors.New("is a local import path")
505+
}
506+
}
496507
base.Fatalf("go: %v", err)
497508
}
498509

@@ -536,49 +547,6 @@ func CreateModFile(ctx context.Context, modPath string) {
536547
}
537548
}
538549

539-
// checkModulePathLax checks that the path meets some minimum requirements
540-
// to avoid confusing users or the module cache. The requirements are weaker
541-
// than those of module.CheckPath to allow room for weakening module path
542-
// requirements in the future, but strong enough to help users avoid significant
543-
// problems.
544-
func checkModulePathLax(p string) error {
545-
// TODO(matloob): Replace calls of this function in this CL with calls
546-
// to module.CheckImportPath once it's been laxened, if it becomes laxened.
547-
// See golang.org/issue/29101 for a discussion about whether to make CheckImportPath
548-
// more lax or more strict.
549-
550-
errorf := func(format string, args ...interface{}) error {
551-
return fmt.Errorf("invalid module path %q: %s", p, fmt.Sprintf(format, args...))
552-
}
553-
554-
// Disallow shell characters " ' * < > ? ` | to avoid triggering bugs
555-
// with file systems and subcommands. Disallow file path separators : and \
556-
// because path separators other than / will confuse the module cache.
557-
// See fileNameOK in golang.org/x/mod/module/module.go.
558-
shellChars := "`" + `"'*<>?|`
559-
fsChars := `\:`
560-
if i := strings.IndexAny(p, shellChars); i >= 0 {
561-
return errorf("contains disallowed shell character %q", p[i])
562-
}
563-
if i := strings.IndexAny(p, fsChars); i >= 0 {
564-
return errorf("contains disallowed path separator character %q", p[i])
565-
}
566-
567-
// Ensure path.IsAbs and build.IsLocalImport are false, and that the path is
568-
// invariant under path.Clean, also to avoid confusing the module cache.
569-
if path.IsAbs(p) {
570-
return errorf("is an absolute path")
571-
}
572-
if build.IsLocalImport(p) {
573-
return errorf("is a local import path")
574-
}
575-
if path.Clean(p) != p {
576-
return errorf("is not clean")
577-
}
578-
579-
return nil
580-
}
581-
582550
// fixVersion returns a modfile.VersionFixer implemented using the Query function.
583551
//
584552
// It resolves commit hashes and branch names to versions,
@@ -918,14 +886,8 @@ func findModulePath(dir string) (string, error) {
918886
}
919887
if rel := search.InDir(dir, filepath.Join(gpdir, "src")); rel != "" && rel != "." {
920888
path := filepath.ToSlash(rel)
921-
// TODO(matloob): replace this with module.CheckImportPath
922-
// once it's been laxened.
923-
// Only checkModulePathLax here. There are some unpublishable
924-
// module names that are compatible with checkModulePathLax
925-
// but they already work in GOPATH so don't break users
926-
// trying to do a build with modules. gorelease will alert users
927-
// publishing their modules to fix their paths.
928-
if err := checkModulePathLax(path); err != nil {
889+
// gorelease will alert users publishing their modules to fix their paths.
890+
if err := module.CheckImportPath(path); err != nil {
929891
badPathErr = err
930892
break
931893
}

src/cmd/go/testdata/script/mod_init_path.txt

+1-1
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
env GO111MODULE=on
22

33
! go mod init .
4-
stderr '^go: invalid module path "\.": is a local import path$'
4+
stderr '^go: malformed module path ".": is a local import path$'
55

66
cd x
77
go mod init example.com/x

src/cmd/go/testdata/script/mod_invalid_path.txt

+3-6
Original file line numberDiff line numberDiff line change
@@ -8,11 +8,8 @@ stderr '^go: no module declaration in go.mod. To specify the module path:\n\tgo
88
# Test that go mod init in GOPATH doesn't add a module declaration
99
# with a path that can't possibly be a module path, because
1010
# it isn't even a valid import path.
11-
# The single quote and backtick are the only characters we don't allow
12-
# in checkModulePathLax, but is allowed in a Windows file name.
13-
# TODO(matloob): choose a different character once
14-
# module.CheckImportPath is laxened and replaces
15-
# checkModulePathLax.
11+
# The single quote and backtick are the only characters which are not allowed
12+
# but are a valid Windows file name.
1613
cd $WORK/'gopath/src/m''d'
1714
! go mod init
1815
stderr 'cannot determine module path'
@@ -21,7 +18,7 @@ stderr 'cannot determine module path'
2118
# possibly be a module path, because it isn't even a valid import path
2219
cd $WORK/gopath/src/badname
2320
! go list .
24-
stderr 'invalid module path'
21+
stderr 'malformed module path'
2522

2623
# Test that an import path containing an element with a leading dot is valid,
2724
# but such a module path is not.

0 commit comments

Comments
 (0)