fix(dlx): print help message when no arguments are provided (#10690)

* fix(dlx): print help message on calling pnpm dlx without arguments

Running `pnpm dlx` with no arguments would crash Node.js with a
TypeError as it attempted to call `.indexOf()` on an undefined variable.
This commit adds a guard clause and displays the help message instead
and exits gracefully.

Fixes #10633

* refactor: dlx

---------

Co-authored-by: Zoltan Kochan <z@kochan.io>
This commit is contained in:
Ishan Gupta
2026-02-28 06:32:53 +05:30
committed by GitHub
parent 143ca78d09
commit f8367e88d2
3 changed files with 21 additions and 1 deletions

View File

@@ -0,0 +1,6 @@
---
"@pnpm/plugin-commands-script-runners": patch
"pnpm": patch
---
Print help message on running pnpm dlx without arguments and exit.

View File

@@ -87,7 +87,10 @@ export type DlxCommandOptions = {
export async function handler (
opts: DlxCommandOptions,
[command, ...args]: string[]
): Promise<{ exitCode: number }> {
): Promise<{ exitCode: number, output?: string }> {
if (!command && (!opts.package || opts.package.length === 0)) {
return { exitCode: 1, output: help() }
}
const pkgs = opts.package ?? [command]
const fullMetadata = (
(

View File

@@ -299,6 +299,17 @@ test('dlx uses the node version specified by --package=node@runtime:<version>',
}
})
test('dlx without arguments prints help text and exits with 1', () => {
prepareEmpty()
const result = execPnpmSync(['dlx'])
expect(result.status).toBe(1)
const output = result.stdout.toString()
expect(output).toMatch(/Run a package in a temporary environment\./)
})
describeOnLinuxOnly('dlx with supportedArchitectures CLI options', () => {
type CPU = 'arm64' | 'x64'
type LibC = 'glibc' | 'musl'