Skip to content

Commit

Permalink
fix(exec): run in the right directory (#3514)
Browse files Browse the repository at this point in the history
  • Loading branch information
zkochan committed Jun 8, 2021
1 parent 25ef252 commit 4add11a
Show file tree
Hide file tree
Showing 3 changed files with 73 additions and 6 deletions.
5 changes: 5 additions & 0 deletions .changeset/tiny-rabbits-protect.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
---
"@pnpm/plugin-commands-script-runners": patch
---

`pnpm exec` should be executed in the context of the current working directory.
11 changes: 5 additions & 6 deletions packages/plugin-commands-script-runners/src/exec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -80,20 +80,19 @@ export async function handler (
passes: 0,
} as RecursiveSummary

const rootDir = opts.lockfileDir ?? opts.workspaceDir ?? opts.dir
let chunks!: string[][]
if (opts.recursive) {
chunks = opts.sort
? sortPackages(opts.selectedProjectsGraph)
: [Object.keys(opts.selectedProjectsGraph).sort()]
} else {
chunks = [[rootDir]]
chunks = [[opts.dir]]
opts.selectedProjectsGraph = {
[rootDir]: {
[opts.dir]: {
dependencies: [],
package: {
...await readProjectManifest(rootDir),
dir: rootDir,
...await readProjectManifest(opts.dir),
dir: opts.dir,
},
},
}
Expand All @@ -116,7 +115,7 @@ export async function handler (
...extraEnv,
[PATH]: [
...opts.extraBinPaths,
path.join(rootDir, 'node_modules/.bin'),
path.join(opts.dir, 'node_modules/.bin'),
process.env[PATH],
].join(path.delimiter),
PNPM_PACKAGE_NAME: opts.selectedProjectsGraph[prefix].package.manifest.name,
Expand Down
63 changes: 63 additions & 0 deletions packages/plugin-commands-script-runners/test/exec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -74,6 +74,69 @@ test('pnpm recursive exec', async () => {
expect(outputs2).toStrictEqual(['project-1', 'project-3'])
})

test('exec inside a workspace package', async () => {
preparePackages([
{
name: 'project-1',
version: '1.0.0',

dependencies: {
'json-append': '1',
},
scripts: {
build: 'node -e "process.stdout.write(\'project-1\')" | json-append ../output1.json && node -e "process.stdout.write(\'project-1\')" | json-append ../output2.json',
},
},
{
name: 'project-2',
version: '1.0.0',

dependencies: {
'json-append': '1',
'project-1': '1',
},
scripts: {
build: 'node -e "process.stdout.write(\'project-2\')" | json-append ../output1.json',
postbuild: 'node -e "process.stdout.write(\'project-2-postbuild\')" | json-append ../output1.json',
prebuild: 'node -e "process.stdout.write(\'project-2-prebuild\')" | json-append ../output1.json',
},
},
{
name: 'project-3',
version: '1.0.0',

dependencies: {
'json-append': '1',
'project-1': '1',
},
scripts: {
build: 'node -e "process.stdout.write(\'project-3\')" | json-append ../output2.json',
},
},
])

await execa('pnpm', [
'install',
'-r',
'--registry',
REGISTRY,
'--store-dir',
path.resolve(DEFAULT_OPTS.storeDir),
])
await exec.handler({
...DEFAULT_OPTS,
dir: path.resolve('project-1'),
recursive: false,
selectedProjectsGraph: {},
}, ['npm', 'run', 'build'])

const { default: outputs1 } = await import(path.resolve('output1.json'))
const { default: outputs2 } = await import(path.resolve('output2.json'))

expect(outputs1).toStrictEqual(['project-1'])
expect(outputs2).toStrictEqual(['project-1'])
})

test('pnpm recursive exec sets PNPM_PACKAGE_NAME env var', async () => {
preparePackages([
{
Expand Down

0 comments on commit 4add11a

Please sign in to comment.