Skip to content

Commit dc72cba

Browse files
authored
refactor!: simplify Configuration.findProjectCwd (#5627)
**What's the problem this PR addresses?** <!-- Describe the rationale of your PR. --> <!-- Link all issues that it closes. (Closes/Resolves #xxxx.) --> `Configuration.findProjectCwd` is written to support both a `lockfile` lookup (which is kind of a mixed lookup anyways) and a `manifest` lookup. `Configuration.find` supports both these lookups and also `ProjectLookup.NONE`, which only looks inside the current folder. `ProjectLookup.MANIFEST` isn't used anywhere, and `ProjectLookup.NONE` is only used inside `@yarnpkg/doctor`, but it should never get to run because `findLockfileWorkspace` runs first and it does a regular mixed lookup which already succeeds even if there's no lockfile but there's a manifest. **How did you fix it?** <!-- A detailed description of your implementation. --> I simplified all of this and got rid of the lookups. Now everything does a mixed ("lockfile") lookup by default. **Checklist** <!--- Don't worry if you miss something, chores are automatically tested. --> <!--- This checklist exists to help you remember doing the chores when you submit a PR. --> <!--- Put an `x` in all the boxes that apply. --> - [X] I have read the [Contributing Guide](https://yarnpkg.com/advanced/contributing). <!-- See https://yarnpkg.com/advanced/contributing#preparing-your-pr-to-be-released for more details. --> <!-- Check with `yarn version check` and fix with `yarn version check -i` --> - [X] I have set the packages that need to be released for my changes to be effective. <!-- The "Testing chores" workflow validates that your PR follows our guidelines. --> <!-- If it doesn't pass, click on it to see details as to what your PR might be missing. --> - [X] I will check that all automated PR checks pass before the PR gets reviewed.
1 parent 9287251 commit dc72cba

File tree

6 files changed

+56
-71
lines changed

6 files changed

+56
-71
lines changed

.yarn/versions/1d7f6642.yml

+34
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,34 @@
1+
releases:
2+
"@yarnpkg/cli": major
3+
"@yarnpkg/core": major
4+
"@yarnpkg/doctor": patch
5+
"@yarnpkg/plugin-dlx": patch
6+
7+
declined:
8+
- "@yarnpkg/plugin-compat"
9+
- "@yarnpkg/plugin-constraints"
10+
- "@yarnpkg/plugin-essentials"
11+
- "@yarnpkg/plugin-exec"
12+
- "@yarnpkg/plugin-file"
13+
- "@yarnpkg/plugin-git"
14+
- "@yarnpkg/plugin-github"
15+
- "@yarnpkg/plugin-http"
16+
- "@yarnpkg/plugin-init"
17+
- "@yarnpkg/plugin-interactive-tools"
18+
- "@yarnpkg/plugin-link"
19+
- "@yarnpkg/plugin-nm"
20+
- "@yarnpkg/plugin-npm"
21+
- "@yarnpkg/plugin-npm-cli"
22+
- "@yarnpkg/plugin-pack"
23+
- "@yarnpkg/plugin-patch"
24+
- "@yarnpkg/plugin-pnp"
25+
- "@yarnpkg/plugin-pnpm"
26+
- "@yarnpkg/plugin-stage"
27+
- "@yarnpkg/plugin-typescript"
28+
- "@yarnpkg/plugin-version"
29+
- "@yarnpkg/plugin-workspace-tools"
30+
- "@yarnpkg/builder"
31+
- "@yarnpkg/extensions"
32+
- "@yarnpkg/nm"
33+
- "@yarnpkg/pnpify"
34+
- "@yarnpkg/sdks"

CHANGELOG.md

+2
Original file line numberDiff line numberDiff line change
@@ -95,6 +95,8 @@ The following changes only affect people writing Yarn plugins:
9595
- `workspace.anchoredLocator` to get the locator that's used throughout the dependency tree.
9696
- `workspace.manifest.version` to get the workspace version.
9797

98+
- `ProjectLookup` has been removed. Both `Configuration.find` and `Configuration.findProjectCwd` now always do a lockfile lookup.
99+
98100
### Installs
99101

100102
- Yarn now caches npm version metadata, leading to faster resolution steps and decreased network data usage.

packages/plugin-dlx/sources/commands/dlx.ts

+2-2
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
import {BaseCommand, WorkspaceRequiredError} from '@yarnpkg/cli';
22
import {Configuration, MessageName, miscUtils, Project, stringifyMessageName} from '@yarnpkg/core';
33
import {scriptUtils, structUtils, formatUtils} from '@yarnpkg/core';
4-
import {NativePath, Filename, ppath, xfs, npath} from '@yarnpkg/fslib';
4+
import {NativePath, ppath, xfs, npath} from '@yarnpkg/fslib';
55
import {Command, Option, Usage} from 'clipanion';
66

77
// eslint-disable-next-line arca/no-default-export
@@ -51,7 +51,7 @@ export default class DlxCommand extends BaseCommand {
5151
await xfs.writeFilePromise(ppath.join(tmpDir, `yarn.lock`), ``);
5252

5353
const targetYarnrc = ppath.join(tmpDir, `.yarnrc.yml`);
54-
const projectCwd = await Configuration.findProjectCwd(this.context.cwd, Filename.lockfile);
54+
const projectCwd = await Configuration.findProjectCwd(this.context.cwd);
5555

5656
// We set enableGlobalCache to true for dlx calls to speed it up but only if the
5757
// project it's run in has enableGlobalCache set to false, otherwise we risk running into

packages/yarnpkg-core/sources/Configuration.ts

+7-39
Original file line numberDiff line numberDiff line change
@@ -999,14 +999,7 @@ async function checkYarnPath({configuration, selfPath}: {configuration: Configur
999999
return yarnPath;
10001000
}
10011001

1002-
export enum ProjectLookup {
1003-
LOCKFILE,
1004-
MANIFEST,
1005-
NONE,
1006-
}
1007-
10081002
export type FindProjectOptions = {
1009-
lookup?: ProjectLookup;
10101003
strict?: boolean;
10111004
usePathCheck?: PortablePath | null;
10121005
useRc?: boolean;
@@ -1096,7 +1089,7 @@ export class Configuration {
10961089
* way around).
10971090
*/
10981091

1099-
static async find(startingCwd: PortablePath, pluginConfiguration: PluginConfiguration | null, {lookup = ProjectLookup.LOCKFILE, strict = true, usePathCheck = null, useRc = true}: FindProjectOptions = {}) {
1092+
static async find(startingCwd: PortablePath, pluginConfiguration: PluginConfiguration | null, {strict = true, usePathCheck = null, useRc = true}: FindProjectOptions = {}) {
11001093
const environmentSettings = getEnvironmentSettings();
11011094
delete environmentSettings.rcFilename;
11021095

@@ -1167,24 +1160,7 @@ export class Configuration {
11671160
// We need to know the project root before being able to truly instantiate
11681161
// our configuration.
11691162

1170-
let projectCwd: PortablePath | null;
1171-
switch (lookup) {
1172-
case ProjectLookup.LOCKFILE: {
1173-
projectCwd = await Configuration.findProjectCwd(startingCwd, Filename.lockfile);
1174-
} break;
1175-
1176-
case ProjectLookup.MANIFEST: {
1177-
projectCwd = await Configuration.findProjectCwd(startingCwd, null);
1178-
} break;
1179-
1180-
case ProjectLookup.NONE: {
1181-
if (xfs.existsSync(ppath.join(startingCwd, `package.json`))) {
1182-
projectCwd = ppath.resolve(startingCwd);
1183-
} else {
1184-
projectCwd = null;
1185-
}
1186-
} break;
1187-
}
1163+
const projectCwd = await Configuration.findProjectCwd(startingCwd);
11881164

11891165
// Great! We now have enough information to really start to setup the
11901166
// core configuration object.
@@ -1416,7 +1392,7 @@ export class Configuration {
14161392
return {path, cwd, data};
14171393
}
14181394

1419-
static async findProjectCwd(startingCwd: PortablePath, lockfileFilename: Filename | null) {
1395+
static async findProjectCwd(startingCwd: PortablePath) {
14201396
let projectCwd = null;
14211397

14221398
let nextCwd = startingCwd;
@@ -1425,19 +1401,11 @@ export class Configuration {
14251401
while (nextCwd !== currentCwd) {
14261402
currentCwd = nextCwd;
14271403

1428-
if (xfs.existsSync(ppath.join(currentCwd, `package.json`)))
1429-
projectCwd = currentCwd;
1404+
if (xfs.existsSync(ppath.join(currentCwd, Filename.lockfile)))
1405+
return currentCwd;
14301406

1431-
if (lockfileFilename !== null) {
1432-
if (xfs.existsSync(ppath.join(currentCwd, lockfileFilename))) {
1433-
projectCwd = currentCwd;
1434-
break;
1435-
}
1436-
} else {
1437-
if (projectCwd !== null) {
1438-
break;
1439-
}
1440-
}
1407+
if (xfs.existsSync(ppath.join(currentCwd, Filename.manifest)))
1408+
projectCwd = currentCwd;
14411409

14421410
nextCwd = ppath.dirname(currentCwd);
14431411
}

packages/yarnpkg-core/sources/index.ts

+1-1
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,7 @@ import * as treeUtils from './treeUtils';
1313

1414
export {CACHE_VERSION, CACHE_CHECKPOINT, Cache} from './Cache';
1515
export {DEFAULT_RC_FILENAME, LEGACY_PLUGINS, TAG_REGEXP} from './Configuration';
16-
export {Configuration, FormatType, ProjectLookup, SettingsType, WindowsLinkType} from './Configuration';
16+
export {Configuration, FormatType, SettingsType, WindowsLinkType} from './Configuration';
1717
export type {PluginConfiguration, SettingsDefinition, PackageExtensionData} from './Configuration';
1818
export type {ConfigurationValueMap, ConfigurationDefinitionMap} from './Configuration';
1919
export type {Fetcher, FetchOptions, FetchResult, MinimalFetchOptions} from './Fetcher';

packages/yarnpkg-doctor/sources/cli.ts

+10-29
Original file line numberDiff line numberDiff line change
@@ -1,15 +1,15 @@
11
#!/usr/bin/env node
22

3-
import {getPluginConfiguration} from '@yarnpkg/cli';
4-
import {Cache, Configuration, Project, Report, Workspace, structUtils, ProjectLookup, Manifest, Descriptor, HardDependencies, ThrowReport, StreamReport, MessageName, Ident, ResolveOptions, FetchOptions} from '@yarnpkg/core';
5-
import {PortablePath, npath, ppath, xfs} from '@yarnpkg/fslib';
6-
import {Cli, Command, Builtins, Option} from 'clipanion';
7-
import globby from 'globby';
8-
import micromatch from 'micromatch';
9-
import {Module} from 'module';
10-
import * as ts from 'typescript';
3+
import {getPluginConfiguration} from '@yarnpkg/cli';
4+
import {Cache, Configuration, Project, Report, Workspace, structUtils, Manifest, Descriptor, HardDependencies, ThrowReport, StreamReport, MessageName, Ident, ResolveOptions, FetchOptions} from '@yarnpkg/core';
5+
import {PortablePath, npath, ppath, xfs} from '@yarnpkg/fslib';
6+
import {Cli, Command, Builtins, Option} from 'clipanion';
7+
import globby from 'globby';
8+
import micromatch from 'micromatch';
9+
import {Module} from 'module';
10+
import * as ts from 'typescript';
1111

12-
import * as ast from './ast';
12+
import * as ast from './ast';
1313

1414
const BUILTINS = new Set([
1515
...(Module.builtinModules || []),
@@ -333,14 +333,7 @@ class EntryCommand extends Command {
333333

334334
const pluginConfiguration = getPluginConfiguration();
335335

336-
const findStandaloneWorkspace = async (manifestCwd: PortablePath) => {
337-
const configuration = await Configuration.find(manifestCwd, pluginConfiguration, {strict: false, lookup: ProjectLookup.NONE});
338-
const {workspace} = await Project.find(configuration, manifestCwd);
339-
340-
return workspace;
341-
};
342-
343-
const findLockfileWorkspace = async (manifestCwd: PortablePath) => {
336+
const findWorkspace = async (manifestCwd: PortablePath) => {
344337
const configuration = await Configuration.find(manifestCwd, pluginConfiguration, {strict: false});
345338
if (!configuration.projectCwd)
346339
return null;
@@ -353,18 +346,6 @@ class EntryCommand extends Command {
353346
return workspace;
354347
};
355348

356-
const findWorkspace = async (manifestCwd: PortablePath) => {
357-
const lockfileWorkspace = await findLockfileWorkspace(manifestCwd);
358-
if (lockfileWorkspace)
359-
return lockfileWorkspace;
360-
361-
const standaloneWorkspace = await findStandaloneWorkspace(manifestCwd);
362-
if (standaloneWorkspace)
363-
return standaloneWorkspace;
364-
365-
return null;
366-
};
367-
368349
const report = await StreamReport.start({
369350
configuration,
370351
stdout: this.context.stdout,

0 commit comments

Comments
 (0)