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(yarn): search parent directories for yarn configuration #29415

Merged
merged 2 commits into from
Jun 3, 2024
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
37 changes: 37 additions & 0 deletions lib/modules/manager/npm/extract/index.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -260,6 +260,15 @@ describe('modules/manager/npm/extract/index', () => {
});

it('reads registryUrls from .yarnrc.yml', async () => {
fs.findLocalSiblingOrParent.mockImplementation(
(packageFile, otherFile): Promise<string | null> => {
if (packageFile === 'package.json' && otherFile === '.yarnrc.yml') {
return Promise.resolve('.yarnrc.yml');
}
return Promise.resolve(null);
},
);

fs.readLocalFile.mockImplementation((fileName): Promise<any> => {
if (fileName === '.yarnrc.yml') {
return Promise.resolve(
Expand All @@ -279,12 +288,22 @@ describe('modules/manager/npm/extract/index', () => {
});

it('reads registryUrls from .yarnrc', async () => {
fs.findLocalSiblingOrParent.mockImplementation(
(packageFile, otherFile): Promise<string | null> => {
if (packageFile === 'package.json' && otherFile === '.yarnrc') {
return Promise.resolve('.yarnrc');
}
return Promise.resolve(null);
},
);

fs.readLocalFile.mockImplementation((fileName): Promise<any> => {
if (fileName === '.yarnrc') {
return Promise.resolve('registry "https://registry.example.com"');
}
return Promise.resolve(null);
});

const res = await npmExtract.extractPackageFile(
input02Content,
'package.json',
Expand All @@ -296,6 +315,15 @@ describe('modules/manager/npm/extract/index', () => {
});

it('resolves registry URLs using the package name if set', async () => {
fs.findLocalSiblingOrParent.mockImplementation(
(packageFile, otherFile): Promise<string | null> => {
if (packageFile === 'package.json' && otherFile === '.yarnrc.yml') {
return Promise.resolve('.yarnrc.yml');
}
return Promise.resolve(null);
},
);

fs.readLocalFile.mockImplementation((fileName): Promise<any> => {
if (fileName === '.yarnrc.yml') {
return Promise.resolve(codeBlock`
Expand Down Expand Up @@ -773,6 +801,15 @@ describe('modules/manager/npm/extract/index', () => {
});

it('sets skipInstalls false if Yarn zero-install is used', async () => {
fs.findLocalSiblingOrParent.mockImplementation(
(packageFile, otherFile): Promise<string | null> => {
if (packageFile === 'package.json' && otherFile === '.yarnrc.yml') {
return Promise.resolve('.yarnrc.yml');
}
return Promise.resolve(null);
},
);

fs.readLocalFile.mockImplementation((fileName): Promise<any> => {
if (fileName === 'yarn.lock') {
return Promise.resolve('# yarn.lock');
Expand Down
28 changes: 22 additions & 6 deletions lib/modules/manager/npm/extract/index.ts
Original file line number Diff line number Diff line change
@@ -1,7 +1,11 @@
import is from '@sindresorhus/is';
import { GlobalConfig } from '../../../../config/global';
import { logger } from '../../../../logger';
import { getSiblingFileName, readLocalFile } from '../../../../util/fs';
import {
findLocalSiblingOrParent,
getSiblingFileName,
readLocalFile,
} from '../../../../util/fs';
import { newlineRegex, regEx } from '../../../../util/regex';
import { NpmDatasource } from '../../../datasource/npm';

Expand Down Expand Up @@ -122,17 +126,29 @@ export async function extractPackageFile(
npmrc = config.npmrc;
}

const yarnrcYmlFileName = getSiblingFileName(packageFile, '.yarnrc.yml');
const yarnZeroInstall = await isZeroInstall(yarnrcYmlFileName);
const yarnrcYmlFileName = await findLocalSiblingOrParent(
packageFile,
'.yarnrc.yml',
);
const yarnZeroInstall = yarnrcYmlFileName
? await isZeroInstall(yarnrcYmlFileName)
: false;

let yarnConfig: YarnConfig | null = null;
const repoYarnrcYml = await readLocalFile(yarnrcYmlFileName, 'utf8');
const repoYarnrcYml = yarnrcYmlFileName
? await readLocalFile(yarnrcYmlFileName, 'utf8')
: null;
if (is.string(repoYarnrcYml) && repoYarnrcYml.trim().length > 0) {
yarnConfig = loadConfigFromYarnrcYml(repoYarnrcYml);
}

const legacyYarnrcFileName = getSiblingFileName(packageFile, '.yarnrc');
const repoLegacyYarnrc = await readLocalFile(legacyYarnrcFileName, 'utf8');
const legacyYarnrcFileName = await findLocalSiblingOrParent(
packageFile,
'.yarnrc',
);
const repoLegacyYarnrc = legacyYarnrcFileName
? await readLocalFile(legacyYarnrcFileName, 'utf8')
: null;
if (is.string(repoLegacyYarnrc) && repoLegacyYarnrc.trim().length > 0) {
yarnConfig = loadConfigFromLegacyYarnrc(repoLegacyYarnrc);
}
Expand Down