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): yarn v2 yarnrc support #7235

Closed
wants to merge 3 commits into from
Closed
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
7 changes: 6 additions & 1 deletion lib/manager/common.ts
Original file line number Diff line number Diff line change
Expand Up @@ -93,7 +93,7 @@ export interface PackageFile<T = Record<string, any>>
packageFileVersion?: string;
parent?: string;
skipInstalls?: boolean;
yarnrc?: string;
yarnrc?: RcFile;
yarnWorkspacesPackages?: string[] | string;
matchStrings?: string[];
}
Expand Down Expand Up @@ -263,3 +263,8 @@ export interface PostUpdateConfig extends ManagerConfig, Record<string, any> {
branchName?: string;
reuseExistingBranch?: boolean;
}

export interface RcFile {
content: string;
fileName: string;
}
8 changes: 3 additions & 5 deletions lib/manager/npm/extract/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,7 @@ import { NpmPackage, NpmPackageDependency } from './common';
import { getLockedVersions } from './locked-versions';
import { detectMonorepos } from './monorepo';
import { mightBeABrowserLibrary } from './type';
import { getYarnRc } from './yarn';

function parseDepName(depType: string, key: string): string {
if (depType !== 'resolutions') {
Expand Down Expand Up @@ -117,11 +118,8 @@ export async function extractPackageFile(
npmrc = undefined;
}
}
const yarnrcFileName = getSiblingFileName(fileName, '.yarnrc');
let yarnrc;
if (!is.string(config.yarnrc)) {
yarnrc = (await readLocalFile(yarnrcFileName, 'utf8')) || undefined;
}

const yarnrc = await getYarnRc(fileName, config);

let lernaDir: string;
let lernaPackages: string[];
Expand Down
33 changes: 32 additions & 1 deletion lib/manager/npm/extract/yarn.ts
Original file line number Diff line number Diff line change
@@ -1,7 +1,12 @@
import * as path from 'path';
import is from '@sindresorhus/is';
import { structUtils } from '@yarnpkg/core';
import { parseSyml } from '@yarnpkg/parsers';
import findUp from 'find-up';
import { getEnv } from '../../../../tools/utils';
import { logger } from '../../../logger';
import { readLocalFile } from '../../../util/fs';
import { readFile, readLocalFile } from '../../../util/fs';
import { ExtractConfig, RcFile } from '../../common';

export async function getYarnLock(
filePath: string
Expand Down Expand Up @@ -41,3 +46,29 @@ export async function getYarnLock(
return { isYarn1: true, cacheVersion: NaN, lockedVersions: {} };
}
}

export async function getYarnRc(
packageFilePath: string,
config: ExtractConfig
): Promise<RcFile> {
if (is.string(config.yarnrc) || config.localDir === undefined) {
return undefined;
}
const yarnRcFileNames = [
'.yarnrc',
'.yarnrc.yml',
Comment on lines +58 to +59
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

.yarnrc.yml overrides (takes precedence over) .yarnrc in Yarn 1 (https://git.io/JUcco, #7220 (comment)).

getEnv('YARN_RC_FILENAME'),
];
const yarnRcPath = await findUp(yarnRcFileNames, {
cwd: path.dirname(path.join(config.localDir, packageFilePath)),
type: 'file',
});
if (yarnRcPath?.startsWith(config.localDir) !== true) {
return undefined;
}
logger.debug({ yarnRcPath }, 'found Yarn config file');
return {
content: await readFile(yarnRcPath, 'utf8'),
fileName: yarnRcPath,
};
}
9 changes: 6 additions & 3 deletions lib/manager/npm/post-update/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -158,12 +158,15 @@ export async function writeExistingFiles(
}
}
if (packageFile.yarnrc) {
logger.debug(`Writing .yarnrc to ${basedir}`);
const yarnrcFilename = upath.join(basedir, '.yarnrc');
const {
fileName: yarnrcFilename,
content: yarnrcContent,
} = packageFile.yarnrc;
logger.debug(`Writing ${yarnrcFilename} to ${basedir}`);
try {
await outputFile(
yarnrcFilename,
packageFile.yarnrc
yarnrcContent
.replace('--install.pure-lockfile true', '')
.replace('--install.frozen-lockfile true', '')
JamieMagee marked this conversation as resolved.
Show resolved Hide resolved
);
Expand Down