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: the dlx and create commands should set npm_config_user_agent #4317

Merged
merged 3 commits into from
Feb 10, 2022
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.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
6 changes: 6 additions & 0 deletions .changeset/thirty-owls-fix.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
---
"@pnpm/plugin-commands-script-runners": patch
"pnpm": patch
---

The `pnpx`, `pnpm dlx`, `pnpm create`, and `pnpm exec` commands should set the `npm_config_user_agent` env variable [#3985](https://github.com/pnpm/pnpm/issues/3985).
12 changes: 3 additions & 9 deletions packages/plugin-commands-script-runners/src/dlx.ts
Original file line number Diff line number Diff line change
Expand Up @@ -6,8 +6,8 @@ import { OUTPUT_OPTIONS } from '@pnpm/common-cli-options-help'
import { Config } from '@pnpm/config'
import rimraf from '@zkochan/rimraf'
import execa from 'execa'
import PATH from 'path-name'
import renderHelp from 'render-help'
import { makeEnv } from './makeEnv'

export const commandNames = ['dlx']

Expand Down Expand Up @@ -43,7 +43,7 @@ export function help () {
export async function handler (
opts: {
package?: string[]
} & Pick<Config, 'reporter'>,
} & Pick<Config, 'reporter' | 'userAgent'>,
params: string[]
) {
const prefix = path.join(fs.realpathSync(os.tmpdir()), `dlx-${process.pid.toString()}`)
Expand All @@ -69,13 +69,7 @@ export async function handler (
stdio: 'inherit',
})
await execa(versionless(scopeless(params[0])), params.slice(1), {
env: {
...process.env,
[PATH]: [
bins,
process.env[PATH],
].join(path.delimiter),
},
env: makeEnv({ userAgent: opts.userAgent, prependPaths: [bins] }),
stdio: 'inherit',
})
}
Expand Down
24 changes: 13 additions & 11 deletions packages/plugin-commands-script-runners/src/exec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -8,10 +8,10 @@ import sortPackages from '@pnpm/sort-packages'
import { Project } from '@pnpm/types'
import execa from 'execa'
import pLimit from 'p-limit'
import PATH from 'path-name'
import pick from 'ramda/src/pick'
import renderHelp from 'render-help'
import existsInDir from './existsInDir'
import { makeEnv } from './makeEnv'
import {
PARALLEL_OPTION_HELP,
shorthands as runShorthands,
Expand Down Expand Up @@ -70,7 +70,7 @@ export async function handler (
reverse?: boolean
sort?: boolean
workspaceConcurrency?: number
} & Pick<Config, 'extraBinPaths' | 'lockfileDir' | 'dir' | 'recursive' | 'workspaceDir'>,
} & Pick<Config, 'extraBinPaths' | 'lockfileDir' | 'dir' | 'userAgent' | 'recursive' | 'workspaceDir'>,
params: string[]
) {
// For backward compatibility
Expand Down Expand Up @@ -119,18 +119,20 @@ export async function handler (
const extraEnv = pnpPath
? makeNodeRequireOption(pnpPath)
: {}
await execa(params[0], params.slice(1), {
cwd: prefix,
env: {
...process.env,
const env = makeEnv({
extraEnv: {
...extraEnv,
[PATH]: [
path.join(opts.dir, 'node_modules/.bin'),
...opts.extraBinPaths,
process.env[PATH],
].join(path.delimiter),
PNPM_PACKAGE_NAME: opts.selectedProjectsGraph?.[prefix]?.package.manifest.name,
},
prependPaths: [
path.join(opts.dir, 'node_modules/.bin'),
...opts.extraBinPaths,
],
userAgent: opts.userAgent,
})
await execa(params[0], params.slice(1), {
cwd: prefix,
env,
stdio: 'inherit',
})
result.passes++
Expand Down
20 changes: 20 additions & 0 deletions packages/plugin-commands-script-runners/src/makeEnv.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
import path from 'path'
import PATH from 'path-name'

export function makeEnv (
opts: {
extraEnv?: NodeJS.ProcessEnv
userAgent?: string
prependPaths: string[]
}
) {
return {
...process.env,
...opts.extraEnv,
npm_config_user_agent: opts.userAgent ?? 'pnpm',
[PATH]: [
...opts.prependPaths,
process.env[PATH],
].join(path.delimiter),
}
}
2 changes: 1 addition & 1 deletion packages/plugin-commands-script-runners/src/run.ts
Original file line number Diff line number Diff line change
Expand Up @@ -114,7 +114,7 @@ For options that may be used with `-r`, see "pnpm help recursive"',
export type RunOpts =
& Omit<RecursiveRunOpts, 'allProjects' | 'selectedProjectsGraph' | 'workspaceDir'>
& { recursive?: boolean }
& Pick<Config, 'dir' | 'engineStrict' | 'extraBinPaths' | 'reporter' | 'scriptsPrependNodePath' | 'scriptShell' | 'shellEmulator' | 'enablePrePostScripts'>
& Pick<Config, 'dir' | 'engineStrict' | 'extraBinPaths' | 'reporter' | 'scriptsPrependNodePath' | 'scriptShell' | 'shellEmulator' | 'enablePrePostScripts' | 'userAgent'>
& (
& { recursive?: false }
& Partial<Pick<Config, 'allProjects' | 'selectedProjectsGraph' | 'workspaceDir'>>
Expand Down
9 changes: 7 additions & 2 deletions packages/plugin-commands-script-runners/test/dlx.ts
Original file line number Diff line number Diff line change
Expand Up @@ -8,10 +8,15 @@ beforeEach((execa as jest.Mock).mockClear)

test('dlx should work with scoped packages', async () => {
prepareEmpty()
const userAgent = 'pnpm/0.0.0'

await dlx.handler({}, ['@foo/bar'])
await dlx.handler({ userAgent }, ['@foo/bar'])

expect(execa).toBeCalledWith('bar', [], expect.anything())
expect(execa).toBeCalledWith('bar', [], expect.objectContaining({
env: expect.objectContaining({
npm_config_user_agent: userAgent,
}),
}))
})

test('dlx should work with versioned packages', async () => {
Expand Down