Skip to content

fix(manager/gomod): perfer to use go version defined as toolchain to update artifacts #34564

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

Merged
merged 22 commits into from
Mar 13, 2025
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
Show all changes
22 commits
Select commit Hold shift + click to select a range
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
82 changes: 81 additions & 1 deletion lib/modules/manager/gomod/artifacts.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2121,6 +2121,86 @@ describe('modules/manager/gomod/artifacts', () => {
expect(execSnapshots).toMatchObject(expectedResult);
});

it('go.mod file contains go toolchain version', async () => {
GlobalConfig.set({ ...adminConfig, binarySource: 'install' });
fs.readLocalFile.mockResolvedValueOnce('Current go.sum');
fs.readLocalFile.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');

const res = await gomod.updateArtifacts({
packageFileName: 'go.mod',
updatedDeps: [{ depName: 'golang.org/x/crypto', newVersion: '0.35.0' }],
newPackageFileContent: `someText\n\ngo 1.13\n\ntoolchain go1.23.6\n\n${gomod1}`,
config: {
updateType: 'minor',
},
});

expect(res).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: 'install-tool golang 1.23.6',
},
{
cmd: 'go get -d -t ./...',
},
]);

expect(datasource.getPkgReleases).toBeCalledTimes(0);
});

it('go.mod file contains full go version without toolchain', async () => {
GlobalConfig.set({ ...adminConfig, binarySource: 'install' });
fs.readLocalFile.mockResolvedValueOnce('Current go.sum');
fs.readLocalFile.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');

const res = await gomod.updateArtifacts({
packageFileName: 'go.mod',
updatedDeps: [{ depName: 'golang.org/x/crypto', newVersion: '0.35.0' }],
newPackageFileContent: `someText\n\ngo 1.23.5\n\n${gomod1}`,
config: {
updateType: 'minor',
},
});

expect(res).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: 'install-tool golang 1.23.5',
},
{
cmd: 'go get -d -t ./...',
},
]);

expect(datasource.getPkgReleases).toBeCalledTimes(0);
});

it('returns artifact notices', async () => {
artifactsExtra.getExtraDepsNotice.mockReturnValue('some extra notice');
GlobalConfig.set({ ...adminConfig, binarySource: 'docker' });
Expand All @@ -2144,7 +2224,7 @@ describe('modules/manager/gomod/artifacts', () => {
updatedDeps: [
{ depName: 'github.com/google/go-github/v24', newVersion: 'v28.0.0' },
],
newPackageFileContent: gomod1,
newPackageFileContent: `someText\n\ngo 1.17\n\n${gomod1}`,
config: {
updateType: 'major',
postUpdateOptions: ['gomodUpdateImportPaths'],
Expand Down
26 changes: 26 additions & 0 deletions lib/modules/manager/gomod/artifacts.ts
Original file line number Diff line number Diff line change
Expand Up @@ -444,6 +444,32 @@ export async function updateArtifacts({
}

function getGoConstraints(content: string): string | undefined {
// prefer toolchain directive when go.mod has one
const toolchainMatch = regEx(/^toolchain\s*go(?<gover>\d+\.\d+\.\d+)$/m).exec(
content,
);
const toolchainVer = toolchainMatch?.groups?.gover;
if (toolchainVer) {
logger.debug(
`Using go version ${toolchainVer} found in toolchain directive`,
);
return toolchainVer;
}

// If go.mod doesn't have toolchain directive and has a full go version spec,
// for example `go 1.23.6`, pick this version, this doesn't match major.minor version spec.
//
// This is because when go.mod have same version defined in go directive and toolchain directive,
// go will remove toolchain directive from go.mod.
//
// For example, go will rewrite `go 1.23.5\ntoolchain go1.23.5` to `go 1.23.5` by default,
// in this case, the go directive is the toolchain directive.
const goFullVersion = regEx(/^go\s*(?<gover>\d+\.\d+\.\d+)$/m).exec(content)
?.groups?.gover;
if (goFullVersion) {
return goFullVersion;
}

const re = regEx(/^go\s*(?<gover>\d+\.\d+)$/m);
const match = re.exec(content);
if (!match?.groups?.gover) {
Expand Down