diff options
author | 2022-03-30 18:56:07 -0700 | |
---|---|---|
committer | 2022-03-30 18:56:07 -0700 | |
commit | 4dedb51a2380bf228d6c3d169fb551ffb5a88f2d (patch) | |
tree | d000b00d9c98cb8b2f3282fd2d08049c4675ce17 /src | |
parent | 3e8d669398b24ea5dd4140c9c8e751cf58b2807d (diff) | |
download | bun-4dedb51a2380bf228d6c3d169fb551ffb5a88f2d.tar.gz bun-4dedb51a2380bf228d6c3d169fb551ffb5a88f2d.tar.zst bun-4dedb51a2380bf228d6c3d169fb551ffb5a88f2d.zip |
[bun.js] Fix `import.meta.resolve` when the path points to a bundled file
Diffstat (limited to 'src')
-rw-r--r-- | src/javascript/jsc/api/bun.zig | 2 | ||||
-rw-r--r-- | src/javascript/jsc/javascript.zig | 78 |
2 files changed, 43 insertions, 37 deletions
diff --git a/src/javascript/jsc/api/bun.zig b/src/javascript/jsc/api/bun.zig index d5ee6a905..73861f5b1 100644 --- a/src/javascript/jsc/api/bun.zig +++ b/src/javascript/jsc/api/bun.zig @@ -788,7 +788,7 @@ fn doResolveWithArgs( var errorable: ErrorableZigString = undefined; if (comptime is_file_path) { - VirtualMachine.resolve( + VirtualMachine.resolveFilePathForAPI( &errorable, ctx.ptr(), specifier, diff --git a/src/javascript/jsc/javascript.zig b/src/javascript/jsc/javascript.zig index ca8c72f75..e17b6e720 100644 --- a/src/javascript/jsc/javascript.zig +++ b/src/javascript/jsc/javascript.zig @@ -1233,6 +1233,7 @@ pub const VirtualMachine = struct { specifier: string, source: string, comptime is_a_file_path: bool, + comptime realpath: bool, ) !void { std.debug.assert(VirtualMachine.vm_loaded); // macOS threadlocal vars are very slow @@ -1289,45 +1290,46 @@ pub const VirtualMachine = struct { ret.result = result; const result_path = result.pathConst() orelse return error.ModuleNotFound; jsc_vm.resolved_count += 1; - - if (jsc_vm.node_modules != null and !strings.eqlComptime(result_path.namespace, "node") and result.isLikelyNodeModule()) { - const node_modules_bundle = jsc_vm.node_modules.?; - - node_module_checker: { - const package_json = result.package_json orelse brk: { - if (jsc_vm.bundler.resolver.packageJSONForResolvedNodeModule(&result)) |pkg| { - break :brk pkg; - } else { - break :node_module_checker; - } - }; - - if (node_modules_bundle.getPackageIDByName(package_json.name)) |possible_pkg_ids| { - const pkg_id: u32 = brk: { - for (possible_pkg_ids) |pkg_id| { - const pkg = node_modules_bundle.bundle.packages[pkg_id]; - if (pkg.hash == package_json.hash) { - break :brk pkg_id; - } + if (comptime !realpath) { + if (jsc_vm.node_modules != null and !strings.eqlComptime(result_path.namespace, "node") and result.isLikelyNodeModule()) { + const node_modules_bundle = jsc_vm.node_modules.?; + + node_module_checker: { + const package_json = result.package_json orelse brk: { + if (jsc_vm.bundler.resolver.packageJSONForResolvedNodeModule(&result)) |pkg| { + break :brk pkg; + } else { + break :node_module_checker; } - break :node_module_checker; }; - const package = &node_modules_bundle.bundle.packages[pkg_id]; + if (node_modules_bundle.getPackageIDByName(package_json.name)) |possible_pkg_ids| { + const pkg_id: u32 = brk: { + for (possible_pkg_ids) |pkg_id| { + const pkg = node_modules_bundle.bundle.packages[pkg_id]; + if (pkg.hash == package_json.hash) { + break :brk pkg_id; + } + } + break :node_module_checker; + }; - if (Environment.isDebug) { - std.debug.assert(strings.eql(node_modules_bundle.str(package.name), package_json.name)); - } + const package = &node_modules_bundle.bundle.packages[pkg_id]; - const package_relative_path = jsc_vm.bundler.fs.relative( - package_json.source.path.name.dirWithTrailingSlash(), - result_path.text, - ); + if (Environment.isDebug) { + std.debug.assert(strings.eql(node_modules_bundle.str(package.name), package_json.name)); + } + + const package_relative_path = jsc_vm.bundler.fs.relative( + package_json.source.path.name.dirWithTrailingSlash(), + result_path.text, + ); - if (node_modules_bundle.findModuleIDInPackage(package, package_relative_path) == null) break :node_module_checker; + if (node_modules_bundle.findModuleIDInPackage(package, package_relative_path) == null) break :node_module_checker; - ret.path = bun_file_import_path; - return; + ret.path = bun_file_import_path; + return; + } } } } @@ -1344,17 +1346,21 @@ pub const VirtualMachine = struct { } pub fn resolveForAPI(res: *ErrorableZigString, global: *JSGlobalObject, specifier: ZigString, source: ZigString) void { - resolveMaybeNeedsTrailingSlash(res, global, specifier, source, false); + resolveMaybeNeedsTrailingSlash(res, global, specifier, source, false, true); + } + + pub fn resolveFilePathForAPI(res: *ErrorableZigString, global: *JSGlobalObject, specifier: ZigString, source: ZigString) void { + resolveMaybeNeedsTrailingSlash(res, global, specifier, source, true, true); } pub fn resolve(res: *ErrorableZigString, global: *JSGlobalObject, specifier: ZigString, source: ZigString) void { - resolveMaybeNeedsTrailingSlash(res, global, specifier, source, true); + resolveMaybeNeedsTrailingSlash(res, global, specifier, source, true, false); } - pub fn resolveMaybeNeedsTrailingSlash(res: *ErrorableZigString, global: *JSGlobalObject, specifier: ZigString, source: ZigString, comptime is_a_file_path: bool) void { + pub fn resolveMaybeNeedsTrailingSlash(res: *ErrorableZigString, global: *JSGlobalObject, specifier: ZigString, source: ZigString, comptime is_a_file_path: bool, comptime realpath: bool) void { var result = ResolveFunctionResult{ .path = "", .result = null }; - _resolve(&result, global, specifier.slice(), source.slice(), is_a_file_path) catch |err| { + _resolve(&result, global, specifier.slice(), source.slice(), is_a_file_path, realpath) catch |err| { // This should almost always just apply to dynamic imports const printed = ResolveError.fmt( |