Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fix bug with // @bun annotation in main thread #3855

Merged
merged 2 commits into from
Jul 28, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
18 changes: 16 additions & 2 deletions src/bun.js/module_loader.zig
Original file line number Diff line number Diff line change
Expand Up @@ -194,8 +194,7 @@ pub const RuntimeTranspilerStore = struct {
referrer: []const u8,
) *anyopaque {
debug("transpile({s})", .{path.text});
var was_new = true;
var job: *TranspilerJob = this.store.getAndSeeIfNew(&was_new);
var job: *TranspilerJob = this.store.get();
var owned_path = Fs.Path.init(bun.default_allocator.dupe(u8, path.text) catch unreachable);
var promise = JSC.JSInternalPromise.create(globalObject);
job.* = TranspilerJob{
Expand Down Expand Up @@ -469,6 +468,21 @@ pub const RuntimeTranspilerStore = struct {
}
}

if (parse_result.already_bundled) {
this.resolved_source = ResolvedSource{
.allocator = null,
.source_code = bun.String.createLatin1(parse_result.source.contents),
.specifier = String.create(specifier),
.source_url = ZigString.init(path.text),
// // TODO: change hash to a bitfield
// .hash = 1,

// having JSC own the memory causes crashes
.hash = 0,
};
return;
}

for (parse_result.ast.import_records.slice()) |*import_record_| {
var import_record: *bun.ImportRecord = import_record_;

Expand Down
2 changes: 1 addition & 1 deletion test/js/node/child_process/child_process-node.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -377,7 +377,7 @@ describe("child_process default options", () => {
});

describe("child_process double pipe", () => {
it.todo("should allow two pipes to be used at once", done => {
it("should allow two pipes to be used at once", done => {
// const { mustCallAtLeast, mustCall } = createCallCheckCtx(done);
const mustCallAtLeast = fn => fn;
const mustCall = fn => fn;
Expand Down
1 change: 1 addition & 0 deletions test/transpiler/async-transpiler-entry.js
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
export { default } from "./async-transpiler-imported.js";
6 changes: 6 additions & 0 deletions test/transpiler/async-transpiler-imported.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
// @bun
export default 42;

if (import.meta.main) {
console.log("Hello world!");
}
30 changes: 30 additions & 0 deletions test/transpiler/runtime-transpiler.test.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,30 @@
import { beforeEach, describe, expect, test } from "bun:test";
import { bunEnv, bunExe } from "harness";

describe("// @bun", () => {
beforeEach(() => {
delete require.cache[require.resolve("./async-transpiler-entry")];
delete require.cache[require.resolve("./async-transpiler-imported")];
});

test("async transpiler", async () => {
const { default: value } = await import("./async-transpiler-entry");
expect(value).toBe(42);
});

test("require()", async () => {
const { default: value } = require("./async-transpiler-entry");
expect(value).toBe(42);
});

test("synchronous", async () => {
const { stdout, exitCode } = Bun.spawnSync({
cmd: [bunExe(), require.resolve("./async-transpiler-imported")],
cwd: import.meta.dir,
env: bunEnv,
stderr: "inherit",
});
expect(stdout.toString()).toBe("Hello world!\n");
expect(exitCode).toBe(0);
});
});