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(exec): run in the right directory #3514

Merged
merged 2 commits into from
Jun 8, 2021
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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