Uh oh!
There was an error while loading. Please reload this page.
- Notifications
You must be signed in to change notification settings - Fork 11.9k
fix(@angular/cli): resolve executables strictly from PATH#33758
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
Uh oh!
There was an error while loading. Please reload this page.
Jump to
Uh oh!
There was an error while loading. Please reload this page.
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,60 @@ | ||
| /** | ||
| * @license | ||
| * Copyright Google LLC All Rights Reserved. | ||
| * | ||
| * Use of this source code is governed by an MIT-style license that can be | ||
| * found in the LICENSE file at https://angular.dev/license | ||
| */ | ||
| import { existsSync } from 'node:fs'; | ||
| import { delimiter, extname, isAbsolute, join } from 'node:path'; | ||
| /** | ||
| * Searches the `PATH` environment variable for a given executable binary name. | ||
| * On Windows, checks extensions in `PATHEXT` (e.g. `.exe`, `.cmd`) if no extension is given. | ||
| * Returns the absolute path of the binary if found on `PATH`, or `undefined` if not found. | ||
| * | ||
| * This prevents `execFileSync` / `spawn` from implicitly resolving executables | ||
| * relative to `process.cwd()` on Windows when passed bare command names. | ||
| * | ||
| * @param binaryName Name of the binary to search for (e.g. 'git'). | ||
| * @returns The absolute path to the binary if found on `PATH`, or `undefined`. | ||
| */ | ||
| export function findExecutableOnPath(binaryName: string): string | undefined { | ||
| const envPath = process.env.PATH || process.env.Path || ''; | ||
Collaborator 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. Nit: Prefer | ||
| if (!envPath) { | ||
| return undefined; | ||
| } | ||
| const isWindows = process.platform === 'win32'; | ||
| const pathExt = process.env.PATHEXT | ||
| ? process.env.PATHEXT.split(delimiter) | ||
| : ['.com', '.exe', '.bat', '.cmd']; | ||
Comment on lines
+30
to
+32
Collaborator 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. TIL about | ||
| const hasExt = isWindows && extname(binaryName) !== ''; | ||
| const extensions = isWindows && !hasExt ? pathExt : ['']; | ||
| for (const rawDir of envPath.split(delimiter)) { | ||
| if (!rawDir) { | ||
| continue; | ||
| } | ||
| const dir = rawDir.startsWith('"') && rawDir.endsWith('"') ? rawDir.slice(1, -1) : rawDir; | ||
Collaborator 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. Question: Do we have to deal with escaping? The fact that we need to parse quotes makes me generally uncomfortable that there's more hidden complexity here. | ||
| if (!isAbsolute(dir)) { | ||
| continue; | ||
| } | ||
| for (const ext of extensions) { | ||
| const candidate = join(dir, binaryName + ext); | ||
| try { | ||
| if (existsSync(candidate)) { | ||
| return candidate; | ||
| } | ||
| } catch { | ||
| // Ignore file system errors (e.g. invalid path or permission error) | ||
| } | ||
| } | ||
| } | ||
| return undefined; | ||
| } | ||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,52 @@ | ||
| /** | ||
| * @license | ||
| * Copyright Google LLC All Rights Reserved. | ||
| * | ||
| * Use of this source code is governed by an MIT-style license that can be | ||
| * found in the LICENSE file at https://angular.dev/license | ||
| */ | ||
| import { dirname } from 'node:path'; | ||
| import { findExecutableOnPath } from './executable'; | ||
| describe('findExecutableOnPath', () => { | ||
| it('should find executable on PATH when it exists', () => { | ||
| // 'node' binary should be present on PATH in any Node test environment | ||
| const nodePath = findExecutableOnPath('node'); | ||
| expect(nodePath).toBeDefined(); | ||
| expect(nodePath).toContain('node'); | ||
| }); | ||
| it('should return undefined when binary does not exist on PATH', () => { | ||
| const nonExistentPath = findExecutableOnPath('non_existent_binary_123456789'); | ||
| expect(nonExistentPath).toBeUndefined(); | ||
| }); | ||
| it('should correctly handle PATH entries wrapped in double quotes', () => { | ||
| const nodePath = findExecutableOnPath('node'); | ||
| if (!nodePath) { | ||
| return; | ||
| } | ||
| const originalPath = process.env.PATH; | ||
| try { | ||
| const dir = dirname(nodePath); | ||
| process.env.PATH = `"${dir}"`; | ||
| const resolved = findExecutableOnPath('node'); | ||
| expect(resolved).toBeDefined(); | ||
| } finally { | ||
| process.env.PATH = originalPath; | ||
| } | ||
| }); | ||
| it('should ignore relative PATH entries', () => { | ||
| const originalPath = process.env.PATH; | ||
| try { | ||
| process.env.PATH = '.'; | ||
| const resolved = findExecutableOnPath('node'); | ||
| expect(resolved).toBeUndefined(); | ||
| } finally { | ||
| process.env.PATH = originalPath; | ||
| } | ||
| }); | ||
| }); |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Question: Is there a specific platform which uses
${Path}as distinct from${PATH}? I've never seen that before.