diff --git a/.yarn/versions/bd149584.yml b/.yarn/versions/bd149584.yml new file mode 100644 index 000000000000..c02874084f98 --- /dev/null +++ b/.yarn/versions/bd149584.yml @@ -0,0 +1,6 @@ +releases: + "@yarnpkg/cli": patch + "@yarnpkg/core": patch + "@yarnpkg/plugin-nm": patch + "@yarnpkg/plugin-pnp": patch + "@yarnpkg/plugin-pnpm": patch diff --git a/packages/plugin-nm/sources/NodeModulesLinker.ts b/packages/plugin-nm/sources/NodeModulesLinker.ts index 15d23f5e5e59..a00955cd7502 100644 --- a/packages/plugin-nm/sources/NodeModulesLinker.ts +++ b/packages/plugin-nm/sources/NodeModulesLinker.ts @@ -9,7 +9,7 @@ import {ZipOpenFS} from import {buildNodeModulesTree} from '@yarnpkg/nm'; import {NodeModulesLocatorMap, buildLocatorMap, NodeModulesHoistingLimits} from '@yarnpkg/nm'; import {parseSyml} from '@yarnpkg/parsers'; -import {jsInstallUtils} from '@yarnpkg/plugin-pnp'; +import {NodeLinker, jsInstallUtils} from '@yarnpkg/plugin-pnp'; import {PnpApi, PackageInformation} from '@yarnpkg/pnp'; import cmdShim from '@zkochan/cmd-shim'; import {UsageError} from 'clipanion'; @@ -108,7 +108,7 @@ export class NodeModulesLinker implements Linker { } private isEnabled(opts: MinimalLinkOptions) { - return opts.project.configuration.get(`nodeLinker`) === `node-modules`; + return opts.project.configuration.get(`nodeLinker`) === NodeLinker.NODE_MODULES; } } @@ -218,7 +218,7 @@ class NodeModulesInstaller implements Installer { } async finalizeInstall() { - if (this.opts.project.configuration.get(`nodeLinker`) !== `node-modules`) + if (this.opts.project.configuration.get(`nodeLinker`) !== NodeLinker.NODE_MODULES) return undefined; const defaultFsLayer = new VirtualFS({ diff --git a/packages/plugin-pnp/sources/PnpLinker.ts b/packages/plugin-pnp/sources/PnpLinker.ts index d19646d6adef..bd4622b1c6ad 100644 --- a/packages/plugin-pnp/sources/PnpLinker.ts +++ b/packages/plugin-pnp/sources/PnpLinker.ts @@ -5,7 +5,7 @@ import {AliasFS, CwdFS, PortablePath, VirtualFS, npath, ppath, xfs} import {generateInlinedScript, generateSplitScript, PackageRegistry, PnpApi, PnpSettings, getESMLoaderTemplate} from '@yarnpkg/pnp'; import {UsageError} from 'clipanion'; -import {getPnpPath} from './index'; +import {NodeLinker, getPnpPath} from './index'; import * as jsInstallUtils from './jsInstallUtils'; import * as pnpUtils from './pnpUtils'; @@ -76,7 +76,7 @@ export class PnpLinker implements Linker { } private isEnabled(opts: MinimalLinkOptions) { - if (opts.project.configuration.get(`nodeLinker`) !== `pnp`) + if (opts.project.configuration.get(`nodeLinker`) !== NodeLinker.PNP) return false; if (opts.project.configuration.get(`pnpMode`) !== this.mode) @@ -239,7 +239,7 @@ export class PnpInstaller implements Installer { if (!this.isEsmEnabled()) await xfs.removePromise(pnpPath.esmLoader); - if (this.opts.project.configuration.get(`nodeLinker`) !== `pnp`) { + if (this.opts.project.configuration.get(`nodeLinker`) !== NodeLinker.PNP) { await xfs.removePromise(pnpPath.cjs); await xfs.removePromise(pnpPath.data); await xfs.removePromise(pnpPath.esmLoader); diff --git a/packages/plugin-pnp/sources/commands/unplug.ts b/packages/plugin-pnp/sources/commands/unplug.ts index e2931b9e4b5a..167d5ade02b8 100644 --- a/packages/plugin-pnp/sources/commands/unplug.ts +++ b/packages/plugin-pnp/sources/commands/unplug.ts @@ -4,6 +4,7 @@ import {structUtils, semverUtils} import {Command, Option, Usage, UsageError} from 'clipanion'; import micromatch from 'micromatch'; +import {NodeLinker} from '../index'; import * as pnpUtils from '../pnpUtils'; // eslint-disable-next-line arca/no-default-export @@ -71,7 +72,7 @@ export default class UnplugCommand extends BaseCommand { if (!workspace) throw new WorkspaceRequiredError(project.cwd, this.context.cwd); - if (configuration.get(`nodeLinker`) !== `pnp`) + if (configuration.get(`nodeLinker`) !== NodeLinker.PNP) throw new UsageError(`This command can only be used if the \`nodeLinker\` option is set to \`pnp\``); await project.restoreInstallState(); diff --git a/packages/plugin-pnp/sources/index.ts b/packages/plugin-pnp/sources/index.ts index 9eeed3fc77ae..5c15e3a84af8 100644 --- a/packages/plugin-pnp/sources/index.ts +++ b/packages/plugin-pnp/sources/index.ts @@ -38,7 +38,7 @@ async function setupScriptEnvironment(project: Project, env: {[key: string]: str // We remove the PnP hook from NODE_OPTIONS because the process can have // NODE_OPTIONS set while changing linkers, which affects build scripts. - if (project.configuration.get(`nodeLinker`) !== `pnp`) { + if (project.configuration.get(`nodeLinker`) !== NodeLinker.PNP) { env.NODE_OPTIONS = nodeOptions; return; } @@ -77,6 +77,12 @@ declare module '@yarnpkg/core' { } } +export enum NodeLinker { + PNP = `pnp`, + PNPM = `pnpm`, + NODE_MODULES = `node-modules`, +} + const plugin: Plugin = { hooks: { populateYarnPaths, @@ -84,9 +90,9 @@ const plugin: Plugin = { }, configuration: { nodeLinker: { - description: `The linker used for installing Node packages, one of: "pnp", "node-modules"`, + description: `The linker used for installing Node packages, one of: ${Object.values(NodeLinker).map(v => `"${v}"`).join(`, `)}}`, type: SettingsType.STRING, - default: `pnp`, + default: NodeLinker.PNP, }, winLinkType: { description: `Whether Yarn should use Windows Junctions or symlinks when creating links on Windows.`, diff --git a/packages/plugin-pnpm/sources/PnpmLinker.ts b/packages/plugin-pnpm/sources/PnpmLinker.ts index 5627b758f5de..0fbe0436ea93 100644 --- a/packages/plugin-pnpm/sources/PnpmLinker.ts +++ b/packages/plugin-pnpm/sources/PnpmLinker.ts @@ -1,6 +1,6 @@ import {Descriptor, FetchResult, formatUtils, Installer, InstallPackageExtraApi, Linker, LinkOptions, LinkType, Locator, LocatorHash, Manifest, MessageName, MinimalLinkOptions, Package, Project, miscUtils, structUtils, WindowsLinkType} from '@yarnpkg/core'; import {Filename, PortablePath, setupCopyIndex, ppath, xfs, DirentNoPath} from '@yarnpkg/fslib'; -import {jsInstallUtils} from '@yarnpkg/plugin-pnp'; +import {NodeLinker, jsInstallUtils} from '@yarnpkg/plugin-pnp'; import {UsageError} from 'clipanion'; export type PnpmCustomData = { @@ -76,7 +76,7 @@ export class PnpmLinker implements Linker { } private isEnabled(opts: MinimalLinkOptions) { - return opts.project.configuration.get(`nodeLinker`) === `pnpm`; + return opts.project.configuration.get(`nodeLinker`) === NodeLinker.PNPM; } } @@ -170,7 +170,7 @@ class PnpmInstaller implements Installer { } async attachInternalDependencies(locator: Locator, dependencies: Array<[Descriptor, Locator]>) { - if (this.opts.project.configuration.get(`nodeLinker`) !== `pnpm`) + if (this.opts.project.configuration.get(`nodeLinker`) !== NodeLinker.PNPM) return; // We don't install those packages at all, because they can't be used anyway @@ -264,7 +264,7 @@ class PnpmInstaller implements Installer { async finalizeInstall() { const storeLocation = getStoreLocation(this.opts.project); - if (this.opts.project.configuration.get(`nodeLinker`) !== `pnpm`) { + if (this.opts.project.configuration.get(`nodeLinker`) !== NodeLinker.PNPM) { await xfs.removePromise(storeLocation); } else { let extraneous: Set; @@ -295,7 +295,7 @@ class PnpmInstaller implements Installer { await this.asyncActions.wait(); await removeIfEmpty(storeLocation); - if (this.opts.project.configuration.get(`nodeLinker`) !== `node-modules`) + if (this.opts.project.configuration.get(`nodeLinker`) !== NodeLinker.NODE_MODULES) await removeIfEmpty(getNodeModulesLocation(this.opts.project)); return {