-
Notifications
You must be signed in to change notification settings - Fork 4.4k
Expand file tree
/
Copy pathresolver.zig
More file actions
4405 lines (3844 loc) · 197 KB
/
resolver.zig
File metadata and controls
4405 lines (3844 loc) · 197 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
pub const DataURL = @import("./data_url.zig").DataURL;
pub const DirInfo = @import("./dir_info.zig");
const debuglog = Output.scoped(.Resolver, .hidden);
pub fn isPackagePath(path: string) bool {
// Always check for posix absolute paths (starts with "/")
// But don't check window's style on posix
// For a more in depth explanation, look above where `isPackagePathNotAbsolute` is used.
return !std.fs.path.isAbsolute(path) and @call(bun.callmod_inline, isPackagePathNotAbsolute, .{path});
}
pub fn isPackagePathNotAbsolute(non_absolute_path: string) bool {
if (Environment.allow_assert) {
assert(!std.fs.path.isAbsolute(non_absolute_path));
assert(!strings.startsWith(non_absolute_path, "/"));
}
return !strings.startsWith(non_absolute_path, "./") and
!strings.startsWith(non_absolute_path, "../") and
!strings.eql(non_absolute_path, ".") and
!strings.eql(non_absolute_path, "..") and if (Environment.isWindows)
(!strings.startsWith(non_absolute_path, ".\\") and
!strings.startsWith(non_absolute_path, "..\\"))
else
true;
}
pub const SideEffectsData = struct {
source: *logger.Source,
range: logger.Range,
// If true, "sideEffects" was an array. If false, "sideEffects" was false.
is_side_effects_array_in_json: bool = false,
};
/// A temporary threadlocal buffer with a lifetime more than the current
/// function call.
const bufs = struct {
// Experimenting with making this one struct instead of a bunch of different
// threadlocal vars yielded no performance improvement on macOS when
// bundling 10 copies of Three.js. It may be worthwhile for more complicated
// packages but we lack a decent module resolution benchmark right now.
// Potentially revisit after https://github.com/oven-sh/bun/issues/2716
pub threadlocal var extension_path: [512]u8 = undefined;
pub threadlocal var tsconfig_match_full_buf: bun.PathBuffer = undefined;
pub threadlocal var tsconfig_match_full_buf2: bun.PathBuffer = undefined;
pub threadlocal var tsconfig_match_full_buf3: bun.PathBuffer = undefined;
pub threadlocal var esm_subpath: [512]u8 = undefined;
pub threadlocal var esm_absolute_package_path: bun.PathBuffer = undefined;
pub threadlocal var esm_absolute_package_path_joined: bun.PathBuffer = undefined;
pub threadlocal var dir_entry_paths_to_resolve: [256]DirEntryResolveQueueItem = undefined;
pub threadlocal var open_dirs: [256]FD = undefined;
pub threadlocal var resolve_without_remapping: bun.PathBuffer = undefined;
pub threadlocal var index: bun.PathBuffer = undefined;
pub threadlocal var dir_info_uncached_filename: bun.PathBuffer = undefined;
pub threadlocal var node_bin_path: bun.PathBuffer = undefined;
pub threadlocal var dir_info_uncached_path: bun.PathBuffer = undefined;
pub threadlocal var tsconfig_base_url: bun.PathBuffer = undefined;
pub threadlocal var relative_abs_path: bun.PathBuffer = undefined;
pub threadlocal var load_as_file_or_directory_via_tsconfig_base_path: bun.PathBuffer = undefined;
pub threadlocal var node_modules_check: bun.PathBuffer = undefined;
pub threadlocal var field_abs_path: bun.PathBuffer = undefined;
pub threadlocal var tsconfig_path_abs: bun.PathBuffer = undefined;
pub threadlocal var check_browser_map: bun.PathBuffer = undefined;
pub threadlocal var remap_path: bun.PathBuffer = undefined;
pub threadlocal var load_as_file: bun.PathBuffer = undefined;
pub threadlocal var remap_path_trailing_slash: bun.PathBuffer = undefined;
pub threadlocal var path_in_global_disk_cache: bun.PathBuffer = undefined;
pub threadlocal var abs_to_rel: bun.PathBuffer = undefined;
pub threadlocal var node_modules_paths_buf: bun.PathBuffer = undefined;
pub threadlocal var import_path_for_standalone_module_graph: bun.PathBuffer = undefined;
pub inline fn bufs(comptime field: std.meta.DeclEnum(@This())) *@TypeOf(@field(@This(), @tagName(field))) {
return &@field(@This(), @tagName(field));
}
}.bufs;
pub const PathPair = struct {
primary: Path,
secondary: ?Path = null,
pub const Iter = struct {
index: u2,
ctx: *PathPair,
pub fn next(i: *Iter) ?*Path {
if (i.next_()) |path_| {
if (path_.is_disabled) {
return i.next();
}
return path_;
}
return null;
}
fn next_(i: *Iter) ?*Path {
const ind = i.index;
i.index +|= 1;
switch (ind) {
0 => return &i.ctx.primary,
1 => return if (i.ctx.secondary) |*sec| sec else null,
else => return null,
}
}
};
pub fn iter(p: *PathPair) Iter {
return Iter{ .ctx = p, .index = 0 };
}
};
pub const SideEffects = enum {
/// The default value conservatively considers all files to have side effects.
has_side_effects,
/// This file was listed as not having side effects by a "package.json"
/// file in one of our containing directories with a "sideEffects" field.
no_side_effects__package_json,
/// This file is considered to have no side effects because the AST was empty
/// after parsing finished. This should be the case for ".d.ts" files.
no_side_effects__empty_ast,
/// This file was loaded using a data-oriented loader (e.g. "text") that is
/// known to not have side effects.
no_side_effects__pure_data,
// /// Same as above but it came from a plugin. We don't want to warn about
// /// unused imports to these files since running the plugin is a side effect.
// /// Removing the import would not call the plugin which is observable.
// no_side_effects__pure_data_from_plugin,
};
pub const Result = struct {
path_pair: PathPair,
jsx: options.JSX.Pragma = options.JSX.Pragma{},
package_json: ?*PackageJSON = null,
diff_case: ?Fs.FileSystem.Entry.Lookup.DifferentCase = null,
// If present, any ES6 imports to this file can be considered to have no side
// effects. This means they should be removed if unused.
primary_side_effects_data: SideEffects = SideEffects.has_side_effects,
// This is the "type" field from "package.json"
module_type: options.ModuleType = options.ModuleType.unknown,
debug_meta: ?DebugMeta = null,
dirname_fd: FD = .invalid,
file_fd: FD = .invalid,
import_kind: ast.ImportKind = undefined,
/// Pack boolean flags to reduce padding overhead.
/// Previously 6 separate bool fields caused ~42+ bytes of padding waste.
flags: Flags = .{},
pub const Flags = packed struct(u8) {
is_external: bool = false,
is_external_and_rewrite_import_path: bool = false,
is_standalone_module: bool = false,
// This is true when the package was loaded from within the node_modules directory.
is_from_node_modules: bool = false,
// If true, unused imports are retained in TypeScript code. This matches the
// behavior of the "importsNotUsedAsValues" field in "tsconfig.json" when the
// value is not "remove".
preserve_unused_imports_ts: bool = false,
emit_decorator_metadata: bool = false,
experimental_decorators: bool = false,
_padding: u1 = 0,
};
pub const Union = union(enum) {
success: Result,
failure: anyerror,
pending: PendingResolution,
not_found: void,
};
pub fn path(this: *Result) ?*Path {
if (!this.path_pair.primary.is_disabled)
return &this.path_pair.primary;
if (this.path_pair.secondary) |*second| {
if (!second.is_disabled) return second;
}
return null;
}
pub fn pathConst(this: *const Result) ?*const Path {
if (!this.path_pair.primary.is_disabled)
return &this.path_pair.primary;
if (this.path_pair.secondary) |*second| {
if (!second.is_disabled) return second;
}
return null;
}
// remember: non-node_modules can have package.json
// checking package.json may not be relevant
pub fn isLikelyNodeModule(this: *const Result) bool {
const path_ = this.pathConst() orelse return false;
return this.flags.is_from_node_modules or strings.indexOf(path_.text, "/node_modules/") != null;
}
// Most NPM modules are CommonJS
// If unspecified, assume CommonJS.
// If internal app code, assume ESM.
pub fn shouldAssumeCommonJS(r: *const Result, kind: ast.ImportKind) bool {
switch (r.module_type) {
.esm => return false,
.cjs => return true,
else => {
if (kind == .require or kind == .require_resolve) {
return true;
}
// If we rely just on isPackagePath, we mess up tsconfig.json baseUrl paths.
return r.isLikelyNodeModule();
},
}
}
pub const DebugMeta = struct {
notes: std.array_list.Managed(logger.Data),
suggestion_text: string = "",
suggestion_message: string = "",
suggestion_range: SuggestionRange,
pub const SuggestionRange = enum { full, end };
pub fn init(allocator: std.mem.Allocator) DebugMeta {
return DebugMeta{ .notes = std.array_list.Managed(logger.Data).init(allocator) };
}
pub fn logErrorMsg(m: *DebugMeta, log: *logger.Log, _source: ?*const logger.Source, r: logger.Range, comptime fmt: string, args: anytype) !void {
if (_source != null and m.suggestion_message.len > 0) {
const suggestion_range = if (m.suggestion_range == .end)
logger.Range{ .loc = logger.Loc{ .start = r.endI() - 1 } }
else
r;
const data = logger.rangeData(_source.?, suggestion_range, m.suggestion_message);
data.location.?.suggestion = m.suggestion_text;
try m.notes.append(data);
}
try log.addMsg(Msg{
.kind = .err,
.data = logger.rangeData(_source, r, std.fmt.allocPrint(m.notes.allocator, fmt, args)),
.notes = try m.toOwnedSlice(),
});
}
};
pub fn hash(this: *const Result, _: string, _: options.Loader) u32 {
const module = this.path_pair.primary.text;
const node_module_root = std.fs.path.sep_str ++ "node_modules" ++ std.fs.path.sep_str;
if (strings.lastIndexOf(module, node_module_root)) |end_| {
const end: usize = end_ + node_module_root.len;
return @as(u32, @truncate(bun.hash(module[end..])));
}
return @as(u32, @truncate(bun.hash(this.path_pair.primary.text)));
}
};
pub const DirEntryResolveQueueItem = struct {
result: allocators.Result,
unsafe_path: string,
safe_path: string = "",
fd: FD = .invalid,
};
pub const DebugLogs = struct {
what: string = "",
indent: MutableString,
notes: std.array_list.Managed(logger.Data),
pub const FlushMode = enum { fail, success };
pub fn init(allocator: std.mem.Allocator) !DebugLogs {
const mutable = try MutableString.init(allocator, 0);
return DebugLogs{
.indent = mutable,
.notes = std.array_list.Managed(logger.Data).init(allocator),
};
}
pub fn deinit(d: DebugLogs) void {
d.notes.deinit();
// d.indent.deinit();
}
pub fn increaseIndent(d: *DebugLogs) void {
@branchHint(.cold);
d.indent.append(" ") catch unreachable;
}
pub fn decreaseIndent(d: *DebugLogs) void {
@branchHint(.cold);
d.indent.list.shrinkRetainingCapacity(d.indent.list.items.len - 1);
}
pub fn addNote(d: *DebugLogs, _text: string) void {
@branchHint(.cold);
var text = _text;
const len = d.indent.len();
if (len > 0) {
var __text = d.notes.allocator.alloc(u8, text.len + len) catch unreachable;
bun.copy(u8, __text, d.indent.list.items);
bun.copy(u8, __text[len..], _text);
text = __text;
d.notes.allocator.free(_text);
}
d.notes.append(logger.rangeData(null, logger.Range.None, text)) catch unreachable;
}
pub fn addNoteFmt(d: *DebugLogs, comptime fmt: string, args: anytype) void {
@branchHint(.cold);
return d.addNote(std.fmt.allocPrint(d.notes.allocator, fmt, args) catch unreachable);
}
};
pub const MatchResult = struct {
path_pair: PathPair,
dirname_fd: FD = .invalid,
file_fd: FD = .invalid,
is_node_module: bool = false,
package_json: ?*PackageJSON = null,
diff_case: ?Fs.FileSystem.Entry.Lookup.DifferentCase = null,
dir_info: ?*DirInfo = null,
module_type: options.ModuleType = .unknown,
is_external: bool = false,
pub const Union = union(enum) {
not_found: void,
success: MatchResult,
pending: PendingResolution,
failure: anyerror,
};
};
pub const PendingResolution = struct {
esm: ESModule.Package.External = .{},
dependency: Dependency.Version = .{},
resolution_id: Install.PackageID = Install.invalid_package_id,
root_dependency_id: Install.DependencyID = Install.invalid_package_id,
import_record_id: u32 = std.math.maxInt(u32),
string_buf: []u8 = "",
tag: Tag,
pub const List = std.MultiArrayList(PendingResolution);
pub fn deinitListItems(list_: List, allocator: std.mem.Allocator) void {
var list = list_;
const dependencies = list.items(.dependency);
const string_bufs = list.items(.string_buf);
for (dependencies) |*dependency| {
dependency.deinit();
}
for (string_bufs) |string_buf| {
allocator.free(string_buf);
}
}
pub fn deinit(this: *PendingResolution, allocator: std.mem.Allocator) void {
this.dependency.deinit();
allocator.free(this.string_buf);
}
pub const Tag = enum {
download,
resolve,
done,
};
pub fn init(
allocator: std.mem.Allocator,
esm: ESModule.Package,
dependency: Dependency.Version,
resolution_id: Install.PackageID,
) !PendingResolution {
return PendingResolution{
.esm = try esm.copy(allocator),
.dependency = dependency,
.resolution_id = resolution_id,
};
}
};
pub const LoadResult = struct {
path: string,
diff_case: ?Fs.FileSystem.Entry.Lookup.DifferentCase,
dirname_fd: FD = .invalid,
file_fd: FD = .invalid,
dir_info: ?*DirInfo = null,
};
// This is a global so even if multiple resolvers are created, the mutex will still work
var resolver_Mutex: Mutex = undefined;
var resolver_Mutex_loaded: bool = false;
const BinFolderArray = bun.BoundedArray(string, 128);
var bin_folders: BinFolderArray = undefined;
var bin_folders_lock: Mutex = .{};
var bin_folders_loaded: bool = false;
pub const AnyResolveWatcher = struct {
context: *anyopaque,
callback: *const (fn (*anyopaque, dir_path: string, dir_fd: FD) void) = undefined,
pub fn watch(this: @This(), dir_path: string, fd: FD) void {
return this.callback(this.context, dir_path, fd);
}
};
pub fn ResolveWatcher(comptime Context: type, comptime onWatch: anytype) type {
return struct {
pub fn init(context: Context) AnyResolveWatcher {
return AnyResolveWatcher{
.context = context,
.callback = watch,
};
}
pub fn watch(this: *anyopaque, dir_path: string, fd: FD) void {
onWatch(bun.cast(Context, this), dir_path, fd);
}
};
}
pub const Resolver = struct {
const ThisResolver = @This();
opts: options.BundleOptions,
fs: *Fs.FileSystem,
log: *logger.Log,
allocator: std.mem.Allocator,
extension_order: []const string = undefined,
timer: Timer = undefined,
care_about_bin_folder: bool = false,
care_about_scripts: bool = false,
/// Read the "browser" field in package.json files?
/// For Bun's runtime, we don't.
care_about_browser_field: bool = true,
debug_logs: ?DebugLogs = null,
elapsed: u64 = 0, // tracing
watcher: ?AnyResolveWatcher = null,
caches: CacheSet,
generation: bun.Generation = 0,
package_manager: ?*PackageManager = null,
onWakePackageManager: PackageManager.WakeHandler = .{},
env_loader: ?*DotEnv.Loader = null,
store_fd: bool = false,
standalone_module_graph: ?*bun.StandaloneModuleGraph = null,
// These are sets that represent various conditions for the "exports" field
// in package.json.
// esm_conditions_default: bun.StringHashMap(bool),
// esm_conditions_import: bun.StringHashMap(bool),
// esm_conditions_require: bun.StringHashMap(bool),
// A special filtered import order for CSS "@import" imports.
//
// The "resolve extensions" setting determines the order of implicit
// extensions to try when resolving imports with the extension omitted.
// Sometimes people create a JavaScript/TypeScript file and a CSS file with
// the same name when they create a component. At a high level, users expect
// implicit extensions to resolve to the JS file when being imported from JS
// and to resolve to the CSS file when being imported from CSS.
//
// Different bundlers handle this in different ways. Parcel handles this by
// having the resolver prefer the same extension as the importing file in
// front of the configured "resolve extensions" order. Webpack's "css-loader"
// plugin just explicitly configures a special "resolve extensions" order
// consisting of only ".css" for CSS files.
//
// It's unclear what behavior is best here. What we currently do is to create
// a special filtered version of the configured "resolve extensions" order
// for CSS files that filters out any extension that has been explicitly
// configured with a non-CSS loader. This still gives users control over the
// order but avoids the scenario where we match an import in a CSS file to a
// JavaScript-related file. It's probably not perfect with plugins in the
// picture but it's better than some alternatives and probably pretty good.
// atImportExtensionOrder []string
// This mutex serves two purposes. First of all, it guards access to "dirCache"
// which is potentially mutated during path resolution. But this mutex is also
// necessary for performance. The "React admin" benchmark mysteriously runs
// twice as fast when this mutex is locked around the whole resolve operation
// instead of around individual accesses to "dirCache". For some reason,
// reducing parallelism in the resolver helps the rest of the bundler go
// faster. I'm not sure why this is but please don't change this unless you
// do a lot of testing with various benchmarks and there aren't any regressions.
mutex: *Mutex,
/// This cache maps a directory path to information about that directory and
/// all parent directories. When interacting with this structure, make sure
/// to validate your keys with `Resolver.assertValidCacheKey`
dir_cache: *DirInfo.HashMap,
/// This is set to false for the runtime. The runtime should choose "main"
/// over "module" in package.json
prefer_module_field: bool = true,
/// This is an array of paths to resolve against. Used for passing an
/// object '{ paths: string[] }' to `require` and `resolve`; This field
/// is overwritten while the resolution happens.
///
/// When this is null, it is as if it is set to `&.{ path.dirname(referrer) }`.
custom_dir_paths: ?[]const bun.String = null,
pub fn getPackageManager(this: *Resolver) *PackageManager {
return this.package_manager orelse brk: {
bun.HTTPThread.init(&.{});
const pm = PackageManager.initWithRuntime(
this.log,
this.opts.install,
// This cannot be the threadlocal allocator. It goes to the HTTP thread.
bun.default_allocator,
.{},
this.env_loader.?,
);
pm.onWake = this.onWakePackageManager;
this.package_manager = pm;
break :brk pm;
};
}
pub inline fn usePackageManager(self: *const ThisResolver) bool {
// TODO(@paperclover): make this configurable. the rationale for disabling
// auto-install in standalone mode is that such executable must either:
//
// - bundle the dependency itself. dynamic `require`/`import` could be
// changed to bundle potential dependencies specified in package.json
//
// - want to load the user's node_modules, which is what currently happens.
//
// auto install, as of writing, is also quite buggy and untested, it always
// installs the latest version regardless of a user's package.json or specifier.
// in addition to being not fully stable, it is completely unexpected to invoke
// a package manager after bundling an executable. if enough people run into
// this, we could implement point 1
if (self.standalone_module_graph) |_| return false;
return self.opts.global_cache.isEnabled();
}
pub fn init1(
allocator: std.mem.Allocator,
log: *logger.Log,
_fs: *Fs.FileSystem,
opts: options.BundleOptions,
) ThisResolver {
if (!resolver_Mutex_loaded) {
resolver_Mutex = .{};
resolver_Mutex_loaded = true;
}
return ThisResolver{
.allocator = allocator,
.dir_cache = DirInfo.HashMap.init(bun.default_allocator),
.mutex = &resolver_Mutex,
.caches = CacheSet.init(allocator),
.opts = opts,
.timer = Timer.start() catch @panic("Timer fail"),
.fs = _fs,
.log = log,
.extension_order = opts.extension_order.default.default,
.care_about_browser_field = opts.target == .browser,
};
}
pub fn deinit(r: *ThisResolver) void {
for (r.dir_cache.values()) |*di| di.deinit();
r.dir_cache.deinit();
}
pub fn isExternalPattern(r: *ThisResolver, import_path: string) bool {
if (r.opts.packages == .external and isPackagePath(import_path)) {
return true;
}
for (r.opts.external.patterns) |pattern| {
if (import_path.len >= pattern.prefix.len + pattern.suffix.len and (strings.startsWith(
import_path,
pattern.prefix,
) and strings.endsWith(
import_path,
pattern.suffix,
))) {
return true;
}
}
return false;
}
pub fn flushDebugLogs(r: *ThisResolver, flush_mode: DebugLogs.FlushMode) !void {
if (r.debug_logs) |*debug| {
if (flush_mode == DebugLogs.FlushMode.fail) {
try r.log.addRangeDebugWithNotes(null, logger.Range{ .loc = logger.Loc{} }, debug.what, try debug.notes.toOwnedSlice());
} else if (@intFromEnum(r.log.level) <= @intFromEnum(logger.Log.Level.verbose)) {
try r.log.addVerboseWithNotes(null, logger.Loc.Empty, debug.what, try debug.notes.toOwnedSlice());
}
}
}
var tracing_start: i128 = if (FeatureFlags.tracing) 0 else undefined;
pub fn resolveAndAutoInstall(
r: *ThisResolver,
source_dir: string,
import_path: string,
kind: ast.ImportKind,
global_cache: GlobalCache,
) Result.Union {
const tracer = bun.perf.trace("ModuleResolver.resolve");
defer tracer.end();
// Only setting 'current_action' in debug mode because module resolution
// is done very often, and has a very low crash rate.
const prev_action = if (Environment.show_crash_trace) bun.crash_handler.current_action;
if (Environment.show_crash_trace) bun.crash_handler.current_action = .{ .resolver = .{
.source_dir = source_dir,
.import_path = import_path,
.kind = kind,
} };
defer if (Environment.show_crash_trace) {
bun.crash_handler.current_action = prev_action;
};
if (Environment.show_crash_trace and bun.cli.debug_flags.hasResolveBreakpoint(import_path)) {
bun.Output.debug("Resolving <green>{s}<r> from <blue>{s}<r>", .{
import_path,
source_dir,
});
@breakpoint();
}
const original_order = r.extension_order;
defer r.extension_order = original_order;
r.extension_order = switch (kind) {
.url, .at_conditional, .at => options.BundleOptions.Defaults.CSSExtensionOrder[0..],
.entry_point_build, .entry_point_run, .stmt, .dynamic => r.opts.extension_order.default.esm,
else => r.opts.extension_order.default.default,
};
if (FeatureFlags.tracing) {
r.timer.reset();
}
defer {
if (FeatureFlags.tracing) {
r.elapsed += r.timer.read();
}
}
if (r.log.level == .verbose) {
if (r.debug_logs != null) {
r.debug_logs.?.deinit();
}
r.debug_logs = DebugLogs.init(r.allocator) catch unreachable;
}
if (import_path.len == 0) return .{ .not_found = {} };
if (r.opts.mark_builtins_as_external) {
if (strings.hasPrefixComptime(import_path, "node:") or
strings.hasPrefixComptime(import_path, "bun:") or
bun.jsc.ModuleLoader.HardcodedModule.Alias.has(import_path, r.opts.target, .{ .rewrite_jest_for_tests = r.opts.rewrite_jest_for_tests }))
{
return .{
.success = Result{
.import_kind = kind,
.path_pair = PathPair{
.primary = Path.init(import_path),
},
.module_type = .cjs,
.primary_side_effects_data = .no_side_effects__pure_data,
.flags = .{ .is_external = true },
},
};
}
}
// Certain types of URLs default to being external for convenience,
// while these rules should not be applied to the entrypoint as it is never external (#12734)
if (kind != .entry_point_build and kind != .entry_point_run and
(r.isExternalPattern(import_path) or
// "fill: url(#filter);"
(kind.isFromCSS() and strings.startsWith(import_path, "#")) or
// "background: url(http://example.com/images/image.png);"
strings.startsWith(import_path, "http://") or
// "background: url(https://example.com/images/image.png);"
strings.startsWith(import_path, "https://") or
// "background: url(//example.com/images/image.png);"
strings.startsWith(import_path, "//")))
{
if (r.debug_logs) |*debug| {
debug.addNote("Marking this path as implicitly external");
r.flushDebugLogs(.success) catch {};
}
return .{
.success = Result{
.import_kind = kind,
.path_pair = PathPair{
.primary = Path.init(import_path),
},
.module_type = if (!kind.isFromCSS()) .esm else .unknown,
.flags = .{ .is_external = true },
},
};
}
if (DataURL.parse(import_path) catch {
return .{ .failure = error.InvalidDataURL };
}) |data_url| {
// "import 'data:text/javascript,console.log(123)';"
// "@import 'data:text/css,body{background:white}';"
const mime = data_url.decodeMimeType();
if (mime.category == .javascript or mime.category == .css or mime.category == .json or mime.category == .text) {
if (r.debug_logs) |*debug| {
debug.addNote("Putting this path in the \"dataurl\" namespace");
r.flushDebugLogs(.success) catch {};
}
return .{
.success = Result{ .path_pair = PathPair{ .primary = Path.initWithNamespace(import_path, "dataurl") } },
};
}
// "background: url(data:image/png;base64,iVBORw0KGgo=);"
if (r.debug_logs) |*debug| {
debug.addNote("Marking this \"dataurl\" as external");
r.flushDebugLogs(.success) catch {};
}
return .{
.success = Result{
.path_pair = PathPair{ .primary = Path.initWithNamespace(import_path, "dataurl") },
.flags = .{ .is_external = true },
},
};
}
// When using `bun build --compile`, module resolution is never
// relative to our special /$bunfs/ directory.
//
// It's always relative to the current working directory of the project root.
//
// ...unless you pass a relative path that exists in the standalone module graph executable.
var source_dir_resolver: bun.path.PosixToWinNormalizer = .{};
const source_dir_normalized = brk: {
if (r.standalone_module_graph) |graph| {
if (bun.StandaloneModuleGraph.isBunStandaloneFilePath(import_path)) {
if (graph.findAssumeStandalonePath(import_path) != null) {
return .{
.success = Result{
.import_kind = kind,
.path_pair = PathPair{
.primary = Path.init(import_path),
},
.module_type = .esm,
.flags = .{ .is_standalone_module = true },
},
};
}
return .{ .not_found = {} };
} else if (bun.StandaloneModuleGraph.isBunStandaloneFilePath(source_dir)) {
if (import_path.len > 2 and isDotSlash(import_path[0..2])) {
const buf = bufs(.import_path_for_standalone_module_graph);
const joined = bun.path.joinAbsStringBuf(source_dir, buf, &.{import_path}, .loose);
// Support relative paths in the graph
if (graph.findAssumeStandalonePath(joined)) |file| {
return .{
.success = Result{
.import_kind = kind,
.path_pair = PathPair{
.primary = Path.init(file.name),
},
.module_type = .esm,
.flags = .{ .is_standalone_module = true },
},
};
}
}
break :brk Fs.FileSystem.instance.top_level_dir;
}
}
// Fail now if there is no directory to resolve in. This can happen for
// virtual modules (e.g. stdin) if a resolve directory is not specified.
//
// TODO: This is skipped for now because it is impossible to set a
// resolveDir so we default to the top level directory instead (this
// is backwards compat with Bun 1.0 behavior)
// See https://github.com/oven-sh/bun/issues/8994 for more details.
if (source_dir.len == 0) {
// if (r.debug_logs) |*debug| {
// debug.addNote("Cannot resolve this path without a directory");
// r.flushDebugLogs(.fail) catch {};
// }
// return .{ .failure = error.MissingResolveDir };
break :brk Fs.FileSystem.instance.top_level_dir;
}
// This can also be hit if you use plugins with non-file namespaces,
// or call the module resolver from javascript (Bun.resolveSync)
// with a faulty parent specifier.
if (!std.fs.path.isAbsolute(source_dir)) {
// if (r.debug_logs) |*debug| {
// debug.addNote("Cannot resolve this path without an absolute directory");
// r.flushDebugLogs(.fail) catch {};
// }
// return .{ .failure = error.InvalidResolveDir };
break :brk Fs.FileSystem.instance.top_level_dir;
}
break :brk source_dir_resolver.resolveCWD(source_dir) catch @panic("Failed to query CWD");
};
// r.mutex.lock();
// defer r.mutex.unlock();
errdefer (r.flushDebugLogs(.fail) catch {});
// A path with a null byte cannot exist on the filesystem. Continuing
// anyways would cause assertion failures.
if (bun.strings.containsChar(import_path, 0)) {
r.flushDebugLogs(.fail) catch {};
return .{ .not_found = {} };
}
var tmp = r.resolveWithoutSymlinks(source_dir_normalized, import_path, kind, global_cache);
// Fragments in URLs in CSS imports are technically expected to work
if (tmp == .not_found and kind.isFromCSS()) try_without_suffix: {
// If resolution failed, try again with the URL query and/or hash removed
const maybe_suffix = bun.strings.indexOfAny(import_path, "?#");
if (maybe_suffix == null or maybe_suffix.? < 1)
break :try_without_suffix;
const suffix = maybe_suffix.?;
if (r.debug_logs) |*debug| {
debug.addNoteFmt("Retrying resolution after removing the suffix {s}", .{import_path[suffix..]});
}
const result2 = r.resolveWithoutSymlinks(source_dir_normalized, import_path[0..suffix], kind, global_cache);
if (result2 == .not_found) break :try_without_suffix;
tmp = result2;
}
switch (tmp) {
.success => |*result| {
if (!strings.eqlComptime(result.path_pair.primary.namespace, "node") and !result.flags.is_standalone_module)
r.finalizeResult(result, kind) catch |err| return .{ .failure = err };
r.flushDebugLogs(.success) catch {};
result.import_kind = kind;
if (comptime Environment.enable_logs) {
if (result.path_pair.secondary) |secondary| {
debuglog(
"resolve({f}, from: {f}, {s}) = {f} (secondary: {f})",
.{
bun.fmt.fmtPath(u8, import_path, .{}),
bun.fmt.fmtPath(u8, source_dir, .{}),
kind.label(),
bun.fmt.fmtPath(u8, if (result.path()) |path| path.text else "<NULL>", .{}),
bun.fmt.fmtPath(u8, secondary.text, .{}),
},
);
} else {
debuglog(
"resolve({f}, from: {f}, {s}) = {f}",
.{
bun.fmt.fmtPath(u8, import_path, .{}),
bun.fmt.fmtPath(u8, source_dir, .{}),
kind.label(),
bun.fmt.fmtPath(u8, if (result.path()) |path| path.text else "<NULL>", .{}),
},
);
}
}
return .{ .success = result.* };
},
.failure => |e| {
r.flushDebugLogs(.fail) catch {};
return .{ .failure = e };
},
.pending => |pending| {
r.flushDebugLogs(.fail) catch {};
return .{ .pending = pending };
},
.not_found => {
r.flushDebugLogs(.fail) catch {};
return .{ .not_found = {} };
},
}
}
pub fn resolve(r: *ThisResolver, source_dir: string, import_path: string, kind: ast.ImportKind) !Result {
switch (r.resolveAndAutoInstall(source_dir, import_path, kind, GlobalCache.disable)) {
.success => |result| return result,
.pending, .not_found => return error.ModuleNotFound,
.failure => |e| return e,
}
}
/// Runs a resolution but also checking if a Bun Bake framework has an
/// override. This is used in one place in the bundler.
pub fn resolveWithFramework(r: *ThisResolver, source_dir: string, import_path: string, kind: ast.ImportKind) !Result {
if (r.opts.framework) |f| {
if (f.built_in_modules.get(import_path)) |mod| {
switch (mod) {
.code => {
return .{
.import_kind = kind,
.path_pair = .{ .primary = Fs.Path.initWithNamespace(import_path, "node") },
.module_type = .esm,
.primary_side_effects_data = .no_side_effects__pure_data,
.flags = .{ .is_external = false },
};
},
.import => |path| return r.resolve(r.fs.top_level_dir, path, .entry_point_build),
}
return .{};
}
}
return r.resolve(source_dir, import_path, kind);
}
const ModuleTypeMap = bun.ComptimeStringMap(options.ModuleType, .{
.{ ".mjs", options.ModuleType.esm },
.{ ".mts", options.ModuleType.esm },
.{ ".cjs", options.ModuleType.cjs },
.{ ".cts", options.ModuleType.cjs },
});
pub fn finalizeResult(r: *ThisResolver, result: *Result, kind: ast.ImportKind) !void {
if (result.flags.is_external) return;
var iter = result.path_pair.iter();
var module_type = result.module_type;
while (iter.next()) |path| {
var dir: *DirInfo = (r.readDirInfo(path.name.dir) catch continue) orelse continue;
var needs_side_effects = true;
if (result.package_json) |existing| {
// if we don't have it here, they might put it in a sideEfffects
// map of the parent package.json
// TODO: check if webpack also does this parent lookup
needs_side_effects = existing.side_effects == .unspecified or existing.side_effects == .glob or existing.side_effects == .mixed;
result.primary_side_effects_data = switch (existing.side_effects) {
.unspecified => .has_side_effects,
.false => .no_side_effects__package_json,
.map => |map| if (map.contains(bun.StringHashMapUnowned.Key.init(path.text))) .has_side_effects else .no_side_effects__package_json,
.glob => if (existing.side_effects.hasSideEffects(path.text)) .has_side_effects else .no_side_effects__package_json,
.mixed => if (existing.side_effects.hasSideEffects(path.text)) .has_side_effects else .no_side_effects__package_json,
};
if (existing.name.len == 0 or r.care_about_bin_folder) result.package_json = null;
}
result.package_json = result.package_json orelse dir.enclosing_package_json;
if (needs_side_effects) {
if (result.package_json) |package_json| {
result.primary_side_effects_data = switch (package_json.side_effects) {
.unspecified => .has_side_effects,
.false => .no_side_effects__package_json,
.map => |map| if (map.contains(bun.StringHashMapUnowned.Key.init(path.text))) .has_side_effects else .no_side_effects__package_json,
.glob => if (package_json.side_effects.hasSideEffects(path.text)) .has_side_effects else .no_side_effects__package_json,
.mixed => if (package_json.side_effects.hasSideEffects(path.text)) .has_side_effects else .no_side_effects__package_json,
};
}
}