-
Notifications
You must be signed in to change notification settings - Fork 2.5k
/
resolver.zig
4232 lines (3680 loc) · 186 KB
/
resolver.zig
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
const bun = @import("root").bun;
const string = bun.string;
const Output = bun.Output;
const Global = bun.Global;
const Environment = bun.Environment;
const strings = bun.strings;
const MutableString = bun.MutableString;
const FeatureFlags = bun.FeatureFlags;
const PathString = bun.PathString;
const stringZ = bun.stringZ;
const default_allocator = bun.default_allocator;
const StoredFileDescriptorType = bun.StoredFileDescriptorType;
const C = bun.C;
const ast = @import("../import_record.zig");
const logger = bun.logger;
const options = @import("../options.zig");
const Fs = @import("../fs.zig");
const std = @import("std");
const cache = @import("../cache.zig");
const sync = @import("../sync.zig");
const TSConfigJSON = @import("./tsconfig_json.zig").TSConfigJSON;
const PackageJSON = @import("./package_json.zig").PackageJSON;
const MacroRemap = @import("./package_json.zig").MacroMap;
const ESModule = @import("./package_json.zig").ESModule;
const BrowserMap = @import("./package_json.zig").BrowserMap;
const CacheSet = cache.Set;
const DataURL = @import("./data_url.zig").DataURL;
pub const DirInfo = @import("./dir_info.zig");
const ResolvePath = @import("./resolve_path.zig");
const NodeFallbackModules = @import("../node_fallbacks.zig");
const Mutex = @import("../lock.zig").Lock;
const StringBoolMap = bun.StringHashMap(bool);
const FileDescriptorType = bun.FileDescriptor;
const JSC = bun.JSC;
const allocators = @import("../allocators.zig");
const Msg = logger.Msg;
const Path = Fs.Path;
const debuglog = Output.scoped(.Resolver, true);
const PackageManager = @import("../install/install.zig").PackageManager;
const Dependency = @import("../install/dependency.zig");
const Install = @import("../install/install.zig");
const Lockfile = @import("../install/lockfile.zig").Lockfile;
const Package = @import("../install/lockfile.zig").Package;
const Resolution = @import("../install/resolution.zig").Resolution;
const Semver = @import("../install/semver.zig");
const DotEnv = @import("../env_loader.zig");
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.MAX_PATH_BYTES]u8 = undefined;
pub threadlocal var tsconfig_match_full_buf2: [bun.MAX_PATH_BYTES]u8 = undefined;
pub threadlocal var tsconfig_match_full_buf3: [bun.MAX_PATH_BYTES]u8 = undefined;
pub threadlocal var esm_subpath: [512]u8 = undefined;
pub threadlocal var esm_absolute_package_path: [bun.MAX_PATH_BYTES]u8 = undefined;
pub threadlocal var esm_absolute_package_path_joined: [bun.MAX_PATH_BYTES]u8 = undefined;
pub threadlocal var dir_entry_paths_to_resolve: [256]DirEntryResolveQueueItem = undefined;
pub threadlocal var open_dirs: [256]std.fs.Dir = undefined;
pub threadlocal var resolve_without_remapping: [bun.MAX_PATH_BYTES]u8 = undefined;
pub threadlocal var index: [bun.MAX_PATH_BYTES]u8 = undefined;
pub threadlocal var dir_info_uncached_filename: [bun.MAX_PATH_BYTES]u8 = undefined;
pub threadlocal var node_bin_path: [bun.MAX_PATH_BYTES]u8 = undefined;
pub threadlocal var dir_info_uncached_path: [bun.MAX_PATH_BYTES]u8 = undefined;
pub threadlocal var tsconfig_base_url: [bun.MAX_PATH_BYTES]u8 = undefined;
pub threadlocal var relative_abs_path: [bun.MAX_PATH_BYTES]u8 = undefined;
pub threadlocal var load_as_file_or_directory_via_tsconfig_base_path: [bun.MAX_PATH_BYTES]u8 = undefined;
pub threadlocal var node_modules_check: [bun.MAX_PATH_BYTES]u8 = undefined;
pub threadlocal var field_abs_path: [bun.MAX_PATH_BYTES]u8 = undefined;
pub threadlocal var tsconfig_path_abs: [bun.MAX_PATH_BYTES]u8 = undefined;
pub threadlocal var check_browser_map: [bun.MAX_PATH_BYTES]u8 = undefined;
pub threadlocal var remap_path: [bun.MAX_PATH_BYTES]u8 = undefined;
pub threadlocal var load_as_file: [bun.MAX_PATH_BYTES]u8 = undefined;
pub threadlocal var remap_path_trailing_slash: [bun.MAX_PATH_BYTES]u8 = undefined;
pub threadlocal var path_in_global_disk_cache: [bun.MAX_PATH_BYTES]u8 = undefined;
pub threadlocal var abs_to_rel: [bun.MAX_PATH_BYTES]u8 = undefined;
pub threadlocal var node_modules_paths_buf: [bun.MAX_PATH_BYTES]u8 = 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 };
}
};
// this is ripped from esbuild, comments included
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,
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,
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,
// 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,
// This is the "type" field from "package.json"
module_type: options.ModuleType = options.ModuleType.unknown,
emit_decorator_metadata: bool = false,
debug_meta: ?DebugMeta = null,
dirname_fd: StoredFileDescriptorType = .zero,
file_fd: StoredFileDescriptorType = .zero,
import_kind: ast.ImportKind = undefined,
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.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.ArrayList(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.ArrayList(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: StoredFileDescriptorType = .zero,
};
pub const DebugLogs = struct {
what: string = "",
indent: MutableString,
notes: std.ArrayList(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.ArrayList(logger.Data).init(allocator),
};
}
pub fn deinit(d: DebugLogs) void {
d.notes.deinit();
// d.indent.deinit();
}
pub fn increaseIndent(d: *DebugLogs) void {
@setCold(true);
d.indent.append(" ") catch unreachable;
}
pub fn decreaseIndent(d: *DebugLogs) void {
@setCold(true);
d.indent.list.shrinkRetainingCapacity(d.indent.list.items.len - 1);
}
pub fn addNote(d: *DebugLogs, _text: string) void {
@setCold(true);
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 {
@setCold(true);
return d.addNote(std.fmt.allocPrint(d.notes.allocator, fmt, args) catch unreachable);
}
};
pub const MatchResult = struct {
path_pair: PathPair,
dirname_fd: StoredFileDescriptorType = .zero,
file_fd: StoredFileDescriptorType = .zero,
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: StoredFileDescriptorType = .zero,
file_fd: StoredFileDescriptorType = .zero,
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 = std.BoundedArray(string, 128);
var bin_folders: BinFolderArray = undefined;
var bin_folders_lock: Mutex = Mutex.init();
var bin_folders_loaded: bool = false;
const Timer = @import("../system_timer.zig").Timer;
pub const AnyResolveWatcher = struct {
context: *anyopaque,
callback: *const (fn (*anyopaque, dir_path: string, dir_fd: StoredFileDescriptorType) void) = undefined,
pub fn watch(this: @This(), dir_path: string, fd: StoredFileDescriptorType) 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: StoredFileDescriptorType) 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,
pub fn getPackageManager(this: *Resolver) *PackageManager {
return this.package_manager orelse brk: {
bun.HTTPThread.init() catch unreachable;
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.?,
) catch @panic("Failed to initialize package manager");
pm.onWake = this.onWakePackageManager;
this.package_manager = pm;
break :brk pm;
};
}
pub inline fn usePackageManager(self: *const ThisResolver) bool {
// TODO(@paperdave): 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 = Mutex.init();
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.isWebLike(),
};
}
pub fn isExternalPattern(r: *ThisResolver, import_path: string) bool {
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 const bunFrameworkPackagePrefix = "bun-framework-";
pub fn resolveFramework(
r: *ThisResolver,
package: string,
pair: *PackageJSON.FrameworkRouterPair,
comptime preference: PackageJSON.LoadFramework,
comptime load_defines: bool,
) !void {
// We want to enable developers to integrate frameworks without waiting on official support.
// But, we still want the command to do the actual framework integration to be succint
// This lets users type "--use next" instead of "--use bun-framework-next"
// If they're using a local file path, we skip this.
if (isPackagePath(package)) {
var prefixed_package_buf: [512]u8 = undefined;
// Prevent the extra lookup if the package is already prefixed, i.e. avoid "bun-framework-next-bun-framework-next"
if (strings.startsWith(package, bunFrameworkPackagePrefix) or package.len + bunFrameworkPackagePrefix.len >= prefixed_package_buf.len) {
return r._resolveFramework(package, pair, preference, load_defines) catch |err| {
switch (err) {
error.ModuleNotFound => {
Output.prettyErrorln("<r><red>ResolveError<r> can't find framework: <b>\"{s}\"<r>.\n\nMaybe it's not installed? Try running this:\n\n <b>bun add -d {s}<r>\n <b>bun bun --use {s}<r>", .{ package, package, package });
Global.exit(1);
},
else => {
return err;
},
}
};
}
prefixed_package_buf[0..bunFrameworkPackagePrefix.len].* = bunFrameworkPackagePrefix.*;
bun.copy(u8, prefixed_package_buf[bunFrameworkPackagePrefix.len..], package);
const prefixed_name = prefixed_package_buf[0 .. bunFrameworkPackagePrefix.len + package.len];
return r._resolveFramework(prefixed_name, pair, preference, load_defines) catch |err| {
switch (err) {
error.ModuleNotFound => {
return r._resolveFramework(package, pair, preference, load_defines) catch |err2| {
switch (err2) {
error.ModuleNotFound => {
Output.prettyErrorln("<r><red>ResolveError<r> can't find framework: <b>\"{s}\"<r>.\n\nMaybe it's not installed? Try running this:\n\n <b>bun add -d {s}\n <b>bun bun --use {s}<r>", .{ package, prefixed_name, package });
Global.exit(1);
},
else => {
return err;
},
}
};
},
else => {
return err;
},
}
};
}
return r._resolveFramework(package, pair, preference, load_defines) catch |err| {
switch (err) {
error.ModuleNotFound => {
Output.prettyError("<r><red>ResolveError<r> can't find local framework: <b>\"{s}\"<r>.", .{package});
Global.exit(1);
},
else => {
return err;
},
}
};
}
fn _resolveFramework(
r: *ThisResolver,
package: string,
pair: *PackageJSON.FrameworkRouterPair,
comptime preference: PackageJSON.LoadFramework,
comptime load_defines: bool,
) !void {
// TODO: make this only parse package.json once
var result = try r.resolve(r.fs.top_level_dir, package, .internal);
// support passing a package.json or path to a package
const pkg: *const PackageJSON = result.package_json orelse r.packageJSONForResolvedNodeModuleWithIgnoreMissingName(&result, true) orelse return error.MissingPackageJSON;
const json = (try r.caches.json.parsePackageJSON(r.log, pkg.source, r.allocator)) orelse return error.JSONParseError;
pkg.loadFrameworkWithPreference(pair, json, r.allocator, load_defines, preference);
const dir = pkg.source.path.sourceDir();
var buf: [bun.MAX_PATH_BYTES]u8 = undefined;
pair.framework.resolved_dir = pkg.source.path.sourceDir();
if (pair.framework.client.isEnabled()) {
var parts = [_]string{ dir, pair.framework.client.path };
const abs = r.fs.abs(&parts);
pair.framework.client.path = try r.allocator.dupe(u8, abs);
pair.framework.resolved = true;
}
if (pair.framework.server.isEnabled()) {
var parts = [_]string{ dir, pair.framework.server.path };
const abs = r.fs.abs(&parts);
pair.framework.server.path = try r.allocator.dupe(u8, abs);
pair.framework.resolved = true;
}
if (pair.framework.fallback.isEnabled()) {
var parts = [_]string{ dir, pair.framework.fallback.path };
const abs = r.fs.abs(&parts);
pair.framework.fallback.path = try r.allocator.dupe(u8, abs);
pair.framework.resolved = true;
}
if (pair.loaded_routes) {
const chosen_dir: string = brk: {
if (pair.router.possible_dirs.len > 0) {
for (pair.router.possible_dirs) |route_dir| {
var parts = [_]string{ r.fs.top_level_dir, std.fs.path.sep_str, route_dir };
const abs = r.fs.join(&parts);
// must end in trailing slash
break :brk (std.os.realpath(abs, &buf) catch continue);
}
return error.MissingRouteDir;
} else {
var parts = [_]string{ r.fs.top_level_dir, std.fs.path.sep_str, pair.router.dir };
const abs = r.fs.join(&parts);
// must end in trailing slash
break :brk std.os.realpath(abs, &buf) catch return error.MissingRouteDir;
}
};
var out = try r.allocator.alloc(u8, chosen_dir.len + 1);
bun.copy(u8, out, chosen_dir);
out[out.len - 1] = '/';
pair.router.dir = out;
pair.router.routes_enabled = true;
}
}
pub fn resolveAndAutoInstall(
r: *ThisResolver,
source_dir: string,
import_path: string,
kind: ast.ImportKind,
global_cache: GlobalCache,
) Result.Union {
const tracer = bun.tracy.traceNamed(@src(), "ModuleResolver.resolve");
defer tracer.end();
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, .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.HardcodedModule.Aliases.has(import_path, r.opts.target))
{
return .{
.success = Result{
.import_kind = kind,
.path_pair = PathPair{
.primary = Path.init(import_path),
},
.is_external = true,
.module_type = .cjs,
.primary_side_effects_data = .no_side_effects__pure_data,
},
};
}
}
// Certain types of URLs default to being external for convenience
if (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),
},
.is_external = true,
.module_type = .esm,
},
};
}
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") },
.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.
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.files.contains(import_path)) {
return .{
.success = Result{
.import_kind = kind,
.path_pair = PathPair{
.primary = Path.init(import_path),
},
.is_standalone_module = true,
.module_type = .esm,
},
};
}
return .{ .not_found = {} };
} else if (bun.StandaloneModuleGraph.isBunStandaloneFilePath(source_dir)) {
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 {});
var tmp = r.resolveWithoutSymlinks(source_dir_normalized, import_path, kind, global_cache);
switch (tmp) {
.success => |*result| {
if (!strings.eqlComptime(result.path_pair.primary.namespace, "node") and !result.is_standalone_module)
r.finalizeResult(result, kind) catch |err| return .{ .failure = err };
r.flushDebugLogs(.success) catch {};
result.import_kind = kind;
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,