Skip to content

Commit

Permalink
modfile: take into account that // indirect comments may not be well …
Browse files Browse the repository at this point in the history
…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: b884ee1
GitHub-Pull-Request: #3
Reviewed-on: https://go-review.googlesource.com/c/mod/+/316569
Reviewed-by: Bryan C. Mills <[email protected]>
Trust: Bryan C. Mills <[email protected]>
Trust: Jay Conrod <[email protected]>
  • Loading branch information
komuw authored and Bryan C. Mills committed May 4, 2021
1 parent fb786b1 commit 67f1c1e
Show file tree
Hide file tree
Showing 2 changed files with 28 additions and 2 deletions.
4 changes: 2 additions & 2 deletions modfile/rule.go
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
26 changes: 26 additions & 0 deletions modfile/rule_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down

0 comments on commit 67f1c1e

Please sign in to comment.