fix(run): stop passing flags as fallback command (#7370)

close #7244

---------

Co-authored-by: Zoltan Kochan <z@kochan.io>
This commit is contained in:
Khải
2023-12-07 01:53:32 +07:00
committed by GitHub
parent 81a2f249e7
commit 1474bfd89a
3 changed files with 41 additions and 1 deletions

View File

@@ -0,0 +1,6 @@
---
"@pnpm/plugin-commands-script-runners": patch
"pnpm": patch
---
Fix a bug where pnpm incorrectly passes a flag to a run handler as a fallback command [#7244](https://github.com/pnpm/pnpm/issues/7244).

View File

@@ -191,11 +191,23 @@ export async function handler (
if (opts.ifPresent) return
if (opts.fallbackCommandUsed) {
if (opts.argv == null) throw new Error('Could not fallback because opts.argv.original was not passed to the script runner')
const params = opts.argv.original.slice(1)
while (params.length > 0 && params[0].startsWith('-') && params[0] !== '--') {
params.shift()
}
if (params.length > 0 && params[0] === '--') {
params.shift()
}
if (params.length === 0) {
throw new PnpmError('UNEXPECTED_BEHAVIOR', 'Params should not be an empty array', {
hint: 'This was a bug caused by programmer error. Please report it',
})
}
return exec({
selectedProjectsGraph: {},
implicitlyFellbackFromRun: true,
...opts,
}, opts.argv.original.slice(1))
}, params)
}
if (opts.workspaceDir) {
const { manifest: rootManifest } = await tryReadProjectManifest(opts.workspaceDir, opts)

View File

@@ -900,3 +900,25 @@ test('pnpm exec command not found (explicit call, with a near name package)', as
expect(error?.message).toBe('Command "cwsay" not found')
expect(error?.hint).toBe('Did you mean "pnpm exec cowsay"?')
})
test('pnpm exec --workspace-root when command not found', async () => {
prepare({})
let error!: any // eslint-disable-line
try {
await run.handler({
...DEFAULT_OPTS,
argv: {
original: ['pnpm', '--workspace-root', 'command-that-does-not-exist'],
},
dir: process.cwd(),
fallbackCommandUsed: true,
recursive: false,
selectedProjectsGraph: {},
}, ['command-that-does-not-exist'])
} catch (err: any) { // eslint-disable-line
error = err
}
expect(error?.failures[0].message).toBe('Command "command-that-does-not-exist" not found')
})