diff --git a/packages/drivers/src/resolve.ts b/packages/drivers/src/resolve.ts index 194e246c91..ba9c90f0fd 100644 --- a/packages/drivers/src/resolve.ts +++ b/packages/drivers/src/resolve.ts @@ -843,6 +843,50 @@ 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.toUpperCase()) + return key === undefined ? undefined : env[key] +} + +/** + * 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, + 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) + 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 + } + } + return undefined } const KILL_TERM_GRACE_MS = 2_000 @@ -1049,6 +1093,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..593349b49d 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,128 @@ 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", () => { + 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" + + 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") + }) +})