Skip to content
Open
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
3 changes: 2 additions & 1 deletion lib/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -20,12 +20,13 @@ const getPathInfo = (cmd, {
pathExt: optPathExt = process.env.PATHEXT,
delimiter: optDelimiter = delimiter,
}) => {
const pathEnvValue = optPath === undefined && isWindows ? process.env.Path : optPath
// If it has a slash, then we don't bother searching the pathenv.
// just check the file itself, and that's it.
const pathEnv = cmd.match(rSlash) ? [''] : [
// windows always checks the cwd first
...(isWindows ? [process.cwd()] : []),
...(optPath || /* istanbul ignore next: very unusual */ '').split(optDelimiter),
...(pathEnvValue || /* istanbul ignore next: very unusual */ '').split(optDelimiter),
]

if (isWindows) {
Expand Down
15 changes: 14 additions & 1 deletion test/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ const fs = require('fs')
const { basename, join, relative, sep, delimiter } = require('path')
const isWindows = process.platform === 'win32'

const ENV_VARS = { PATH: process.env.PATH, PATHEXT: process.env.PATHEXT }
const ENV_VARS = { PATH: process.env.PATH, Path: process.env.Path, PATHEXT: process.env.PATHEXT }
const PLATFORM = Object.getOwnPropertyDescriptor(process, 'platform')

const runTest = async (t, exec, expect, { platforms = ['posix', 'win32'], ..._opt } = {}) => {
Expand Down Expand Up @@ -114,6 +114,19 @@ t.test('find when executable', async t => {
})
})

t.test('falls back to process.env.Path on Windows', async t => {
const fixture = t.testdir({ 'foo.sh': 'echo foo\n' })
const foo = join(fixture, 'foo.sh')
fs.chmodSync(foo, 0o755)
delete process.env.PATH
process.env.Path = fixture

await runTest(t, basename(foo), foo, {
platforms: ['win32'],
pathExt: '.sh',
})
})

t.test('find all', async t => {
const cmdName = 'x.cmd'
const fixture = t.testdir({
Expand Down