@@ -11,25 +11,15 @@ var __awaiter = (this && this.__awaiter) || function (thisArg, _arguments, P, ge
11
11
Object . defineProperty ( exports , "__esModule" , { value : true } ) ;
12
12
const fs = require ( "fs" ) ;
13
13
const path = require ( "path" ) ;
14
+ const { runfiles : _defaultRunfiles , _BAZEL_OUT_REGEX } = require ( '../runfiles/index.js' ) ;
14
15
const VERBOSE_LOGS = ! ! process . env [ 'VERBOSE_LOGS' ] ;
15
- const BAZEL_OUT_REGEX = / ( \/ b a z e l - o u t \/ | \/ b a z e l - ~ 1 \/ x 6 4 _ w i ~ 1 \/ ) / ;
16
16
function log_verbose ( ...m ) {
17
17
if ( VERBOSE_LOGS )
18
18
console . error ( '[link_node_modules.js]' , ...m ) ;
19
19
}
20
20
function log_error ( error ) {
21
21
console . error ( '[link_node_modules.js] An error has been reported:' , error , error . stack ) ;
22
22
}
23
- function panic ( m ) {
24
- throw new Error ( `Internal error! Please run again with
25
- --define=VERBOSE_LOG=1
26
- and file an issue: https://github.com/bazelbuild/rules_nodejs/issues/new?template=bug_report.md
27
- Include as much of the build output as you can without disclosing anything confidential.
28
-
29
- Error:
30
- ${ m }
31
- ` ) ;
32
- }
33
23
function mkdirp ( p ) {
34
24
return __awaiter ( this , void 0 , void 0 , function * ( ) {
35
25
if ( p && ! ( yield exists ( p ) ) ) {
@@ -141,125 +131,6 @@ function resolveExternalWorkspacePath(workspace, startCwd, isExecroot, execroot,
141
131
}
142
132
} ) ;
143
133
}
144
- class Runfiles {
145
- constructor ( env ) {
146
- if ( ! ! env [ 'RUNFILES_MANIFEST_FILE' ] ) {
147
- this . manifest = this . loadRunfilesManifest ( env [ 'RUNFILES_MANIFEST_FILE' ] ) ;
148
- }
149
- else if ( ! ! env [ 'RUNFILES_DIR' ] ) {
150
- this . dir = path . resolve ( env [ 'RUNFILES_DIR' ] ) ;
151
- }
152
- else {
153
- panic ( 'Every node program run under Bazel must have a $RUNFILES_DIR or $RUNFILES_MANIFEST_FILE environment variable' ) ;
154
- }
155
- if ( env [ 'RUNFILES_MANIFEST_ONLY' ] === '1' && ! env [ 'RUNFILES_MANIFEST_FILE' ] ) {
156
- log_verbose ( `Workaround https://github.com/bazelbuild/bazel/issues/7994
157
- RUNFILES_MANIFEST_FILE should have been set but wasn't.
158
- falling back to using runfiles symlinks.
159
- If you want to test runfiles manifest behavior, add
160
- --spawn_strategy=standalone to the command line.` ) ;
161
- }
162
- this . workspace = env [ 'BAZEL_WORKSPACE' ] || undefined ;
163
- const target = env [ 'BAZEL_TARGET' ] ;
164
- if ( ! ! target && ! target . startsWith ( '@' ) ) {
165
- this . package = target . split ( ':' ) [ 0 ] . replace ( / ^ \/ \/ / , '' ) ;
166
- }
167
- }
168
- lookupDirectory ( dir ) {
169
- if ( ! this . manifest )
170
- return undefined ;
171
- let result ;
172
- for ( const [ k , v ] of this . manifest ) {
173
- if ( k . startsWith ( `${ dir } /external` ) )
174
- continue ;
175
- if ( k . startsWith ( dir ) ) {
176
- const l = k . length - dir . length ;
177
- const maybe = v . substring ( 0 , v . length - l ) ;
178
- if ( maybe . match ( BAZEL_OUT_REGEX ) ) {
179
- return maybe ;
180
- }
181
- else {
182
- result = maybe ;
183
- }
184
- }
185
- }
186
- return result ;
187
- }
188
- loadRunfilesManifest ( manifestPath ) {
189
- log_verbose ( `using runfiles manifest ${ manifestPath } ` ) ;
190
- const runfilesEntries = new Map ( ) ;
191
- const input = fs . readFileSync ( manifestPath , { encoding : 'utf-8' } ) ;
192
- for ( const line of input . split ( '\n' ) ) {
193
- if ( ! line )
194
- continue ;
195
- const [ runfilesPath , realPath ] = line . split ( ' ' ) ;
196
- runfilesEntries . set ( runfilesPath , realPath ) ;
197
- }
198
- return runfilesEntries ;
199
- }
200
- resolve ( modulePath ) {
201
- if ( path . isAbsolute ( modulePath ) ) {
202
- return modulePath ;
203
- }
204
- const result = this . _resolve ( modulePath , undefined ) ;
205
- if ( result ) {
206
- return result ;
207
- }
208
- const e = new Error ( `could not resolve module ${ modulePath } ` ) ;
209
- e . code = 'MODULE_NOT_FOUND' ;
210
- throw e ;
211
- }
212
- _resolve ( moduleBase , moduleTail ) {
213
- if ( this . manifest ) {
214
- const result = this . lookupDirectory ( moduleBase ) ;
215
- if ( result ) {
216
- if ( moduleTail ) {
217
- const maybe = path . join ( result , moduleTail || '' ) ;
218
- if ( fs . existsSync ( maybe ) ) {
219
- return maybe ;
220
- }
221
- }
222
- else {
223
- return result ;
224
- }
225
- }
226
- }
227
- if ( exports . runfiles . dir ) {
228
- const maybe = path . join ( exports . runfiles . dir , moduleBase , moduleTail || '' ) ;
229
- if ( fs . existsSync ( maybe ) ) {
230
- return maybe ;
231
- }
232
- }
233
- const dirname = path . dirname ( moduleBase ) ;
234
- if ( dirname == '.' ) {
235
- return undefined ;
236
- }
237
- return this . _resolve ( dirname , path . join ( path . basename ( moduleBase ) , moduleTail || '' ) ) ;
238
- }
239
- resolveWorkspaceRelative ( modulePath ) {
240
- if ( ! this . workspace ) {
241
- throw new Error ( 'workspace could not be determined from the environment; make sure BAZEL_WORKSPACE is set' ) ;
242
- }
243
- return this . resolve ( path . posix . join ( this . workspace , modulePath ) ) ;
244
- }
245
- resolvePackageRelative ( modulePath ) {
246
- if ( ! this . workspace ) {
247
- throw new Error ( 'workspace could not be determined from the environment; make sure BAZEL_WORKSPACE is set' ) ;
248
- }
249
- if ( this . package === undefined ) {
250
- throw new Error ( 'package could not be determined from the environment; make sure BAZEL_TARGET is set' ) ;
251
- }
252
- return this . resolve ( path . posix . join ( this . workspace , this . package , modulePath ) ) ;
253
- }
254
- patchRequire ( ) {
255
- const requirePatch = process . env [ 'BAZEL_NODE_PATCH_REQUIRE' ] ;
256
- if ( ! requirePatch ) {
257
- throw new Error ( 'require patch location could not be determined from the environment' ) ;
258
- }
259
- require ( requirePatch ) ;
260
- }
261
- }
262
- exports . Runfiles = Runfiles ;
263
134
function exists ( p ) {
264
135
return __awaiter ( this , void 0 , void 0 , function * ( ) {
265
136
return ( ( yield gracefulLstat ( p ) ) !== null ) ;
@@ -372,7 +243,7 @@ function findExecroot(startCwd) {
372
243
if ( existsSync ( `${ startCwd } /bazel-out` ) ) {
373
244
return startCwd ;
374
245
}
375
- const bazelOutMatch = startCwd . match ( BAZEL_OUT_REGEX ) ;
246
+ const bazelOutMatch = startCwd . match ( _BAZEL_OUT_REGEX ) ;
376
247
return bazelOutMatch ? startCwd . slice ( 0 , bazelOutMatch . index ) : undefined ;
377
248
}
378
249
function main ( args , runfiles ) {
@@ -511,7 +382,7 @@ function main(args, runfiles) {
511
382
try {
512
383
target = runfiles . resolve ( runfilesPath ) ;
513
384
if ( runfiles . manifest && modulePath . startsWith ( `${ bin } /` ) ) {
514
- if ( ! target . match ( BAZEL_OUT_REGEX ) ) {
385
+ if ( ! target . match ( _BAZEL_OUT_REGEX ) ) {
515
386
const e = new Error ( `could not resolve module ${ runfilesPath } in output tree` ) ;
516
387
e . code = 'MODULE_NOT_FOUND' ;
517
388
throw e ;
@@ -565,7 +436,6 @@ function main(args, runfiles) {
565
436
} ) ;
566
437
}
567
438
exports . main = main ;
568
- exports . runfiles = new Runfiles ( process . env ) ;
569
439
if ( require . main === module ) {
570
440
if ( Number ( process . versions . node . split ( '.' ) [ 0 ] ) < 10 ) {
571
441
console . error ( `ERROR: rules_nodejs linker requires Node v10 or greater, but is running on ${ process . versions . node } ` ) ;
@@ -575,7 +445,7 @@ if (require.main === module) {
575
445
}
576
446
( ( ) => __awaiter ( void 0 , void 0 , void 0 , function * ( ) {
577
447
try {
578
- process . exitCode = yield main ( process . argv . slice ( 2 ) , exports . runfiles ) ;
448
+ process . exitCode = yield main ( process . argv . slice ( 2 ) , _defaultRunfiles ) ;
579
449
}
580
450
catch ( e ) {
581
451
log_error ( e ) ;
0 commit comments