diff --git a/packages/core/package-manager/src/Pnpm.js b/packages/core/package-manager/src/Pnpm.js index ef61ff2673d..932983feaee 100644 --- a/packages/core/package-manager/src/Pnpm.js +++ b/packages/core/package-manager/src/Pnpm.js @@ -2,6 +2,8 @@ import type {PackageInstaller, InstallerOptions} from './types'; +import path from 'path'; +import fs from 'fs'; import commandExists from 'command-exists'; import spawn from 'cross-spawn'; import logger from '@parcel/logger'; @@ -9,7 +11,7 @@ import split from 'split2'; import JSONParseStream from './JSONParseStream'; import promiseFromProcess from './promiseFromProcess'; import {registerSerializableClass} from '@parcel/core'; -import {npmSpecifierFromModuleRequest} from './utils'; +import {exec, npmSpecifierFromModuleRequest} from './utils'; // $FlowFixMe import pkg from '../package.json'; @@ -62,6 +64,8 @@ type PNPMResults = {| |}; let hasPnpm: ?boolean; +let pnpmVersion: ?number; + export class Pnpm implements PackageInstaller { static async exists(): Promise { if (hasPnpm != null) { @@ -82,9 +86,23 @@ export class Pnpm implements PackageInstaller { cwd, saveDev = true, }: InstallerOptions): Promise { + if (pnpmVersion == null) { + let version = await exec('pnpm --version'); + pnpmVersion = parseInt(version.stdout, 10); + } + let args = ['add', '--reporter', 'ndjson']; if (saveDev) { - args.push('-D', '-W'); + args.push('-D'); + } + if (pnpmVersion >= 7) { + if (fs.existsSync(path.join(cwd, 'pnpm-workspace.yaml'))) { + // installs in workspace root (regardless of cwd) + args.push('-w'); + } + } else { + // ignores workspace root check + args.push('-W'); } args = args.concat(modules.map(npmSpecifierFromModuleRequest)); @@ -148,9 +166,9 @@ export class Pnpm implements PackageInstaller { if (addedCount > 0 || removedCount > 0) { logger.log({ origin: '@parcel/package-manager', - message: `Added ${addedCount} and ${ - removedCount > 0 ? `removed ${removedCount}` : '' - } packages via pnpm`, + message: `Added ${addedCount} ${ + removedCount > 0 ? `and removed ${removedCount} ` : '' + }packages via pnpm`, }); } diff --git a/packages/core/package-manager/src/Yarn.js b/packages/core/package-manager/src/Yarn.js index 24d33e431f8..b541ecb3ca1 100644 --- a/packages/core/package-manager/src/Yarn.js +++ b/packages/core/package-manager/src/Yarn.js @@ -4,20 +4,17 @@ import type {PackageInstaller, InstallerOptions} from './types'; import commandExists from 'command-exists'; import spawn from 'cross-spawn'; -import {exec as _exec} from 'child_process'; -import {promisify} from 'util'; import logger from '@parcel/logger'; import split from 'split2'; import JSONParseStream from './JSONParseStream'; import promiseFromProcess from './promiseFromProcess'; import {registerSerializableClass} from '@parcel/core'; -import {npmSpecifierFromModuleRequest} from './utils'; +import {exec, npmSpecifierFromModuleRequest} from './utils'; // $FlowFixMe import pkg from '../package.json'; const YARN_CMD = 'yarn'; -const exec = promisify(_exec); type YarnStdOutMessage = | {| diff --git a/packages/core/package-manager/src/utils.js b/packages/core/package-manager/src/utils.js index 9d90f58c507..27a752cb67f 100644 --- a/packages/core/package-manager/src/utils.js +++ b/packages/core/package-manager/src/utils.js @@ -7,6 +7,14 @@ import type {FileSystem} from '@parcel/fs'; import invariant from 'assert'; import ThrowableDiagnostic from '@parcel/diagnostic'; import {resolveConfig} from '@parcel/utils'; +import {exec as _exec} from 'child_process'; +import {promisify} from 'util'; + +export const exec: ( + command: string, + options?: child_process$execOpts, +) => Promise<{|stdout: string | Buffer, stderr: string | Buffer|}> = + promisify(_exec); export function npmSpecifierFromModuleRequest( moduleRequest: ModuleRequest, diff --git a/packages/utils/node-resolver-core/src/Wrapper.js b/packages/utils/node-resolver-core/src/Wrapper.js index 05a308013d1..0e199457c54 100644 --- a/packages/utils/node-resolver-core/src/Wrapper.js +++ b/packages/utils/node-resolver-core/src/Wrapper.js @@ -281,6 +281,9 @@ export default class NodeResolver { }, ); + // Need to clear the resolver caches after installing the package + this.resolversByEnv.clear(); + // Re-resolve return this.resolve({ ...options,