From 67f1c1edc27ada9b0cffe84ccdd4db2d4ff56edf Mon Sep 17 00:00:00 2001 From: komuW Date: Tue, 4 May 2021 16:01:26 +0000 Subject: [PATCH] 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 Trust: Bryan C. Mills Trust: Jay Conrod --- modfile/rule.go | 4 ++-- modfile/rule_test.go | 26 ++++++++++++++++++++++++++ 2 files changed, 28 insertions(+), 2 deletions(-) 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 @@ func setIndirect(line *Line, indirect bool) { } // 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 @@ -76,6 +76,32 @@ var setRequireTests = []struct { } 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