Skip to content

Commit 2bf9ae4

Browse files
committed
fix(core): preserve storage-node side effect + env fallback for inheritContext scopes
Two follow-ups from review of the TriggerClient PR: 1. The bare side-effect import `import "@trigger.dev/core/v3/sdk-scope-storage"` from SDK code (triggerClient.ts, auth.ts) was at risk of being tree-shaken away by bundlers that respect `"sideEffects": false` on `@trigger.dev/core`. Whitelist the storage-node module in core's `sideEffects` array so bundlers keep the install side effect. Without this, the scope silently degrades to no-op in production bundles even though Node-runtime tests pass. 2. `auth.withAuth({ baseURL: "..." }, fn)` regressed for callers relying on `TRIGGER_SECRET_KEY` from the env: the scoped accessToken getter returned undefined instead of falling back to the env var, so a partial override (just baseURL) broke auth. Restore env fallback inside the scope, but gate it on `inheritContext: true` so it only applies to withAuth-style scopes, not to TriggerClient instances (whose isolation guarantee requires identity fields to come only from the constructor config). Adds an `auth.withAuth` test that covers the partial-override-with-env case so the regression can't return.
1 parent 878b5a4 commit 2bf9ae4

3 files changed

Lines changed: 48 additions & 4 deletions

File tree

packages/core/package.json

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -171,7 +171,11 @@
171171
]
172172
}
173173
},
174-
"sideEffects": false,
174+
"sideEffects": [
175+
"./dist/esm/v3/sdkScope/storage-node.js",
176+
"./dist/commonjs/v3/sdkScope/storage-node.js",
177+
"./src/v3/sdkScope/storage-node.ts"
178+
],
175179
"scripts": {
176180
"clean": "rimraf dist .tshy .tshy-build .turbo src/v3/vendor",
177181
"update-version": "tsx ../../scripts/updateVersion.ts",

packages/core/src/v3/apiClientManager/index.ts

Lines changed: 23 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -50,7 +50,18 @@ export class APIClientManagerAPI {
5050
get accessToken(): string | undefined {
5151
const scoped = sdkScope.getStore();
5252
if (scoped) {
53-
return scoped.apiClientConfig.accessToken ?? scoped.apiClientConfig.secretKey;
53+
const value = scoped.apiClientConfig.accessToken ?? scoped.apiClientConfig.secretKey;
54+
if (value !== undefined) return value;
55+
// `inheritContext: true` scopes (e.g. `auth.withAuth` partial
56+
// overrides) still fall back to the process env so callers who
57+
// rely on TRIGGER_SECRET_KEY don't lose auth when they only
58+
// wanted to override baseURL. Isolated scopes (TriggerClient)
59+
// intentionally do not fall back — the constructor enforces
60+
// accessToken is provided.
61+
if (scoped.inheritContext) {
62+
return getEnvVar("TRIGGER_SECRET_KEY") ?? getEnvVar("TRIGGER_ACCESS_TOKEN");
63+
}
64+
return undefined;
5465
}
5566
const config = this.#getConfig();
5667
return (
@@ -64,8 +75,17 @@ export class APIClientManagerAPI {
6475
get branchName(): string | undefined {
6576
const scoped = sdkScope.getStore();
6677
if (scoped) {
67-
const value = scoped.apiClientConfig.previewBranch ?? undefined;
68-
return value ? value : undefined;
78+
const value = scoped.apiClientConfig.previewBranch;
79+
if (value) return value;
80+
// Same inheritContext gating as accessToken: withAuth-style
81+
// scopes inherit env-derived branch; TriggerClient instances
82+
// stay isolated from process env for identity.
83+
if (scoped.inheritContext) {
84+
const envValue =
85+
getEnvVar("TRIGGER_PREVIEW_BRANCH") ?? getEnvVar("VERCEL_GIT_COMMIT_REF");
86+
return envValue ? envValue : undefined;
87+
}
88+
return undefined;
6989
}
7090
const config = this.#getConfig();
7191
const value =

packages/trigger-sdk/src/v3/triggerClient.test.ts

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -201,6 +201,26 @@ describe("auth.withAuth", () => {
201201
apiClientManager.disable();
202202
});
203203

204+
it("inherits TRIGGER_SECRET_KEY from env when called with a partial config", async () => {
205+
vi.stubEnv("TRIGGER_SECRET_KEY", "tr_dev_env_token");
206+
207+
let observed: string | undefined;
208+
await auth.withAuth({ baseURL: "https://override.example.com" }, async () => {
209+
observed = apiClientManager.accessToken;
210+
});
211+
212+
// The scoped `inheritContext: true` path falls back to TRIGGER_SECRET_KEY
213+
// so callers can override only baseURL without re-passing the token.
214+
expect(observed).toBe("tr_dev_env_token");
215+
// baseURL override still applies.
216+
expect(
217+
await auth.withAuth(
218+
{ baseURL: "https://override.example.com" },
219+
async () => apiClientManager.baseURL
220+
)
221+
).toBe("https://override.example.com");
222+
});
223+
204224
it("does not stomp on a parallel withAuth call with a different config", async () => {
205225
configure({ accessToken: "tr_global" });
206226

0 commit comments

Comments
 (0)