From cfef3bc78953841d5e0057dc4bdc41f343a01464 Mon Sep 17 00:00:00 2001 From: Mina Sayed Date: Sun, 29 Nov 2026 02:18:11 +0200 Subject: [PATCH] fix(cli): restrict config stripping to resolved config path --- .changeset/fix-cli-config-strip-path.md | 5 +++ packages/cli-v3/src/config.test.ts | 54 ++++++++++++++++++++++++- packages/cli-v3/src/config.ts | 21 ++++++---- 3 files changed, 72 insertions(+), 8 deletions(-) create mode 100644 .changeset/fix-cli-config-strip-path.md diff --git a/.changeset/fix-cli-config-strip-path.md b/.changeset/fix-cli-config-strip-path.md new file mode 100644 index 00000000000..a6ec744d32f --- /dev/null +++ b/.changeset/fix-cli-config-strip-path.md @@ -0,0 +1,5 @@ +--- +"trigger.dev": patch +--- + +Prevent unrelated files with names matching the Trigger configuration file from being transformed during bundling. diff --git a/packages/cli-v3/src/config.test.ts b/packages/cli-v3/src/config.test.ts index 12e5451bc74..54e45e6cfcc 100644 --- a/packages/cli-v3/src/config.test.ts +++ b/packages/cli-v3/src/config.test.ts @@ -1,8 +1,10 @@ +import { build } from "esbuild"; import { mkdtemp, mkdir, rm, writeFile } from "node:fs/promises"; import { tmpdir } from "node:os"; import { join } from "node:path"; +import { runInNewContext } from "node:vm"; import { afterEach, describe, expect, it, vi } from "vitest"; -import { loadConfig } from "./config.js"; +import { configPlugin, loadConfig } from "./config.js"; const projectDirs: string[] = []; @@ -89,3 +91,53 @@ describe("loadConfig bun runtime warnings", () => { expect(await outputWhileLoading("bun-1.4")).not.toMatch(/deprecated/i); }); }); + +describe("configPlugin", () => { + it("strips build options only from the resolved config file", async () => { + const cwd = await createProject(); + await writeFile( + join(cwd, "trigger.config.ts"), + `export default { + project: "proj_runtime_config_test", + maxDuration: 60, + dirs: ["./trigger"], + build: { external: ["build-only-marker"] }, +}; +` + ); + await mkdir(join(cwd, "nested")); + await writeFile(join(cwd, "nested", "trigger.config.ts"), 'export const value = "nested";'); + await writeFile(join(cwd, "other-trigger.config.ts"), 'export const value = "suffix";'); + await writeFile( + join(cwd, "entry.ts"), + `export { default as config } from "./trigger.config"; +export { value as nested } from "./nested/trigger.config"; +export { value as suffix } from "./other-trigger.config"; +` + ); + + const resolvedConfig = await loadConfig({ cwd, warn: false }); + const plugin = configPlugin(resolvedConfig); + expect(plugin).toBeDefined(); + + const result = await build({ + absWorkingDir: cwd, + entryPoints: ["entry.ts"], + bundle: true, + write: false, + platform: "node", + format: "cjs", + plugins: [plugin!], + }); + + const module = { exports: {} }; + runInNewContext(result.outputFiles[0].text, { module, exports: module.exports }); + + expect(module.exports).toMatchObject({ + config: { project: "proj_runtime_config_test", build: {} }, + nested: "nested", + suffix: "suffix", + }); + expect(result.outputFiles[0].text).not.toContain("build-only-marker"); + }); +}); diff --git a/packages/cli-v3/src/config.ts b/packages/cli-v3/src/config.ts index d65c3c8e7a3..4e90647e407 100644 --- a/packages/cli-v3/src/config.ts +++ b/packages/cli-v3/src/config.ts @@ -18,8 +18,8 @@ import { import * as c12 from "c12"; import { defu } from "defu"; import type * as esbuild from "esbuild"; -import { readdir } from "node:fs/promises"; -import { basename, dirname, isAbsolute, join, relative } from "node:path"; +import { readdir, realpath } from "node:fs/promises"; +import { basename, dirname, isAbsolute, join, relative, resolve } from "node:path"; import { findWorkspaceDir, resolveLockfile, resolvePackageJSON, resolveTSConfig } from "pkg-types"; import { generateCode, loadFile } from "./imports/magicast.js"; import { logger } from "./utilities/logger.js"; @@ -111,14 +111,21 @@ export function configPlugin(resolvedConfig: ResolvedConfig): esbuild.Plugin | u // We need to strip the "build" key from the config file, so build dependencies don't make it into the final bundle return { name: "trigger-config-strip", - setup(build) { - const filename = basename(configFile); - // Convert the filename to a regex to filter against - const filter = new RegExp(`${filename.replace(/\./g, "\\.")}$`); + async setup(build) { + const configPath = build.initialOptions.preserveSymlinks + ? resolve(configFile) + : await realpath(configFile); + const filename = basename(configPath); + // Narrow the callback to likely matches, then verify the full path. + const filter = new RegExp(`${filename.replace(/[.*+?^${}()|[\]\\]/g, "\\$&")}$`); logger.debug("trigger-config-strip.filter", filter); - build.onLoad({ filter }, async (args) => { + build.onLoad({ filter, namespace: "file" }, async (args) => { + if (resolve(args.path) !== configPath) { + return; + } + logger.debug("trigger-config-strip.onLoad", args); const $mod = await loadFile(args.path);