modfile: take into account that // indirect comments may not be well formatted
When there is an // indirect comment next to a dependency that is not actually indirect;
go mod tidy should remove it.
This was not the case when the //indirect comment was badly formatted.
We now check whether such a comment exists irrespective of the formatting.
Updates golang/go#45932
Change-Id: I6a7dca23059a0aca6f8f940da975a0d79f701571
GitHub-Last-Rev: b884ee1a3ecf821f9b6b68bf061bbe0c521c8143
GitHub-Pull-Request: golang/mod#3
Reviewed-on: https://go-review.googlesource.com/c/mod/+/316569
Reviewed-by: Bryan C. Mills <bcmills@google.com>
Trust: Bryan C. Mills <bcmills@google.com>
Trust: Jay Conrod <jayconrod@google.com>
diff --git a/modfile/rule.go b/modfile/rule.go
index 3f603fa..d8242de 100644
--- a/modfile/rule.go
+++ b/modfile/rule.go
@@ -505,8 +505,8 @@
}
// Removing comment.
- f := strings.Fields(line.Suffix[0].Token)
- if len(f) == 2 {
+ f := strings.TrimSpace(strings.TrimPrefix(line.Suffix[0].Token, string(slashSlash)))
+ if f == "indirect" {
// Remove whole comment.
line.Suffix = nil
return
diff --git a/modfile/rule_test.go b/modfile/rule_test.go
index d721c71..d581ba2 100644
--- a/modfile/rule_test.go
+++ b/modfile/rule_test.go
@@ -77,6 +77,32 @@
out string
}{
{
+ `https://golang.org/issue/45932`,
+ `module m
+ require (
+ x.y/a v1.2.3 //indirect
+ x.y/b v1.2.3
+ x.y/c v1.2.3
+ )
+ `,
+ []struct {
+ path string
+ vers string
+ indirect bool
+ }{
+ {"x.y/a", "v1.2.3", false},
+ {"x.y/b", "v1.2.3", false},
+ {"x.y/c", "v1.2.3", false},
+ },
+ `module m
+ require (
+ x.y/a v1.2.3
+ x.y/b v1.2.3
+ x.y/c v1.2.3
+ )
+ `,
+ },
+ {
`existing`,
`module m
require (