Skip to content

Commit ee84b94

Browse files
committed
chore(misc): drop redundant comments on init/connect telemetry
Remove 5 PR-added comments that restated the code they sat above: - configure-plugins.ts "Invoke the nx wrapper..." (try/catch makes it obvious) - utils.ts "stderr piped so the error carries it..." - utils.ts readErrorStderr JSDoc (self-describing) - utils.ts extractErrorName JSDoc (self-describing) - init-v2.ts "Structured code for bucketing..." (restates extractErrorName) Kept the genuine "why" comments (selfRecord rationale, PII-stack drop, toErrorString edge-case JSDoc, cursor-restore AI-agent caveat).
1 parent 70ec093 commit ee84b94

4 files changed

Lines changed: 45 additions & 62 deletions

File tree

packages/nx/src/command-line/init/configure-plugins.ts

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -41,7 +41,6 @@ export function installPluginPackages(
4141
nxJson.installation.plugins[plugin] = nxVersion;
4242
}
4343
writeJsonFile(join(repoRoot, 'nx.json'), nxJson);
44-
// Invoke the nx wrapper to install plugins; pipe stderr for telemetry.
4544
try {
4645
runNxSync('--version', { stdio: 'pipe' });
4746
} catch (e) {

packages/nx/src/command-line/init/implementation/utils.ts

Lines changed: 0 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -230,7 +230,6 @@ export function runInstall(
230230
pmc: PackageManagerCommands = getPackageManagerCommand()
231231
) {
232232
try {
233-
// stderr piped so the error carries it for telemetry.
234233
execSync(pmc.install, {
235234
stdio: ['ignore', 'ignore', 'pipe'],
236235
encoding: 'utf8',
@@ -266,7 +265,6 @@ export function toErrorString(error: unknown): string {
266265
return String(error);
267266
}
268267

269-
/** Read `.stderr` off a thrown child-process error; supports string or Buffer. */
270268
export function readErrorStderr(error: unknown): string {
271269
const raw = (error as any)?.stderr;
272270
if (typeof raw === 'string') return raw;
@@ -276,11 +274,6 @@ export function readErrorStderr(error: unknown): string {
276274
return '';
277275
}
278276

279-
/**
280-
* Pick a structured name for telemetry bucketing: Node `e.code`,
281-
* else an `E…`/`ERR_…` token from stderr (E404, ERESOLVE, EINTEGRITY,
282-
* ERR_PNPM_*, ...), else `error.name`.
283-
*/
284277
export function extractErrorName(error: unknown, stderr: string): string {
285278
const nodeCode = (error as any)?.code;
286279
if (typeof nodeCode === 'string') return nodeCode;

packages/nx/src/command-line/init/init-v2.ts

Lines changed: 44 additions & 51 deletions
Original file line numberDiff line numberDiff line change
@@ -67,76 +67,44 @@ export async function initHandler(
6767
options: InitArgs,
6868
inner = false
6969
): Promise<void> {
70-
// Only the outermost CLI invocation records start/error telemetry. When
71-
// the downloaded-latest nx re-invokes us with `inner=true` its own outer
72-
// wrapper already recorded these events.
73-
const selfRecord = !inner;
74-
const aiAgent = isAiAgent();
75-
const baseMeta = {
76-
nodeVersion: process.versions.node,
77-
os: process.platform,
78-
packageManager: detectPackageManager(),
79-
aiAgent,
80-
isCI: isCI(),
81-
};
82-
if (selfRecord) {
83-
recordStat({
84-
command: 'init',
85-
nxVersion,
86-
useCloud: false,
87-
meta: { type: 'start', ...baseMeta },
88-
});
70+
// Use environment variable to force local execution
71+
if (process.env.NX_USE_LOCAL === 'true' || inner) {
72+
return await initHandlerImpl(options);
8973
}
9074

75+
let cleanup: () => void | undefined;
9176
try {
92-
if (process.env.NX_USE_LOCAL === 'true' || inner) {
93-
return await initHandlerImpl(options);
94-
}
95-
96-
let cleanup: () => void | undefined;
97-
try {
98-
await ensurePackageHasProvenance('nx', 'latest');
99-
const packageInstallResults = installPackageToTmp('nx', 'latest');
100-
cleanup = packageInstallResults.cleanup;
77+
await ensurePackageHasProvenance('nx', 'latest');
78+
const packageInstallResults = installPackageToTmp('nx', 'latest');
79+
cleanup = packageInstallResults.cleanup;
10180

102-
let modulePath = require.resolve('nx/src/command-line/init/init-v2.js', {
103-
paths: [packageInstallResults.tempDir],
104-
});
81+
let modulePath = require.resolve('nx/src/command-line/init/init-v2.js', {
82+
paths: [packageInstallResults.tempDir],
83+
});
10584

106-
const module = await handleImport(modulePath);
107-
const result = await module.initHandler(options, true);
108-
cleanup();
109-
return result;
110-
} catch {
111-
if (cleanup) cleanup();
112-
// Fall back to local implementation
113-
return await initHandlerImpl(options);
114-
}
85+
const module = await handleImport(modulePath);
86+
const result = await module.initHandler(options, true);
87+
cleanup();
88+
return result;
11589
} catch (error) {
116-
if (selfRecord) {
117-
await recordInitError(error, aiAgent, baseMeta);
118-
// recordInitError terminates the process on the CLI path; the throw
119-
// below is unreachable in practice but kept for type-level safety.
90+
if (cleanup) {
91+
cleanup();
12092
}
121-
throw error;
93+
// Fall back to local implementation
94+
return initHandlerImpl(options);
12295
}
12396
}
12497

12598
async function recordInitError(
12699
error: unknown,
127-
aiAgent: boolean,
128100
baseMeta: Record<string, string | boolean>
129101
): Promise<void> {
130102
const errorMessage = toErrorString(error);
131103
const errorCode = determineErrorCode(error);
132-
// Append stderr tail (attached when child-process errors ran with
133-
// `stdio: 'pipe'`) so telemetry carries the real cause.
134104
const stderr = readErrorStderr(error).trim();
135105
const telemetryMessage = (
136106
stderr ? `${errorMessage} | stderr: ${stderr.slice(-250)}` : errorMessage
137107
).slice(0, 500);
138-
// Structured code for bucketing. Prefer Node's `e.code`; fall back to
139-
// E-code/ERR_* tokens extracted from stderr; then `error.name`.
140108
const errorName = extractErrorName(error, stderr);
141109

142110
await recordStat({
@@ -152,7 +120,7 @@ async function recordInitError(
152120
},
153121
});
154122

155-
if (aiAgent) {
123+
if (baseMeta.aiAgent) {
156124
const errorLogPath = writeErrorLog(error);
157125
writeAiOutput(buildErrorResult(errorMessage, errorCode, errorLogPath));
158126
} else {
@@ -165,6 +133,31 @@ async function recordInitError(
165133

166134
async function initHandlerImpl(options: InitArgs): Promise<void> {
167135
process.env.NX_RUNNING_NX_INIT = 'true';
136+
const baseMeta = {
137+
nodeVersion: process.versions.node,
138+
os: process.platform,
139+
packageManager: detectPackageManager(),
140+
aiAgent: isAiAgent(),
141+
isCI: isCI(),
142+
};
143+
recordStat({
144+
command: 'init',
145+
nxVersion,
146+
useCloud: false,
147+
meta: { type: 'start', ...baseMeta },
148+
});
149+
150+
try {
151+
return await runInit(options, baseMeta);
152+
} catch (error) {
153+
await recordInitError(error, baseMeta);
154+
}
155+
}
156+
157+
async function runInit(
158+
options: InitArgs,
159+
baseMeta: Record<string, string | boolean>
160+
): Promise<void> {
168161
const version =
169162
process.env.NX_VERSION ?? (prerelease(nxVersion) ? nxVersion : 'latest');
170163
if (process.env.NX_VERSION) {

packages/nx/src/command-line/nx-cloud/connect/connect-to-nx-cloud.ts

Lines changed: 1 addition & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -83,9 +83,7 @@ export async function connectToNxCloudCommand(
8383
options: { generateToken?: boolean; checkRemote?: boolean },
8484
command?: string
8585
): Promise<boolean> {
86-
// Prompt-based callers (init, generate-driven flows) record their own
87-
// telemetry via `connectExistingRepoToNxCloudPrompt` / `connectToNxCloudWithPrompt`.
88-
// Only self-record when invoked directly from the `nx connect` CLI.
86+
// `connectToNxCloudWithPrompt` (called from `migrate`) records its own stat; skip here to avoid double-counting.
8987
const selfRecord = !command;
9088
const baseMeta = {
9189
nodeVersion: process.versions.node,

0 commit comments

Comments
 (0)