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 5 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
62 changes: 51 additions & 11 deletions lib/modules/manager/gomod/artifacts.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ import { codeBlock } from 'common-tags';
import { join } from 'upath';
import { mockDeep } from 'vitest-mock-extended';
import { envMock, mockExecAll } from '../../../../test/exec-util';
import { env, fs, git, mocked, partial } from '../../../../test/util';
import { env, fs, git, partial } from '../../../../test/util';
import { GlobalConfig } from '../../../config/global';
import type { RepoGlobalConfig } from '../../../config/types';
import * as docker from '../../../util/exec/docker';
Expand Down Expand Up @@ -31,9 +31,9 @@ vi.mock('./artifacts-extra', () => mockDeep());

process.env.CONTAINERBASE = 'true';

const datasource = mocked(_datasource);
const hostRules = mocked(_hostRules);
const artifactsExtra = mocked(_artifactsExtra);
const datasource = vi.mocked(_datasource);
const hostRules = vi.mocked(_hostRules);
const artifactsExtra = vi.mocked(_artifactsExtra);

const gomod1 = codeBlock`
module github.com/renovate-tests/gomod1
Expand Down Expand Up @@ -1820,7 +1820,6 @@ describe('modules/manager/gomod/artifacts', () => {
it('updates import paths with specific tool version from constraint', async () => {
fs.readLocalFile.mockResolvedValueOnce('Current go.sum');
fs.readLocalFile.mockResolvedValueOnce(null); // vendor modules filename
fs.readLocalFile.mockResolvedValueOnce('go.mod file');
const execSnapshots = mockExecAll();
git.getRepoStatus.mockResolvedValueOnce(
partial<StatusResult>({
Expand Down Expand Up @@ -1879,7 +1878,6 @@ describe('modules/manager/gomod/artifacts', () => {
it('updates import paths with latest tool version on invalid version constraint', async () => {
fs.readLocalFile.mockResolvedValueOnce('Current go.sum');
fs.readLocalFile.mockResolvedValueOnce(null); // vendor modules filename
fs.readLocalFile.mockResolvedValueOnce('go.mod file');
const execSnapshots = mockExecAll();
git.getRepoStatus.mockResolvedValueOnce(
partial<StatusResult>({
Expand Down Expand Up @@ -1997,7 +1995,6 @@ describe('modules/manager/gomod/artifacts', () => {
GlobalConfig.set({ ...adminConfig, binarySource: 'install' });
fs.readLocalFile.mockResolvedValueOnce('Current go.sum');
fs.readLocalFile.mockResolvedValueOnce(null); // vendor modules filename
fs.readLocalFile.mockResolvedValueOnce(null);
const execSnapshots = mockExecAll();
git.getRepoStatus.mockResolvedValueOnce(
partial<StatusResult>({
Expand Down Expand Up @@ -2056,7 +2053,6 @@ describe('modules/manager/gomod/artifacts', () => {
GlobalConfig.set({ ...adminConfig, binarySource: 'docker' });
fs.readLocalFile.mockResolvedValueOnce('Current go.sum');
fs.readLocalFile.mockResolvedValueOnce(null); // vendor modules filename
fs.readLocalFile.mockResolvedValueOnce('someText\n\ngo 1.17\n\n');
const execSnapshots = mockExecAll();
git.getRepoStatus.mockResolvedValueOnce(
partial<StatusResult>({
Expand All @@ -2075,7 +2071,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 Expand Up @@ -2125,12 +2121,56 @@ 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');
datasource.getPkgReleases.mockResolvedValueOnce({
releases: [
{ version: '1.17.0' },
{ version: '1.23.6' },
{ version: '1.24.0' },
],
});
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 ./...',
},
]);
});

it('returns artifact notices', async () => {
artifactsExtra.getExtraDepsNotice.mockReturnValue('some extra notice');
GlobalConfig.set({ ...adminConfig, binarySource: 'docker' });
fs.readLocalFile.mockResolvedValueOnce('Current go.sum');
fs.readLocalFile.mockResolvedValueOnce(null); // vendor modules filename
fs.readLocalFile.mockResolvedValueOnce('someText\n\ngo 1.17\n\n');
mockExecAll();
git.getRepoStatus.mockResolvedValueOnce(
partial<StatusResult>({
Expand All @@ -2149,7 +2189,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
17 changes: 9 additions & 8 deletions lib/modules/manager/gomod/artifacts.ts
Original file line number Diff line number Diff line change
Expand Up @@ -191,7 +191,7 @@ export async function updateArtifacts({
}
}
const goConstraints =
config.constraints?.go ?? (await getGoConstraints(goModFileName));
config.constraints?.go ?? getGoConstraints(massagedGoMod);

try {
await writeLocalFile(goModFileName, massagedGoMod);
Expand Down Expand Up @@ -444,15 +444,16 @@ export async function updateArtifacts({
}
}

async function getGoConstraints(
goModFileName: string,
): Promise<string | undefined> {
const content = (await readLocalFile(goModFileName, 'utf8')) ?? null;
if (!content) {
return undefined;
function getGoConstraints(goModContent: string): string | undefined {
// prefer toolchain directive when go.mod has one
const toolchain = regEx(/^toolchain\s*go(?<gover>\d+\.\d+\.\d+)$/m);
const toolchainMatch = toolchain.exec(goModContent);
if (toolchainMatch?.groups?.gover) {
return toolchainMatch.groups.gover;
}

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