@@ -52,29 +52,19 @@ const {
52
52
} = require ( 'internal/v8/startup_snapshot' ) ;
53
53
54
54
function prepareMainThreadExecution ( expandArgv1 = false , initializeModules = true ) {
55
- prepareExecution ( {
56
- expandArgv1,
57
- initializeModules,
58
- isMainThread : true ,
59
- } ) ;
55
+ return prepareExecution ( expandArgv1 , initializeModules , true ) ;
60
56
}
61
57
62
58
function prepareWorkerThreadExecution ( ) {
63
- prepareExecution ( {
64
- expandArgv1 : false ,
65
- initializeModules : false , // Will need to initialize it after policy setup
66
- isMainThread : false ,
67
- } ) ;
59
+ prepareExecution ( undefined , false , false ) ; // Will need to initialize modules after policy setup
68
60
}
69
61
70
- function prepareExecution ( options ) {
71
- const { expandArgv1, initializeModules, isMainThread } = options ;
72
-
62
+ function prepareExecution ( expandArgv1 , initializeModules , isMainThread ) {
73
63
refreshRuntimeOptions ( ) ;
74
64
reconnectZeroFillToggle ( ) ;
75
65
76
- // Patch the process object with legacy properties and normalizations
77
- patchProcessObject ( expandArgv1 ) ;
66
+ // Patch the process object and get the resolved main entry point.
67
+ const mainEntry = patchProcessObject ( expandArgv1 ) ;
78
68
setupTraceCategoryState ( ) ;
79
69
setupInspectorHooks ( ) ;
80
70
setupWarningHandler ( ) ;
@@ -131,6 +121,8 @@ function prepareExecution(options) {
131
121
if ( initializeModules ) {
132
122
setupUserModules ( ) ;
133
123
}
124
+
125
+ return mainEntry ;
134
126
}
135
127
136
128
function setupSymbolDisposePolyfill ( ) {
@@ -176,12 +168,21 @@ function refreshRuntimeOptions() {
176
168
refreshOptions ( ) ;
177
169
}
178
170
171
+ /**
172
+ * Patch the process object with legacy properties and normalizations.
173
+ * Replace `process.argv[0]` with `process.execPath`, preserving the original `argv[0]` value as `process.argv0`.
174
+ * Replace `process.argv[1]` with the resolved file path of the entry point, if requested and a replacement can be
175
+ * found; preserving the original value as `process.argv1`.
176
+ * @param {boolean } expandArgv1 - Whether to replace `process.argv[1]` with the resolved file path of the main entry
177
+ * point.
178
+ */
179
179
function patchProcessObject ( expandArgv1 ) {
180
180
const binding = internalBinding ( 'process_methods' ) ;
181
181
binding . patchProcessObject ( process ) ;
182
182
183
183
require ( 'internal/process/per_thread' ) . refreshHrtimeBuffer ( ) ;
184
184
185
+ // Since we replace process.argv[0] below, preserve the original value in case the user needs it.
185
186
ObjectDefineProperty ( process , 'argv0' , {
186
187
__proto__ : null ,
187
188
enumerable : true ,
@@ -190,16 +191,27 @@ function patchProcessObject(expandArgv1) {
190
191
value : process . argv [ 0 ] ,
191
192
} ) ;
192
193
194
+ // Since we usually replace process.argv[1] below, preserve the original value in case the user needs it.
195
+ ObjectDefineProperty ( process , 'argv1' , {
196
+ __proto__ : null ,
197
+ enumerable : true ,
198
+ // Only set it to true during snapshot building.
199
+ configurable : isBuildingSnapshot ( ) ,
200
+ value : process . argv [ 1 ] ,
201
+ } ) ;
202
+
193
203
process . exitCode = undefined ;
194
204
process . _exiting = false ;
195
205
process . argv [ 0 ] = process . execPath ;
196
206
197
- if ( expandArgv1 && process . argv [ 1 ] &&
198
- ! StringPrototypeStartsWith ( process . argv [ 1 ] , '-' ) ) {
199
- // Expand process.argv[1] into a full path.
200
- const path = require ( 'path' ) ;
207
+ // If a requested, update process.argv[1] to replace whatever the user provided with the resolved absolute URL or file
208
+ // path of the entry point.
209
+ /** @type {ReturnType<determineMainEntry> } */
210
+ let mainEntry ;
211
+ if ( expandArgv1 ) {
212
+ mainEntry = determineMainEntry ( process . argv [ 1 ] ) ;
201
213
try {
202
- process . argv [ 1 ] = path . resolve ( process . argv [ 1 ] ) ;
214
+ process . argv [ 1 ] = mainEntry ;
203
215
} catch {
204
216
// Continue regardless of error.
205
217
}
@@ -226,6 +238,21 @@ function patchProcessObject(expandArgv1) {
226
238
addReadOnlyProcessAlias ( 'traceDeprecation' , '--trace-deprecation' ) ;
227
239
addReadOnlyProcessAlias ( '_breakFirstLine' , '--inspect-brk' , false ) ;
228
240
addReadOnlyProcessAlias ( '_breakNodeFirstLine' , '--inspect-brk-node' , false ) ;
241
+
242
+ return mainEntry ;
243
+ }
244
+
245
+ /**
246
+ * For non-STDIN input, we need to resolve what was specified on the command line into a path.
247
+ * @param {string } input - What was specified on the command line as the main entry point.
248
+ */
249
+ function determineMainEntry ( input = process . argv [ 1 ] ) {
250
+ if ( ! input || StringPrototypeStartsWith ( input , '-' ) ) { // STDIN
251
+ return undefined ;
252
+ }
253
+ // Expand the main entry point into a full path.
254
+ const { resolve } = require ( 'path' ) ;
255
+ return resolve ( input ) ;
229
256
}
230
257
231
258
function addReadOnlyProcessAlias ( name , option , enumerable = true ) {
0 commit comments