Skip to content

Commit c4cd7ce

Browse files
authored
fix(vscode): repair error reporting (#2881)
1 parent ae5d0ae commit c4cd7ce

File tree

1 file changed

+28
-27
lines changed

1 file changed

+28
-27
lines changed

libs/vscode/mcp/src/lib/periodic-ai-check.ts

Lines changed: 28 additions & 27 deletions
Original file line numberDiff line numberDiff line change
@@ -105,7 +105,15 @@ async function constructCommand(flags: string, forceNpx = false) {
105105
vscodeLogger,
106106
);
107107

108-
return `${forceNpx ? `npx -y ${cacheParam} --ignore-scripts` : packageManagerCommands.dlx} nx@latest configure-ai-agents ${flags}`.trim();
108+
let dlx = packageManagerCommands.dlx;
109+
110+
// there are older versions of nx that have this outdated config
111+
// 'yarn' isn't actually a dlx command it's only for local packages
112+
if (dlx === 'yarn' || dlx === 'npx') {
113+
dlx = 'npx -y --ignore-scripts';
114+
}
115+
116+
return `${forceNpx ? `npx -y ${cacheParam} --ignore-scripts` : dlx} nx@latest configure-ai-agents ${flags}`.trim();
109117
}
110118

111119
async function getNxLatestVersion(): Promise<string | undefined> {
@@ -124,16 +132,17 @@ async function getNxLatestVersion(): Promise<string | undefined> {
124132
async function doRunAiAgentCheck(
125133
workspacePath: string,
126134
forceNpx = false,
127-
): Promise<Error[]> {
135+
): Promise<[string, Error][]> {
128136
let callbackStdout = '';
129137
let callbackStderr = '';
130138
let weKilledIt = false;
131139
let commandStartTime = 0;
140+
let command = '';
132141

133-
const errors: Error[] = [];
142+
const errors: [string, Error][] = [];
134143

135144
try {
136-
const command = await constructCommand('--check', forceNpx);
145+
command = await constructCommand('--check', forceNpx);
137146
await new Promise((resolve, reject) => {
138147
commandStartTime = Date.now();
139148
const childProcess = spawn(command, {
@@ -197,7 +206,7 @@ async function doRunAiAgentCheck(
197206
});
198207
});
199208
} catch (e) {
200-
errors.push(e as any);
209+
errors.push([command, e as Error]);
201210
if (!forceNpx) {
202211
const rerunErrors = await doRunAiAgentCheck(workspacePath, true);
203212
errors.push(...rerunErrors);
@@ -208,6 +217,7 @@ async function doRunAiAgentCheck(
208217
}
209218

210219
async function getErrorInformation(
220+
command: string,
211221
e: Error,
212222
workspacePath: string,
213223
nxLatestVersion: string,
@@ -260,6 +270,7 @@ async function getErrorInformation(
260270

261271
let errorMessage = [
262272
'AIFAIL',
273+
`COMMAND:${command}`,
263274
`ELAPSED:${((e as any).elapsedTime / 1000).toFixed(2)}s`,
264275
`WKI:${weKilledIt}`,
265276
`NODEVERSION:${nodeVersion}`,
@@ -275,7 +286,7 @@ async function getErrorInformation(
275286
`MESSAGE:${originalMessage}`,
276287
].join('|');
277288

278-
errorMessage = errorMessage.replace(
289+
errorMessage = errorMessage.replaceAll(
279290
'https://registry.npmjs.org/',
280291
'OFFICIAL_NPM_REGISTRY',
281292
);
@@ -295,32 +306,17 @@ async function getErrorInformation(
295306
'npm error 502',
296307
'npm error 500',
297308
'FETCH_ERROR',
309+
'ERR_PNPM_UNSUPPORTED_ENGINE',
310+
'ERR_INVALID_AUTH',
311+
'ERR_SOCKET_TIMEOUT',
312+
'EBADENGINE',
313+
'This program is blocked by group policy',
298314
];
299315
// there are certain error messages we can't do anything about
300316
// let's track those separately but not throw
301317
if (reasons.some((reason) => errorMessage.includes(reason))) {
302-
getTelemetry().logUsage('ai.configure-agents-check-expected-error', {
303-
cause: 'network-or-auth',
304-
});
305-
return;
306-
}
307-
308-
let engineStrict = false;
309-
try {
310-
const npmSetting = await promisify(exec)('npm config get engine-strict', {
311-
cwd: workspacePath,
312-
});
313-
engineStrict = npmSetting.stdout.trim() === 'true';
314-
} catch (e) {
315-
// ignore
316-
}
317-
if (engineStrict && errorMessage.includes('EBADENGINE')) {
318-
getTelemetry().logUsage('ai.configure-agents-check-expected-error', {
319-
cause: 'engine-strict',
320-
});
321318
return;
322319
}
323-
getTelemetry().logUsage('ai.configure-agents-check-error');
324320

325321
return errorMessage;
326322
}
@@ -381,7 +377,8 @@ async function runAiAgentCheck() {
381377
const errorsWithInformation = [];
382378
for (const error of errors) {
383379
const errorInformation = await getErrorInformation(
384-
error,
380+
error[0],
381+
error[1],
385382
workspacePath,
386383
nxLatestVersion,
387384
);
@@ -390,9 +387,13 @@ async function runAiAgentCheck() {
390387
}
391388
}
392389
if (errorsWithInformation.length > 0) {
390+
getTelemetry().logUsage('ai.configure-agents-check-error');
393391
throw new Error(
394392
`Error 1: \n ${errorsWithInformation[0]}\n\nError 2: \n ${errorsWithInformation[1]}\n`,
395393
);
394+
} else {
395+
getTelemetry().logUsage('ai.configure-agents-check-expected-error');
396+
return;
396397
}
397398
}
398399

0 commit comments

Comments
 (0)