From 08ff944d6aae24d55db10e9921f7be1d96cf0964 Mon Sep 17 00:00:00 2001 From: Jarred Sumner <709451+Jarred-Sumner@users.noreply.github.com> Date: Sat, 6 Jan 2024 18:02:30 -0800 Subject: [PATCH] Fixes #5178 --- src/bun.js/javascript.zig | 4 +-- src/cli/build_command.zig | 4 +-- src/resolver/resolver.zig | 40 ++++++++++++++++++---------- src/standalone_bun.zig | 2 +- src/string_immutable.zig | 4 +++ test/bundler/bundler_compile.test.ts | 28 ++++++++++++++++++- 6 files changed, 62 insertions(+), 20 deletions(-) diff --git a/src/bun.js/javascript.zig b/src/bun.js/javascript.zig index f2d4d7aeb0a69e..a96a3dd4c22641 100644 --- a/src/bun.js/javascript.zig +++ b/src/bun.js/javascript.zig @@ -1713,7 +1713,7 @@ pub const VirtualMachine = struct { source_to_use, normalized_specifier, if (is_esm) .stmt else .require, - .read_only, + if (jsc_vm.standalone_module_graph == null) .read_only else .disable, )) { .success => |r| r, .failure => |e| e, @@ -2091,7 +2091,7 @@ pub const VirtualMachine = struct { this.bundler.fs.top_level_dir, normalizeSource(preload), .stmt, - .read_only, + if (this.standalone_module_graph == null) .read_only else .disable, )) { .success => |r| r, .failure => |e| { diff --git a/src/cli/build_command.zig b/src/cli/build_command.zig index 4870edaf8e3381..87e1df32369fb4 100644 --- a/src/cli/build_command.zig +++ b/src/cli/build_command.zig @@ -109,8 +109,8 @@ pub const BuildCommand = struct { // We never want to hit the filesystem for these files // This "compiled" protocol is specially handled by the module resolver. - this_bundler.options.public_path = "compiled://root/"; - this_bundler.resolver.opts.public_path = "compiled://root/"; + this_bundler.options.public_path = "/$bunfs/root/"; + this_bundler.resolver.opts.public_path = "/$bunfs/root/"; if (outfile.len == 0) { outfile = std.fs.path.basename(this_bundler.options.entry_points[0]); diff --git a/src/resolver/resolver.zig b/src/resolver/resolver.zig index 4bdf74c9c75351..18bdc62c830bb3 100644 --- a/src/resolver/resolver.zig +++ b/src/resolver/resolver.zig @@ -798,7 +798,7 @@ pub const Resolver = struct { pub fn resolveAndAutoInstall( r: *ThisResolver, - source_dir: string, + source_dir_: string, import_path: string, kind: ast.ImportKind, global_cache: GlobalCache, @@ -864,22 +864,34 @@ pub const Resolver = struct { }; } - if (r.standalone_module_graph) |graph| { - if (strings.hasPrefixComptime(import_path, "compiled://")) { - if (graph.files.contains(import_path)) { - return .{ - .success = Result{ - .import_kind = kind, - .path_pair = PathPair{ - .primary = Path.init(import_path), + // 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. + const source_dir = brk: { + if (r.standalone_module_graph) |graph| { + if (strings.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, }, - .is_standalone_module = true, - .module_type = .esm, - }, - }; + }; + } + + return .{ .not_found = {} }; + } else if (strings.isBunStandaloneFilePath(source_dir_)) { + break :brk Fs.FileSystem.instance.top_level_dir; } } - } + break :brk source_dir_; + }; if (DataURL.parse(import_path) catch { return .{ .failure = error.InvalidDataURL }; diff --git a/src/standalone_bun.zig b/src/standalone_bun.zig index 24cee2a834fe7a..7c01a5910d2722 100644 --- a/src/standalone_bun.zig +++ b/src/standalone_bun.zig @@ -19,7 +19,7 @@ pub const StandaloneModuleGraph = struct { } pub fn find(this: *const StandaloneModuleGraph, name: []const u8) ?*File { - if (!bun.strings.hasPrefixComptime(name, "compiled://root/")) { + if (!bun.strings.isBunStandaloneFilePath(name)) { return null; } diff --git a/src/string_immutable.zig b/src/string_immutable.zig index 024b0bee03adc9..c3758b3400128d 100644 --- a/src/string_immutable.zig +++ b/src/string_immutable.zig @@ -955,6 +955,10 @@ pub fn hasPrefixComptime(self: string, comptime alt: anytype) bool { return self.len >= alt.len and eqlComptimeCheckLenWithType(u8, self[0..alt.len], alt, false); } +pub fn isBunStandaloneFilePath(self: string) bool { + return hasPrefixComptime(self, "/$bunfs/"); +} + pub fn hasPrefixComptimeUTF16(self: []const u16, comptime alt: []const u8) bool { return self.len >= alt.len and eqlComptimeCheckLenWithType(u16, self[0..alt.len], comptime toUTF16Literal(alt), false); } diff --git a/test/bundler/bundler_compile.test.ts b/test/bundler/bundler_compile.test.ts index d52cc84a633c03..13198747f0cf37 100644 --- a/test/bundler/bundler_compile.test.ts +++ b/test/bundler/bundler_compile.test.ts @@ -13,6 +13,18 @@ describe("bundler", () => { }, run: { stdout: "Hello, world!" }, }); + itBundled("compile/pathToFileURLWorks", { + compile: true, + files: { + "/entry.ts": /* js */ ` + import {pathToFileURL, fileURLToPath} from 'bun'; + console.log(pathToFileURL(import.meta.path).href + " " + fileURLToPath(import.meta.url)); + if (fileURLToPath(import.meta.url) !== import.meta.path) throw "fail"; + if (pathToFileURL(import.meta.path).href !== import.meta.url) throw "fail"; + `, + }, + run: { stdout: `file:///$bunfs/root/out /$bunfs/root/out`, setCwd: true }, + }); itBundled("compile/VariousBunAPIs", { compile: true, files: { @@ -144,11 +156,25 @@ describe("bundler", () => { files: { "/entry.tsx": /* tsx */ ` const req = (x) => require(x); - req('express'); + console.log(req('express')); `, }, run: { error: 'Cannot find package "express"', + setCwd: true, + }, + compile: true, + }); + itBundled("compile/CanRequireLocalPackages", { + files: { + "/entry.tsx": /* tsx */ ` + const req = (x) => require(x); + console.log(req('react/package.json').version); + `, + }, + run: { + stdout: require("react/package.json").version, + setCwd: false, }, compile: true, });