From ed504766d003aa9270b63d6219197a024f7814bc Mon Sep 17 00:00:00 2001 From: "Ralph Sto. Domingo" Date: Fri, 20 Nov 2026 10:28:33 +0800 Subject: [PATCH 1/2] fix(drivers): report a missing npm on Windows as missing, not as a failed install MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit cmd.exe answers a missing npm with exit 1 and a localised "'npm' is not recognized" line, so the exit-127 check never fired and the driver install returned the raw cmd.exe text. After a failed run on Windows, look npm up the way cmd.exe does (current directory, then PATH x PATHEXT); when it isn't there, answer with the "npm is not available on PATH … Install Node.js, then run:" message and its Windows-quoted command. Co-Authored-By: Claude Opus 5.5 Claude-Session: https://claude.ai/code/session_01VEmKQu1Qn99YbrBzhKdtAr --- packages/drivers/src/resolve.ts | 51 ++++++++++ packages/drivers/test/resolve-unit.test.ts | 110 +++++++++++++++++++++ 2 files changed, 161 insertions(+) diff --git a/packages/drivers/src/resolve.ts b/packages/drivers/src/resolve.ts index 194e246c91..8a82c7ebef 100644 --- a/packages/drivers/src/resolve.ts +++ b/packages/drivers/src/resolve.ts @@ -843,6 +843,48 @@ interface KillTreeOptions { interface RunNpmOptions { spawnProcess?: SpawnProcess killTree?: (child: ReturnType) => Promise + platform?: NodeJS.Platform + /** Whether cmd.exe can find npm from `cwd`. Consulted only after a failed run on Windows. */ + npmOnPath?: (cwd: string) => boolean +} + +/** False for anything unreadable as well as absent: a PATH entry can name a file, or a directory we may not stat. */ +function isRegularFile(file: string): boolean { + try { + return fs.statSync(file).isFile() + } catch { + return false + } +} + +/** An environment variable by name, ignoring case as Windows does (`Path` and `PATH` are one variable). */ +function windowsEnv(env: NodeJS.ProcessEnv, name: string): string | undefined { + const key = Object.keys(env).find((k) => k.toUpperCase() === name) + return key === undefined ? undefined : env[key] +} + +/** + * Where cmd.exe would find `command`: the current directory, then each PATH + * entry, trying each PATHEXT extension in turn. Undefined when it would not. + */ +export function findOnWindowsPath( + command: string, + cwd: string, + env: NodeJS.ProcessEnv = process.env, + isFile: (file: string) => boolean = isRegularFile, +): string | undefined { + const extensions = (windowsEnv(env, "PATHEXT") || ".COM;.EXE;.BAT;.CMD").split(";").filter(Boolean) + const dirs = (windowsEnv(env, "PATH") ?? "") + .split(";") + .map((entry) => entry.trim().replace(/^"(.*)"$/, "$1")) + .filter(Boolean) + for (const dir of [cwd, ...dirs]) { + for (const ext of extensions) { + const file = path.win32.resolve(cwd, dir, command + ext) + if (isFile(file)) return file + } + } + return undefined } const KILL_TERM_GRACE_MS = 2_000 @@ -1049,6 +1091,15 @@ function runNpm( }) child.on("close", (code) => { if (state !== "running") return + // POSIX shells exit 127 when npm is missing. cmd.exe exits 1, the same as + // a failed install, and words its "is not recognized" line in the system's + // language, so on Windows look npm up the way cmd.exe does instead. + const platform = options.platform ?? process.platform + const npmOnPath = options.npmOnPath ?? ((dir: string) => findOnWindowsPath("npm", dir) !== undefined) + if (code !== 0 && platform === "win32" && !npmOnPath(cwd)) { + finish(127) + return + } finish(code ?? 1) }) }) diff --git a/packages/drivers/test/resolve-unit.test.ts b/packages/drivers/test/resolve-unit.test.ts index a068d5da4a..7ed247dcef 100644 --- a/packages/drivers/test/resolve-unit.test.ts +++ b/packages/drivers/test/resolve-unit.test.ts @@ -28,6 +28,7 @@ import { driverInstallDir, driverLabel, driverSearchRoots, + findOnWindowsPath, isDriverInstalled, loadOptionalDriver, packageNameOf, @@ -1080,3 +1081,112 @@ describe("runNpm timeout", () => { expect(result.output).toContain("cleanup exploded") }) }) + +describe("findOnWindowsPath", () => { + // Windows paths compare case-insensitively, as NTFS does. + const files = (...paths: string[]) => { + const set = new Set(paths.map((p) => p.toLowerCase())) + return (file: string) => set.has(file.toLowerCase()) + } + const cwd = "C:\\Users\\x\\AppData\\Local\\altimate-code\\drivers" + + test("finds npm.cmd in a PATH entry, reading Path and PATHEXT in any case", () => { + const env = { Path: "C:\\Windows\\System32;C:\\Program Files\\nodejs\\", PathExt: ".COM;.EXE;.BAT;.CMD" } + expect(findOnWindowsPath("npm", cwd, env, files("C:\\Program Files\\nodejs\\npm.cmd"))).toBe( + "C:\\Program Files\\nodejs\\npm.CMD", + ) + }) + + test("a quoted PATH entry is read without its quotes", () => { + const env = { PATH: '"C:\\Program Files\\nodejs";C:\\Windows' } + expect(findOnWindowsPath("npm", cwd, env, files("C:\\Program Files\\nodejs\\npm.cmd"))).toBeDefined() + }) + + test("an extensionless npm (the POSIX script) does not count: cmd.exe only runs PATHEXT files", () => { + const env = { PATH: "C:\\Program Files\\nodejs" } + expect(findOnWindowsPath("npm", cwd, env, files("C:\\Program Files\\nodejs\\npm"))).toBeUndefined() + }) + + test("undefined when no PATH entry holds npm, or there is no PATH at all", () => { + const isFile = files("D:\\elsewhere\\npm.cmd") + expect(findOnWindowsPath("npm", cwd, { PATH: "C:\\Windows\\System32;C:\\Windows" }, isFile)).toBeUndefined() + expect(findOnWindowsPath("npm", cwd, {}, isFile)).toBeUndefined() + }) + + test("the current directory is searched first, and a relative entry is read from it", () => { + expect(findOnWindowsPath("npm", cwd, { PATH: "" }, files(`${cwd}\\npm.cmd`))).toBe(`${cwd}\\npm.CMD`) + expect(findOnWindowsPath("npm", cwd, { PATH: "tools" }, files(`${cwd}\\tools\\npm.exe`))).toBe( + `${cwd}\\tools\\npm.EXE`, + ) + }) +}) + +describe("runNpm when npm is missing on Windows", () => { + // What cmd.exe prints, and its exit code, when npm is not on PATH (Windows 11, English). + const NOT_RECOGNIZED = "'npm' is not recognized as an internal or external command,\r\noperable program or batch file.\r\n" + + async function run(options: { platform: NodeJS.Platform; npmOnPath: boolean; stderr: string; code: number }) { + const child = fakeChild() + const lookups: string[] = [] + const result = _testing.runNpm(["install", "@databricks/sql"], tmpRoot, 60_000, { + spawnProcess: (() => child as any) as typeof spawn, + platform: options.platform, + npmOnPath: (dir) => { + lookups.push(dir) + return options.npmOnPath + }, + }) + child.stderr.write(options.stderr) + await new Promise((resolve) => setImmediate(resolve)) + child.emit("close", options.code) + return { ...(await result), lookups } + } + + test("cmd.exe's exit 1 with npm not on PATH is reported as a missing npm (127)", async () => { + const result = await run({ platform: "win32", npmOnPath: false, stderr: NOT_RECOGNIZED, code: 1 }) + expect(result.code).toBe(127) + expect(result.lookups).toEqual([tmpRoot]) + }) + + test("a failed install with npm on PATH keeps its exit code and output", async () => { + const result = await run({ platform: "win32", npmOnPath: true, stderr: "npm error code ENOTFOUND", code: 1 }) + expect(result.code).toBe(1) + expect(result.output).toContain("ENOTFOUND") + }) + + test("a successful run never looks npm up", async () => { + const result = await run({ platform: "win32", npmOnPath: false, stderr: "", code: 0 }) + expect(result.code).toBe(0) + expect(result.lookups).toEqual([]) + }) + + test("outside Windows the exit code stands and npm is not looked up", async () => { + const result = await run({ platform: "linux", npmOnPath: false, stderr: "boom", code: 1 }) + expect(result.code).toBe(1) + expect(result.lookups).toEqual([]) + }) + + test("the install answers with the npm-missing message and the manual command", async () => { + process.env["ALTIMATE_DRIVER_DIR"] = path.join(tmpRoot, "My Drivers") + const child = fakeChild() + const runNpm = (args: string[], cwd: string, timeoutMs: number) => { + const result = _testing.runNpm(args, cwd, timeoutMs, { + spawnProcess: (() => child as any) as typeof spawn, + platform: "win32", + npmOnPath: () => false, + }) + child.stderr.write(NOT_RECOGNIZED) + setImmediate(() => child.emit("close", 1)) + return result + } + + const result = await installOptionalDriver("databricks", { force: true, runNpm }) + + expect(result.installed).toBe(false) + expect(result.error).toBe( + "npm is not available on PATH, so Databricks cannot be installed automatically. " + + `Install Node.js, then run: npm install --prefix ${shellQuote(path.join(tmpRoot, "My Drivers"))} @databricks/sql`, + ) + expect(result.error).not.toContain("is not recognized") + }) +}) From c6b2114da398286e7713186f1ee52c2450aa0e74 Mon Sep 17 00:00:00 2001 From: "Ralph Sto. Domingo" Date: Fri, 20 Nov 2026 10:58:29 +0800 Subject: [PATCH 2/2] fix(drivers): skip the current directory when cmd.exe does With NoDefaultCurrentDirectoryInExePath set (any value), cmd.exe no longer looks in the current directory, so neither does the npm lookup. Env names now match in any case on both sides. Co-Authored-By: Claude Opus 5.5 Claude-Session: https://claude.ai/code/session_01VEmKQu1Qn99YbrBzhKdtAr --- packages/drivers/src/resolve.ts | 10 ++++++---- packages/drivers/test/resolve-unit.test.ts | 20 ++++++++++++++++++-- 2 files changed, 24 insertions(+), 6 deletions(-) diff --git a/packages/drivers/src/resolve.ts b/packages/drivers/src/resolve.ts index 8a82c7ebef..ba9c90f0fd 100644 --- a/packages/drivers/src/resolve.ts +++ b/packages/drivers/src/resolve.ts @@ -859,13 +859,14 @@ function isRegularFile(file: string): boolean { /** An environment variable by name, ignoring case as Windows does (`Path` and `PATH` are one variable). */ function windowsEnv(env: NodeJS.ProcessEnv, name: string): string | undefined { - const key = Object.keys(env).find((k) => k.toUpperCase() === name) + const key = Object.keys(env).find((k) => k.toUpperCase() === name.toUpperCase()) return key === undefined ? undefined : env[key] } /** - * Where cmd.exe would find `command`: the current directory, then each PATH - * entry, trying each PATHEXT extension in turn. Undefined when it would not. + * Where cmd.exe would find `command`: the current directory (unless + * NoDefaultCurrentDirectoryInExePath is set), then each PATH entry, trying each + * PATHEXT extension in turn. Undefined when it would not. */ export function findOnWindowsPath( command: string, @@ -878,7 +879,8 @@ export function findOnWindowsPath( .split(";") .map((entry) => entry.trim().replace(/^"(.*)"$/, "$1")) .filter(Boolean) - for (const dir of [cwd, ...dirs]) { + const searchDirs = windowsEnv(env, "NoDefaultCurrentDirectoryInExePath") === undefined ? [cwd, ...dirs] : dirs + for (const dir of searchDirs) { for (const ext of extensions) { const file = path.win32.resolve(cwd, dir, command + ext) if (isFile(file)) return file diff --git a/packages/drivers/test/resolve-unit.test.ts b/packages/drivers/test/resolve-unit.test.ts index 7ed247dcef..593349b49d 100644 --- a/packages/drivers/test/resolve-unit.test.ts +++ b/packages/drivers/test/resolve-unit.test.ts @@ -1114,16 +1114,32 @@ describe("findOnWindowsPath", () => { }) test("the current directory is searched first, and a relative entry is read from it", () => { - expect(findOnWindowsPath("npm", cwd, { PATH: "" }, files(`${cwd}\\npm.cmd`))).toBe(`${cwd}\\npm.CMD`) + const both = files(`${cwd}\\npm.cmd`, "C:\\Program Files\\nodejs\\npm.cmd") + expect(findOnWindowsPath("npm", cwd, { PATH: "C:\\Program Files\\nodejs" }, both)).toBe(`${cwd}\\npm.CMD`) expect(findOnWindowsPath("npm", cwd, { PATH: "tools" }, files(`${cwd}\\tools\\npm.exe`))).toBe( `${cwd}\\tools\\npm.EXE`, ) }) + + test("NoDefaultCurrentDirectoryInExePath, in any case and with any value, skips the current directory", () => { + const isFile = files(`${cwd}\\npm.cmd`) + expect(findOnWindowsPath("npm", cwd, { PATH: "", NODEFAULTCURRENTDIRECTORYINEXEPATH: "" }, isFile)).toBeUndefined() + expect(findOnWindowsPath("npm", cwd, { PATH: "", NoDefaultCurrentDirectoryInExePath: "1" }, isFile)).toBeUndefined() + expect( + findOnWindowsPath( + "npm", + cwd, + { PATH: "C:\\Program Files\\nodejs", NoDefaultCurrentDirectoryInExePath: "1" }, + files(`${cwd}\\npm.cmd`, "C:\\Program Files\\nodejs\\npm.cmd"), + ), + ).toBe("C:\\Program Files\\nodejs\\npm.CMD") + }) }) describe("runNpm when npm is missing on Windows", () => { // What cmd.exe prints, and its exit code, when npm is not on PATH (Windows 11, English). - const NOT_RECOGNIZED = "'npm' is not recognized as an internal or external command,\r\noperable program or batch file.\r\n" + const NOT_RECOGNIZED = + "'npm' is not recognized as an internal or external command,\r\noperable program or batch file.\r\n" async function run(options: { platform: NodeJS.Platform; npmOnPath: boolean; stderr: string; code: number }) { const child = fakeChild()