@@ -27,31 +27,44 @@ export type PackageManagerRequest = {
27
27
} ;
28
28
29
29
function 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 ;
31
33
}
32
34
33
35
export async function getLastKnownGood ( ) : Promise < Record < string , string > > {
34
36
let raw : string ;
35
37
try {
36
38
raw = await fs . promises . readFile ( getLastKnownGoodFilePath ( ) , `utf8` ) ;
37
39
} 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
+ }
39
44
throw err ;
40
45
}
41
46
42
47
try {
43
48
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
+ }
46
57
Object . entries ( parsed ) . forEach ( ( [ key , value ] ) => {
47
58
if ( typeof value !== `string` ) {
48
59
// Ensure that all entries are strings.
60
+ debugUtils . log ( `Ignoring key ${ key } in LastKnownGood file as its value is not a string` ) ;
49
61
delete parsed [ key ] ;
50
62
}
51
63
} ) ;
52
64
return parsed ;
53
65
} catch {
54
66
// Ignore errors; too bad
67
+ debugUtils . log ( `Invalid LastKnowGood file in Corepack home (maybe not JSON parsable)` ) ;
55
68
return { } ;
56
69
}
57
70
}
@@ -161,20 +174,26 @@ export class Engine {
161
174
162
175
const lastKnownGood = await getLastKnownGood ( ) ;
163
176
const lastKnownGoodForThisPackageManager = getLastKnownGoodFromFileContent ( lastKnownGood , packageManager ) ;
164
- if ( lastKnownGoodForThisPackageManager )
177
+ if ( lastKnownGoodForThisPackageManager ) {
178
+ debugUtils . log ( `Search for default version: Found ${ packageManager } @${ lastKnownGoodForThisPackageManager } in LastKnownGood file` ) ;
165
179
return lastKnownGoodForThisPackageManager ;
180
+ }
166
181
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 } ` ) ;
168
184
return definition . default ;
185
+ }
169
186
170
187
const reference = await corepackUtils . fetchLatestStableVersion ( definition . fetchLatestFrom ) ;
188
+ debugUtils . log ( `Search for default version: found in remote registry ${ packageManager } @${ reference } ` ) ;
171
189
172
190
try {
173
191
await activatePackageManager ( lastKnownGood , {
174
192
name : packageManager ,
175
193
reference,
176
194
} ) ;
177
195
} catch {
196
+ debugUtils . log ( `Search for default version: could not activate registry version` ) ;
178
197
// If for some reason, we cannot update the last known good file, we
179
198
// ignore the error.
180
199
}
@@ -240,6 +259,7 @@ export class Engine {
240
259
241
260
switch ( result . type ) {
242
261
case `NoProject` :
262
+ debugUtils . log ( `Falling back to ${ fallbackDescriptor . name } @${ fallbackDescriptor . range } as no project manifest were found` ) ;
243
263
return fallbackDescriptor ;
244
264
245
265
case `NoSpec` : {
@@ -257,17 +277,20 @@ export class Engine {
257
277
await specUtils . setLocalPackageManager ( path . dirname ( result . target ) , installSpec ) ;
258
278
}
259
279
280
+ debugUtils . log ( `Falling back to ${ fallbackDescriptor . name } @${ fallbackDescriptor . range } in the absence of "packageManage" field in ${ result . target } ` ) ;
260
281
return fallbackDescriptor ;
261
282
}
262
283
263
284
case `Found` : {
264
285
if ( result . spec . name !== locator . name ) {
265
286
if ( transparent ) {
287
+ debugUtils . log ( `Falling back to ${ fallbackDescriptor . name } @${ fallbackDescriptor . range } in a ${ result . spec . name } @${ result . spec . range } project` ) ;
266
288
return fallbackDescriptor ;
267
289
} else {
268
290
throw new UsageError ( `This project is configured to use ${ result . spec . name } because ${ result . target } has a "packageManager" field` ) ;
269
291
}
270
292
} else {
293
+ debugUtils . log ( `Using ${ result . spec . name } @${ result . spec . range } as defined in project manifest ${ result . target } ` ) ;
271
294
return result . spec ;
272
295
}
273
296
}
0 commit comments