Skip to content

Commit 5a7a9d9

Browse files
silverwindclaude
andcommitted
Remove matching replace directives when updating go.mod dependencies
Co-Authored-By: Claude Opus 4.6 <noreply@anthropic.com>
1 parent b4664ad commit 5a7a9d9

File tree

3 files changed

+20
-0
lines changed

3 files changed

+20
-0
lines changed

fixtures/go-update/go.mod

Lines changed: 6 additions & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

index.test.ts

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1167,6 +1167,8 @@ test.concurrent("go update", async ({expect = globalExpect}: any = {}) => {
11671167
expect(matches).toBeTruthy();
11681168
expect(matches?.length).toBe(4);
11691169

1170+
expect(updatedContent).not.toContain("replace");
1171+
11701172
const updatedMain = await readFile(join(testGoModDir, "main.go"), "utf8");
11711173
expect(updatedMain).not.toContain("go-github/v70");
11721174
expect(updatedMain).toMatch(/go-github\/v\d+\/github/);

index.ts

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -971,6 +971,17 @@ function updatePyprojectToml(pkgStr: string, deps: Deps): string {
971971
return newPkgStr;
972972
}
973973

974+
function removeGoReplace(content: string, name: string): string {
975+
const e = esc(name);
976+
// Remove single-line: replace <name> [version] => <replacement> [version]
977+
content = content.replace(new RegExp(`^replace\\s+${e}(\\s+v\\S+)?\\s+=>\\s+\\S+(\\s+v\\S+)?\\s*\\n`, "gm"), "");
978+
// Remove entry from replace block
979+
content = content.replace(new RegExp(`^\\s+${e}(\\s+v\\S+)?\\s+=>\\s+\\S+(\\s+v\\S+)?\\s*\\n`, "gm"), "");
980+
// Remove empty replace blocks
981+
content = content.replace(/^replace\s*\(\s*\)\s*\n/gm, "");
982+
return content;
983+
}
984+
974985
function updateGoMod(pkgStr: string, deps: Deps): [string, Record<string, string>] {
975986
let newPkgStr = pkgStr;
976987
const majorVersionRewrites: Record<string, string> = {};
@@ -988,6 +999,7 @@ function updateGoMod(pkgStr: string, deps: Deps): [string, Record<string, string
988999
} else {
9891000
newPkgStr = newPkgStr.replace(new RegExp(`(${esc(name)}) +v${esc(oldValue)}`, "g"), `$1 v${newValue}`);
9901001
}
1002+
newPkgStr = removeGoReplace(newPkgStr, name);
9911003
}
9921004
return [newPkgStr, majorVersionRewrites];
9931005
}

0 commit comments

Comments
 (0)