From a2adbe20fcafa4f1aa2f75e74d7b6fdc40b303f1 Mon Sep 17 00:00:00 2001 From: abhinavgautam01 Date: Sun, 15 Nov 2026 22:37:53 +0530 Subject: [PATCH 1/2] Record go.mod replace directives without applying them --- README.md | 22 ++++ gomod_replace_test.go | 83 ++++++++++++++ internal/core/types.go | 17 +++ internal/golang/golang.go | 195 +++++++++++++++++++++------------ internal/golang/golang_test.go | 62 +++++++++++ manifests.go | 10 ++ 6 files changed, 319 insertions(+), 70 deletions(-) create mode 100644 gomod_replace_test.go diff --git a/README.md b/README.md index c31bc48..7540dfb 100644 --- a/README.md +++ b/README.md @@ -436,6 +436,27 @@ manifests, and `Vagrantfile`. The Maven parser includes parents, dependencies, dependency management, plugins, plugin dependencies, plugin management, build extensions, and their profile-scoped forms. +### Replacement + +```go +type Replacement struct { + Name string // Package the directive targets + Version string // Targeted version; empty targets every version + ReplacementName string // Package used instead, when the replacement is a package + ReplacementVersion string // Version of ReplacementName, when given + Source Source // Replacement that is not a package, such as a local directory + Location string // Opaque parser-defined identity within the manifest +} +``` + +`Replacements` records directives that substitute the package or source used +for another package, in source order. `Parse` does not apply them, so +`Dependencies` still reports each requirement as written. A `go.mod` replace +only takes effect when that file is the main module, which `Parse` cannot know. +`go.mod` replace directives are recorded with a `path` source for a local +directory and a `ReplacementName` otherwise. `Declarations` omit modules named +on the left side of a replace directive. + ### ParseResult ```go @@ -449,6 +470,7 @@ type ParseResult struct { Digest string // file-level verification value, when present Dependencies []Dependency Declarations []Declaration + Replacements []Replacement // Replace or override directives, not applied Sources []Source // Ordered manifest-level source declarations Scripts map[string][]string // Hook or task names with ordered commands or paths Publication Publication // Package publication settings diff --git a/gomod_replace_test.go b/gomod_replace_test.go new file mode 100644 index 0000000..c9e6f4a --- /dev/null +++ b/gomod_replace_test.go @@ -0,0 +1,83 @@ +package manifests + +import ( + "reflect" + "testing" +) + +func TestGoModReplacements(t *testing.T) { + content := []byte(`module example.com/app + +go 1.26 + +require ( + example.com/library v1.0.0 + example.com/forked v1.2.0 + example.com/local v0.1.0 + "example.com/quoted" v2.0.0//note + example.com/kept v3.0.0 // indirect +) + +replace example.com/library => example.com/library v1.1.0 + +replace ( + example.com/forked v1.2.0 => github.com/me/forked v1.2.1 // fork + example.com/forked => github.com/me/forked v1.3.0 + example.com/local => ../local + example.com/local v0.1.0 => "C:\\src\\local" + example.com/quoted => "../dir with spaces" + example.com/versionless => example.com/other + example.com/missing => +) +`) + result, err := Parse("go.mod", content) + if err != nil { + t.Fatal(err) + } + + wantDeps := map[string]string{ + "example.com/library": "v1.0.0", + "example.com/forked": "v1.2.0", + "example.com/local": "v0.1.0", + "example.com/quoted": "v2.0.0", + "example.com/kept": "v3.0.0", + } + if len(result.Dependencies) != len(wantDeps) { + t.Fatalf("dependencies = %+v", result.Dependencies) + } + for _, dep := range result.Dependencies { + if wantDeps[dep.Name] != dep.Version { + t.Errorf("dependency %s %s, want version %q", dep.Name, dep.Version, wantDeps[dep.Name]) + } + } + + path := func(dir string) Source { return Source{Kind: SourcePath, Value: dir} } + want := []Replacement{ + {Name: "example.com/library", ReplacementName: "example.com/library", ReplacementVersion: "v1.1.0", Location: "replace/example.com%2Flibrary"}, + {Name: "example.com/forked", Version: "v1.2.0", ReplacementName: "github.com/me/forked", ReplacementVersion: "v1.2.1", Location: "replace/example.com%2Fforked"}, + {Name: "example.com/forked", ReplacementName: "github.com/me/forked", ReplacementVersion: "v1.3.0", Location: "replace/example.com%2Fforked/2"}, + {Name: "example.com/local", Source: path("../local"), Location: "replace/example.com%2Flocal"}, + {Name: "example.com/local", Version: "v0.1.0", Source: path(`C:\src\local`), Location: "replace/example.com%2Flocal/2"}, + {Name: "example.com/quoted", Source: path("../dir with spaces"), Location: "replace/example.com%2Fquoted"}, + {Name: "example.com/versionless", ReplacementName: "example.com/other", Location: "replace/example.com%2Fversionless"}, + } + if !reflect.DeepEqual(result.Replacements, want) { + t.Errorf("replacements =\n%+v\nwant\n%+v", result.Replacements, want) + } + + // Declarations still omit replaced modules, so the quoted require must + // decode to match the unquoted replace. + if len(result.Declarations) != 1 || result.Declarations[0].Name != "example.com/kept" { + t.Errorf("declarations = %+v", result.Declarations) + } +} + +func TestGoModWithoutReplacements(t *testing.T) { + result, err := Parse("go.mod", []byte("module example.com/app\n\nrequire example.com/a v1.0.0\n")) + if err != nil { + t.Fatal(err) + } + if result.Replacements != nil { + t.Errorf("replacements = %+v, want none", result.Replacements) + } +} diff --git a/internal/core/types.go b/internal/core/types.go index 4c5c38b..ef8ac96 100644 --- a/internal/core/types.go +++ b/internal/core/types.go @@ -101,6 +101,21 @@ type ArtifactIntegrity struct { Digest string } +// Replacement records a directive that substitutes the package or source used +// for another package, without applying it. Name and Version identify the +// target; an empty Version targets every version. ReplacementName and +// ReplacementVersion name the package used instead. Source holds a replacement +// that is not a package, such as a local directory. Location is +// ecosystem-specific and should be treated as an opaque identity. +type Replacement struct { + Name string + Version string + ReplacementName string + ReplacementVersion string + Source Source + Location string +} + // Declaration is a dependency-like reference at a stable logical location // in a manifest. Version is the requirement as written in that location, // before effective-model resolution or inheritance. Location is @@ -145,6 +160,8 @@ type Result struct { Digest string Dependencies []Dependency Declarations []Declaration + // Replacements holds replace or override directives in source order. + Replacements []Replacement // Sources preserves manifest-level source declarations in source order. // These entries are configuration, not evidence that any dependency was // resolved from a particular source. diff --git a/internal/golang/golang.go b/internal/golang/golang.go index d712eb8..4ab9777 100644 --- a/internal/golang/golang.go +++ b/internal/golang/golang.go @@ -3,6 +3,8 @@ package golang import ( "net/url" "regexp" + "slices" + "strconv" "strings" "github.com/git-pkgs/manifests/internal/core" @@ -28,12 +30,6 @@ type moduleVersion struct { } var ( - // Single-line require: require example.com/pkg v1.2.3 - singleRequireRegex = regexp.MustCompile(`^\s*require\s+(\S+)\s+(\S+)`) - - // Multi-line require block entry: example.com/pkg v1.2.3 // indirect - requireEntryRegex = regexp.MustCompile(`^\s*(\S+)\s+(\S+)(?:\s*//.*)?$`) - // Single-line tool: tool example.com/pkg/cmd/foo singleToolRegex = regexp.MustCompile(`^\s*tool\s+(\S+)`) @@ -44,8 +40,8 @@ var ( func (p *goModParser) Parse(filename string, content []byte) (*core.Result, error) { text := string(content) tools := collectToolPaths(text) - replaced := collectReplacedModules(text) - deps, declarations := collectRequireDeps(text, tools, replaced) + replacements := collectReplacements(text) + deps, declarations := collectRequireDeps(text, tools, replacedModules(replacements)) var modulePath string for line := range strings.SplitSeq(text, "\n") { @@ -56,7 +52,7 @@ func (p *goModParser) Parse(filename string, content []byte) (*core.Result, erro } } - return &core.Result{Name: modulePath, Dependencies: deps, Declarations: declarations}, nil + return &core.Result{Name: modulePath, Dependencies: deps, Declarations: declarations, Replacements: replacements}, nil } // collectToolPaths scans go.mod lines for tool directives (both single-line and block form) @@ -104,45 +100,47 @@ func collectToolPaths(text string) map[string]bool { func collectRequireDeps(text string, tools map[string]bool, replaced map[moduleVersion]bool) ([]core.Dependency, []core.Declaration) { var deps []core.Dependency var declarations []core.Declaration - inRequireBlock := false locations := make(map[string]int) - - for line := range strings.SplitSeq(text, "\n") { - trimmed := strings.TrimSpace(line) - - if trimmed == "" || strings.HasPrefix(trimmed, "//") { - continue - } - - if strings.HasPrefix(trimmed, "require (") || trimmed == "require (" { - inRequireBlock = true - continue - } - - if inRequireBlock && trimmed == ")" { - inRequireBlock = false + for _, entry := range goModDirectives(text, "require") { + const requireFields = 2 // path version + if len(entry.tokens) < requireFields { continue } + dep := newRequireDep(entry.tokens[0], entry.tokens[1], entry.line, tools) + deps = append(deps, dep) + appendGoDeclaration(&declarations, locations, dep, replaced) + } + return deps, declarations +} - if strings.HasPrefix(trimmed, "require ") && !strings.Contains(trimmed, "(") { - if match := singleRequireRegex.FindStringSubmatch(trimmed); match != nil { - dep := newRequireDep(match[1], match[2], line, tools) - deps = append(deps, dep) - appendGoDeclaration(&declarations, locations, dep, replaced) - } - continue - } +// goModDirective is one single-line directive or block entry, with the verb +// removed from its tokens. +type goModDirective struct { + tokens []string + line string +} - if inRequireBlock { - if match := requireEntryRegex.FindStringSubmatch(trimmed); match != nil { - dep := newRequireDep(match[1], match[2], line, tools) - deps = append(deps, dep) - appendGoDeclaration(&declarations, locations, dep, replaced) - } +// goModDirectives returns the entries of every verb directive in either +// single-line or block form. +func goModDirectives(text, verb string) []goModDirective { + var entries []goModDirective + inBlock := false + for line := range strings.SplitSeq(text, "\n") { + tokens := goModTokens(line) + switch { + case len(tokens) == 0: + case inBlock && tokens[0] == ")": + inBlock = false + case inBlock: + entries = append(entries, goModDirective{tokens: tokens, line: line}) + case tokens[0] != verb: + case len(tokens) == 2 && tokens[1] == "(": + inBlock = true + default: + entries = append(entries, goModDirective{tokens: tokens[1:], line: line}) } } - - return deps, declarations + return entries } // appendGoDeclaration records a require directive unless a replace directive @@ -167,45 +165,102 @@ func appendGoDeclaration( }) } -// collectReplacedModules returns module paths named on the left side of a -// replace directive, in either single-line or block form. -func collectReplacedModules(text string) map[moduleVersion]bool { - replaced := make(map[moduleVersion]bool) - inReplaceBlock := false - for line := range strings.SplitSeq(text, "\n") { - trimmed := strings.TrimSpace(line) - if trimmed == "" || strings.HasPrefix(trimmed, "//") { +// collectReplacements records every replace directive in source order, +// without applying it, since Go only honours replace in the main module. +func collectReplacements(text string) []core.Replacement { + var replacements []core.Replacement + locations := make(map[string]int) + for _, entry := range goModDirectives(text, "replace") { + arrow := slices.Index(entry.tokens, "=>") + if arrow < 1 || arrow == len(entry.tokens)-1 { continue } - if strings.HasPrefix(trimmed, "replace (") { - inReplaceBlock = true - continue + replacement := core.Replacement{Name: entry.tokens[0]} + if arrow > 1 { + replacement.Version = entry.tokens[1] } - if inReplaceBlock && trimmed == ")" { - inReplaceBlock = false - continue + right := entry.tokens[arrow+1:] + if isLocalReplacement(right[0]) { + replacement.Source = core.Source{Kind: core.SourcePath, Value: right[0]} + } else { + replacement.ReplacementName = right[0] + if len(right) > 1 { + replacement.ReplacementVersion = right[1] + } } + replacement.Location = core.NextLocation(locations, "replace/"+url.PathEscape(replacement.Name)) + replacements = append(replacements, replacement) + } + return replacements +} + +// replacedModules returns the left side of each replacement. +func replacedModules(replacements []core.Replacement) map[moduleVersion]bool { + replaced := make(map[moduleVersion]bool, len(replacements)) + for _, replacement := range replacements { + replaced[moduleVersion{path: replacement.Name, version: replacement.Version}] = true + } + return replaced +} - spec := "" - if strings.HasPrefix(trimmed, "replace ") && !strings.Contains(trimmed, "(") { - spec = strings.TrimSpace(strings.TrimPrefix(trimmed, "replace ")) - } else if inReplaceBlock { - spec = trimmed +// goModTokens splits a go.mod line into tokens as the go command does: tokens +// are separated by white space, double-quoted strings are decoded and "//" +// outside a quoted string begins a comment, even directly after a token. +func goModTokens(line string) []string { + var tokens []string + for { + line = strings.TrimLeft(line, " \t\r") + if line == "" || strings.HasPrefix(line, "//") { + return tokens } - left, _, ok := strings.Cut(spec, "=>") - if !ok { - continue + quoted := line[0] == '"' + end := strings.IndexAny(line, " \t\r") + if quoted { + end = quotedStringEnd(line) + } else if comment := strings.Index(line, "//"); comment >= 0 && (end < 0 || comment < end) { + end = comment + } + if end < 0 { + end = len(line) } - fields := strings.Fields(left) - if len(fields) > 0 { - module := moduleVersion{path: fields[0]} - if len(fields) > 1 { - module.version = fields[1] + token := line[:end] + if quoted { + if unquoted, err := strconv.Unquote(token); err == nil { + token = unquoted } - replaced[module] = true } + tokens = append(tokens, token) + line = line[end:] } - return replaced +} + +// quotedStringEnd returns the index just past the closing quote of the +// double-quoted string that starts s, or -1 when it is unterminated. +func quotedStringEnd(s string) int { + for i := 1; i < len(s); i++ { + switch s[i] { + case '\\': + i++ + case '"': + return i + 1 + } + } + return -1 +} + +// isLocalReplacement reports whether a replacement path names a directory. +// Like modfile.IsDirectoryPath, it accepts Unix and Windows path forms on any +// host because go.mod files move between systems. +func isLocalReplacement(path string) bool { + return path == "." || path == ".." || + strings.HasPrefix(path, "./") || strings.HasPrefix(path, "../") || + strings.HasPrefix(path, `.\`) || strings.HasPrefix(path, `..\`) || + strings.HasPrefix(path, "/") || strings.HasPrefix(path, `\`) || + len(path) >= 2 && isASCIILetter(path[0]) && path[1] == ':' +} + +func isASCIILetter(c byte) bool { + return 'a' <= c && c <= 'z' || 'A' <= c && c <= 'Z' } // newRequireDep builds a Dependency from a parsed require entry, determining diff --git a/internal/golang/golang_test.go b/internal/golang/golang_test.go index 13c42d2..156a73f 100644 --- a/internal/golang/golang_test.go +++ b/internal/golang/golang_test.go @@ -2,6 +2,7 @@ package golang import ( "os" + "slices" "testing" "github.com/git-pkgs/manifests/internal/core" @@ -76,6 +77,67 @@ func TestGoMod(t *testing.T) { if depMap["github.com/gomodule/redigo"].Scope != core.Runtime { t.Errorf("redigo scope = %v, want Runtime", depMap["github.com/gomodule/redigo"].Scope) } + + fork := "example.com/fork/net" + local := core.Source{Kind: core.SourcePath, Value: "./fork/net"} + wantReplacements := []core.Replacement{ + {Name: "bad/thing", Version: "v1.4.5", ReplacementName: "good/thing", ReplacementVersion: "v1.4.5", Location: "replace/bad%2Fthing"}, + {Name: "golang.org/x/net", Version: "v1.2.3", ReplacementName: fork, ReplacementVersion: "v1.4.5", Location: "replace/golang.org%2Fx%2Fnet"}, + {Name: "golang.org/x/net", ReplacementName: fork, ReplacementVersion: "v1.4.5", Location: "replace/golang.org%2Fx%2Fnet/2"}, + {Name: "golang.org/x/net", Version: "v1.2.3", Source: local, Location: "replace/golang.org%2Fx%2Fnet/3"}, + {Name: "golang.org/x/net", Source: local, Location: "replace/golang.org%2Fx%2Fnet/4"}, + } + if !slices.Equal(res.Replacements, wantReplacements) { + t.Errorf("replacements = %+v, want %+v", res.Replacements, wantReplacements) + } +} + +func TestGoModTokens(t *testing.T) { + for line, want := range map[string][]string{ + `example.com/a v1 => example.com/b v2`: {"example.com/a", "v1", "=>", "example.com/b", "v2"}, + `a => "../dir with spaces" // comment`: {"a", "=>", "../dir with spaces"}, + "a\t=>\t\"quoted\\\"name\" v1": {"a", "=>", `quoted"name`, "v1"}, + `a => "unterminated`: {"a", "=>", `"unterminated`}, + `// only a comment`: nil, + `a=>b`: {"a=>b"}, + `a => b v1.1.0//note`: {"a", "=>", "b", "v1.1.0"}, + `a => b//note`: {"a", "=>", "b"}, + `a => "x//y"//note`: {"a", "=>", "x//y"}, + } { + if got := goModTokens(line); !slices.Equal(got, want) { + t.Errorf("goModTokens(%q) = %q, want %q", line, got, want) + } + } +} + +func TestIsLocalReplacement(t *testing.T) { + for path, want := range map[string]bool{ + ".": true, "..": true, "./fork": true, "../fork": true, `.\fork`: true, `..\fork`: true, + "/src/absolute": true, `\src\fork`: true, "C:/src/fork": true, `c:\src\fork`: true, + "example.com/fork": false, "github.com/me/fork": false, "fork": false, "1:fork": false, + } { + if got := isLocalReplacement(path); got != want { + t.Errorf("isLocalReplacement(%q) = %v, want %v", path, got, want) + } + } +} + +func TestGoModQuotedRequire(t *testing.T) { + content := []byte("module example.com/app\n\nrequire \"example.com/a\" v1.0.0//note\n\nrequire (\n\t\"example.com/b\" v2.0.0//note\n\texample.com/c v3.0.0// indirect\n)\n") + res, err := (&goModParser{}).Parse("go.mod", content) + if err != nil { + t.Fatal(err) + } + want := []core.Dependency{ + {Name: "example.com/a", Version: "v1.0.0", Scope: core.Runtime, Direct: true}, + {Name: "example.com/b", Version: "v2.0.0", Scope: core.Runtime, Direct: true}, + {Name: "example.com/c", Version: "v3.0.0", Scope: core.Runtime, Direct: false}, + } + if !slices.EqualFunc(res.Dependencies, want, func(a, b core.Dependency) bool { + return a.Name == b.Name && a.Version == b.Version && a.Scope == b.Scope && a.Direct == b.Direct + }) { + t.Errorf("dependencies = %+v, want %+v", res.Dependencies, want) + } } func TestGoModDeclarations(t *testing.T) { diff --git a/manifests.go b/manifests.go index 9147b1b..a7c496a 100644 --- a/manifests.go +++ b/manifests.go @@ -47,6 +47,11 @@ type ArtifactIntegrity = core.ArtifactIntegrity // metadata fields remain source-compatible. type Declaration = core.Declaration +// Replacement records a directive, such as a go.mod replace, that substitutes +// the package or source used for another package. Parse does not apply it, +// because whether it takes effect depends on how the manifest is used. +type Replacement = core.Replacement + // Source preserves a literal manifest source declaration. It does not report // a resolved package location. type Source = core.Source @@ -103,6 +108,10 @@ type ParseResult struct { // their logical locations. Unlike Dependencies, these entries are not // merged, inherited, or otherwise resolved into an effective model. Declarations []Declaration + // Replacements holds replace or override directives in source order. + // Dependencies are reported as written, so a caller that knows the + // directives apply can substitute them itself. + Replacements []Replacement // Sources preserves manifest-level source declarations in source order. // A source declaration does not imply that any dependency resolved there. Sources []Source @@ -175,6 +184,7 @@ func Parse(filename string, content []byte, opts ...Options) (*ParseResult, erro Digest: res.Digest, Dependencies: res.Dependencies, Declarations: res.Declarations, + Replacements: res.Replacements, Sources: res.Sources, Scripts: res.Scripts, Publication: res.Publication, From f95130c30b21a1103d668b767ab36868f98a5d73 Mon Sep 17 00:00:00 2001 From: abhinavgautam01 Date: Mon, 16 Nov 2026 09:53:06 +0530 Subject: [PATCH 2/2] Tokenize go.mod lines in linear time and handle parentheses as tokens --- gomod_replace_test.go | 67 ++++++++++++++++++++++++++++++++++ internal/core/types.go | 7 +--- internal/golang/golang.go | 30 ++++++++++++--- internal/golang/golang_test.go | 4 ++ 4 files changed, 98 insertions(+), 10 deletions(-) diff --git a/gomod_replace_test.go b/gomod_replace_test.go index c9e6f4a..92cb493 100644 --- a/gomod_replace_test.go +++ b/gomod_replace_test.go @@ -2,7 +2,9 @@ package manifests import ( "reflect" + "strings" "testing" + "time" ) func TestGoModReplacements(t *testing.T) { @@ -81,3 +83,68 @@ func TestGoModWithoutReplacements(t *testing.T) { t.Errorf("replacements = %+v, want none", result.Replacements) } } + +func TestGoModParenthesisTokens(t *testing.T) { + content := []byte(`module example.com/app + +require( + example.com/a v1.0.0 +) + +require ( ) + +require() + +replace( + example.com/a => ../a +) + +replace ( ) + +replace() +`) + result, err := Parse("go.mod", content) + if err != nil { + t.Fatal(err) + } + if len(result.Dependencies) != 1 || result.Dependencies[0].Name != "example.com/a" || result.Dependencies[0].Version != "v1.0.0" { + t.Errorf("dependencies = %+v", result.Dependencies) + } + want := []Replacement{{Name: "example.com/a", Source: Source{Kind: SourcePath, Value: "../a"}, Location: "replace/example.com%2Fa"}} + if !reflect.DeepEqual(result.Replacements, want) { + t.Errorf("replacements = %+v, want %+v", result.Replacements, want) + } + if len(result.Declarations) != 0 { + t.Errorf("declarations = %+v, want none", result.Declarations) + } +} + +func TestGoModEmptyOneLineBlockKeepsLaterDirectives(t *testing.T) { + content := []byte("module example.com/app\n\nrequire ( )\nrequire example.com/b v2.0.0\n\nreplace ( )\nreplace example.com/b => ../b\n") + result, err := Parse("go.mod", content) + if err != nil { + t.Fatal(err) + } + if len(result.Dependencies) != 1 || result.Dependencies[0].Name != "example.com/b" { + t.Errorf("dependencies = %+v", result.Dependencies) + } + if len(result.Replacements) != 1 || result.Replacements[0].Name != "example.com/b" { + t.Errorf("replacements = %+v", result.Replacements) + } +} + +func TestGoModLongLine(t *testing.T) { + const tokens = 1 << 20 + content := []byte("module example.com/app\n\nrequire example.com/a v1.0.0" + strings.Repeat(" a", tokens) + "\n") + started := time.Now() + result, err := Parse("go.mod", content) + if err != nil { + t.Fatal(err) + } + if elapsed := time.Since(started); elapsed > 5*time.Second { + t.Errorf("parsing a %d-token line took %v", tokens, elapsed) + } + if len(result.Dependencies) != 1 || result.Dependencies[0].Version != "v1.0.0" { + t.Errorf("dependencies = %+v", result.Dependencies) + } +} diff --git a/internal/core/types.go b/internal/core/types.go index ef8ac96..a413a8b 100644 --- a/internal/core/types.go +++ b/internal/core/types.go @@ -102,11 +102,8 @@ type ArtifactIntegrity struct { } // Replacement records a directive that substitutes the package or source used -// for another package, without applying it. Name and Version identify the -// target; an empty Version targets every version. ReplacementName and -// ReplacementVersion name the package used instead. Source holds a replacement -// that is not a package, such as a local directory. Location is -// ecosystem-specific and should be treated as an opaque identity. +// for another package, without applying it. Location is ecosystem-specific and +// should be treated as an opaque identity. type Replacement struct { Name string Version string diff --git a/internal/golang/golang.go b/internal/golang/golang.go index 4ab9777..3b75b6d 100644 --- a/internal/golang/golang.go +++ b/internal/golang/golang.go @@ -136,6 +136,8 @@ func goModDirectives(text, verb string) []goModDirective { case tokens[0] != verb: case len(tokens) == 2 && tokens[1] == "(": inBlock = true + case len(tokens) == 3 && tokens[1] == "(" && tokens[2] == ")": + // An empty block on one line has no entries. default: entries = append(entries, goModDirective{tokens: tokens[1:], line: line}) } @@ -204,8 +206,9 @@ func replacedModules(replacements []core.Replacement) map[moduleVersion]bool { } // goModTokens splits a go.mod line into tokens as the go command does: tokens -// are separated by white space, double-quoted strings are decoded and "//" -// outside a quoted string begins a comment, even directly after a token. +// are separated by white space, unquoted parentheses are tokens of their own, +// double-quoted strings are decoded and "//" outside a quoted string begins a +// comment, even directly after a token. func goModTokens(line string) []string { var tokens []string for { @@ -214,11 +217,11 @@ func goModTokens(line string) []string { return tokens } quoted := line[0] == '"' - end := strings.IndexAny(line, " \t\r") + var end int if quoted { end = quotedStringEnd(line) - } else if comment := strings.Index(line, "//"); comment >= 0 && (end < 0 || comment < end) { - end = comment + } else { + end = unquotedTokenEnd(line) } if end < 0 { end = len(line) @@ -234,6 +237,23 @@ func goModTokens(line string) []string { } } +// unquotedTokenEnd returns the length of the unquoted token that starts s, +// stopping at white space, a parenthesis or "//". A leading parenthesis is a +// one-byte token. It stops at the token end, so tokenizing stays linear. +func unquotedTokenEnd(s string) int { + for i := 0; i < len(s); i++ { + switch c := s[i]; { + case c == '(' || c == ')': + return max(i, 1) + case c == ' ' || c == '\t' || c == '\r': + return i + case c == '/' && i+1 < len(s) && s[i+1] == '/': + return i + } + } + return len(s) +} + // quotedStringEnd returns the index just past the closing quote of the // double-quoted string that starts s, or -1 when it is unterminated. func quotedStringEnd(s string) int { diff --git a/internal/golang/golang_test.go b/internal/golang/golang_test.go index 156a73f..e543f55 100644 --- a/internal/golang/golang_test.go +++ b/internal/golang/golang_test.go @@ -103,6 +103,10 @@ func TestGoModTokens(t *testing.T) { `a => b v1.1.0//note`: {"a", "=>", "b", "v1.1.0"}, `a => b//note`: {"a", "=>", "b"}, `a => "x//y"//note`: {"a", "=>", "x//y"}, + `require(`: {"require", "("}, + `require ( )`: {"require", "(", ")"}, + `replace()// note`: {"replace", "(", ")"}, + `a => "../(dir)"`: {"a", "=>", "../(dir)"}, } { if got := goModTokens(line); !slices.Equal(got, want) { t.Errorf("goModTokens(%q) = %q, want %q", line, got, want)