@@ -32,10 +32,6 @@ type MinimalServicePathOptions = {
3232 includeMissingUserBinDefaults ?: boolean ;
3333} ;
3434
35- type BuildServicePathOptions = MinimalServicePathOptions & {
36- env ?: Record < string , string | undefined > ;
37- } ;
38-
3935type SharedServiceEnvironmentFields = {
4036 stateDir : string | undefined ;
4137 configPath : string | undefined ;
@@ -239,16 +235,10 @@ function resolveSystemPathDirs(platform: NodeJS.Platform): string[] {
239235 return [ ] ;
240236}
241237
242- /**
243- * Resolve common user bin directories for macOS.
244- * These are paths where npm global installs and node version managers typically place binaries.
245- *
246- * Key differences from Linux:
247- * - fnm: macOS uses ~/Library/Application Support/fnm (not ~/.local/share/fnm)
248- * - pnpm: macOS uses ~/Library/pnpm (not ~/.local/share/pnpm)
249- */
250- function resolveDarwinUserBinDirs (
238+ /** Resolve common user bin directories while preserving platform-specific manager roots. */
239+ function resolveUserBinDirs (
251240 home : string | undefined ,
241+ platform : "darwin" | "linux" ,
252242 env ?: Record < string , string | undefined > ,
253243 existsSync : ( candidate : string ) => boolean = fs . existsSync ,
254244 options : Pick < MinimalServicePathOptions , "cwd" | "home" | "includeMissingUserBinDefaults" > = { } ,
@@ -261,76 +251,42 @@ function resolveDarwinUserBinDirs(
261251 const pathOptions = { ...options , home } ;
262252 const includeMissingUserBinDefaults = options . includeMissingUserBinDefaults ?? true ;
263253
264- // Env-configured bin roots (override defaults when present).
265- // Note: FNM_DIR on macOS defaults to ~/Library/Application Support/fnm
266- // Note: PNPM_HOME on macOS defaults to ~/Library/pnpm
267254 addCommonEnvConfiguredBinDirs ( dirs , env , pathOptions ) ;
268- // nvm: no stable default path, relies on env or user's shell config
269- // User must set NVM_DIR and source nvm.sh for it to work
270- addEnvConfiguredBinDir ( dirs , env ?. NVM_DIR , pathOptions ) ;
271- // fnm: use aliases/default (not current)
255+ addEnvConfiguredBinDir (
256+ dirs ,
257+ platform === "darwin" ? env ?. NVM_DIR : appendSubdir ( env ?. NVM_DIR , "current/bin" ) ,
258+ pathOptions ,
259+ ) ;
272260 addEnvConfiguredBinDir ( dirs , appendSubdir ( env ?. FNM_DIR , "aliases/default/bin" ) , pathOptions ) ;
273- // pnpm 10 placed binaries directly in PNPM_HOME; pnpm 11 uses PNPM_HOME/bin.
274-
275- // Common user bin directories
276- addCommonUserBinDirs ( dirs , home , existsSync , includeMissingUserBinDefaults ) ;
277-
278- // Nix Home Manager (cross-platform)
279- addNixProfileBinDirs ( dirs , home , env , pathOptions , includeMissingUserBinDefaults , existsSync ) ;
280-
281- // Node version managers - macOS specific paths
282- // nvm: no stable default path, depends on user's shell configuration
283- // fnm: macOS default is ~/Library/Application Support/fnm, not ~/.fnm
284- addExistingDir ( dirs , `${ home } /Library/Application Support/fnm/aliases/default/bin` , existsSync ) ; // fnm default
285- addExistingDir ( dirs , `${ home } /.fnm/aliases/default/bin` , existsSync ) ; // fnm if customized to ~/.fnm
286- // pnpm: macOS default is ~/Library/pnpm, not ~/.local/share/pnpm
287- addExistingDir ( dirs , `${ home } /Library/pnpm/bin` , existsSync ) ; // pnpm 11 default
288- addExistingDir ( dirs , `${ home } /Library/pnpm` , existsSync ) ; // pnpm default
289- addExistingDir ( dirs , `${ home } /.local/share/pnpm/bin` , existsSync ) ; // pnpm 11 XDG fallback
290- addExistingDir ( dirs , `${ home } /.local/share/pnpm` , existsSync ) ; // pnpm XDG fallback
291-
292- return dirs ;
293- }
294-
295- /**
296- * Resolve common user bin directories for Linux.
297- * These are paths where npm global installs and node version managers typically place binaries.
298- */
299- function resolveLinuxUserBinDirs (
300- home : string | undefined ,
301- env ?: Record < string , string | undefined > ,
302- existsSync : ( candidate : string ) => boolean = fs . existsSync ,
303- options : Pick < MinimalServicePathOptions , "cwd" | "home" | "includeMissingUserBinDefaults" > = { } ,
304- ) : string [ ] {
305- if ( ! home ) {
306- return [ ] ;
261+ if ( platform === "linux" ) {
262+ addEnvConfiguredBinDir ( dirs , appendSubdir ( env ?. FNM_DIR , "current/bin" ) , pathOptions ) ;
307263 }
308-
309- const dirs : string [ ] = [ ] ;
310- const pathOptions = { ...options , home } ;
311- const includeMissingUserBinDefaults = options . includeMissingUserBinDefaults ?? true ;
312-
313- // Env-configured bin roots (override defaults when present).
314- addCommonEnvConfiguredBinDirs ( dirs , env , pathOptions ) ;
315- addEnvConfiguredBinDir ( dirs , appendSubdir ( env ?. NVM_DIR , "current/bin" ) , pathOptions ) ;
316- addEnvConfiguredBinDir ( dirs , appendSubdir ( env ?. FNM_DIR , "aliases/default/bin" ) , pathOptions ) ;
317- addEnvConfiguredBinDir ( dirs , appendSubdir ( env ?. FNM_DIR , "current/bin" ) , pathOptions ) ;
318-
319- // Common user bin directories
320264 addCommonUserBinDirs ( dirs , home , existsSync , includeMissingUserBinDefaults ) ;
321-
322- // Nix Home Manager (cross-platform)
323265 addNixProfileBinDirs ( dirs , home , env , pathOptions , includeMissingUserBinDefaults , existsSync ) ;
324-
325- // Node version managers
326- addExistingDir ( dirs , `${ home } /.nvm/current/bin` , existsSync ) ; // nvm with current symlink
327- addExistingDir ( dirs , `${ home } /.local/share/fnm/aliases/default/bin` , existsSync ) ; // fnm default
328- addExistingDir ( dirs , `${ home } /.local/share/fnm/current/bin` , existsSync ) ; // fnm legacy current symlink
329- addExistingDir ( dirs , `${ home } /.fnm/aliases/default/bin` , existsSync ) ; // fnm if customized to ~/.fnm
330- addExistingDir ( dirs , `${ home } /.fnm/current/bin` , existsSync ) ; // fnm legacy current symlink
331- addExistingDir ( dirs , `${ home } /.local/share/pnpm/bin` , existsSync ) ; // pnpm 11 global bin
332- addExistingDir ( dirs , `${ home } /.local/share/pnpm` , existsSync ) ; // pnpm global bin
333-
266+ // macOS uses Library roots; Linux uses XDG/current symlinks. Preserve both
267+ // the pnpm root (v10) and its bin subdirectory (v11) in their original order.
268+ const managerDirs =
269+ platform === "darwin"
270+ ? [
271+ "Library/Application Support/fnm/aliases/default/bin" ,
272+ ".fnm/aliases/default/bin" ,
273+ "Library/pnpm/bin" ,
274+ "Library/pnpm" ,
275+ ".local/share/pnpm/bin" ,
276+ ".local/share/pnpm" ,
277+ ]
278+ : [
279+ ".nvm/current/bin" ,
280+ ".local/share/fnm/aliases/default/bin" ,
281+ ".local/share/fnm/current/bin" ,
282+ ".fnm/aliases/default/bin" ,
283+ ".fnm/current/bin" ,
284+ ".local/share/pnpm/bin" ,
285+ ".local/share/pnpm" ,
286+ ] ;
287+ for ( const directory of managerDirs ) {
288+ addExistingDir ( dirs , `${ home } /${ directory } ` , existsSync ) ;
289+ }
334290 return dirs ;
335291}
336292
@@ -342,43 +298,22 @@ function getMinimalServicePathParts(options: MinimalServicePathOptions = {}): st
342298 return [ ] ;
343299 }
344300
345- const parts : string [ ] = [ ] ;
346301 const extraDirs = options . extraDirs ?? [ ] ;
347302 const systemDirs = resolveSystemPathDirs ( platform ) ;
348303 const includeUserDirs = options . includeUserDirs ?? platform !== "darwin" ;
349304
350305 const existsSync = options . existsSync ?? fs . existsSync ;
351- const userDirs = includeUserDirs
352- ? platform === "linux"
353- ? resolveLinuxUserBinDirs ( options . home , options . env , existsSync , options )
354- : platform === "darwin"
355- ? resolveDarwinUserBinDirs ( options . home , options . env , existsSync , options )
356- : [ ]
357- : [ ] ;
358-
359- const add = ( dir : string ) => {
360- if ( ! dir ) {
361- return ;
362- }
363- if ( ! parts . includes ( dir ) ) {
364- parts . push ( dir ) ;
365- }
366- } ;
367-
368- for ( const dir of extraDirs ) {
369- add ( dir ) ;
370- }
371- for ( const dir of systemDirs ) {
372- add ( dir ) ;
373- }
374- for ( const dir of userDirs ) {
375- add ( dir ) ;
376- }
306+ const userDirs =
307+ includeUserDirs && ( platform === "linux" || platform === "darwin" )
308+ ? resolveUserBinDirs ( options . home , platform , options . env , existsSync , options )
309+ : [ ] ;
377310
378- return parts ;
311+ return [ ... new Set ( [ ... extraDirs , ... systemDirs , ... userDirs ] . filter ( Boolean ) ) ] ;
379312}
380313
381- export function getMinimalServicePathPartsFromEnv ( options : BuildServicePathOptions = { } ) : string [ ] {
314+ export function getMinimalServicePathPartsFromEnv (
315+ options : MinimalServicePathOptions = { } ,
316+ ) : string [ ] {
382317 const env = options . env ?? process . env ;
383318 return getMinimalServicePathParts ( {
384319 ...options ,
@@ -387,13 +322,8 @@ export function getMinimalServicePathPartsFromEnv(options: BuildServicePathOptio
387322 } ) ;
388323}
389324
390- function buildMinimalServicePath ( options : BuildServicePathOptions = { } ) : string {
325+ function buildMinimalServicePath ( options : MinimalServicePathOptions = { } ) : string {
391326 const env = options . env ?? process . env ;
392- const platform = options . platform ?? process . platform ;
393- if ( platform === "win32" ) {
394- return env . PATH ?? "" ;
395- }
396-
397327 return getMinimalServicePathPartsFromEnv ( { ...options , env } ) . join ( path . posix . delimiter ) ;
398328}
399329
0 commit comments