Skip to content

Commit

Permalink
fix: set NODE_PATH when is turned on (#5251)
Browse files Browse the repository at this point in the history
Co-authored-by: Zoltan Kochan <z@kochan.io>
  • Loading branch information
d3lm and zkochan committed Aug 23, 2022
1 parent a12d1a0 commit 2aa22e4
Show file tree
Hide file tree
Showing 29 changed files with 143 additions and 13 deletions.
15 changes: 15 additions & 0 deletions .changeset/strong-panthers-agree.md
@@ -0,0 +1,15 @@
---
"@pnpm/config": minor
"@pnpm/core": minor
"@pnpm/headless": minor
"@pnpm/plugin-commands-deploy": minor
"@pnpm/plugin-commands-installation": minor
"@pnpm/plugin-commands-listing": minor
"@pnpm/plugin-commands-outdated": minor
"@pnpm/plugin-commands-patching": minor
"@pnpm/plugin-commands-publishing": minor
"@pnpm/plugin-commands-rebuild": minor
"@pnpm/plugin-commands-script-runners": minor
---

Set `NODE_PATH` when `preferSymlinkedExecutables` is enabled.
1 change: 1 addition & 0 deletions packages/config/package.json
Expand Up @@ -44,6 +44,7 @@
"can-write-to-dir": "^1.1.1",
"is-subdir": "^1.2.0",
"normalize-registry-url": "2.0.0",
"path-absolute": "^1.0.1",
"path-name": "^1.0.0",
"ramda": "npm:@pnpm/ramda@0.28.1",
"realpath-missing": "^1.1.0",
Expand Down
1 change: 1 addition & 0 deletions packages/config/src/Config.ts
Expand Up @@ -19,6 +19,7 @@ export interface Config {
cliOptions: Record<string, any>, // eslint-disable-line
useBetaCli: boolean
extraBinPaths: string[]
extraEnv: Record<string, string>
filter: string[]
filterProd: string[]
rawLocalConfig: Record<string, any>, // eslint-disable-line
Expand Down
16 changes: 16 additions & 0 deletions packages/config/src/index.ts
Expand Up @@ -12,6 +12,7 @@ import camelcase from 'camelcase'
import normalizeRegistryUrl from 'normalize-registry-url'
import fromPairs from 'ramda/src/fromPairs'
import realpathMissing from 'realpath-missing'
import pathAbsolute from 'path-absolute'
import whichcb from 'which'
import { checkGlobalBinDir } from './checkGlobalBinDir'
import getScopeRegistries from './getScopeRegistries'
Expand Down Expand Up @@ -402,6 +403,21 @@ export default async (
} else {
pnpmConfig.extraBinPaths = []
}

if (pnpmConfig.preferSymlinkedExecutables) {
const cwd = pnpmConfig.lockfileDir ?? pnpmConfig.dir

const virtualStoreDir = pnpmConfig.virtualStoreDir
? pnpmConfig.virtualStoreDir
: pnpmConfig.modulesDir
? path.join(pnpmConfig.modulesDir, '.pnpm')
: 'node_modules/.pnpm'

pnpmConfig.extraEnv = {
NODE_PATH: pathAbsolute(path.join(virtualStoreDir, 'node_modules'), cwd),
}
}

if (pnpmConfig['shamefullyFlatten']) {
warnings.push('The "shamefully-flatten" setting has been renamed to "shamefully-hoist". Also, in most cases you won\'t need "shamefully-hoist". Since v4, a semistrict node_modules structure is on by default (via hoist-pattern=[*]).')
pnpmConfig.shamefullyHoist = true
Expand Down
1 change: 1 addition & 0 deletions packages/core/src/install/extendInstallOptions.ts
Expand Up @@ -24,6 +24,7 @@ export interface StrictInstallOptions {
frozenLockfileIfExists: boolean
enablePnp: boolean
extraBinPaths: string[]
extraEnv: Record<string, string>
hoistingLimits?: HoistingLimits
useLockfile: boolean
saveLockfile: boolean
Expand Down
13 changes: 10 additions & 3 deletions packages/core/src/install/index.ts
Expand Up @@ -228,6 +228,7 @@ export async function mutateModules (
async function _install (): Promise<UpdatedProject[]> {
const scriptsOpts: RunLifecycleHooksConcurrentlyOptions = {
extraBinPaths: opts.extraBinPaths,
extraEnv: opts.extraEnv,
rawConfig: opts.rawConfig,
scriptsPrependNodePath: opts.scriptsPrependNodePath,
scriptShell: opts.scriptShell,
Expand Down Expand Up @@ -961,9 +962,12 @@ const _installInContext: InstallFunction = async (projects, ctx, opts) => {
const depPaths = Object.keys(dependenciesGraph)
const rootNodes = depPaths.filter((depPath) => dependenciesGraph[depPath].depth === 0)

let extraEnv: Record<string, string> | undefined
let extraEnv: Record<string, string> | undefined = opts.scriptsOpts.extraEnv
if (opts.enablePnp) {
extraEnv = makeNodeRequireOption(path.join(opts.lockfileDir, '.pnp.cjs'))
extraEnv = {
...extraEnv,
...makeNodeRequireOption(path.join(opts.lockfileDir, '.pnp.cjs')),
}
}
await buildModules(dependenciesGraph, rootNodes, {
childConcurrency: opts.childConcurrency,
Expand Down Expand Up @@ -1107,7 +1111,10 @@ const _installInContext: InstallFunction = async (projects, ctx, opts) => {
])
if (!opts.ignoreScripts) {
if (opts.enablePnp) {
opts.scriptsOpts.extraEnv = makeNodeRequireOption(path.join(opts.lockfileDir, '.pnp.cjs'))
opts.scriptsOpts.extraEnv = {
...opts.scriptsOpts.extraEnv,
...makeNodeRequireOption(path.join(opts.lockfileDir, '.pnp.cjs')),
}
}
const projectsToBeBuilt = projectsWithTargetDirs.filter(({ mutation }) => mutation === 'install') as ProjectToBeInstalled[]
await runLifecycleHooksConcurrently(['preinstall', 'install', 'postinstall', 'prepare'],
Expand Down
9 changes: 7 additions & 2 deletions packages/headless/src/index.ts
Expand Up @@ -100,6 +100,7 @@ export interface HeadlessOptions {
enablePnp?: boolean
engineStrict: boolean
extraBinPaths?: string[]
extraEnv?: Record<string, string>
extraNodePaths?: string[]
preferSymlinkedExecutables?: boolean
hoistingLimits?: HoistingLimits
Expand Down Expand Up @@ -186,6 +187,7 @@ export default async (opts: HeadlessOptions) => {
const scriptsOpts = {
optional: false,
extraBinPaths: opts.extraBinPaths,
extraEnv: opts.extraEnv,
rawConfig: opts.rawConfig,
scriptsPrependNodePath: opts.scriptsPrependNodePath,
scriptShell: opts.scriptShell,
Expand Down Expand Up @@ -427,9 +429,12 @@ export default async (opts: HeadlessOptions) => {
if (opts.hoistPattern != null) {
extraBinPaths.unshift(path.join(virtualStoreDir, 'node_modules/.bin'))
}
let extraEnv: Record<string, string> | undefined
let extraEnv: Record<string, string> | undefined = opts.extraEnv
if (opts.enablePnp) {
extraEnv = makeNodeRequireOption(path.join(opts.lockfileDir, '.pnp.cjs'))
extraEnv = {
...extraEnv,
...makeNodeRequireOption(path.join(opts.lockfileDir, '.pnp.cjs')),
}
}
await buildModules(graph, Array.from(directNodes), {
childConcurrency: opts.childConcurrency,
Expand Down
1 change: 1 addition & 0 deletions packages/plugin-commands-deploy/test/utils/index.ts
Expand Up @@ -12,6 +12,7 @@ export const DEFAULT_OPTS = {
ca: undefined,
cacheDir: '../cache',
cert: undefined,
extraEnv: {},
cliOptions: {},
fetchRetries: 2,
fetchRetryFactor: 90,
Expand Down
1 change: 1 addition & 0 deletions packages/plugin-commands-installation/src/install.ts
Expand Up @@ -279,6 +279,7 @@ export type InstallCommandOptions = Pick<Config,
| 'virtualStoreDir'
| 'workspaceConcurrency'
| 'workspaceDir'
| 'extraEnv'
> & CreateStoreControllerOptions & {
argv: {
original: string[]
Expand Down
1 change: 1 addition & 0 deletions packages/plugin-commands-installation/src/installDeps.ts
Expand Up @@ -75,6 +75,7 @@ export type InstallDepsOptions = Pick<Config,
| 'optional'
| 'workspaceConcurrency'
| 'workspaceDir'
| 'extraEnv'
> & CreateStoreControllerOptions & {
argv: {
original: string[]
Expand Down
1 change: 1 addition & 0 deletions packages/plugin-commands-installation/test/add.ts
Expand Up @@ -16,6 +16,7 @@ const DEFAULT_OPTIONS = {
bail: false,
bin: 'node_modules/.bin',
cacheDir: path.join(tmp, 'cache'),
extraEnv: {},
cliOptions: {},
include: {
dependencies: true,
Expand Down
1 change: 1 addition & 0 deletions packages/plugin-commands-installation/test/global.ts
Expand Up @@ -16,6 +16,7 @@ const DEFAULT_OPTIONS = {
bail: false,
bin: 'node_modules/.bin',
cacheDir: path.join(tmp, 'cache'),
extraEnv: {},
cliOptions: {},
include: {
dependencies: true,
Expand Down
Expand Up @@ -14,6 +14,7 @@ const DEFAULT_OPTIONS = {
bail: false,
bin: 'node_modules/.bin',
cacheDir: path.join(TMP, 'cache'),
extraEnv: {},
cliOptions: {},
include: {
dependencies: true,
Expand Down
1 change: 1 addition & 0 deletions packages/plugin-commands-installation/test/prune.ts
Expand Up @@ -13,6 +13,7 @@ const DEFAULT_OPTIONS = {
},
bail: false,
bin: 'node_modules/.bin',
extraEnv: {},
cliOptions: {},
include: {
dependencies: true,
Expand Down
Expand Up @@ -25,6 +25,7 @@ const DEFAULT_OPTIONS = {
},
bail: false,
bin: 'node_modules/.bin',
extraEnv: {},
cliOptions: {},
include: {
dependencies: true,
Expand Down
1 change: 1 addition & 0 deletions packages/plugin-commands-installation/test/utils/index.ts
Expand Up @@ -12,6 +12,7 @@ export const DEFAULT_OPTS = {
ca: undefined,
cacheDir: '../cache',
cert: undefined,
extraEnv: {},
cliOptions: {},
fetchRetries: 2,
fetchRetryFactor: 90,
Expand Down
1 change: 1 addition & 0 deletions packages/plugin-commands-listing/test/utils/index.ts
Expand Up @@ -11,6 +11,7 @@ export const DEFAULT_OPTS = {
bin: 'node_modules/.bin',
ca: undefined,
cert: undefined,
extraEnv: {},
cliOptions: {},
fetchRetries: 2,
fetchRetryFactor: 90,
Expand Down
1 change: 1 addition & 0 deletions packages/plugin-commands-outdated/test/utils/index.ts
Expand Up @@ -12,6 +12,7 @@ export const DEFAULT_OPTS = {
ca: undefined,
cacheDir: '../cache',
cert: undefined,
extraEnv: {},
cliOptions: {},
fetchRetries: 2,
fetchRetryFactor: 90,
Expand Down
1 change: 1 addition & 0 deletions packages/plugin-commands-patching/test/utils/index.ts
Expand Up @@ -12,6 +12,7 @@ export const DEFAULT_OPTS = {
ca: undefined,
cacheDir: '../cache',
cert: undefined,
extraEnv: {},
cliOptions: {},
fetchRetries: 2,
fetchRetryFactor: 90,
Expand Down
1 change: 1 addition & 0 deletions packages/plugin-commands-publishing/src/publish.ts
Expand Up @@ -171,6 +171,7 @@ Do you want to continue?`,
const _runScriptsIfPresent = runScriptsIfPresent.bind(null, {
depPath: dir,
extraBinPaths: opts.extraBinPaths,
extraEnv: opts.extraEnv,
pkgRoot: dir,
rawConfig: opts.rawConfig,
rootModulesDir: await realpathMissing(path.join(dir, 'node_modules')),
Expand Down
Expand Up @@ -27,6 +27,7 @@ Partial<Pick<Config,
| 'force'
| 'dryRun'
| 'extraBinPaths'
| 'extraEnv'
| 'fetchRetries'
| 'fetchRetryFactor'
| 'fetchRetryMaxtimeout'
Expand Down
Expand Up @@ -9,6 +9,7 @@ export interface StrictRebuildOptions {
cacheDir: string
childConcurrency: number
extraBinPaths: string[]
extraEnv: Record<string, string>
lockfileDir: string
nodeLinker: 'isolated' | 'hoisted' | 'pnp'
scriptShell?: string
Expand Down
2 changes: 2 additions & 0 deletions packages/plugin-commands-rebuild/src/implementation/index.ts
Expand Up @@ -154,6 +154,7 @@ export async function rebuildProjects (
const store = await createOrConnectStoreController(opts)
const scriptsOpts = {
extraBinPaths: ctx.extraBinPaths,
extraEnv: opts.extraEnv,
rawConfig: opts.rawConfig,
scriptsPrependNodePath: opts.scriptsPrependNodePath,
scriptShell: opts.scriptShell,
Expand Down Expand Up @@ -278,6 +279,7 @@ async function _rebuild (
await runPostinstallHooks({
depPath,
extraBinPaths: ctx.extraBinPaths,
extraEnv: opts.extraEnv,
optional: pkgSnapshot.optional === true,
pkgRoot,
rawConfig: opts.rawConfig,
Expand Down
9 changes: 5 additions & 4 deletions packages/plugin-commands-script-runners/src/exec.ts
Expand Up @@ -83,7 +83,7 @@ export async function handler (
sort?: boolean
workspaceConcurrency?: number
shellMode?: boolean
} & Pick<Config, 'extraBinPaths' | 'lockfileDir' | 'dir' | 'userAgent' | 'recursive' | 'workspaceDir'>,
} & Pick<Config, 'extraBinPaths' | 'extraEnv' | 'lockfileDir' | 'dir' | 'userAgent' | 'recursive' | 'workspaceDir'>,
params: string[]
) {
// For backward compatibility
Expand Down Expand Up @@ -129,9 +129,10 @@ export async function handler (
limitRun(async () => {
try {
const pnpPath = workspacePnpPath ?? await existsPnp(prefix)
const extraEnv = pnpPath
? makeNodeRequireOption(pnpPath)
: {}
const extraEnv = {
...opts.extraEnv,
...(pnpPath ? makeNodeRequireOption(pnpPath) : {}),
}
const env = makeEnv({
extraEnv: {
...extraEnv,
Expand Down
8 changes: 6 additions & 2 deletions packages/plugin-commands-script-runners/src/run.ts
Expand Up @@ -116,7 +116,7 @@ For options that may be used with `-r`, see "pnpm help recursive"',
export type RunOpts =
& Omit<RecursiveRunOpts, 'allProjects' | 'selectedProjectsGraph' | 'workspaceDir'>
& { recursive?: boolean }
& Pick<Config, 'dir' | 'engineStrict' | 'extraBinPaths' | 'reporter' | 'scriptsPrependNodePath' | 'scriptShell' | 'shellEmulator' | 'enablePrePostScripts' | 'userAgent'>
& Pick<Config, 'dir' | 'engineStrict' | 'extraBinPaths' | 'reporter' | 'scriptsPrependNodePath' | 'scriptShell' | 'shellEmulator' | 'enablePrePostScripts' | 'userAgent' | 'extraEnv'>
& (
& { recursive?: false }
& Partial<Pick<Config, 'allProjects' | 'selectedProjectsGraph' | 'workspaceDir'>>
Expand Down Expand Up @@ -174,6 +174,7 @@ so you may run "pnpm -w run ${scriptName}"`,
const lifecycleOpts: RunLifecycleHookOptions = {
depPath: dir,
extraBinPaths: opts.extraBinPaths,
extraEnv: opts.extraEnv,
pkgRoot: dir,
rawConfig: opts.rawConfig,
rootModulesDir: await realpathMissing(path.join(dir, 'node_modules')),
Expand All @@ -188,7 +189,10 @@ so you may run "pnpm -w run ${scriptName}"`,
const pnpPath = (opts.workspaceDir && await existsPnp(opts.workspaceDir)) ??
await existsPnp(dir)
if (pnpPath) {
lifecycleOpts.extraEnv = makeNodeRequireOption(pnpPath)
lifecycleOpts.extraEnv = {
...lifecycleOpts.extraEnv,
...makeNodeRequireOption(pnpPath),
}
}
try {
if (
Expand Down
8 changes: 6 additions & 2 deletions packages/plugin-commands-script-runners/src/runRecursive.ts
Expand Up @@ -21,7 +21,7 @@ export type RecursiveRunOpts = Pick<Config,
| 'shellEmulator'
| 'stream'
> & Required<Pick<Config, 'allProjects' | 'selectedProjectsGraph' | 'workspaceDir'>> &
Partial<Pick<Config, 'extraBinPaths' | 'bail' | 'reverse' | 'sort' | 'workspaceConcurrency'>> &
Partial<Pick<Config, 'extraBinPaths' | 'extraEnv' | 'bail' | 'reverse' | 'sort' | 'workspaceConcurrency'>> &
{
ifPresent?: boolean
}
Expand Down Expand Up @@ -72,6 +72,7 @@ export default async (
const lifecycleOpts: RunLifecycleHookOptions = {
depPath: prefix,
extraBinPaths: opts.extraBinPaths,
extraEnv: opts.extraEnv,
pkgRoot: prefix,
rawConfig: opts.rawConfig,
rootModulesDir: await realpathMissing(path.join(prefix, 'node_modules')),
Expand All @@ -83,7 +84,10 @@ export default async (
}
const pnpPath = workspacePnpPath ?? await existsPnp(prefix)
if (pnpPath) {
lifecycleOpts.extraEnv = makeNodeRequireOption(pnpPath)
lifecycleOpts.extraEnv = {
...lifecycleOpts.extraEnv,
...makeNodeRequireOption(pnpPath),
}
}
if (
opts.enablePrePostScripts &&
Expand Down

0 comments on commit 2aa22e4

Please sign in to comment.