Skip to content

fix(sdk): envvars.update() ReferenceError: name is not defined outside task context - #4431

Closed
karandhaodiyal28-hash wants to merge 1 commit into
triggerdotdev:mainfrom
karandhaodiyal28-hash:fix-envvars-update-name-4264
Closed

fix(sdk): envvars.update() ReferenceError: name is not defined outside task context#4431
karandhaodiyal28-hash wants to merge 1 commit into
triggerdotdev:mainfrom
karandhaodiyal28-hash:fix-envvars-update-name-4264

Conversation

@karandhaodiyal28-hash

Copy link
Copy Markdown

Closes#4264

Problem

Calling envvars.update(projectRef, slug, name, params) from outside a task run (a plain Node script / CI deploy step) always throws:

ReferenceError: name is not defined

create, del, retrieve, and list all work from the same context — only update is affected.

Root cause

update()'s implementation signature is:

exportfunctionupdate(projectRefOrName: string,slugOrParams: string|UpdateEnvironmentVariableParams,nameOrRequestOptions?: string|ApiRequestOptions,params?: UpdateEnvironmentVariableParams,requestOptions?: ApiRequestOptions)

There is no name parameter (the extra params argument shifts the name into nameOrRequestOptions). But the non-task-context branch did:

$name=name!;

retrieve/del legitimately have a name parameter, so that line works there — it was carried into update without renaming to nameOrRequestOptions. When taskContext.ctx is undefined (outside a task), this branch runs and dereferences the out-of-scope nameReferenceError.

Fix

Use nameOrRequestOptions, guarded like the sibling slug/projectRef/params checks in the same branch:

if(typeofnameOrRequestOptions!=="string"){thrownewError("name is required");}$projectRef=projectRefOrName;$slug=slugOrParams;$name=nameOrRequestOptions;$params=params;

This removes the ReferenceError, correctly routes the env var name, and gives a clear error if name is genuinely omitted (instead of a confusing crash).

Tests

Added packages/trigger-sdk/src/v3/envvars.test.ts (modeled on auth.test.ts — local HTTP server + apiClientManager.runWithConfig):

  • update() outside a task context no longer throws ReferenceError and dispatches to /projects/{ref}/envvars/{slug}/{name} with the name resolved.
  • omitting the name throws a clear "name is required".

Added a changeset (@trigger.dev/sdk patch).

Verification note

I verified the overload-resolution logic with a standalone Node reproduction (before → ReferenceError: name is not defined; after → name resolved correctly; missing name → "name is required"). The full monorepo test suite/build is heavy on my Windows machine, so I couldn't run vitest locally end-to-end — CI will exercise the added test.

The non-task-context branch of envvars.update() assigned \ = name!, but update()'s implementation signature has no name parameter (it uses nameOrRequestOptions). Calling envvars.update() outside a task run therefore threw ReferenceError: name is not defined. Use nameOrRequestOptions with a type guard, matching the other envvars functions.
Closestriggerdotdev#4264
@changeset-bot

Copy link
Copy Markdown

🦋 Changeset detected

Latest commit: 2c8db30

The changes in this PR will be included in the next version bump.

This PR includes changesets to release 27 packages
NameType
@trigger.dev/sdkPatch
@trigger.dev/pythonPatch
@internal/dashboard-agentPatch
@internal/sdk-compat-testsPatch
@trigger.dev/buildPatch
@trigger.dev/corePatch
@trigger.dev/react-hooksPatch
@trigger.dev/redis-workerPatch
@trigger.dev/rscPatch
@trigger.dev/schema-to-jsonPatch
@trigger.dev/databasePatch
@trigger.dev/otlp-importerPatch
@trigger.dev/rbacPatch
@trigger.dev/ssoPatch
trigger.devPatch
@internal/cachePatch
@internal/clickhousePatch
@internal/llm-model-catalogPatch
@internal/metrics-pipelinePatch
@internal/redisPatch
@internal/replicationPatch
@internal/run-enginePatch
@internal/run-storePatch
@internal/schedule-enginePatch
@internal/testcontainersPatch
@internal/tracingPatch
@internal/tsqlPatch

Not sure what this means? Click here to learn what changesets are.

Click here if you're a maintainer who wants to add another changeset to this PR

@github-actions

Copy link
Copy Markdown
Contributor

Hi @karandhaodiyal28-hash, thanks for your interest in contributing!

This project requires that pull request authors are vouched, and you are not in the list of vouched users.

This PR will be closed automatically. See https://github.com/triggerdotdev/trigger.dev/blob/main/CONTRIBUTING.md for more details.

@devin-ai-integrationdevin-ai-integrationBot left a comment

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.

Devin Review found 2 potential issues.

Open in Devin Review

Comment on lines +57 to +66
it("throws a clear error when the name is missing", async () => {
const key = "tr_prod_0123456789abcdefghijklmn";

await apiClientManager.runWithConfig({ baseURL: baseUrl, accessToken: key }, async () => {
await expect(
// @ts-expect-error deliberately omitting the name argument
envvars.update("proj_ref", "prod", { value: "abc" })
).rejects.toThrow("name is required");
});
});

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.

🔍 Second test will fail: update() throws synchronously and hits the params check first

envvars.update("proj_ref", "prod", { value: "abc" }) maps to projectRefOrName="proj_ref", slugOrParams="prod", nameOrRequestOptions={value:"abc"}, params=undefined. In packages/trigger-sdk/src/v3/envvars.ts:335-337 the if (!params) throw new Error("params is required") check runs before the newly added name check, so the message is "params is required", not "name is required". Moreover the throw is synchronous (the function never returns a promise in this path), so await expect(envvars.update(...)).rejects.toThrow(...) will not catch it — the argument expression throws while being evaluated and the test errors out. This test needs expect(() => envvars.update(...)).toThrow(...) and a case that actually reaches the name check (e.g. update("proj_ref", "prod", undefined as any, { value: "abc" })).

Open in Devin Review

Was this helpful? React with 👍 or 👎 to provide feedback.

Comment on lines +339 to 346
if (typeof nameOrRequestOptions !== "string") {
throw new Error("name is required");
}

$projectRef = projectRefOrName;
$slug = slugOrParams;
$name = name!;
$name = nameOrRequestOptions;
$params = params;

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.

🔍 Pre-existing mis-assignment in the in-task branch of update()

In the taskContext.ctx branch, $projectRef = slugOrParams and $slug = slugOrParams ?? ctx.environment.slug both take the same argument (packages/trigger-sdk/src/v3/envvars.ts:307-313), and $name falls back to ctx.environment.slug when no name is given. For the 4-arg overload called inside a task this sends the slug as the project ref, so envvars.update(projectRef, slug, name, params) inside a task targets the wrong project. This is outside the diff hunks but is the same argument-routing family of bugs the PR is fixing and would be worth correcting in the same pass (correct form mirrors create/upload: $projectRef = projectRefOrName, $slug = typeof slugOrParams === "string" ? slugOrParams : ctx.environment.slug).

Open in Devin Review

Was this helpful? React with 👍 or 👎 to provide feedback.

@coderabbitai

coderabbitaiBot commented Jul 31, 2026

Copy link
Copy Markdown
Contributor

Review Change Stack

Caution

Review failed

The pull request is closed.

ℹ️ Recent review info
⚙️ Run configuration

Configuration used: Repository UI

Review profile: CHILL

Plan: Pro Plus

Run ID: 93c45d1a-53b6-4526-a6b8-59f585012855

📥 Commits

Reviewing files that changed from the base of the PR and between debfa2b and 2c8db30.

📒 Files selected for processing (3)
  • .changeset/fix-envvars-update-name.md
  • packages/trigger-sdk/src/v3/envvars.test.ts
  • packages/trigger-sdk/src/v3/envvars.ts

Walkthrough

envvars.update() now validates its name argument before use. It throws "name is required" when the name is absent or invalid. Tests cover valid and missing names outside a task context by using a local HTTP server. A patch changeset documents the fix.

✨ Finishing Touches
🧪 Generate unit tests (beta)
  • Create PR with unit tests

Thanks for using CodeRabbit! It's free for OSS, and your support helps us grow. If you like it, consider giving us a shout-out.

❤️ Share

Comment @coderabbitai help to get the list of available commands.

Sign up for freeto join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

bug: envvars.update() throws "ReferenceError: name is not defined" outside a task context

1 participant

@karandhaodiyal28-hash