Skip to content

Commit ec6631c

Browse files
authored
fix(gomod): read contraints from new go.mod (renovatebot#34655)
1 parent b9484f1 commit ec6631c

File tree

3 files changed

+12
-24
lines changed

3 files changed

+12
-24
lines changed

lib/modules/manager/gomod/artifacts.spec.ts

Lines changed: 5 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@ import { codeBlock } from 'common-tags';
22
import { join } from 'upath';
33
import { mockDeep } from 'vitest-mock-extended';
44
import { envMock, mockExecAll } from '../../../../test/exec-util';
5-
import { env, fs, git, mocked, partial } from '../../../../test/util';
5+
import { env, fs, git, partial } from '../../../../test/util';
66
import { GlobalConfig } from '../../../config/global';
77
import type { RepoGlobalConfig } from '../../../config/types';
88
import * as docker from '../../../util/exec/docker';
@@ -31,9 +31,9 @@ vi.mock('./artifacts-extra', () => mockDeep());
3131

3232
process.env.CONTAINERBASE = 'true';
3333

34-
const datasource = mocked(_datasource);
35-
const hostRules = mocked(_hostRules);
36-
const artifactsExtra = mocked(_artifactsExtra);
34+
const datasource = vi.mocked(_datasource);
35+
const hostRules = vi.mocked(_hostRules);
36+
const artifactsExtra = vi.mocked(_artifactsExtra);
3737

3838
const gomod1 = codeBlock`
3939
module github.com/renovate-tests/gomod1
@@ -1820,7 +1820,6 @@ describe('modules/manager/gomod/artifacts', () => {
18201820
it('updates import paths with specific tool version from constraint', async () => {
18211821
fs.readLocalFile.mockResolvedValueOnce('Current go.sum');
18221822
fs.readLocalFile.mockResolvedValueOnce(null); // vendor modules filename
1823-
fs.readLocalFile.mockResolvedValueOnce('go.mod file');
18241823
const execSnapshots = mockExecAll();
18251824
git.getRepoStatus.mockResolvedValueOnce(
18261825
partial<StatusResult>({
@@ -1879,7 +1878,6 @@ describe('modules/manager/gomod/artifacts', () => {
18791878
it('updates import paths with latest tool version on invalid version constraint', async () => {
18801879
fs.readLocalFile.mockResolvedValueOnce('Current go.sum');
18811880
fs.readLocalFile.mockResolvedValueOnce(null); // vendor modules filename
1882-
fs.readLocalFile.mockResolvedValueOnce('go.mod file');
18831881
const execSnapshots = mockExecAll();
18841882
git.getRepoStatus.mockResolvedValueOnce(
18851883
partial<StatusResult>({
@@ -1997,7 +1995,6 @@ describe('modules/manager/gomod/artifacts', () => {
19971995
GlobalConfig.set({ ...adminConfig, binarySource: 'install' });
19981996
fs.readLocalFile.mockResolvedValueOnce('Current go.sum');
19991997
fs.readLocalFile.mockResolvedValueOnce(null); // vendor modules filename
2000-
fs.readLocalFile.mockResolvedValueOnce(null);
20011998
const execSnapshots = mockExecAll();
20021999
git.getRepoStatus.mockResolvedValueOnce(
20032000
partial<StatusResult>({
@@ -2056,7 +2053,6 @@ describe('modules/manager/gomod/artifacts', () => {
20562053
GlobalConfig.set({ ...adminConfig, binarySource: 'docker' });
20572054
fs.readLocalFile.mockResolvedValueOnce('Current go.sum');
20582055
fs.readLocalFile.mockResolvedValueOnce(null); // vendor modules filename
2059-
fs.readLocalFile.mockResolvedValueOnce('someText\n\ngo 1.17\n\n');
20602056
const execSnapshots = mockExecAll();
20612057
git.getRepoStatus.mockResolvedValueOnce(
20622058
partial<StatusResult>({
@@ -2075,7 +2071,7 @@ describe('modules/manager/gomod/artifacts', () => {
20752071
updatedDeps: [
20762072
{ depName: 'github.com/google/go-github/v24', newVersion: 'v28.0.0' },
20772073
],
2078-
newPackageFileContent: gomod1,
2074+
newPackageFileContent: `someText\n\ngo 1.17\n\n${gomod1}`,
20792075
config: {
20802076
updateType: 'major',
20812077
postUpdateOptions: ['gomodUpdateImportPaths'],
@@ -2130,7 +2126,6 @@ describe('modules/manager/gomod/artifacts', () => {
21302126
GlobalConfig.set({ ...adminConfig, binarySource: 'docker' });
21312127
fs.readLocalFile.mockResolvedValueOnce('Current go.sum');
21322128
fs.readLocalFile.mockResolvedValueOnce(null); // vendor modules filename
2133-
fs.readLocalFile.mockResolvedValueOnce('someText\n\ngo 1.17\n\n');
21342129
mockExecAll();
21352130
git.getRepoStatus.mockResolvedValueOnce(
21362131
partial<StatusResult>({

lib/modules/manager/gomod/artifacts.ts

Lines changed: 5 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -191,7 +191,7 @@ export async function updateArtifacts({
191191
}
192192
}
193193
const goConstraints =
194-
config.constraints?.go ?? (await getGoConstraints(goModFileName));
194+
config.constraints?.go ?? getGoConstraints(newGoModContent);
195195

196196
try {
197197
await writeLocalFile(goModFileName, massagedGoMod);
@@ -208,9 +208,8 @@ export async function updateArtifacts({
208208
GONOSUMDB: process.env.GONOSUMDB,
209209
GOSUMDB: process.env.GOSUMDB,
210210
GOINSECURE: process.env.GOINSECURE,
211-
GOFLAGS: useModcacherw(goConstraints)
212-
? '-modcacherw'
213-
: /* istanbul ignore next: hard to test */ null,
211+
/* v8 ignore next -- TODO: add test */
212+
GOFLAGS: useModcacherw(goConstraints) ? '-modcacherw' : null,
214213
CGO_ENABLED: GlobalConfig.get('binarySource') === 'docker' ? '0' : null,
215214
...getGitEnvironmentVariables(['go']),
216215
},
@@ -444,17 +443,11 @@ export async function updateArtifacts({
444443
}
445444
}
446445

447-
async function getGoConstraints(
448-
goModFileName: string,
449-
): Promise<string | undefined> {
450-
const content = (await readLocalFile(goModFileName, 'utf8')) ?? null;
451-
if (!content) {
452-
return undefined;
453-
}
446+
function getGoConstraints(content: string): string | undefined {
454447
const re = regEx(/^go\s*(?<gover>\d+\.\d+)$/m);
455448
const match = re.exec(content);
456449
if (!match?.groups?.gover) {
457450
return undefined;
458451
}
459-
return '^' + match.groups.gover;
452+
return `^${match.groups.gover}`;
460453
}

lib/modules/manager/gomod/update.ts

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -23,13 +23,13 @@ export function updateDependency({
2323
logger.warn('gomod manager does not support replacement updates yet');
2424
return null;
2525
}
26-
// istanbul ignore if: should never happen
26+
/* v8 ignore next 3: should never happen */
2727
if (!currentName || !upgrade.managerData) {
2828
return null;
2929
}
3030
const currentNameNoVersion = getNameWithNoVersion(currentName);
3131
const lines = fileContent.split(newlineRegex);
32-
// istanbul ignore if: hard to test
32+
/* v8 ignore next 4: hard to test */
3333
if (lines.length <= upgrade.managerData.lineNumber) {
3434
logger.warn('go.mod current line no longer exists after update');
3535
return null;

0 commit comments

Comments
 (0)