diff options
author | 2023-10-17 14:10:25 -0700 | |
---|---|---|
committer | 2023-10-17 14:10:25 -0700 | |
commit | 7458b969c5d9971e89d187b687e1924e78da427e (patch) | |
tree | ee3dbf95c728cf407bf49a27826b541e9264a8bd /src/bun.js/module_loader.zig | |
parent | d4a2c29131ec154f5e4db897d4deedab2002cbc4 (diff) | |
parent | e91436e5248d947b50f90b4a7402690be8a41f39 (diff) | |
download | bun-7458b969c5d9971e89d187b687e1924e78da427e.tar.gz bun-7458b969c5d9971e89d187b687e1924e78da427e.tar.zst bun-7458b969c5d9971e89d187b687e1924e78da427e.zip |
Merge branch 'main' into postinstall_3
Diffstat (limited to 'src/bun.js/module_loader.zig')
-rw-r--r-- | src/bun.js/module_loader.zig | 112 |
1 files changed, 59 insertions, 53 deletions
diff --git a/src/bun.js/module_loader.zig b/src/bun.js/module_loader.zig index cf86cb460..9a46d403b 100644 --- a/src/bun.js/module_loader.zig +++ b/src/bun.js/module_loader.zig @@ -154,21 +154,26 @@ const BunDebugHolder = struct { pub var lock: bun.Lock = undefined; }; -fn dumpSource(specifier: string, printer: anytype) !void { +/// Dumps the module source to a file in /tmp/bun-debug-src/{filepath} +/// +/// This can technically fail if concurrent access across processes happens, or permission issues. +/// Errors here should always be ignored. +fn dumpSource(specifier: string, printer: anytype) void { if (BunDebugHolder.dir == null) { - BunDebugHolder.dir = try std.fs.cwd().makeOpenPathIterable("/tmp/bun-debug-src/", .{}); + BunDebugHolder.dir = std.fs.cwd().makeOpenPathIterable("/tmp/bun-debug-src/", .{}) catch return; BunDebugHolder.lock = bun.Lock.init(); } BunDebugHolder.lock.lock(); defer BunDebugHolder.lock.unlock(); + const dir = BunDebugHolder.dir orelse return; if (std.fs.path.dirname(specifier)) |dir_path| { - var parent = try BunDebugHolder.dir.?.dir.makeOpenPathIterable(dir_path[1..], .{}); + var parent = dir.dir.makeOpenPathIterable(dir_path[1..], .{}) catch return; defer parent.close(); - try parent.dir.writeFile(std.fs.path.basename(specifier), printer.ctx.getWritten()); + parent.dir.writeFile(std.fs.path.basename(specifier), printer.ctx.getWritten()) catch return; } else { - try BunDebugHolder.dir.?.dir.writeFile(std.fs.path.basename(specifier), printer.ctx.getWritten()); + dir.dir.writeFile(std.fs.path.basename(specifier), printer.ctx.getWritten()) catch return; } } @@ -363,18 +368,15 @@ pub const RuntimeTranspilerStore = struct { var package_json: ?*PackageJSON = null; const hash = JSC.Watcher.getHash(path.text); - if (vm.bun_dev_watcher) |watcher| { - if (watcher.indexOf(hash)) |index| { - const _fd = watcher.watchlist.items(.fd)[index]; - fd = if (_fd > 0) _fd else null; - package_json = watcher.watchlist.items(.package_json)[index]; - } - } else if (vm.bun_watcher) |watcher| { - if (watcher.indexOf(hash)) |index| { - const _fd = watcher.watchlist.items(.fd)[index]; - fd = if (_fd > 0) _fd else null; - package_json = watcher.watchlist.items(.package_json)[index]; - } + switch (vm.bun_watcher) { + .hot, .watch => { + if (vm.bun_watcher.indexOf(hash)) |index| { + const _fd = vm.bun_watcher.watchlist().items(.fd)[index]; + fd = if (_fd > 0) _fd else null; + package_json = vm.bun_watcher.watchlist().items(.package_json)[index]; + } + }, + else => {}, } // this should be a cheap lookup because 24 bytes == 8 * 3 so it's read 3 machine words @@ -405,6 +407,7 @@ pub const RuntimeTranspilerStore = struct { .file_hash = hash, .macro_remappings = macro_remappings, .jsx = bundler.options.jsx, + .emit_decorator_metadata = bundler.options.emit_decorator_metadata, .virtual_source = null, .dont_bundle_twice = true, .allow_commonjs = true, @@ -438,9 +441,9 @@ pub const RuntimeTranspilerStore = struct { ) orelse { if (vm.isWatcherEnabled()) { if (input_file_fd != 0) { - if (vm.bun_watcher != null and !is_node_override and std.fs.path.isAbsolute(path.text) and !strings.contains(path.text, "node_modules")) { + if (!is_node_override and std.fs.path.isAbsolute(path.text) and !strings.contains(path.text, "node_modules")) { should_close_input_file_fd = false; - vm.bun_watcher.?.addFile( + vm.bun_watcher.addFile( input_file_fd, path.text, hash, @@ -459,11 +462,11 @@ pub const RuntimeTranspilerStore = struct { if (vm.isWatcherEnabled()) { if (input_file_fd != 0) { - if (vm.bun_watcher != null and !is_node_override and + if (!is_node_override and std.fs.path.isAbsolute(path.text) and !strings.contains(path.text, "node_modules")) { should_close_input_file_fd = false; - vm.bun_watcher.?.addFile( + vm.bun_watcher.addFile( input_file_fd, path.text, hash, @@ -547,7 +550,7 @@ pub const RuntimeTranspilerStore = struct { } if (comptime Environment.dump_source) { - dumpSource(specifier, &printer) catch {}; + dumpSource(specifier, &printer); } this.resolved_source = ResolvedSource{ @@ -689,6 +692,7 @@ pub const ModuleLoader = struct { .onPackageDownloadError = onPackageDownloadError, .progress_bar = true, }, + true, PackageManager.Options.LogLevel.default, ) catch unreachable; } else { @@ -701,6 +705,7 @@ pub const ModuleLoader = struct { .onPackageManifestError = onPackageManifestError, .onPackageDownloadError = onPackageDownloadError, }, + true, PackageManager.Options.LogLevel.default_no_progress, ) catch unreachable; } @@ -1232,7 +1237,7 @@ pub const ModuleLoader = struct { } if (comptime Environment.dump_source) { - try dumpSource(specifier, &printer); + dumpSource(specifier, &printer); } var commonjs_exports = try bun.default_allocator.alloc(ZigString, parse_result.ast.commonjs_export_names.len); @@ -1244,8 +1249,8 @@ pub const ModuleLoader = struct { var resolved_source = jsc_vm.refCountedResolvedSource(printer.ctx.written, bun.String.init(specifier), path.text, null, false); if (parse_result.input_fd) |fd_| { - if (jsc_vm.bun_watcher != null and std.fs.path.isAbsolute(path.text) and !strings.contains(path.text, "node_modules")) { - jsc_vm.bun_watcher.?.addFile( + if (std.fs.path.isAbsolute(path.text) and !strings.contains(path.text, "node_modules")) { + jsc_vm.bun_watcher.addFile( fd_, path.text, this.hash, @@ -1379,18 +1384,10 @@ pub const ModuleLoader = struct { var fd: ?StoredFileDescriptorType = null; var package_json: ?*PackageJSON = null; - if (jsc_vm.bun_dev_watcher) |watcher| { - if (watcher.indexOf(hash)) |index| { - const _fd = watcher.watchlist.items(.fd)[index]; - fd = if (_fd > 0) _fd else null; - package_json = watcher.watchlist.items(.package_json)[index]; - } - } else if (jsc_vm.bun_watcher) |watcher| { - if (watcher.indexOf(hash)) |index| { - const _fd = watcher.watchlist.items(.fd)[index]; - fd = if (_fd > 0) _fd else null; - package_json = watcher.watchlist.items(.package_json)[index]; - } + if (jsc_vm.bun_watcher.indexOf(hash)) |index| { + const _fd = jsc_vm.bun_watcher.watchlist().items(.fd)[index]; + fd = if (_fd > 0) _fd else null; + package_json = jsc_vm.bun_watcher.watchlist().items(.package_json)[index]; } var old = jsc_vm.bundler.log; @@ -1438,6 +1435,7 @@ pub const ModuleLoader = struct { .file_hash = hash, .macro_remappings = macro_remappings, .jsx = jsc_vm.bundler.options.jsx, + .emit_decorator_metadata = jsc_vm.bundler.options.emit_decorator_metadata, .virtual_source = virtual_source, .dont_bundle_twice = true, .allow_commonjs = true, @@ -1470,9 +1468,9 @@ pub const ModuleLoader = struct { if (comptime !disable_transpilying) { if (jsc_vm.isWatcherEnabled()) { if (input_file_fd != 0) { - if (jsc_vm.bun_watcher != null and !is_node_override and std.fs.path.isAbsolute(path.text) and !strings.contains(path.text, "node_modules")) { + if (!is_node_override and std.fs.path.isAbsolute(path.text) and !strings.contains(path.text, "node_modules")) { should_close_input_file_fd = false; - jsc_vm.bun_watcher.?.addFile( + jsc_vm.bun_watcher.addFile( input_file_fd, path.text, hash, @@ -1513,9 +1511,9 @@ pub const ModuleLoader = struct { if (comptime !disable_transpilying) { if (jsc_vm.isWatcherEnabled()) { if (input_file_fd != 0) { - if (jsc_vm.bun_watcher != null and !is_node_override and std.fs.path.isAbsolute(path.text) and !strings.contains(path.text, "node_modules")) { + if (!is_node_override and std.fs.path.isAbsolute(path.text) and !strings.contains(path.text, "node_modules")) { should_close_input_file_fd = false; - jsc_vm.bun_watcher.?.addFile( + jsc_vm.bun_watcher.addFile( input_file_fd, path.text, hash, @@ -1635,7 +1633,7 @@ pub const ModuleLoader = struct { }; if (comptime Environment.dump_source) { - try dumpSource(specifier, &printer); + dumpSource(specifier, &printer); } var commonjs_exports = try bun.default_allocator.alloc(ZigString, parse_result.ast.commonjs_export_names.len); @@ -1715,7 +1713,7 @@ pub const ModuleLoader = struct { // const hash = http.Watcher.getHash(path.text); // if (jsc_vm.watcher) |watcher| { // if (watcher.indexOf(hash)) |index| { - // const _fd = watcher.watchlist.items(.fd)[index]; + // const _fd = watcher.watchlist().items(.fd)[index]; // fd = if (_fd > 0) _fd else null; // } // } @@ -1946,14 +1944,21 @@ pub const ModuleLoader = struct { } } - const synchronous_loader = loader orelse - // Unknown extensions are to be treated as file loader - if (jsc_vm.has_loaded or jsc_vm.is_in_preload) - options.Loader.file - else - // Unless it's potentially the main module - // This is important so that "bun run ./foo-i-have-no-extension" works - options.Loader.js; + const synchronous_loader = loader orelse loader: { + if (jsc_vm.has_loaded or jsc_vm.is_in_preload) { + // Extensionless files in this context are treated as the JS loader + if (path.name.ext.len == 0) { + break :loader options.Loader.tsx; + } + + // Unknown extensions are to be treated as file loader + break :loader options.Loader.file; + } else { + // Unless it's potentially the main module + // This is important so that "bun run ./foo-i-have-no-extension" works + break :loader options.Loader.tsx; + } + }; var promise: ?*JSC.JSInternalPromise = null; ret.* = ErrorableResolvedSource.ok( @@ -1981,6 +1986,7 @@ pub const ModuleLoader = struct { if (err == error.PluginError) { return null; } + VirtualMachine.processFetchLog(globalObject, specifier_ptr.*, referrer.*, &log, ret, err); return null; }, @@ -2152,7 +2158,7 @@ pub const ModuleLoader = struct { const path = Fs.Path.init(specifier); const loader = if (loader_ != ._none) - options.Loader.fromString(@tagName(loader_)).? + options.Loader.fromAPI(loader_) else jsc_vm.bundler.options.loaders.get(path.name.ext) orelse brk: { if (strings.eqlLong(specifier, jsc_vm.main, true)) { @@ -2171,7 +2177,7 @@ pub const ModuleLoader = struct { referrer_slice.slice(), specifier_ptr.*, path, - options.Loader.fromString(@tagName(loader)).?, + loader, &log, &virtual_source, ret, |