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(dlx): set saveProd to true for getting pkgName from dependencies #7540

Merged
merged 3 commits into from
Jan 19, 2024
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
6 changes: 6 additions & 0 deletions .changeset/nasty-apricots-taste.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
---
"@pnpm/plugin-commands-script-runners": patch
"pnpm": patch
---

Set saveProd to true for getting pkgName from dependencies [7424](https://github.com/pnpm/pnpm/issues/7424).
10 changes: 9 additions & 1 deletion exec/plugin-commands-script-runners/src/dlx.ts
Original file line number Diff line number Diff line change
Expand Up @@ -95,6 +95,10 @@ export async function handler (
dir: prefix,
lockfileDir: prefix,
rootProjectManifestDir: prefix, // This property won't be used as rootProjectManifest will be undefined
saveProd: true, // dlx will be looking for the package in the "dependencies" field!
saveDev: false,
saveOptional: false,
savePeer: false,
}, pkgs)
const binName = opts.package
? command
Expand All @@ -119,7 +123,11 @@ export async function handler (

async function getPkgName (pkgDir: string) {
const manifest = await readPackageJsonFromDir(pkgDir)
return Object.keys(manifest.dependencies ?? {})[0]
const dependencyNames = Object.keys(manifest.dependencies ?? {})
if (dependencyNames.length === 0) {
throw new PnpmError('DLX_NO_DEP', 'dlx was unable to find the installed dependency in "dependencies"')
}
return dependencyNames[0]
}

async function getBinName (modulesDir: string, pkgName: string): Promise<string> {
Expand Down
13 changes: 12 additions & 1 deletion pnpm/test/run.ts
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
import { promises as fs, mkdirSync } from 'fs'
import path from 'path'
import PATH_NAME from 'path-name'
import { prepare, preparePackages } from '@pnpm/prepare'
import { prepare, prepareEmpty, preparePackages } from '@pnpm/prepare'
import isWindows from 'is-windows'
import { execPnpm, execPnpmSync } from './utils'

Expand Down Expand Up @@ -259,3 +259,14 @@ test('--reporter-hide-prefix should hide workspace prefix', async () => {
expect(output).toContain('2')
expect(output).not.toContain('script2: 2')
})

test('dlx should work with npm_config_save_dev env variable', async () => {
prepareEmpty()
const result = execPnpmSync(['dlx', '@foo/touch-file-one-bin@latest'], {
env: {
npm_config_save_dev: 'true',
},
stdio: 'inherit',
})
expect(result.status).toBe(0)
})