Skip to content

Commit 6019d7b

Browse files
authored
fix: make DEBUG=corepack more useful (#538)
1 parent 06e5872 commit 6019d7b

File tree

3 files changed

+33
-8
lines changed

3 files changed

+33
-8
lines changed

sources/Engine.ts

+29-6
Original file line numberDiff line numberDiff line change
@@ -27,31 +27,44 @@ export type PackageManagerRequest = {
2727
};
2828

2929
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;
3133
}
3234

3335
export 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
}

sources/corepackUtils.ts

+2-2
Original file line numberDiff line numberDiff line change
@@ -207,7 +207,7 @@ export async function installVersion(installTarget: string, locator: Locator, {s
207207

208208
const corepackData = JSON.parse(corepackContent);
209209

210-
debugUtils.log(`Reusing ${locator.name}@${locator.reference}`);
210+
debugUtils.log(`Reusing ${locator.name}@${locator.reference} found in ${installFolder}`);
211211

212212
return {
213213
hash: corepackData.hash as string,
@@ -333,7 +333,7 @@ export async function installVersion(installTarget: string, locator: Locator, {s
333333
}
334334
}
335335

336-
debugUtils.log(`Install finished`);
336+
debugUtils.log(`Download and install of ${locator.name}@${locator.reference} is finished`);
337337

338338
return {
339339
location: installFolder,

sources/specUtils.ts

+2
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@ import path from 'path';
44
import semverValid from 'semver/functions/valid';
55

66
import {PreparedPackageManagerInfo} from './Engine';
7+
import * as debugUtils from './debugUtils';
78
import {NodeError} from './nodeUtils';
89
import * as nodeUtils from './nodeUtils';
910
import {Descriptor, isSupportedPackageManager} from './types';
@@ -93,6 +94,7 @@ export async function loadSpec(initialCwd: string): Promise<LoadSpecResult> {
9394
continue;
9495

9596
const manifestPath = path.join(currCwd, `package.json`);
97+
debugUtils.log(`Checking ${manifestPath}`);
9698
let content: string;
9799
try {
98100
content = await fs.promises.readFile(manifestPath, `utf8`);

0 commit comments

Comments
 (0)