@@ -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
12598async 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
166134async 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 ) {
0 commit comments