Skip to content

Commit 49f84bc

Browse files
Jay Conrodrsc
authored andcommitted
[release-branch.go1.17] modfile: in SetRequireSeparateIndirect, convert lines to blocks
When reading go.mod, SetRequireSeparateIndirect will insert new requirements into the last uncommented direct-only or indirect-only block OR line. If the last such statement is a line, SetRequireSeparateIndirect converts it to a block before inserting new requirements. Cleanup will convert it back to a line later if no requirements are inserted. For golang/go#47756 Change-Id: Id8ee3b0ce2d005488ddb3d9a5349115bd93938e7 Reviewed-on: https://go-review.googlesource.com/c/mod/+/348576 Trust: Jay Conrod <[email protected]> Run-TryBot: Jay Conrod <[email protected]> TryBot-Result: Go Bot <[email protected]> Reviewed-by: Bryan C. Mills <[email protected]> Reviewed-by: Michael Matloob <[email protected]> (cherry picked from commit 37dd689) Reviewed-on: https://go-review.googlesource.com/c/mod/+/351318
1 parent 57376c6 commit 49f84bc

File tree

2 files changed

+82
-31
lines changed

2 files changed

+82
-31
lines changed

modfile/rule.go

Lines changed: 56 additions & 29 deletions
Original file line numberDiff line numberDiff line change
@@ -1009,9 +1009,8 @@ func (f *File) SetRequireSeparateIndirect(req []*Require) {
10091009
// We may insert new requirements into the last uncommented
10101010
// direct-only and indirect-only blocks. We may also move requirements
10111011
// to the opposite block if their indirect markings change.
1012-
lastDirectBlock, lastIndirectBlock *LineBlock
1013-
lastDirectBlockIndex = -1
1014-
lastIndirectBlockIndex = -1
1012+
lastDirectIndex = -1
1013+
lastIndirectIndex = -1
10151014

10161015
// If there are no direct-only or indirect-only blocks, a new block may
10171016
// be inserted after the last require line or block.
@@ -1033,6 +1032,13 @@ func (f *File) SetRequireSeparateIndirect(req []*Require) {
10331032
}
10341033
lastRequireIndex = i
10351034
requireLineOrBlockCount++
1035+
if !hasComments(stmt.Comments) {
1036+
if isIndirect(stmt) {
1037+
lastIndirectIndex = i
1038+
} else {
1039+
lastDirectIndex = i
1040+
}
1041+
}
10361042

10371043
case *LineBlock:
10381044
if len(stmt.Token) == 0 || stmt.Token[0] != "require" {
@@ -1054,46 +1060,67 @@ func (f *File) SetRequireSeparateIndirect(req []*Require) {
10541060
}
10551061
}
10561062
if allDirect {
1057-
lastDirectBlock = stmt
1058-
lastDirectBlockIndex = i
1063+
lastDirectIndex = i
10591064
}
10601065
if allIndirect {
1061-
lastIndirectBlock = stmt
1062-
lastIndirectBlockIndex = i
1066+
lastIndirectIndex = i
10631067
}
10641068
}
10651069
}
10661070

10671071
oneFlatUncommentedBlock := requireLineOrBlockCount == 1 &&
10681072
!hasComments(*f.Syntax.Stmt[lastRequireIndex].Comment())
10691073

1070-
// Create direct and indirect blocks if needed. It's okay if they're empty.
1071-
// Cleanup will remove them and convert one-line blocks to lines.
1072-
if lastDirectBlock == nil {
1073-
lastDirectBlock = &LineBlock{Token: []string{"require"}}
1074-
i := len(f.Syntax.Stmt)
1075-
if lastIndirectBlockIndex >= 0 {
1076-
i = lastIndirectBlockIndex
1077-
lastIndirectBlockIndex++
1078-
} else if lastRequireIndex >= 0 {
1079-
i = lastRequireIndex + 1
1080-
}
1074+
// Create direct and indirect blocks if needed. Convert lines into blocks
1075+
// if needed. If we end up with an empty block or a one-line block,
1076+
// Cleanup will delete it or convert it to a line later.
1077+
insertBlock := func(i int) *LineBlock {
1078+
block := &LineBlock{Token: []string{"require"}}
10811079
f.Syntax.Stmt = append(f.Syntax.Stmt, nil)
10821080
copy(f.Syntax.Stmt[i+1:], f.Syntax.Stmt[i:])
1083-
f.Syntax.Stmt[i] = lastDirectBlock
1084-
lastDirectBlockIndex = i
1081+
f.Syntax.Stmt[i] = block
1082+
return block
10851083
}
10861084

1087-
if lastIndirectBlock == nil {
1088-
lastIndirectBlock = &LineBlock{Token: []string{"require"}}
1089-
i := len(f.Syntax.Stmt)
1090-
if lastDirectBlockIndex >= 0 {
1091-
i = lastDirectBlockIndex + 1
1085+
ensureBlock := func(i int) *LineBlock {
1086+
switch stmt := f.Syntax.Stmt[i].(type) {
1087+
case *LineBlock:
1088+
return stmt
1089+
case *Line:
1090+
block := &LineBlock{
1091+
Token: []string{"require"},
1092+
Line: []*Line{stmt},
1093+
}
1094+
stmt.Token = stmt.Token[1:] // remove "require"
1095+
stmt.InBlock = true
1096+
f.Syntax.Stmt[i] = block
1097+
return block
1098+
default:
1099+
panic(fmt.Sprintf("unexpected statement: %v", stmt))
10921100
}
1093-
f.Syntax.Stmt = append(f.Syntax.Stmt, nil)
1094-
copy(f.Syntax.Stmt[i+1:], f.Syntax.Stmt[i:])
1095-
f.Syntax.Stmt[i] = lastIndirectBlock
1096-
lastIndirectBlockIndex = i
1101+
}
1102+
1103+
var lastDirectBlock *LineBlock
1104+
if lastDirectIndex < 0 {
1105+
if lastIndirectIndex >= 0 {
1106+
lastDirectIndex = lastIndirectIndex
1107+
lastIndirectIndex++
1108+
} else if lastRequireIndex >= 0 {
1109+
lastDirectIndex = lastRequireIndex + 1
1110+
} else {
1111+
lastDirectIndex = len(f.Syntax.Stmt)
1112+
}
1113+
lastDirectBlock = insertBlock(lastDirectIndex)
1114+
} else {
1115+
lastDirectBlock = ensureBlock(lastDirectIndex)
1116+
}
1117+
1118+
var lastIndirectBlock *LineBlock
1119+
if lastIndirectIndex < 0 {
1120+
lastIndirectIndex = lastDirectIndex + 1
1121+
lastIndirectBlock = insertBlock(lastIndirectIndex)
1122+
} else {
1123+
lastIndirectBlock = ensureBlock(lastIndirectIndex)
10971124
}
10981125

10991126
// Delete requirements we don't want anymore.

modfile/rule_test.go

Lines changed: 26 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -339,6 +339,28 @@ var setRequireSeparateIndirectTests = []struct {
339339
)
340340
`,
341341
},
342+
{
343+
`existing_line`,
344+
`module m
345+
require x.y/a v1.0.0
346+
require x.y/c v1.0.0 // indirect
347+
`,
348+
[]require{
349+
{"x.y/a", "v1.2.3", false},
350+
{"x.y/b", "v1.2.3", false},
351+
{"x.y/c", "v1.2.3", true},
352+
{"x.y/d", "v1.2.3", true},
353+
},
354+
`module m
355+
require (
356+
x.y/a v1.2.3
357+
x.y/b v1.2.3
358+
)
359+
require (
360+
x.y/c v1.2.3 // indirect
361+
x.y/d v1.2.3 // indirect
362+
)`,
363+
},
342364
{
343365
`existing_multi`,
344366
`module m
@@ -366,7 +388,10 @@ var setRequireSeparateIndirectTests = []struct {
366388
{"x.y/g", "v1.2.3", false},
367389
},
368390
`module m
369-
require x.y/a v1.2.3
391+
require (
392+
x.y/a v1.2.3
393+
x.y/h v1.2.3
394+
)
370395
require x.y/b v1.2.3 // indirect; demoted to indirect
371396
require x.y/c v1.2.3 // not v1.2.3!
372397
require x.y/d v1.2.3 // comment kept
@@ -376,7 +401,6 @@ var setRequireSeparateIndirectTests = []struct {
376401
require x.y/g v1.2.3
377402
require x.y/i v1.2.3 // indirect
378403
require x.y/j v1.2.3 // indirect
379-
require x.y/h v1.2.3
380404
`,
381405
},
382406
{

0 commit comments

Comments
 (0)