Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix(manager/gomod): add multi-line replace detection #17111

Merged
merged 12 commits into from
Aug 11, 2022
73 changes: 73 additions & 0 deletions lib/modules/manager/gomod/extract.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -40,5 +40,78 @@ describe('modules/manager/gomod/extract', () => {
expect(res).toHaveLength(58);
expect(res?.filter((e) => e.skipReason)).toHaveLength(0);
});

it('extracts replace directives from multi-line and single line', () => {
const goMod = `
module github.com/renovate-tests/gomod
go 1.18
replace golang.org/x/foo => github.com/pravesht/gocql v0.0.0
replace (
k8s.io/client-go => k8s.io/client-go v0.21.9
)
replace (
k8s.io/cloud-provider => k8s.io/cloud-provider v0.17.3
k8s.io/cluster-bootstrap => k8s.io/cluster-bootstrap v0.17.3 // indirect
k8s.io/code-generator => k8s.io/code-generator v0.17.3
)`;
const res = extractPackageFile(goMod);
expect(res).toEqual({
constraints: {
go: '^1.18',
},
deps: [
{
managerData: {
lineNumber: 2,
},
depName: 'go',
depType: 'golang',
currentValue: '1.18',
datasource: 'golang-version',
versioning: 'npm',
rangeStrategy: 'replace',
},
{
managerData: {
lineNumber: 3,
},
depName: 'github.com/pravesht/gocql',
depType: 'replace',
currentValue: 'v0.0.0',
datasource: 'go',
},
{
managerData: {
lineNumber: 5,
multiLine: true,
},
depName: 'k8s.io/client-go',
depType: 'replace',
currentValue: 'v0.21.9',
datasource: 'go',
},
{
managerData: {
lineNumber: 8,
multiLine: true,
},
depName: 'k8s.io/cloud-provider',
depType: 'replace',
currentValue: 'v0.17.3',
datasource: 'go',
},
{
managerData: {
lineNumber: 10,
multiLine: true,
},
depName: 'k8s.io/code-generator',
depType: 'replace',
currentValue: 'v0.17.3',
datasource: 'go',
},
],
});
});
});
});
11 changes: 11 additions & 0 deletions lib/modules/manager/gomod/extract.ts
Original file line number Diff line number Diff line change
Expand Up @@ -88,6 +88,17 @@ export function extractPackageFile(content: string): PackageFile | null {
);
lineNumber = reachedLine;
deps.push(...detectedDeps);
} else if (line.trim() === 'replace (') {
logger.trace(`Matched multi-line replace on line ${lineNumber}`);
const matcher = regEx(/^\s+[^\s]+[\s]+[=][>]\s+([^\s]+)\s+([^\s]+)/);
const { reachedLine, detectedDeps } = parseMultiLine(
lineNumber,
lines,
matcher,
'replace'
);
lineNumber = reachedLine;
deps.push(...detectedDeps);
}
}
} catch (err) /* istanbul ignore next */ {
Expand Down
20 changes: 20 additions & 0 deletions lib/modules/manager/gomod/update.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -318,6 +318,26 @@ describe('modules/manager/gomod/update', () => {
expect(res).toContain('github.com/caarlos0/env/v6 v6.1.0');
});

it('handles multiline replace update', () => {
const fileContent = `
go 1.18
replace (
k8s.io/client-go => k8s.io/client-go v0.21.9
)`;
const upgrade = {
depName: 'k8s.io/client-go',
managerData: { lineNumber: 3, multiLine: true },
newValue: 'v2.2.2',
depType: 'replace',
currentValue: 'v0.21.9',
newMajor: 2,
updateType: 'major' as UpdateType,
};
const res = updateDependency({ fileContent, upgrade });
expect(res).not.toEqual(fileContent);
expect(res).toContain('k8s.io/client-go/v2 => k8s.io/client-go v2.2.2');
});

it('should return null for replacement', () => {
const res = updateDependency({
fileContent: '',
Expand Down
12 changes: 9 additions & 3 deletions lib/modules/manager/gomod/update.ts
Original file line number Diff line number Diff line change
Expand Up @@ -46,9 +46,15 @@ export function updateDependency({
updateLineExp = regEx(/(?<depPart>go)(?<divider>\s+)[^\s]+/);
}
if (depType === 'replace') {
updateLineExp = regEx(
/^(?<depPart>replace\s+[^\s]+[\s]+[=][>]+\s+)(?<divider>[^\s]+\s+)[^\s]+/
);
if (upgrade.managerData.multiLine) {
updateLineExp = regEx(
/^(?<depPart>\s+[^\s]+[\s]+[=][>]+\s+)(?<divider>[^\s]+\s+)[^\s]+/
);
} else {
updateLineExp = regEx(
/^(?<depPart>replace\s+[^\s]+[\s]+[=][>]+\s+)(?<divider>[^\s]+\s+)[^\s]+/
rarkins marked this conversation as resolved.
Show resolved Hide resolved
);
}
} else if (depType === 'require') {
if (upgrade.managerData.multiLine) {
updateLineExp = regEx(/^(?<depPart>\s+[^\s]+)(?<divider>\s+)[^\s]+/);
Expand Down