[mod] modfile: improve directory path detection and error text consistency

23 views
Skip to first unread message

Gopher Robot (Gerrit)

unread,
Oct 25, 2023, 6:55:39 PM10/25/23
to Dmitri Shuralyov, goph...@pubsubhelper.golang.org, golang-...@googlegroups.com, Bryan Mills, Go LUCI, Dmitri Shuralyov, Michael Matloob, golang-co...@googlegroups.com

Gopher Robot submitted this change.

View Change

Approvals: Gopher Robot: TryBots succeeded Dmitri Shuralyov: Looks good to me, but someone else must approve Go LUCI: TryBots succeeded Dmitri Shuralyov: Run legacy TryBots; Automatically submit change Bryan Mills: Looks good to me, approved
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 <dmit...@google.com>
Reviewed-by: Bryan Mills <bcm...@google.com>
Auto-Submit: Dmitri Shuralyov <dmit...@golang.org>
Run-TryBot: Dmitri Shuralyov <dmit...@golang.org>
TryBot-Result: Gopher Robot <go...@golang.org>
LUCI-TryBot-Result: Go LUCI <golang...@luci-project-accounts.iam.gserviceaccount.com>
---
M modfile/rule.go
1 file changed, 6 insertions(+), 6 deletions(-)

diff --git a/modfile/rule.go b/modfile/rule.go
index e0869fa..35fd1f5 100644
--- a/modfile/rule.go
+++ b/modfile/rule.go
@@ -542,7 +542,7 @@
if strings.Contains(ns, "@") {
return nil, errorf("replacement module must match format 'path version', not 'path@version'")
}
- return nil, errorf("replacement module without version must be directory path (rooted or starting with ./ or ../)")
+ return nil, errorf("replacement module without version must be directory path (rooted or starting with . or ..)")
}
if filepath.Separator == '/' && strings.Contains(ns, `\`) {
return nil, errorf("replacement directory appears to be Windows path (on a non-windows system)")
@@ -555,7 +555,6 @@
}
if IsDirectoryPath(ns) {
return nil, errorf("replacement module directory path %q cannot have version", ns)
-
}
}
return &Replace{
@@ -679,14 +678,15 @@
}
}

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


To view, visit change 500335. To unsubscribe, or for help writing mail filters, visit settings.

Gerrit-MessageType: merged
Gerrit-Project: mod
Gerrit-Branch: master
Gerrit-Change-Id: I8fa4a2c66bc83a1ccafc453b96f3bb33dc222cd1
Gerrit-Change-Number: 500335
Gerrit-PatchSet: 2
Gerrit-Owner: Dmitri Shuralyov <dmit...@golang.org>
Gerrit-Reviewer: Bryan Mills <bcm...@google.com>
Gerrit-Reviewer: Dmitri Shuralyov <dmit...@golang.org>
Gerrit-Reviewer: Dmitri Shuralyov <dmit...@google.com>
Gerrit-Reviewer: Gopher Robot <go...@golang.org>
Gerrit-CC: Michael Matloob <mat...@golang.org>
Reply all
Reply to author
Forward
0 new messages