Skip to content

Commit

Permalink
fix: the dlx, create, and exec commands should set npm_config_user_agent
Browse files Browse the repository at this point in the history
close #3985
  • Loading branch information
zkochan committed Feb 10, 2022
1 parent fe1ff2f commit 804a35b
Show file tree
Hide file tree
Showing 6 changed files with 48 additions and 22 deletions.
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 `dlx`, `create`, and `exec` commands should set the `npm_config_user_agent` env variable.
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
21 changes: 11 additions & 10 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 @@ -121,16 +121,17 @@ export async function handler (
: {}
await execa(params[0], params.slice(1), {
cwd: prefix,
env: {
...process.env,
...extraEnv,
[PATH]: [
env: makeEnv({
extraEnv: {
...extraEnv,
PNPM_PACKAGE_NAME: opts.selectedProjectsGraph?.[prefix]?.package.manifest.name,
},
prependPaths: [
path.join(opts.dir, 'node_modules/.bin'),
...opts.extraBinPaths,
process.env[PATH],
].join(path.delimiter),
PNPM_PACKAGE_NAME: opts.selectedProjectsGraph?.[prefix]?.package.manifest.name,
},
],
userAgent: opts.userAgent,
}),
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

0 comments on commit 804a35b

Please sign in to comment.