@@ -8,7 +8,7 @@ import { YarnBerryNodeModulesCollector } from "./yarnBerryNodeModulesCollector"
88import { YarnNodeModulesCollector } from "./yarnNodeModulesCollector"
99import { BunNodeModulesCollector } from "./bunNodeModulesCollector"
1010import { Lazy } from "lazy-val"
11- import { spawn , log } from "builder-util"
11+ import { spawn , log , exists } from "builder-util"
1212import * as fs from "fs-extra"
1313import * as path from "path"
1414
@@ -55,6 +55,10 @@ export const determinePackageManagerEnv = ({ projectDir, appDir, workspaceRoot }
5555 if ( root != null ) {
5656 // re-detect package manager from workspace root, this seems particularly necessary for pnpm workspaces
5757 const actualPm = await detectPackageManager ( [ root ] )
58+ log . info (
59+ { pm : actualPm . pm , config : actualPm . corepackConfig , resolved : actualPm . resolvedDirectory , projectDir } ,
60+ `detected workspace root for project using ${ actualPm . detectionMethod } `
61+ )
5862 return {
5963 pm : actualPm . pm ,
6064 workspaceRoot : Promise . resolve ( actualPm . resolvedDirectory ) ,
@@ -67,55 +71,65 @@ export const determinePackageManagerEnv = ({ projectDir, appDir, workspaceRoot }
6771 } )
6872
6973async function findWorkspaceRoot ( pm : PM , cwd : string ) : Promise < string | undefined > {
70- let command : { command : string ; args : string [ ] } | undefined
74+ let command : { command : string ; args : string [ ] }
7175
7276 switch ( pm ) {
7377 case PM . PNPM :
7478 command = { command : "pnpm" , args : [ "--workspace-root" , "exec" , "pwd" ] }
7579 break
76-
7780 case PM . YARN_BERRY :
78- command = { command : "yarn" , args : [ "config " , "get " , "workspaceRoot " ] }
81+ command = { command : "yarn" , args : [ "workspaces " , "list " , "--json " ] }
7982 break
80-
8183 case PM . YARN : {
8284 command = { command : "yarn" , args : [ "workspaces" , "info" , "--silent" ] }
8385 break
8486 }
85-
8687 case PM . BUN :
8788 command = { command : "bun" , args : [ "pm" , "ls" , "--json" ] }
8889 break
89-
9090 case PM . NPM :
9191 default :
9292 command = { command : "npm" , args : [ "prefix" , "-w" ] }
9393 break
9494 }
9595
9696 const output = await spawn ( command . command , command . args , { cwd, stdio : [ "ignore" , "pipe" , "ignore" ] } )
97- . then ( it => {
98- const out = it ?. trim ( )
97+ . then ( async it => {
98+ const out : string | undefined = it ?. trim ( )
99+ if ( ! out ) {
100+ return undefined
101+ }
99102 if ( pm === PM . YARN ) {
100103 JSON . parse ( out ) // if JSON valid, workspace detected
101- return findNearestWithWorkspacesField ( cwd )
104+ return findNearestPackageJsonWithWorkspacesField ( cwd )
102105 } else if ( pm === PM . BUN ) {
103106 const json = JSON . parse ( out )
104107 if ( Array . isArray ( json ) && json . length > 0 ) {
105- return findNearestWithWorkspacesField ( cwd )
108+ return findNearestPackageJsonWithWorkspacesField ( cwd )
109+ }
110+ } else if ( pm === PM . YARN_BERRY ) {
111+ const lines = out
112+ . split ( "\n" )
113+ . map ( l => l . trim ( ) )
114+ . filter ( Boolean )
115+ for ( const line of lines ) {
116+ const parsed = JSON . parse ( line )
117+ if ( parsed . location != null ) {
118+ const potential = path . resolve ( cwd , parsed . location )
119+ return ( await exists ( potential ) ) ? findNearestPackageJsonWithWorkspacesField ( potential ) : undefined
120+ }
106121 }
107122 }
108- return ! out ? .length || out === "undefined" ? undefined : out
123+ return out . length === 0 || out === "undefined" ? undefined : out
109124 } )
110- . catch ( ( ) => findNearestWithWorkspacesField ( cwd ) )
111-
112- log . debug ( { root : output || cwd } , output ? "workspace root detected" : "workspace root not detected, using project root" )
125+ . catch ( ( ) => findNearestPackageJsonWithWorkspacesField ( cwd ) )
113126 return output
114127}
115128
116- async function findNearestWithWorkspacesField ( dir : string ) : Promise < string | undefined > {
129+ async function findNearestPackageJsonWithWorkspacesField ( dir : string ) : Promise < string | undefined > {
117130 let current = dir
118131 while ( true ) {
132+ log . debug ( { path : current } , "checking for potential workspace root" )
119133 const pkgPath = path . join ( current , "package.json" )
120134 try {
121135 const pkg = JSON . parse ( await fs . readFile ( pkgPath , "utf8" ) )
0 commit comments