@@ -27,31 +27,44 @@ export type PackageManagerRequest = {
2727} ;
2828
2929function getLastKnownGoodFilePath ( ) {
30- return path . join ( folderUtils . getCorepackHomeFolder ( ) , `lastKnownGood.json` ) ;
30+ const lkg = path . join ( folderUtils . getCorepackHomeFolder ( ) , `lastKnownGood.json` ) ;
31+ debugUtils . log ( `LastKnownGood file would be located at ${ lkg } ` ) ;
32+ return lkg ;
3133}
3234
3335export async function getLastKnownGood ( ) : Promise < Record < string , string > > {
3436 let raw : string ;
3537 try {
3638 raw = await fs . promises . readFile ( getLastKnownGoodFilePath ( ) , `utf8` ) ;
3739 } catch ( err ) {
38- if ( ( err as NodeError ) ?. code === `ENOENT` ) return { } ;
40+ if ( ( err as NodeError ) ?. code === `ENOENT` ) {
41+ debugUtils . log ( `No LastKnownGood version found in Corepack home.` ) ;
42+ return { } ;
43+ }
3944 throw err ;
4045 }
4146
4247 try {
4348 const parsed = JSON . parse ( raw ) ;
44- if ( ! parsed ) return { } ;
45- if ( typeof parsed !== `object` ) return { } ;
49+ if ( ! parsed ) {
50+ debugUtils . log ( `Invalid LastKnowGood file in Corepack home (JSON parsable, but falsy)` ) ;
51+ return { } ;
52+ }
53+ if ( typeof parsed !== `object` ) {
54+ debugUtils . log ( `Invalid LastKnowGood file in Corepack home (JSON parsable, but non-object)` ) ;
55+ return { } ;
56+ }
4657 Object . entries ( parsed ) . forEach ( ( [ key , value ] ) => {
4758 if ( typeof value !== `string` ) {
4859 // Ensure that all entries are strings.
60+ debugUtils . log ( `Ignoring key ${ key } in LastKnownGood file as its value is not a string` ) ;
4961 delete parsed [ key ] ;
5062 }
5163 } ) ;
5264 return parsed ;
5365 } catch {
5466 // Ignore errors; too bad
67+ debugUtils . log ( `Invalid LastKnowGood file in Corepack home (maybe not JSON parsable)` ) ;
5568 return { } ;
5669 }
5770}
@@ -161,20 +174,26 @@ export class Engine {
161174
162175 const lastKnownGood = await getLastKnownGood ( ) ;
163176 const lastKnownGoodForThisPackageManager = getLastKnownGoodFromFileContent ( lastKnownGood , packageManager ) ;
164- if ( lastKnownGoodForThisPackageManager )
177+ if ( lastKnownGoodForThisPackageManager ) {
178+ debugUtils . log ( `Search for default version: Found ${ packageManager } @${ lastKnownGoodForThisPackageManager } in LastKnownGood file` ) ;
165179 return lastKnownGoodForThisPackageManager ;
180+ }
166181
167- if ( process . env . COREPACK_DEFAULT_TO_LATEST === `0` )
182+ if ( process . env . COREPACK_DEFAULT_TO_LATEST === `0` ) {
183+ debugUtils . log ( `Search for default version: As defined in environment, defaulting to internal config ${ packageManager } @${ definition . default } ` ) ;
168184 return definition . default ;
185+ }
169186
170187 const reference = await corepackUtils . fetchLatestStableVersion ( definition . fetchLatestFrom ) ;
188+ debugUtils . log ( `Search for default version: found in remote registry ${ packageManager } @${ reference } ` ) ;
171189
172190 try {
173191 await activatePackageManager ( lastKnownGood , {
174192 name : packageManager ,
175193 reference,
176194 } ) ;
177195 } catch {
196+ debugUtils . log ( `Search for default version: could not activate registry version` ) ;
178197 // If for some reason, we cannot update the last known good file, we
179198 // ignore the error.
180199 }
@@ -240,6 +259,7 @@ export class Engine {
240259
241260 switch ( result . type ) {
242261 case `NoProject` :
262+ debugUtils . log ( `Falling back to ${ fallbackDescriptor . name } @${ fallbackDescriptor . range } as no project manifest were found` ) ;
243263 return fallbackDescriptor ;
244264
245265 case `NoSpec` : {
@@ -257,17 +277,20 @@ export class Engine {
257277 await specUtils . setLocalPackageManager ( path . dirname ( result . target ) , installSpec ) ;
258278 }
259279
280+ debugUtils . log ( `Falling back to ${ fallbackDescriptor . name } @${ fallbackDescriptor . range } in the absence of "packageManage" field in ${ result . target } ` ) ;
260281 return fallbackDescriptor ;
261282 }
262283
263284 case `Found` : {
264285 if ( result . spec . name !== locator . name ) {
265286 if ( transparent ) {
287+ debugUtils . log ( `Falling back to ${ fallbackDescriptor . name } @${ fallbackDescriptor . range } in a ${ result . spec . name } @${ result . spec . range } project` ) ;
266288 return fallbackDescriptor ;
267289 } else {
268290 throw new UsageError ( `This project is configured to use ${ result . spec . name } because ${ result . target } has a "packageManager" field` ) ;
269291 }
270292 } else {
293+ debugUtils . log ( `Using ${ result . spec . name } @${ result . spec . range } as defined in project manifest ${ result . target } ` ) ;
271294 return result . spec ;
272295 }
273296 }
0 commit comments