Thanks to visit codestin.com
Credit goes to github.com

Skip to content

Commit 4169c1f

Browse files
committed
modules: Ignore non-require blocks in go.mod rewrite
The Go 1.24 tool directive uses single-token entries inside a tool ( ... ) block. The previous splitter treated any tab-indented line as a require entry, causing an index out of range panic when running hugo mod tidy on a module with a tool block. Track the require block state explicitly so other blocks (tool, replace, exclude, retract) are left untouched. Fixes #14783
1 parent 7574e35 commit 4169c1f

3 files changed

Lines changed: 38 additions & 3 deletions

File tree

modules/client.go

Lines changed: 16 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1045,18 +1045,32 @@ func (modules goModules) GetMain() *goModule {
10451045

10461046
func getModlineSplitter(isGoMod bool) func(line string) []string {
10471047
if isGoMod {
1048+
var inRequireBlock bool
10481049
return func(line string) []string {
10491050
if strings.HasPrefix(line, "require (") {
1051+
inRequireBlock = true
10501052
return nil
10511053
}
1052-
if !strings.HasPrefix(line, "require") && !strings.HasPrefix(line, "\t") {
1054+
if inRequireBlock {
1055+
if strings.HasPrefix(line, ")") {
1056+
inRequireBlock = false
1057+
return nil
1058+
}
1059+
if !strings.HasPrefix(line, "\t") {
1060+
return nil
1061+
}
1062+
} else if !strings.HasPrefix(line, "require ") {
10531063
return nil
10541064
}
10551065
line = strings.TrimPrefix(line, "require")
10561066
line = strings.TrimSpace(line)
10571067
line = strings.TrimSuffix(line, "// indirect")
10581068

1059-
return strings.Fields(line)
1069+
parts := strings.Fields(line)
1070+
if len(parts) < 2 {
1071+
return nil
1072+
}
1073+
return parts
10601074
}
10611075
}
10621076

modules/client_test.go

Lines changed: 14 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -210,14 +210,27 @@ func TestGetModlineSplitter(t *testing.T) {
210210

211211
gomodSplitter := getModlineSplitter(true)
212212

213+
c.Assert(gomodSplitter("require ("), qt.IsNil)
213214
c.Assert(gomodSplitter("\tgithub.com/BurntSushi/toml v0.3.1"), qt.DeepEquals, []string{"github.com/BurntSushi/toml", "v0.3.1"})
214215
c.Assert(gomodSplitter("\tgithub.com/cpuguy83/go-md2man v1.0.8 // indirect"), qt.DeepEquals, []string{"github.com/cpuguy83/go-md2man", "v1.0.8"})
215-
c.Assert(gomodSplitter("require ("), qt.IsNil)
216216

217217
gosumSplitter := getModlineSplitter(false)
218218
c.Assert(gosumSplitter("github.com/BurntSushi/toml v0.3.1"), qt.DeepEquals, []string{"github.com/BurntSushi/toml", "v0.3.1"})
219219
}
220220

221+
// Issue 14783
222+
func TestGetModlineSplitterIgnoresToolBlockIssue14783(t *testing.T) {
223+
c := qt.New(t)
224+
225+
gomodSplitter := getModlineSplitter(true)
226+
227+
c.Assert(gomodSplitter("tool ("), qt.IsNil)
228+
c.Assert(gomodSplitter("\tgithub.com/gohugoio/hugo"), qt.IsNil)
229+
c.Assert(gomodSplitter(")"), qt.IsNil)
230+
c.Assert(gomodSplitter("require ("), qt.IsNil)
231+
c.Assert(gomodSplitter("\tgithub.com/foo/bar v1.2.3"), qt.DeepEquals, []string{"github.com/foo/bar", "v1.2.3"})
232+
}
233+
221234
func TestClientConfigToEnv(t *testing.T) {
222235
c := qt.New(t)
223236

testscripts/commands/mod_tidy.txt

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -11,9 +11,17 @@ go 1.19
1111

1212
require github.com/bep/empty-hugo-module v1.0.0
1313

14+
tool (
15+
github.com/gohugoio/hugo
16+
)
17+
1418
module github.com/gohugoio/testmod
1519
-- golden/go.mod.cleaned --
1620
go 1.19
1721

1822

23+
tool (
24+
github.com/gohugoio/hugo
25+
)
26+
1927
module github.com/gohugoio/testmod

0 commit comments

Comments
 (0)