Skip to content

Commit

Permalink
node:worker_threads: fix assertion when require is used with 'eval:tr…
Browse files Browse the repository at this point in the history
…ue' (#13108)
  • Loading branch information
nektro authored Aug 7, 2024
1 parent c63c55c commit ff0f9d5
Show file tree
Hide file tree
Showing 5 changed files with 57 additions and 7 deletions.
4 changes: 2 additions & 2 deletions src/bun.js/module_loader.zig
Original file line number Diff line number Diff line change
Expand Up @@ -1687,7 +1687,7 @@ pub const ModuleLoader = struct {
}
}

var parse_result = switch (disable_transpilying or
var parse_result: ParseResult = switch (disable_transpilying or
(loader == .json and !path.isJSONCFile())) {
inline else => |return_file_only| brk: {
break :brk jsc_vm.bundler.parseMaybeReturnFileOnly(
Expand Down Expand Up @@ -1939,7 +1939,7 @@ pub const ModuleLoader = struct {
if (parse_result.ast.exports_kind == .cjs and parse_result.source.path.isFile()) {
const actual_package_json: *PackageJSON = package_json orelse brk2: {
// this should already be cached virtually always so it's fine to do this
const dir_info = (jsc_vm.bundler.resolver.readDirInfo(parse_result.source.path.name.dir) catch null) orelse
const dir_info = (jsc_vm.bundler.resolver.readDirInfo(parse_result.source.path.name.dirOrDot()) catch null) orelse
break :brk .javascript;

break :brk2 dir_info.package_json orelse dir_info.enclosing_package_json;
Expand Down
6 changes: 2 additions & 4 deletions src/bun.js/web_worker.zig
Original file line number Diff line number Diff line change
Expand Up @@ -101,8 +101,6 @@ pub const WebWorker = struct {
defer parent.bundler.setLog(prev_log);
defer temp_log.deinit();

var resolved_entry_point: bun.resolver.Result = undefined;

const path = brk: {
const str = spec_slice.slice();
if (parent.standalone_module_graph) |graph| {
Expand All @@ -120,13 +118,13 @@ pub const WebWorker = struct {
}
}

resolved_entry_point = parent.bundler.resolveEntryPoint(str) catch {
var resolved_entry_point: bun.resolver.Result = parent.bundler.resolveEntryPoint(str) catch {
const out = temp_log.toJS(parent.global, bun.default_allocator, "Error resolving Worker entry point").toBunString(parent.global);
error_message.* = out;
return null;
};

const entry_path = resolved_entry_point.path() orelse {
const entry_path: *bun.fs.Path = resolved_entry_point.path() orelse {
error_message.* = bun.String.static("Worker entry point is missing");
return null;
};
Expand Down
2 changes: 1 addition & 1 deletion src/fs.zig
Original file line number Diff line number Diff line change
Expand Up @@ -1616,7 +1616,7 @@ pub const PathName = struct {
dir = _path[0 .. dir.len + 2];
}

return PathName{
return .{
.dir = dir,
.base = base,
.ext = ext,
Expand Down
3 changes: 3 additions & 0 deletions test/js/node/worker_threads/fixture-argv.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
module.exports = {
argv: Bun.argv,
};
49 changes: 49 additions & 0 deletions test/js/node/worker_threads/worker_threads.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,8 @@ import wt, {
MessagePort,
Worker,
} from "worker_threads";
import { resolve, relative, join } from "node:path";
import fs from "node:fs";

test("support eval in worker", async () => {
const worker = new Worker(`postMessage(1 + 1)`, {
Expand Down Expand Up @@ -197,3 +199,50 @@ test("you can override globalThis.postMessage", async () => {
expect(message).toBe("Hello from worker!");
await worker.terminate();
});

test("support require in eval", async () => {
const worker = new Worker(`postMessage(require('process').argv[0])`, { eval: true });
const result = await new Promise(resolve => {
worker.on("message", resolve);
worker.on("error", resolve);
});
expect(result).toBe(Bun.argv[0]);
await worker.terminate();
});

test("support require in eval for a file", async () => {
const cwd = process.cwd();
console.log("cwd", cwd);
const dir = import.meta.dir;
const testfile = resolve(dir, "fixture-argv.js");
const realpath = relative(cwd, testfile).replaceAll("\\", "/");
console.log("realpath", realpath);
expect(() => fs.accessSync(join(cwd, realpath))).not.toThrow();
const worker = new Worker(`postMessage(require('./${realpath}').argv[0])`, { eval: true });
const result = await new Promise(resolve => {
worker.on("message", resolve);
worker.on("error", resolve);
});
expect(result).toBe(Bun.argv[0]);
await worker.terminate();
});

test("support require in eval for a file that doesnt exist", async () => {
const worker = new Worker(`postMessage(require('./fixture-invalid.js').argv[0])`, { eval: true });
const result = await new Promise(resolve => {
worker.on("message", resolve);
worker.on("error", resolve);
});
expect(result.toString()).toInclude(`error: Cannot find module "./fixture-invalid.js" from "blob:`);
await worker.terminate();
});

test("support worker eval that throws", async () => {
const worker = new Worker(`postMessage(throw new Error("boom"))`, { eval: true });
const result = await new Promise(resolve => {
worker.on("message", resolve);
worker.on("error", resolve);
});
expect(result.toString()).toInclude(`error: Unexpected throw`);
await worker.terminate();
});

0 comments on commit ff0f9d5

Please sign in to comment.