Repository navigation
fix(drivers): report a missing npm on Windows as missing, not as a failed install #1790
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
base: main
Are you sure you want to change the base?
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change | ||||||||||||||||||||||||||||||
|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|
|
|
@@ -843,6 +843,50 @@ interface KillTreeOptions { | |||||||||||||||||||||||||||||||
| interface RunNpmOptions { | ||||||||||||||||||||||||||||||||
| spawnProcess?: SpawnProcess | ||||||||||||||||||||||||||||||||
| killTree?: (child: ReturnType<typeof spawn>) => Promise<KillTreeResult> | ||||||||||||||||||||||||||||||||
| 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 | ||||||||||||||||||||||||||||||||
| } | ||||||||||||||||||||||||||||||||
| } | ||||||||||||||||||||||||||||||||
|
Comment on lines
+852
to
+858
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. [🟠 MEDIUM] Using synchronous file system operations ( Consider refactoring Suggested change:
Suggested change
|
||||||||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||||||||
| /** 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] | ||||||||||||||||||||||||||||||||
| } | ||||||||||||||||||||||||||||||||
|
Comment on lines
+861
to
+864
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. [🔵 LOW] The Suggested change:
Suggested change
|
||||||||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||||||||
| /** | ||||||||||||||||||||||||||||||||
| * 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( | ||||||||||||||||||||||||||||||||
|
ralphstodomingo marked this conversation as resolved.
Resolved
|
||||||||||||||||||||||||||||||||
| 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) | ||||||||||||||||||||||||||||||||
|
ralphstodomingo marked this conversation as resolved.
Resolved
|
||||||||||||||||||||||||||||||||
| const dirs = (windowsEnv(env, "PATH") ?? "") | ||||||||||||||||||||||||||||||||
| .split(";") | ||||||||||||||||||||||||||||||||
|
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Preserve semicolons inside quoted PATH entries. With If npm then exits 1 after a registry or build failure, this lookup returns Split only on semicolons outside quotation marks. Add a regression case for this directory.
|
||||||||||||||||||||||||||||||||
| .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 | ||||||||||||||||||||||||||||||||
|
ralphstodomingo marked this conversation as resolved.
Resolved
|
||||||||||||||||||||||||||||||||
| } | ||||||||||||||||||||||||||||||||
| } | ||||||||||||||||||||||||||||||||
| 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) | ||||||||||||||||||||||||||||||||
| }) | ||||||||||||||||||||||||||||||||
| }) | ||||||||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||||||||
Uh oh!
There was an error while loading. Please reload this page.