From bc500863d40de26e55ea881ed2d83115d0def85d Mon Sep 17 00:00:00 2001 From: abhinavgautam01 Date: Fri, 13 Nov 2026 15:34:57 +0530 Subject: [PATCH 1/3] Apply go.mod replace directives to parsed dependencies --- README.md | 2 + internal/golang/golang.go | 99 +++++++++++++++++++++++----- internal/golang/golang_test.go | 115 +++++++++++++++++++++++++++++++-- 3 files changed, 194 insertions(+), 22 deletions(-) diff --git a/README.md b/README.md index c31bc48..ef24f6b 100644 --- a/README.md +++ b/README.md @@ -203,6 +203,8 @@ replacement can install a different source than the declared repository. **Supplement files:** go.sum is parsed as a supplement rather than a lockfile. It provides integrity hashes that can be matched against go.mod dependencies by name and version, but it doesn't represent a standalone dependency tree. +**Replace directives:** go.mod dependencies report the module Go builds with. A `replace` directive that names a module and version substitutes that module and version, with a version-specific replacement taking precedence over one for every version. Dependencies replaced by a local directory are omitted because they have no published version. `Declarations` keep the require directives that are not replaced. + ## API ### Parse diff --git a/internal/golang/golang.go b/internal/golang/golang.go index d712eb8..a2cdf52 100644 --- a/internal/golang/golang.go +++ b/internal/golang/golang.go @@ -2,6 +2,7 @@ package golang import ( "net/url" + "path/filepath" "regexp" "strings" @@ -27,6 +28,14 @@ type moduleVersion struct { version string } +// moduleReplacement is the right side of a replace directive. A local +// replacement names a directory and has no version. +type moduleReplacement struct { + path string + version string + local bool +} + var ( // Single-line require: require example.com/pkg v1.2.3 singleRequireRegex = regexp.MustCompile(`^\s*require\s+(\S+)\s+(\S+)`) @@ -101,7 +110,8 @@ func collectToolPaths(text string) map[string]bool { // collectRequireDeps scans go.mod lines for require directives (both single-line and block form) // and returns dependencies, marking tool-related modules as development scope. -func collectRequireDeps(text string, tools map[string]bool, replaced map[moduleVersion]bool) ([]core.Dependency, []core.Declaration) { +// Dependencies reflect replace directives; declarations keep the require form. +func collectRequireDeps(text string, tools map[string]bool, replaced map[moduleVersion]moduleReplacement) ([]core.Dependency, []core.Declaration) { var deps []core.Dependency var declarations []core.Declaration inRequireBlock := false @@ -127,7 +137,7 @@ func collectRequireDeps(text string, tools map[string]bool, replaced map[moduleV 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) + deps = appendReplacedDep(deps, dep, replaced) appendGoDeclaration(&declarations, locations, dep, replaced) } continue @@ -136,7 +146,7 @@ func collectRequireDeps(text string, tools map[string]bool, replaced map[moduleV if inRequireBlock { if match := requireEntryRegex.FindStringSubmatch(trimmed); match != nil { dep := newRequireDep(match[1], match[2], line, tools) - deps = append(deps, dep) + deps = appendReplacedDep(deps, dep, replaced) appendGoDeclaration(&declarations, locations, dep, replaced) } } @@ -145,16 +155,44 @@ func collectRequireDeps(text string, tools map[string]bool, replaced map[moduleV return deps, declarations } +// findReplacement returns the replace directive that applies to a required +// module. A replacement naming the version takes precedence over one that +// applies to every version. +func findReplacement(replaced map[moduleVersion]moduleReplacement, path, version string) (moduleReplacement, bool) { + if replacement, ok := replaced[moduleVersion{path: path, version: version}]; ok { + return replacement, true + } + replacement, ok := replaced[moduleVersion{path: path}] + return replacement, ok +} + +// appendReplacedDep appends dependency as Go builds it. A module replacement +// supplies the module path and version. A local replacement has no version, +// so the dependency is omitted. +func appendReplacedDep(deps []core.Dependency, dependency core.Dependency, replaced map[moduleVersion]moduleReplacement) []core.Dependency { + replacement, ok := findReplacement(replaced, dependency.Name, dependency.Version) + if !ok { + return append(deps, dependency) + } + if replacement.local { + return deps + } + if replacement.version != "" { + dependency.Name = replacement.path + dependency.Version = replacement.version + } + return append(deps, dependency) +} + // appendGoDeclaration records a require directive unless a replace directive // changes that module's source. func appendGoDeclaration( declarations *[]core.Declaration, locations map[string]int, dependency core.Dependency, - replaced map[moduleVersion]bool, + replaced map[moduleVersion]moduleReplacement, ) { - if replaced[moduleVersion{path: dependency.Name}] || - replaced[moduleVersion{path: dependency.Name, version: dependency.Version}] { + if _, ok := findReplacement(replaced, dependency.Name, dependency.Version); ok { return } location := core.NextLocation(locations, "require/"+url.PathEscape(dependency.Name)) @@ -167,10 +205,11 @@ 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) +// collectReplacedModules maps the left side of each replace directive, in +// either single-line or block form, to its replacement. Go rejects conflicting +// replacements; the first one is kept. +func collectReplacedModules(text string) map[moduleVersion]moduleReplacement { + replaced := make(map[moduleVersion]moduleReplacement) inReplaceBlock := false for line := range strings.SplitSeq(text, "\n") { trimmed := strings.TrimSpace(line) @@ -192,22 +231,48 @@ func collectReplacedModules(text string) map[moduleVersion]bool { } else if inReplaceBlock { spec = trimmed } - left, _, ok := strings.Cut(spec, "=>") + left, right, ok := strings.Cut(spec, "=>") if !ok { continue } fields := strings.Fields(left) - if len(fields) > 0 { - module := moduleVersion{path: fields[0]} - if len(fields) > 1 { - module.version = fields[1] - } - replaced[module] = true + if len(fields) == 0 { + continue + } + module := moduleVersion{path: fields[0]} + if len(fields) > 1 { + module.version = fields[1] + } + if _, exists := replaced[module]; !exists { + replaced[module] = parseReplacement(right) } } return replaced } +// parseReplacement parses the right side of a replace directive. +func parseReplacement(right string) moduleReplacement { + right, _, _ = strings.Cut(right, "//") + fields := strings.Fields(right) + if len(fields) == 0 { + return moduleReplacement{} + } + replacement := moduleReplacement{path: fields[0], local: isLocalReplacement(fields[0])} + if len(fields) > 1 && !replacement.local { + replacement.version = fields[1] + } + return replacement +} + +// isLocalReplacement reports whether a replacement path names a directory, +// following the go.mod rule for relative and absolute paths. +func isLocalReplacement(path string) bool { + return path == "." || path == ".." || + strings.HasPrefix(path, "./") || strings.HasPrefix(path, "../") || + strings.HasPrefix(path, `.\`) || strings.HasPrefix(path, `..\`) || + filepath.IsAbs(path) +} + // newRequireDep builds a Dependency from a parsed require entry, determining // scope based on whether the module is used by a tool directive. func newRequireDep(name, version, rawLine string, tools map[string]bool) core.Dependency { diff --git a/internal/golang/golang_test.go b/internal/golang/golang_test.go index 13c42d2..a3368c1 100644 --- a/internal/golang/golang_test.go +++ b/internal/golang/golang_test.go @@ -52,11 +52,15 @@ func TestGoMod(t *testing.T) { } } - // Check single-line require - if net, ok := depMap["golang.org/x/net"]; !ok { - t.Error("expected golang.org/x/net dependency") - } else if net.Version != "v1.2.3" { - t.Errorf("net version = %q, want %q", net.Version, "v1.2.3") + // The single-line require of golang.org/x/net v1.2.3 is replaced by the + // first matching replace directive. + if _, ok := depMap["golang.org/x/net"]; ok { + t.Error("golang.org/x/net should be reported as its replacement") + } + if net, ok := depMap["example.com/fork/net"]; !ok { + t.Error("expected example.com/fork/net dependency") + } else if net.Version != "v1.4.5" || !net.Direct { + t.Errorf("fork/net = %+v, want direct v1.4.5", net) } // Check tool dependencies are marked as Development scope @@ -136,6 +140,107 @@ replace ( } } +func TestGoModReplaceDirectives(t *testing.T) { + content := []byte(`module example.com/application + +go 1.26 + +require ( + example.com/same-path v1.0.0 + example.com/forked v1.2.0 // indirect + example.com/pinned v2.0.0 + example.com/unpinned v3.0.0 + example.com/local v0.3.0 + example.com/local-version v0.4.0 + example.com/absolute v0.5.0 + example.com/tool v6.0.0 + example.com/untouched v7.0.0 + example.com/missing-version v8.0.0 +) + +tool example.com/tool/cmd/tool + +replace example.com/same-path => example.com/same-path v1.1.0 + +replace ( + example.com/forked v1.2.0 => github.com/me/forked v1.2.1 // patched fork + example.com/pinned v1.9.0 => example.com/pinned v1.9.9 + example.com/pinned v2.0.0 => example.com/pinned v2.0.1 + example.com/pinned => example.com/pinned v9.9.9 + example.com/unpinned v2.0.0 => example.com/unpinned v2.0.1 + example.com/local => ../local + example.com/local-version v0.4.0 => ./vendor/local + example.com/absolute => /src/absolute + example.com/tool => github.com/me/tool v6.0.1 + example.com/missing-version => example.com/elsewhere // no version +) +`) + + result, err := (&goModParser{}).Parse("go.mod", content) + if err != nil { + t.Fatalf("Parse failed: %v", err) + } + + got := make(map[string]core.Dependency) + for _, dep := range result.Dependencies { + if _, dup := got[dep.Name]; dup { + t.Errorf("duplicate dependency %s", dep.Name) + } + got[dep.Name] = dep + } + want := map[string]struct { + version string + scope core.Scope + direct bool + }{ + "example.com/same-path": {"v1.1.0", core.Runtime, true}, + "github.com/me/forked": {"v1.2.1", core.Runtime, false}, + "example.com/pinned": {"v2.0.1", core.Runtime, true}, + "example.com/unpinned": {"v3.0.0", core.Runtime, true}, + "github.com/me/tool": {"v6.0.1", core.Development, true}, + "example.com/untouched": {"v7.0.0", core.Runtime, true}, + "example.com/missing-version": {"v8.0.0", core.Runtime, true}, + } + if len(got) != len(want) { + t.Fatalf("Dependencies = %+v, want %d entries", result.Dependencies, len(want)) + } + for name, expected := range want { + dep, ok := got[name] + if !ok { + t.Errorf("missing dependency %s", name) + continue + } + if dep.Version != expected.version || dep.Scope != expected.scope || dep.Direct != expected.direct { + t.Errorf("%s = %+v, want %+v", name, dep, expected) + } + } + for _, name := range []string{"example.com/local", "example.com/local-version", "example.com/absolute", "example.com/forked", "example.com/tool"} { + if _, ok := got[name]; ok { + t.Errorf("%s should be replaced or omitted", name) + } + } + + // Declarations keep the require form and omit replaced modules. + declared := make(map[string]bool) + for _, declaration := range result.Declarations { + declared[declaration.Name] = true + } + if len(declared) != 2 || !declared["example.com/unpinned"] || !declared["example.com/untouched"] { + t.Errorf("Declarations = %+v, want unpinned and untouched", result.Declarations) + } +} + +func TestIsLocalReplacement(t *testing.T) { + for path, want := range map[string]bool{ + ".": true, "..": true, "./fork": true, "../fork": true, `.\fork`: true, `..\fork`: true, + "example.com/fork": false, "github.com/me/fork": false, "fork": false, + } { + if got := isLocalReplacement(path); got != want { + t.Errorf("isLocalReplacement(%q) = %v, want %v", path, got, want) + } + } +} + func TestGoSum(t *testing.T) { content, err := os.ReadFile("../../testdata/golang/go.sum") if err != nil { From a314f3ebbdf7afad99632180922baeaecaf8d0e3 Mon Sep 17 00:00:00 2001 From: abhinavgautam01 Date: Fri, 13 Nov 2026 16:40:51 +0530 Subject: [PATCH 2/3] Decode quoted replace paths and detect local paths lexically --- internal/golang/golang.go | 93 +++++++++++++++++++++++++--------- internal/golang/golang_test.go | 57 ++++++++++++++++++++- 2 files changed, 125 insertions(+), 25 deletions(-) diff --git a/internal/golang/golang.go b/internal/golang/golang.go index a2cdf52..80cbbef 100644 --- a/internal/golang/golang.go +++ b/internal/golang/golang.go @@ -2,8 +2,9 @@ package golang import ( "net/url" - "path/filepath" "regexp" + "slices" + "strconv" "strings" "github.com/git-pkgs/manifests/internal/core" @@ -226,51 +227,95 @@ func collectReplacedModules(text string) map[moduleVersion]moduleReplacement { } spec := "" - if strings.HasPrefix(trimmed, "replace ") && !strings.Contains(trimmed, "(") { - spec = strings.TrimSpace(strings.TrimPrefix(trimmed, "replace ")) + if strings.HasPrefix(trimmed, "replace ") { + spec = strings.TrimPrefix(trimmed, "replace ") } else if inReplaceBlock { spec = trimmed } - left, right, ok := strings.Cut(spec, "=>") - if !ok { + tokens := goModTokens(spec) + arrow := slices.Index(tokens, "=>") + if arrow < 1 { continue } - fields := strings.Fields(left) - if len(fields) == 0 { - continue - } - module := moduleVersion{path: fields[0]} - if len(fields) > 1 { - module.version = fields[1] + module := moduleVersion{path: tokens[0]} + if arrow > 1 { + module.version = tokens[1] } if _, exists := replaced[module]; !exists { - replaced[module] = parseReplacement(right) + replaced[module] = parseReplacement(tokens[arrow+1:]) } } return replaced } -// parseReplacement parses the right side of a replace directive. -func parseReplacement(right string) moduleReplacement { - right, _, _ = strings.Cut(right, "//") - fields := strings.Fields(right) - if len(fields) == 0 { +// parseReplacement parses the tokens on the right side of a replace directive. +func parseReplacement(tokens []string) moduleReplacement { + if len(tokens) == 0 { return moduleReplacement{} } - replacement := moduleReplacement{path: fields[0], local: isLocalReplacement(fields[0])} - if len(fields) > 1 && !replacement.local { - replacement.version = fields[1] + replacement := moduleReplacement{path: tokens[0], local: isLocalReplacement(tokens[0])} + if len(tokens) > 1 && !replacement.local { + replacement.version = tokens[1] } return replacement } -// isLocalReplacement reports whether a replacement path names a directory, -// following the go.mod rule for relative and absolute paths. +// 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 a token +// starting with "//" begins a comment. +func goModTokens(line string) []string { + var tokens []string + for { + line = strings.TrimLeft(line, " \t\r") + if line == "" || strings.HasPrefix(line, "//") { + return tokens + } + quoted := line[0] == '"' + end := strings.IndexAny(line, " \t\r") + if quoted { + end = quotedStringEnd(line) + } + if end < 0 { + end = len(line) + } + token := line[:end] + if quoted { + if unquoted, err := strconv.Unquote(token); err == nil { + token = unquoted + } + } + tokens = append(tokens, token) + line = line[end:] + } +} + +// 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, `..\`) || - filepath.IsAbs(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 a3368c1..7524c64 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" @@ -230,10 +231,64 @@ replace ( } } +func TestGoModQuotedReplaceDirectives(t *testing.T) { + content := []byte(`module example.com/application + +go 1.26 + +require ( + example.com/quoted-fork v1.0.0 + example.com/quoted-local v1.0.0 + example.com/paren-local v1.0.0 + example.com/slashes v1.0.0 + example.com/windows v1.0.0 +) + +replace example.com/quoted-fork => "example.com/fork" "v1.1.0" // quoted "fork" +replace "example.com/quoted-local" => "../local fork" // spaces in a quoted path +replace example.com/paren-local => "../local (copy)" +replace example.com/slashes => "example.com/a//b" v1.2.0 +replace example.com/windows => C:/src/windows +`) + + result, err := (&goModParser{}).Parse("go.mod", content) + if err != nil { + t.Fatalf("Parse failed: %v", err) + } + want := map[string]string{"example.com/fork": "v1.1.0", "example.com/a//b": "v1.2.0"} + if len(result.Dependencies) != len(want) { + t.Fatalf("Dependencies = %+v, want %v", result.Dependencies, want) + } + for _, dep := range result.Dependencies { + if want[dep.Name] != dep.Version { + t.Errorf("dependency %s %s, want one of %v", dep.Name, dep.Version, want) + } + } + if len(result.Declarations) != 0 { + t.Errorf("Declarations = %+v, want none for replaced modules", result.Declarations) + } +} + +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"}, + } { + 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, - "example.com/fork": false, "github.com/me/fork": false, "fork": false, + "/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) From d819f586432ba5f10295aa2871d15a0a89098311 Mon Sep 17 00:00:00 2001 From: abhinavgautam01 Date: Fri, 13 Nov 2026 16:53:10 +0530 Subject: [PATCH 3/3] End unquoted go.mod tokens at adjacent comments --- internal/golang/golang.go | 6 ++++-- internal/golang/golang_test.go | 11 ++++++++++- 2 files changed, 14 insertions(+), 3 deletions(-) diff --git a/internal/golang/golang.go b/internal/golang/golang.go index 80cbbef..fb827d1 100644 --- a/internal/golang/golang.go +++ b/internal/golang/golang.go @@ -261,8 +261,8 @@ func parseReplacement(tokens []string) moduleReplacement { } // 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 a token -// starting with "//" begins a comment. +// 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 { @@ -274,6 +274,8 @@ func goModTokens(line string) []string { 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) diff --git a/internal/golang/golang_test.go b/internal/golang/golang_test.go index 7524c64..5c0b722 100644 --- a/internal/golang/golang_test.go +++ b/internal/golang/golang_test.go @@ -242,6 +242,7 @@ require ( example.com/paren-local v1.0.0 example.com/slashes v1.0.0 example.com/windows v1.0.0 + example.com/adjacent-comment v1.0.0 ) replace example.com/quoted-fork => "example.com/fork" "v1.1.0" // quoted "fork" @@ -249,13 +250,18 @@ replace "example.com/quoted-local" => "../local fork" // spaces in a quoted path replace example.com/paren-local => "../local (copy)" replace example.com/slashes => "example.com/a//b" v1.2.0 replace example.com/windows => C:/src/windows +replace example.com/adjacent-comment => example.com/adjacent-comment v1.3.0//note `) result, err := (&goModParser{}).Parse("go.mod", content) if err != nil { t.Fatalf("Parse failed: %v", err) } - want := map[string]string{"example.com/fork": "v1.1.0", "example.com/a//b": "v1.2.0"} + want := map[string]string{ + "example.com/fork": "v1.1.0", + "example.com/a//b": "v1.2.0", + "example.com/adjacent-comment": "v1.3.0", + } if len(result.Dependencies) != len(want) { t.Fatalf("Dependencies = %+v, want %v", result.Dependencies, want) } @@ -277,6 +283,9 @@ func TestGoModTokens(t *testing.T) { `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)