Skip to content
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

fix(manger/npm): apply config.npmrc during extraction, not in post-update #19812

Merged
merged 7 commits into from
Jan 20, 2023
Merged
Show file tree
Hide file tree
Changes from 5 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
22 changes: 16 additions & 6 deletions lib/modules/manager/npm/extract/index.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -165,25 +165,35 @@ describe('modules/manager/npm/extract/index', () => {
'package.json',
{}
);
expect(res?.npmrc).toBeDefined();
expect(res?.npmrc).toBe('save-exact = true\n');
});

it('ignores .npmrc when config.npmrc is defined and npmrcMerge=false', async () => {
it('uses config.npmrc if no .npmrc exists', async () => {
fs.readLocalFile = jest.fn(() => null);
const res = await npmExtract.extractPackageFile(
input01Content,
'package.json',
{ ...defaultConfig, npmrc: 'config-npmrc' }
);
expect(res?.npmrc).toBe('config-npmrc');
});

it('uses config.npmrc if .npmrc does exist but npmrcMerge=false', async () => {
fs.readLocalFile = jest.fn((fileName) => {
if (fileName === '.npmrc') {
return 'some-npmrc\n';
return 'repo-npmrc\n';
}
return null;
});
const res = await npmExtract.extractPackageFile(
input01Content,
'package.json',
{ npmrc: 'some-configured-npmrc' }
{ npmrc: 'config-npmrc' }
);
expect(res?.npmrc).toBeUndefined();
expect(res?.npmrc).toBe('config-npmrc');
});

it('reads .npmrc when config.npmrc is merged', async () => {
it('merges config.npmrc and repo .npmrc when npmrcMerge=true', async () => {
fs.readLocalFile = jest.fn((fileName) => {
if (fileName === '.npmrc') {
return 'repo-npmrc\n';
Expand Down
3 changes: 3 additions & 0 deletions lib/modules/manager/npm/extract/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -104,6 +104,7 @@ export async function extractPackageFile(
{ npmrcFileName },
'Repo .npmrc file is ignored due to config.npmrc with config.npmrcMerge=false'
);
npmrc = config.npmrc;
} else {
npmrc = config.npmrc ?? '';
if (npmrc.length) {
Expand All @@ -130,6 +131,8 @@ export async function extractPackageFile(
}
npmrc += repoNpmrc;
}
} else if (is.string(config.npmrc)) {
npmrc = config.npmrc;
}

const yarnrcYmlFileName = getSiblingFileName(fileName, '.yarnrc.yml');
Expand Down
37 changes: 37 additions & 0 deletions lib/modules/manager/npm/post-update/index.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -219,6 +219,43 @@ describe('modules/manager/npm/post-update/index', () => {
]);
});

it('writes .npmrc files', async () => {
await writeExistingFiles(updateConfig, {
npm: [
// This package's npmrc should be written verbatim.
{ packageFile: 'packages/core/package.json', npmrc: '#dummy' },
// No npmrc content should be written for this package.
{ packageFile: 'packages/core/package.json' },
],
});

expect(fs.writeLocalFile).toHaveBeenCalledOnce();
expect(fs.writeLocalFile).toHaveBeenCalledWith(
'packages/core/.npmrc',
'#dummy\n'
simon-abbott marked this conversation as resolved.
Show resolved Hide resolved
);
});

it('only sources npmrc content from package config', async () => {
await writeExistingFiles(
{ ...updateConfig, npmrc: '#foobar' },
{
npm: [
// This package's npmrc should be written verbatim.
{ packageFile: 'packages/core/package.json', npmrc: '#dummy' },
// No npmrc content should be written for this package.
{ packageFile: 'packages/core/package.json' },
],
}
);

expect(fs.writeLocalFile).toHaveBeenCalledOnce();
expect(fs.writeLocalFile).toHaveBeenCalledWith(
'packages/core/.npmrc',
'#dummy\n'
);
});

it('works only on relevant folders', async () => {
git.getFile.mockResolvedValueOnce(
Fixtures.get('update-lockfile-massage-1/package-lock.json')
Expand Down
2 changes: 1 addition & 1 deletion lib/modules/manager/npm/post-update/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -143,7 +143,7 @@ export async function writeExistingFiles(
for (const packageFile of npmFiles) {
// TODO #7154
const basedir = upath.dirname(packageFile.packageFile!);
const npmrc: string = packageFile.npmrc ?? config.npmrc;
const npmrc = packageFile.npmrc;
simon-abbott marked this conversation as resolved.
Show resolved Hide resolved
const npmrcFilename = upath.join(basedir, '.npmrc');
if (is.string(npmrc)) {
try {
Expand Down