Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
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
138 changes: 138 additions & 0 deletions lib/modules/manager/gomod/artifacts.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2692,4 +2692,142 @@ describe('modules/manager/gomod/artifacts', () => {
}),
).rejects.toThrow(TEMPORARY_ERROR);
});

it('uses -modfile flag for non-default go.mod filename', async () => {
fs.findLocalSiblingOrParent.mockResolvedValueOnce(null);
fs.readLocalFile.mockResolvedValueOnce('Current tools.sum');
const execSnapshots = mockExecAll();
git.getRepoStatus.mockResolvedValueOnce(
partial<StatusResult>({
modified: ['tools.sum'],
}),
);
fs.readLocalFile.mockResolvedValueOnce('New tools.sum');
fs.readLocalFile.mockResolvedValueOnce(gomod1);
expect(
await gomod.updateArtifacts({
packageFileName: 'tools.mod',
updatedDeps: [],
newPackageFileContent: gomod1,
config,
}),
).toEqual([
{
file: {
contents: 'New tools.sum',
path: 'tools.sum',
type: 'addition',
},
},
]);
expect(execSnapshots).toMatchObject([
{
cmd: 'go get -modfile=tools.mod -d -t ./...',
options: { cwd: '/tmp/github/some/repo' },
},
]);
});

it('uses -modfile flag with go mod tidy for non-default go.mod filename', async () => {
fs.findLocalSiblingOrParent.mockResolvedValueOnce(null);
fs.readLocalFile.mockResolvedValueOnce('Current tools.sum');
const execSnapshots = mockExecAll();
git.getRepoStatus.mockResolvedValueOnce(
partial<StatusResult>({
modified: ['tools.sum'],
}),
);
fs.readLocalFile.mockResolvedValueOnce('New tools.sum');
fs.readLocalFile.mockResolvedValueOnce(gomod1);
expect(
await gomod.updateArtifacts({
packageFileName: 'tools.mod',
updatedDeps: [],
newPackageFileContent: gomod1,
config: {
...config,
postUpdateOptions: ['gomodTidy'],
},
}),
).toEqual([
{
file: {
contents: 'New tools.sum',
path: 'tools.sum',
type: 'addition',
},
},
]);
expect(execSnapshots).toMatchObject([
{
cmd: 'go get -modfile=tools.mod -d -t ./...',
options: { cwd: '/tmp/github/some/repo' },
},
{
cmd: 'go mod tidy -modfile=tools.mod',
options: { cwd: '/tmp/github/some/repo' },
},
{
cmd: 'go mod tidy -modfile=tools.mod',
options: { cwd: '/tmp/github/some/repo' },
},
]);
});

it('uses -modfile flag with go mod vendor for non-default go.mod filename', async () => {
fs.findLocalSiblingOrParent.mockResolvedValueOnce('vendor');
fs.readLocalFile.mockResolvedValueOnce('Current tools.sum');
fs.readLocalFile.mockResolvedValueOnce('vendor modules'); // vendor modules filename
const execSnapshots = mockExecAll();
git.getRepoStatus.mockResolvedValueOnce(
partial<StatusResult>({
modified: ['tools.sum'],
not_added: [],
deleted: [],
}),
);
fs.readLocalFile.mockResolvedValueOnce('New tools.sum');
fs.readLocalFile.mockResolvedValueOnce(gomod1);
expect(
await gomod.updateArtifacts({
packageFileName: 'tools.mod',
updatedDeps: [],
newPackageFileContent: gomod1,
config: {
...config,
postUpdateOptions: ['gomodTidy'],
},
}),
).toEqual([
{
file: {
contents: 'New tools.sum',
path: 'tools.sum',
type: 'addition',
},
},
]);
expect(execSnapshots).toMatchObject([
{
cmd: 'go get -modfile=tools.mod -d -t ./...',
options: { cwd: '/tmp/github/some/repo' },
},
{
cmd: 'go mod tidy -modfile=tools.mod',
options: { cwd: '/tmp/github/some/repo' },
},
{
cmd: 'go mod vendor -modfile=tools.mod',
options: { cwd: '/tmp/github/some/repo' },
},
{
cmd: 'go mod tidy -modfile=tools.mod',
options: { cwd: '/tmp/github/some/repo' },
},
{
cmd: 'go mod tidy -modfile=tools.mod',
options: { cwd: '/tmp/github/some/repo' },
},
]);
});
});
15 changes: 10 additions & 5 deletions lib/modules/manager/gomod/artifacts.ts
Original file line number Diff line number Diff line change
Expand Up @@ -121,6 +121,11 @@ export async function updateArtifacts({
return null;
}
const goModDir = upath.dirname(goModFileName);
const goModFileBaseName = upath.basename(goModFileName);
const modFileFlag =
goModFileBaseName === 'go.mod'
? ''
: ` -modfile=${quote(goModFileBaseName)}`;

// The "vendor" directory can be next to the go.mod, but also in the parent directory in case
// the go workspaces are used.
Expand Down Expand Up @@ -239,7 +244,7 @@ export async function updateArtifacts({
}
}

let args = `get `;
let args = `get${modFileFlag} `;

if (goConstraints && !semver.intersects(goConstraints, `>=1.18`)) {
// For Go versions < 1.18, we need to use the -d flag to avoid builds or installs
Expand Down Expand Up @@ -287,7 +292,7 @@ export async function updateArtifacts({
config.postUpdateOptions?.includes('gomodTidyE') === true ||
(config.updateType === 'major' && isImportPathUpdateRequired));
if (isGoModTidyRequired) {
args = 'mod tidy' + tidyOpts;
args = `mod tidy${modFileFlag}${tidyOpts}`;
logger.debug('go mod tidy command included');
execCommands.push(`${cmd} ${args}`);
}
Expand All @@ -314,21 +319,21 @@ export async function updateArtifacts({
logger.debug('using go work sync');
execCommands.push(`${cmd} ${args}`);
} else {
args = 'mod vendor';
args = `mod vendor${modFileFlag}`;
logger.debug('using go mod vendor');
execCommands.push(`${cmd} ${args}`);
}

if (isGoModTidyRequired) {
args = 'mod tidy' + tidyOpts;
args = `mod tidy${modFileFlag}${tidyOpts}`;
logger.debug('go mod tidy command included');
execCommands.push(`${cmd} ${args}`);
}
}

// We tidy one more time as a solution for #6795
if (isGoModTidyRequired) {
args = 'mod tidy' + tidyOpts;
args = `mod tidy${modFileFlag}${tidyOpts}`;
logger.debug('go mod tidy command included');
execCommands.push(`${cmd} ${args}`);
}
Expand Down
Loading