Skip to content
Closed
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
5 changes: 5 additions & 0 deletions .changeset/fix-bun-worker-node-env.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
---
"trigger.dev": patch
---

Deployed Bun 1.4 tasks now see `NODE_ENV=production` when bytecode compilation is enabled.
7 changes: 5 additions & 2 deletions packages/cli-v3/src/build/bundle.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -148,10 +148,11 @@ function dockerAvailable() {
}

describe.skipIf(process.platform !== "linux" || !dockerAvailable())("compiled bun worker", () => {
// Catches a compiled task seeing NODE_ENV=development even when launched in production.
// Catches: a Bun upgrade that makes import.meta read-only or changes the embedded paths, so
// compiled tasks see file:///$bunfs/root/ URLs and can't read files next to them in /app, or
// fork() and spawn(process.execPath) start the run worker again instead of bun
it("points import.meta at the files under /app and relaunches run bun", async () => {
it("uses production mode, preserves task paths, and relaunches bun", async () => {
const dir = await mkdtemp(join(tmpdir(), "trigger-bun-compile-"));
const out = join(dir, "out");

Expand All @@ -173,6 +174,7 @@ export async function probe() {
child.stdout!.on("data", (chunk) => (forked += chunk));
await new Promise((resolve) => child.on("exit", resolve));
return {
nodeEnv: process.env.NODE_ENV,
url: import.meta.url,
dirname: import.meta.dirname,
data: readFileSync(new URL("./data.json", import.meta.url), "utf8"),
Expand Down Expand Up @@ -204,7 +206,7 @@ export async function probe() {
outfile,
entryPoints: ["/app/worker.mjs", "/app/src/trigger/task.mjs"],
});
const script = `${args.map((arg) => `'${arg}'`).join(" ")} >/dev/null && ${outfile}`;
const script = `unset NODE_ENV && ${args.map((arg) => `'${arg}'`).join(" ")} >/dev/null && NODE_ENV=production ${outfile}`;

const result = spawnSync(
"docker",
Expand All @@ -230,6 +232,7 @@ export async function probe() {

expect(result.status, result.stderr).toBe(0);
expect(JSON.parse(result.stdout)).toEqual({
nodeEnv: "production",
url: "file:///app/src/trigger/task.mjs",
dirname: "/app/src/trigger",
data: '"next to the task"',
Expand Down
3 changes: 3 additions & 0 deletions packages/cli-v3/src/deploy/buildImage.ts
Original file line number Diff line number Diff line change
Expand Up @@ -1040,6 +1040,9 @@ export function bunCompileArgs(
compiledRunWorker: NonNullable<GenerateContainerfileOptions["compiledRunWorker"]>
) {
return [
"env",

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

πŸ” New executable requirement for custom images

bunCompileArgs now requires env on PATH. The custom image requirements list BusyBox, which can provide the applet without exposing it as a standalone command.

Devin Review


Was this helpful? React with πŸ‘ or πŸ‘Ž to provide feedback.

// Bun inlines process.env.NODE_ENV when compiling the deployed worker.
"NODE_ENV=production",

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

🟑 Compiled tasks ignore configured environment mode

bunCompileArgs overrides an explicit build-time NODE_ENV, so Bun compiles direct reads to production. Those reads stay unchanged even when the run worker installs a different runtime value.

Learn more

A custom build layer can set environment variables for the image build, and parseGenerateOptions exposes those values to build commands. Before this change, Bun read that environment when compiling. The new command replaces NODE_ENV even when it is explicitly set. The run worker still applies the project’s runtime variables, but they cannot change a string already compiled into the task.

Example: A staging deployment sets NODE_ENV=development in both its build layer and dashboard environment. A task that checks process.env.NODE_ENV === "development" no longer takes its staging branch after redeploying with bytecode enabled.

Recommended fix: Use production as the compiler default rather than replacing an explicit build value. Add a compiled-worker case with an explicit non-production value alongside the default-production case. If dashboard changes must take effect without a rebuild, verify a Bun setting or bundle transform that preserves runtime reads instead of folding them.

Devin Review


Was this helpful? React with πŸ‘ or πŸ‘Ž to provide feedback.

"bun",
"build",
"--compile",
Expand Down