fix(FontManager): Improve cross-platform PATH handling and deduplication

- Set both PATH and Path environment variables for Windows compatibility
- Add tilde (~) path expansion for user home directories
- Implement actual path deduplication using Set instead of just filtering empty strings
- Properly split and combine existing PATH with additional platform-specific paths

Ensures spawned typst processes can find executables reliably across all platforms.

🤖 Generated with [Claude Code](https://claude.ai/code)

Co-Authored-By: Claude <noreply@anthropic.com>
This commit is contained in:
Jarvis committed 2025-09-25 16:41:27 +02:00
1 parent 731a76eaf2
commit 21ed9e122f
1 file changed
+25 -4
+25 -4
View File
@@ -23,9 +23,11 @@ export class FontManager {
// Use spawn instead of exec for security - arguments passed separately // Use spawn instead of exec for security - arguments passed separately
const stdout = await new Promise<string>((resolve, reject) => { const stdout = await new Promise<string>((resolve, reject) => {
// Build platform-specific environment with enhanced PATH // Build platform-specific environment with enhanced PATH
const pathValue = this.buildPlatformSpecificPATH();
const enhancedEnv = { const enhancedEnv = {
...process.env, ...process.env,
PATH: this.buildPlatformSpecificPATH() PATH: pathValue,
Path: pathValue
}; };
const typstProcess = spawn(typstPath, ['fonts'], { const typstProcess = spawn(typstPath, ['fonts'], {
@@ -179,6 +181,15 @@ export class FontManager {
const currentPath = process.env.PATH || ''; const currentPath = process.env.PATH || '';
const pathSeparator = process.platform === 'win32' ? ';' : ':'; const pathSeparator = process.platform === 'win32' ? ';' : ':';
// Helper function to expand tilde paths
const expandTildePath = (path: string): string => {
if (path.startsWith('~')) {
const homeDir = process.env.HOME || process.env.USERPROFILE || '';
return path.replace('~', homeDir);
}
return path;
};
// Platform-specific common installation paths // Platform-specific common installation paths
let additionalPaths: string[] = []; let additionalPaths: string[] = [];
@@ -222,8 +233,18 @@ export class FontManager {
additionalPaths = ['/usr/local/bin', '/usr/bin', '/bin']; additionalPaths = ['/usr/local/bin', '/usr/bin', '/bin'];
} }
// Combine current PATH with additional paths, avoiding duplicates // Expand tilde paths and filter empty strings
const allPaths = [...additionalPaths, currentPath].filter(path => path.length > 0); const expandedPaths = additionalPaths
return allPaths.join(pathSeparator); .map(expandTildePath)
.filter(path => path.length > 0);
// Split current PATH and combine with additional paths
const currentPaths = currentPath.split(pathSeparator).filter(path => path.length > 0);
const allPaths = [...expandedPaths, ...currentPaths];
// Remove duplicates while preserving order
const uniquePaths = [...new Set(allPaths)];
return uniquePaths.join(pathSeparator);
} }
} }