fix: the value of INIT_CWD env variable for dep scripts

PR #2897
This commit is contained in:
Zoltan Kochan
2020-09-26 17:05:42 +03:00
committed by GitHub
parent a11aff2990
commit 203e65ac89
6 changed files with 43 additions and 20 deletions

View File

@@ -0,0 +1,5 @@
---
"@pnpm/build-modules": patch
---
The INIT_CWD env variable is always set to the lockfile directory, for scripts of dependencies.

View File

@@ -0,0 +1,5 @@
---
"@pnpm/lifecycle": minor
---
A new option added to set the INIT_CWD env variable for scripts: opts.initCwd.

View File

@@ -79,6 +79,7 @@ async function buildDependency (
const hasSideEffects = await runPostinstallHooks({
depPath,
extraBinPaths: opts.extraBinPaths,
initCwd: opts.lockfileDir,
optional: depNode.optional,
pkgRoot: depNode.dir,
prepare: depNode.prepare,

View File

@@ -11,6 +11,7 @@ export async function runPostinstallHooks (
opts: {
depPath: string
extraBinPaths?: string[]
initCwd?: string
optional?: boolean
pkgRoot: string
prepare?: boolean

View File

@@ -8,6 +8,7 @@ export interface RunLifecycleHookOptions {
args?: string[]
depPath: string
extraBinPaths?: string[]
initCwd?: string
optional?: boolean
pkgRoot: string
rawConfig: object
@@ -52,7 +53,10 @@ export default async function runLifecycleHook (
config: opts.rawConfig,
dir: opts.rootModulesDir,
extraBinPaths: opts.extraBinPaths ?? [],
extraEnv: { PNPM_SCRIPT_SRC_DIR: opts.pkgRoot },
extraEnv: {
INIT_CWD: opts.initCwd ?? process.cwd(),
PNPM_SCRIPT_SRC_DIR: opts.pkgRoot,
},
log: {
clearProgress: noop,
info: noop,

View File

@@ -11,6 +11,7 @@ import promisifyTape from 'tape-promise'
import { testDefaults } from '../utils'
import rimraf = require('@zkochan/rimraf')
import loadJsonFile = require('load-json-file')
import fs = require('mz/fs')
import exists = require('path-exists')
import PATH = require('path-name')
import sinon = require('sinon')
@@ -142,32 +143,38 @@ test('installation fails if lifecycle script fails', async (t: tape.Test) => {
}
})
// TODO: unskip
// For some reason this fails on CI environments
// eslint-disable-next-line @typescript-eslint/dot-notation
test.skip('creates env for scripts', async (t: tape.Test) => {
test('INIT_CWD is always set to lockfile directory', async (t: tape.Test) => {
prepareEmpty(t)
const manifest = await addDependenciesToPackage({
scripts: {
install: 'node -e "process.stdout.write(process.env.INIT_CWD)" | json-append output.json',
const rootDir = process.cwd()
await fs.mkdir('subd')
process.chdir('subd')
await mutateModules([
{
buildIndex: 0,
mutation: 'install',
manifest: {
dependencies: {
'json-append': '1.1.1',
'write-lifecycle-env': '1.0.0',
},
scripts: {
install: 'node -e "process.stdout.write(process.env.INIT_CWD)" | json-append output.json',
},
},
rootDir,
},
}, ['json-append@1.1.1'], await testDefaults())
await install(manifest, await testDefaults())
], await testDefaults({
fastUnpack: false,
lockfileDir: rootDir,
}))
const output = await loadJsonFile('output.json')
const childEnv = await loadJsonFile<{ INIT_CWD: string }>(path.join(rootDir, 'node_modules/write-lifecycle-env/env.json'))
t.equal(childEnv.INIT_CWD, rootDir)
const output = await loadJsonFile(path.join(rootDir, 'output.json'))
t.deepEqual(output, [process.cwd()])
})
test('INIT_CWD is set correctly', async (t: tape.Test) => {
prepareEmpty(t)
await addDependenciesToPackage({}, ['write-lifecycle-env'], await testDefaults({ fastUnpack: false }))
const childEnv = await loadJsonFile<{ INIT_CWD: string }>(path.resolve('node_modules', 'write-lifecycle-env', 'env.json'))
t.equal(childEnv.INIT_CWD, process.cwd())
})
// TODO: duplicate this test to @pnpm/lifecycle
test("reports child's output", async (t: tape.Test) => {
prepareEmpty(t)