Skip to content

Commit 9845f64

Browse files
MRagunandhan24Michael Smith
authored andcommitted
fix: show a warning when a delimiter is present in the path
1 parent 47b11aa commit 9845f64

2 files changed

Lines changed: 46 additions & 0 deletions

File tree

lib/set-path.js

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,4 @@
1+
const { log } = require('proc-log')
12
const { resolve, dirname, delimiter } = require('path')
23
// the path here is relative, even though it does not need to be
34
// in order to make the posix tests pass in windows
@@ -14,6 +15,13 @@ const setPATH = (projectPath, binPaths, env) => {
1415

1516
const pathArr = []
1617
if (binPaths) {
18+
for (const bin of binPaths) {
19+
if (bin.includes(delimiter)) {
20+
const event = env.npm_lifecycle_event
21+
const context = event ? `"${event}" script` : 'script execution'
22+
log.warn('run-script', `Path contains delimiter ("${delimiter}"), ${context} may not behave as expected.`)
23+
}
24+
}
1725
pathArr.push(...binPaths)
1826
}
1927
// unshift the ./node_modules/.bin from every folder

test/make-spawn-args.js

Lines changed: 38 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,7 @@
11
const t = require('tap')
22
const spawk = require('spawk')
3+
const { delimiter } = require('node:path')
4+
const setPATH = require('../lib/set-path.js')
35
const runScript = require('..')
46

57
const pkg = {
@@ -141,4 +143,40 @@ t.test('spawn args', async t => {
141143
}))
142144
t.ok(spawk.done())
143145
})
146+
147+
await t.test('binPaths containing delimiter logs warning', async t => {
148+
const warnings = []
149+
const onWarn = (...args) => warnings.push(args)
150+
process.on('log', onWarn)
151+
t.teardown(() => process.off('log', onWarn))
152+
153+
spawk.spawn(
154+
/.*/,
155+
false,
156+
e => (e.env.PATH || e.env.Path).includes(`/path/with${delimiter}delimiter`)
157+
)
158+
await t.resolves(() => runScript({
159+
pkg,
160+
binPaths: [`/path/with${delimiter}delimiter`],
161+
path: testdir,
162+
event: 'test',
163+
}))
164+
const warning = warnings.find(w => w[0] === 'warn' && w[1] === 'run-script' && w[2].includes('delimiter'))
165+
t.ok(warning, 'warning should be logged when binPath contains delimiter')
166+
t.match(warning[2], '"test" script')
167+
t.ok(spawk.done())
168+
})
169+
170+
await t.test('binPaths containing delimiter uses generic message without event', async t => {
171+
const warnings = []
172+
const onWarn = (...args) => warnings.push(args)
173+
process.on('log', onWarn)
174+
t.teardown(() => process.off('log', onWarn))
175+
176+
setPATH(testdir, [`/path/with${delimiter}delimiter`], {})
177+
178+
const warning = warnings.find(w => w[0] === 'warn' && w[1] === 'run-script' && w[2].includes('delimiter'))
179+
t.ok(warning, 'warning should be logged with generic message when no event')
180+
t.match(warning[2], 'script execution')
181+
})
144182
})

0 commit comments

Comments
 (0)