Skip to content

Commit

Permalink
fix(gomod): skip updating import path for incompatible versions (#20812)
Browse files Browse the repository at this point in the history
  • Loading branch information
maxbrunet committed Mar 14, 2023
1 parent 182b01a commit 7b627e3
Show file tree
Hide file tree
Showing 4 changed files with 75 additions and 5 deletions.
49 changes: 49 additions & 0 deletions lib/modules/manager/gomod/artifacts.spec.ts
Expand Up @@ -1434,6 +1434,55 @@ describe('modules/manager/gomod/artifacts', () => {
]);
});

it('skips updating import paths when incompatible version', async () => {
fs.readLocalFile
.mockResolvedValueOnce('Current go.sum')
.mockResolvedValueOnce(null); // vendor modules filename
const execSnapshots = mockExecAll();
git.getRepoStatus.mockResolvedValueOnce(
partial<StatusResult>({
modified: ['go.sum'],
})
);
fs.readLocalFile
.mockResolvedValueOnce('New go.sum')
.mockResolvedValueOnce('New go.mod');
expect(
await gomod.updateArtifacts({
packageFileName: 'go.mod',
updatedDeps: [
{
depName: 'github.com/docker/docker',
newVersion: 'v23.0.0+incompatible',
},
],
newPackageFileContent: gomod1,
config: {
...config,
updateType: 'major',
postUpdateOptions: ['gomodUpdateImportPaths'],
},
})
).toEqual([
{ file: { type: 'addition', path: 'go.sum', contents: 'New go.sum' } },
{ file: { type: 'addition', path: 'go.mod', contents: 'New go.mod' } },
]);
expect(execSnapshots).toMatchObject([
{
cmd: 'go get -d -t ./...',
options: { cwd: '/tmp/github/some/repo' },
},
{
cmd: 'go mod tidy',
options: { cwd: '/tmp/github/some/repo' },
},
{
cmd: 'go mod tidy',
options: { cwd: '/tmp/github/some/repo' },
},
]);
});

it('skips gomodTidy without gomodUpdateImportPaths on major update', async () => {
fs.readLocalFile.mockResolvedValueOnce('Current go.sum');
fs.readLocalFile.mockResolvedValueOnce(null); // vendor modules filename
Expand Down
7 changes: 5 additions & 2 deletions lib/modules/manager/gomod/artifacts.ts
Expand Up @@ -129,14 +129,17 @@ function getUpdateImportPathCmds(
}

const updateImportCommands = updatedDeps
.filter(({ newVersion }) => valid(newVersion))
.filter(
({ newVersion }) =>
valid(newVersion) && !newVersion!.endsWith('+incompatible')
)
.map(({ depName, newVersion }) => ({
depName: depName!,
newMajor: major(newVersion!),
}))
// Skip path updates going from v0 to v1
.filter(
({ depName, newMajor }) => depName.startsWith('gopkg.in') || newMajor > 1
({ depName, newMajor }) => depName.startsWith('gopkg.in/') || newMajor > 1
)

.map(
Expand Down
21 changes: 19 additions & 2 deletions lib/modules/manager/gomod/update.spec.ts
Expand Up @@ -96,7 +96,7 @@ describe('modules/manager/gomod/update', () => {
depType: 'require',
};
const res = updateDependency({ fileContent: gomod1, upgrade });
expect(res).not.toEqual(gomod2);
expect(res).not.toEqual(gomod1);
expect(res).toContain('github.com/pkg/errors/v2 v2.0.0');
});

Expand All @@ -112,10 +112,27 @@ describe('modules/manager/gomod/update', () => {
};
const res = updateDependency({ fileContent: gomod1, upgrade });
expect(res).toMatchSnapshot();
expect(res).not.toEqual(gomod2);
expect(res).not.toEqual(gomod1);
expect(res).toContain('gopkg.in/russross/blackfriday.v2 v2.0.0');
});

it('skip replacing incompatible major updates', () => {
const upgrade = {
depName: 'github.com/Azure/azure-sdk-for-go',
managerData: { lineNumber: 8 },
newMajor: 26,
updateType: 'major' as UpdateType,
currentValue: 'v25.1.0+incompatible',
newValue: 'v26.0.0+incompatible',
depType: 'require',
};
const res = updateDependency({ fileContent: gomod1, upgrade });
expect(res).not.toEqual(gomod1);
expect(res).toContain(
'github.com/Azure/azure-sdk-for-go v26.0.0+incompatible'
);
});

it('returns null if mismatch', () => {
const upgrade = {
depName: 'github.com/aws/aws-sdk-go',
Expand Down
3 changes: 2 additions & 1 deletion lib/modules/manager/gomod/update.ts
Expand Up @@ -105,7 +105,8 @@ export function updateDependency({
);
} else if (
upgrade.newMajor! > 1 &&
!newLine.includes(`/v${upgrade.newMajor}`)
!newLine.includes(`/v${upgrade.newMajor}`) &&
!upgrade.newValue!.endsWith('+incompatible')
) {
if (depName === depNameNoVersion) {
// If package currently has no version, pin to latest one.
Expand Down

0 comments on commit 7b627e3

Please sign in to comment.