Skip to content

Commit 6e58e47

Browse files
dmitshurgopherbot
authored andcommitted
modfile: improve directory path detection and error text consistency
An error text suggests a directory path needs to start with ./ or ../ if it's a relative path, but in reality relative paths with .\ and ..\ prefix (such as those that are used on Windows) are also accepted. Furthermore, a relative path like ./ or ../ is fine, as are ./. and ../., but the cleaner and shorter equivalent relative paths . and .. are reported as if they're not directory paths (even though a module path cannot consist of nothing but dots). Fix those inconsistencies and make IsDirectoryPath report true on "." and ".." paths as expected, and make its documentation clear that a path like "sub/dir", despite being a relative path, is interpreted as a module path. For golang/go#60572. Change-Id: I8fa4a2c66bc83a1ccafc453b96f3bb33dc222cd1 Reviewed-on: https://go-review.googlesource.com/c/mod/+/500335 Reviewed-by: Dmitri Shuralyov <[email protected]> Reviewed-by: Bryan Mills <[email protected]> Auto-Submit: Dmitri Shuralyov <[email protected]> Run-TryBot: Dmitri Shuralyov <[email protected]> TryBot-Result: Gopher Robot <[email protected]> LUCI-TryBot-Result: Go LUCI <[email protected]>
1 parent 5b69280 commit 6e58e47

File tree

1 file changed

+6
-6
lines changed

1 file changed

+6
-6
lines changed

modfile/rule.go

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -542,7 +542,7 @@ func parseReplace(filename string, line *Line, verb string, args []string, fix V
542542
if strings.Contains(ns, "@") {
543543
return nil, errorf("replacement module must match format 'path version', not 'path@version'")
544544
}
545-
return nil, errorf("replacement module without version must be directory path (rooted or starting with ./ or ../)")
545+
return nil, errorf("replacement module without version must be directory path (rooted or starting with . or ..)")
546546
}
547547
if filepath.Separator == '/' && strings.Contains(ns, `\`) {
548548
return nil, errorf("replacement directory appears to be Windows path (on a non-windows system)")
@@ -555,7 +555,6 @@ func parseReplace(filename string, line *Line, verb string, args []string, fix V
555555
}
556556
if IsDirectoryPath(ns) {
557557
return nil, errorf("replacement module directory path %q cannot have version", ns)
558-
559558
}
560559
}
561560
return &Replace{
@@ -679,14 +678,15 @@ func (f *WorkFile) add(errs *ErrorList, line *Line, verb string, args []string,
679678
}
680679
}
681680

682-
// IsDirectoryPath reports whether the given path should be interpreted
683-
// as a directory path. Just like on the go command line, relative paths
681+
// IsDirectoryPath reports whether the given path should be interpreted as a directory path.
682+
// Just like on the go command line, relative paths starting with a '.' or '..' path component
684683
// and rooted paths are directory paths; the rest are module paths.
685684
func IsDirectoryPath(ns string) bool {
686685
// Because go.mod files can move from one system to another,
687686
// we check all known path syntaxes, both Unix and Windows.
688-
return strings.HasPrefix(ns, "./") || strings.HasPrefix(ns, "../") || strings.HasPrefix(ns, "/") ||
689-
strings.HasPrefix(ns, `.\`) || strings.HasPrefix(ns, `..\`) || strings.HasPrefix(ns, `\`) ||
687+
return ns == "." || strings.HasPrefix(ns, "./") || strings.HasPrefix(ns, `.\`) ||
688+
ns == ".." || strings.HasPrefix(ns, "../") || strings.HasPrefix(ns, `..\`) ||
689+
strings.HasPrefix(ns, "/") || strings.HasPrefix(ns, `\`) ||
690690
len(ns) >= 2 && ('A' <= ns[0] && ns[0] <= 'Z' || 'a' <= ns[0] && ns[0] <= 'z') && ns[1] == ':'
691691
}
692692

0 commit comments

Comments
 (0)