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: set NODE_PATH when is turned on #5251

Merged
merged 9 commits into from Aug 23, 2022
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
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
12 changes: 12 additions & 0 deletions packages/config/src/index.ts
Expand Up @@ -402,6 +402,18 @@ export default async (
} else {
pnpmConfig.extraBinPaths = []
}

if (pnpmConfig.preferSymlinkedExecutables && pnpmConfig.virtualStoreDir) {
pnpmConfig.extraEnv = {
NODE_PATH: path.join(
pnpmConfig.lockfileDir ?? pnpmConfig.dir,
'node_modules',
pnpmConfig.virtualStoreDir,
d3lm marked this conversation as resolved.
Show resolved Hide resolved
'node_modules'
),
}
}

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-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-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
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
10 changes: 8 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 All @@ -134,6 +134,8 @@ export async function handler (
opts: RunOpts,
params: string[]
) {
console.log(opts.extraEnv)

let dir: string
const [scriptName, ...passedThruArgs] = params
if (opts.recursive) {
Expand Down Expand Up @@ -174,6 +176,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 +191,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