diff options
author | 2021-10-06 19:26:28 -0700 | |
---|---|---|
committer | 2021-10-06 19:26:28 -0700 | |
commit | 1e8babe1852361a2ace6c01dc699c08115462eba (patch) | |
tree | 5cc8768401b38ead3b58a72b38e7dad20037f936 | |
parent | 69d3b5ca605f3ce66b4e7441f0528618f81d7505 (diff) | |
download | bun-1e8babe1852361a2ace6c01dc699c08115462eba.tar.gz bun-1e8babe1852361a2ace6c01dc699c08115462eba.tar.zst bun-1e8babe1852361a2ace6c01dc699c08115462eba.zip |
HTTP paths must resolve from ./$PROJECT_ROOT unless explicitly marked absolute.
This fixes the issue with deeply nested PNPM workspace packages. Happened when using @emotion/react inside a PNPM workspace when @emotion/react was hoisted to the workspace root but react was in a child node_modules folder. It would fail to resolve `"react/jsx-dev-runtime"` because it didn't exist in any node_modules folder relative to `@emotion/react`'s real path.
-rw-r--r-- | src/bundler.zig | 30 |
1 files changed, 15 insertions, 15 deletions
diff --git a/src/bundler.zig b/src/bundler.zig index 93c920a7d..87f37d220 100644 --- a/src/bundler.zig +++ b/src/bundler.zig @@ -2551,6 +2551,7 @@ pub const Bundler = struct { // This is public so it can be used by the HTTP handler when matching against public dir. pub threadlocal var tmp_buildfile_buf: [std.fs.MAX_PATH_BYTES]u8 = undefined; threadlocal var tmp_buildfile_buf2: [std.fs.MAX_PATH_BYTES]u8 = undefined; + threadlocal var tmp_buildfile_buf3: [std.fs.MAX_PATH_BYTES]u8 = undefined; // We try to be mostly stateless when serving // This means we need a slightly different resolver setup @@ -2558,7 +2559,7 @@ pub const Bundler = struct { bundler: *ThisBundler, log: *logger.Log, allocator: *std.mem.Allocator, - relative_path: string, + path_to_use_: string, _extension: string, comptime client_entry_point_enabled: bool, comptime serve_as_package_path: bool, @@ -2570,23 +2571,15 @@ pub const Bundler = struct { bundler.setLog(log); defer bundler.setLog(old_log); - if (strings.eqlComptime(relative_path, "__runtime.js")) { + var path_to_use = path_to_use_; + + if (strings.eqlComptime(path_to_use, "__runtime.js")) { return ServeResult{ .file = options.OutputFile.initBuf(runtime.Runtime.sourceContent(), "__runtime.js", .js), .mime_type = MimeType.javascript, }; } - var absolute_path = if (comptime serve_as_package_path) - relative_path - else - resolve_path.joinAbsStringBuf( - bundler.fs.top_level_dir, - &tmp_buildfile_buf, - &([_][]const u8{relative_path}), - .auto, - ); - defer { js_ast.Expr.Data.Store.reset(); js_ast.Stmt.Data.Store.reset(); @@ -2597,8 +2590,15 @@ pub const Bundler = struct { // absolute_path = absolute_path[0 .. absolute_path.len - ".js".len]; // } - const resolved = if (comptime !client_entry_point_enabled) (try bundler.resolver.resolve(bundler.fs.top_level_dir, absolute_path, .stmt)) else brk: { - const absolute_pathname = Fs.PathName.init(absolute_path); + // All non-absolute paths are ./paths + if (path_to_use[0] != '/' and path_to_use[0] != '.') { + tmp_buildfile_buf3[0..2].* = "./".*; + @memcpy(tmp_buildfile_buf3[2..], path_to_use.ptr, path_to_use.len); + path_to_use = tmp_buildfile_buf3[0 .. 2 + path_to_use.len]; + } + + const resolved = if (comptime !client_entry_point_enabled) (try bundler.resolver.resolve(bundler.fs.top_level_dir, path_to_use, .stmt)) else brk: { + const absolute_pathname = Fs.PathName.init(path_to_use); const loader_for_ext = bundler.options.loader(absolute_pathname.ext); @@ -2625,7 +2625,7 @@ pub const Bundler = struct { } } - break :brk (try bundler.resolver.resolve(bundler.fs.top_level_dir, absolute_path, .stmt)); + break :brk (try bundler.resolver.resolve(bundler.fs.top_level_dir, path_to_use, .stmt)); }; const path = (resolved.pathConst() orelse return error.ModuleNotFound); |