Fixes executable path resolution on Windows
Updates the executable path resolution logic to correctly identify executables on Windows systems by using the 'where' command instead of 'which', which is not available on Windows. This change ensures that the application can locate and execute dependencies properly, regardless of the operating system. It also uses `path.isAbsolute` to accurately determine if a user-provided path is absolute, improving the reliability of path resolution.
This commit is contained in:
2 files changed
+19
-7
No files matched your search
@@ -10,6 +10,7 @@
|
|||||||
*/
|
*/
|
||||||
|
|
||||||
import { DEPENDENCY_CONSTANTS } from './constants';
|
import { DEPENDENCY_CONSTANTS } from './constants';
|
||||||
|
import * as path from 'path';
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Information about a checked dependency executable.
|
* Information about a checked dependency executable.
|
||||||
@@ -145,7 +146,10 @@ export class ExecutableChecker {
|
|||||||
const stdout = await new Promise<string>((resolve, reject) => {
|
const stdout = await new Promise<string>((resolve, reject) => {
|
||||||
const env = this.getAugmentedEnv(additionalPaths);
|
const env = this.getAugmentedEnv(additionalPaths);
|
||||||
|
|
||||||
const whichProcess = spawn('which', [executableName], {
|
// Use platform-specific command: 'where' on Windows, 'which' on Unix
|
||||||
|
const whichCommand = process.platform === 'win32' ? 'where' : 'which';
|
||||||
|
|
||||||
|
const whichProcess = spawn(whichCommand, [executableName], {
|
||||||
stdio: ['pipe', 'pipe', 'pipe'],
|
stdio: ['pipe', 'pipe', 'pipe'],
|
||||||
env: env
|
env: env
|
||||||
});
|
});
|
||||||
@@ -213,8 +217,8 @@ export class ExecutableChecker {
|
|||||||
* ```
|
* ```
|
||||||
*/
|
*/
|
||||||
public static async resolveExecutablePath(userPath: string | undefined, defaultName: string, additionalPaths: string[] = []): Promise<string> {
|
public static async resolveExecutablePath(userPath: string | undefined, defaultName: string, additionalPaths: string[] = []): Promise<string> {
|
||||||
// If user provided a full path (contains /), use it as-is
|
// If user provided an absolute path, use it as-is
|
||||||
if (userPath && userPath.trim() !== '' && userPath.includes('/')) {
|
if (userPath && userPath.trim() !== '' && path.isAbsolute(userPath)) {
|
||||||
return userPath;
|
return userPath;
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -246,8 +250,8 @@ export class ExecutableChecker {
|
|||||||
* @see {@link resolveExecutablePath} for detailed resolution logic and examples
|
* @see {@link resolveExecutablePath} for detailed resolution logic and examples
|
||||||
*/
|
*/
|
||||||
public static resolveExecutablePathSync(userPath: string | undefined, defaultName: string, additionalPaths: string[] = []): string {
|
public static resolveExecutablePathSync(userPath: string | undefined, defaultName: string, additionalPaths: string[] = []): string {
|
||||||
// If user provided a full path (contains /), use it as-is
|
// If user provided an absolute path, use it as-is
|
||||||
if (userPath && userPath.trim() !== '' && userPath.includes('/')) {
|
if (userPath && userPath.trim() !== '' && path.isAbsolute(userPath)) {
|
||||||
return userPath;
|
return userPath;
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -256,8 +260,12 @@ export class ExecutableChecker {
|
|||||||
|
|
||||||
// Try to find the executable using which command synchronously
|
// Try to find the executable using which command synchronously
|
||||||
const { spawnSync } = require('child_process');
|
const { spawnSync } = require('child_process');
|
||||||
|
|
||||||
|
// Use platform-specific command: 'where' on Windows, 'which' on Unix
|
||||||
|
const whichCommand = process.platform === 'win32' ? 'where' : 'which';
|
||||||
|
|
||||||
try {
|
try {
|
||||||
const result = spawnSync('which', [searchName], {
|
const result = spawnSync(whichCommand, [searchName], {
|
||||||
encoding: 'utf8',
|
encoding: 'utf8',
|
||||||
env: this.getAugmentedEnv(additionalPaths)
|
env: this.getAugmentedEnv(additionalPaths)
|
||||||
});
|
});
|
||||||
|
|||||||
@@ -27,13 +27,17 @@ export class PathResolver {
|
|||||||
|
|
||||||
// Try to find the executable using which command
|
// Try to find the executable using which command
|
||||||
const { spawnSync } = require('child_process');
|
const { spawnSync } = require('child_process');
|
||||||
|
|
||||||
|
// Use platform-specific command: 'where' on Windows, 'which' on Unix
|
||||||
|
const whichCommand = process.platform === 'win32' ? 'where' : 'which';
|
||||||
|
|
||||||
try {
|
try {
|
||||||
const augmentedEnv = {
|
const augmentedEnv = {
|
||||||
...process.env,
|
...process.env,
|
||||||
PATH: `/opt/homebrew/bin:/usr/local/bin:/usr/bin:/bin:${process.env.PATH || ''}`
|
PATH: `/opt/homebrew/bin:/usr/local/bin:/usr/bin:/bin:${process.env.PATH || ''}`
|
||||||
};
|
};
|
||||||
|
|
||||||
const result = spawnSync('which', [defaultName], {
|
const result = spawnSync(whichCommand, [defaultName], {
|
||||||
encoding: 'utf8',
|
encoding: 'utf8',
|
||||||
env: augmentedEnv
|
env: augmentedEnv
|
||||||
});
|
});
|
||||||
|
|||||||
Reference in new issue
Block a user