A function that exists. A deployment that exists. A command that still says the function does not exist.

That was issue #1169 in google/clasp, and it turned out to be a very small bug with a very misleading error message. The fix landed as PR #1184, was merged on December 3, 2026, and shipped in v3.5.0.

This is the short version of what was wrong, how the deployment selection works now, and what I took away from my first PR to clasp.

TL;DR

  • clasp run-function --nondev sent the project's script ID in the scripts.run path field.
  • The Apps Script API expects an API-executable deployment ID there, so the API returned 404 with a message that blamed a missing function.
  • The fix adds --deployment-id, auto-selects the only API-executable deployment when there is exactly one, and fails early with an actionable message when there are zero or several.
  • Six new tests cover selection, explicit IDs, both error paths, and the CLI wiring. npm test, npm run check, and npm run compile passed, and CI reported 8 checks passed.

The problem: a 404 that points at the wrong thing

The failing command looked like this:

clasp run-function claspPing --nondev 

And the output was:

Script function not found. Please make sure script is deployed as API executable. 

The function was there. The script was deployed. The caller owned the script and the deployment. So the error was technically accurate about the symptom and completely wrong about the cause: the API had not rejected the function, it had rejected the resource identifier in the URL.

The issue author did the most useful thing anyone can do in a bug report: they ran a control experiment with the same OAuth token and the same request body, changing only the path ID and devMode.

Path ID devMode Result
Script ID true 404
Script ID false 404
API-executable deployment ID false 200 — "pong"
API-executable deployment ID true 404

That table is the whole bug in four lines. scripts.run wanted the deployment ID. clasp kept giving it the script ID.

The non-dev path was the clearest case to fix: --nondev is supposed to execute a deployed, immutable version, and an API-executable deployment is exactly that resource.

Root cause: two identifiers that look interchangeable

A clasp project has a script ID in .clasp.json:

{ "scriptId": "1AbC...", "rootDir": "src" } 

That ID identifies the Apps Script project. It is what you use for clasp push, clasp pull, and most project-level API calls.

A deployment ID identifies a specific deployment of that project:

AKfycby... 

Most deployments are versioned snapshots. They also have entry points. An API executable deployment has an entry point of type EXECUTION_API, which is what makes it callable through scripts.run.

The trap is the API field name. In the Apps Script REST API, the path parameter for scripts.run is literally called scriptId, but for an API execution it must contain the deployment ID of an API-executable deployment. The name says one thing; the semantics say another. clasp took the field name at face value and passed the project script ID.

There is a second subtlety: a project can have several deployments. The @HEAD deployment tracks the latest saved code, while versioned deployments are fixed snapshots. If you have more than one API-executable deployment, there is no single correct answer for which one --nondev should run. Picking one silently would be worse than failing, because the command would execute an unexpected version of the code.

The solution: explicit first, unique second, ambiguous never

The fix touches the command, the core function, and the wiring between them.

First, run-function gets an explicit option:

clasp run-function sendEmail --nondev --deployment-id <deploymentId> 

Then Functions.runFunction decides which identifier to put in the request path:

const executionId = deploymentId ?? (devMode ? scriptId : await this.findExecutionApiDeploymentId()); try { const request = { scriptId: executionId, requestBody: { function: functionName, parameters: parameters ?? [], devMode, }, }; // ... } 

The selection logic is deliberately boring:

private async findExecutionApiDeploymentId(): Promise<string> { const deployments = await this.project.listDeployments(); const executionApiDeployments = deployments.results.filter( deployment => deployment.deploymentId && deployment.entryPoints?.some( entryPoint => entryPoint.entryPointType === 'EXECUTION_API', ), ); if (executionApiDeployments.length === 1) { return executionApiDeployments[0].deploymentId!; } if (executionApiDeployments.length === 0) { throw new Error( 'No API-executable deployment found. Create one with "clasp create-deployment" or pass --deployment-id.', ); } throw new Error( 'Multiple API-executable deployments found. Pass --deployment-id to choose which deployment to run.', ); } 

The rules are:

  1. Explicit wins. If --deployment-id is passed, use it. No lookup, no guessing.
  2. Exactly one API-executable deployment? Use it automatically. This keeps the common case short.
  3. Zero? Fail before calling scripts.run and tell the user how to create one.
  4. More than one? Fail and ask for --deployment-id. Running the wrong version silently is not a helpful default.

Two implementation details made this fit into the existing code:

  • Functions now receives the Project instance, so it can call listDeployments() without duplicating API plumbing.
  • Dev mode keeps the old script-ID behavior unless --deployment-id is passed. I scoped the change to the documented non-dev path for backwards compatibility; changing both modes at once would have made the PR much harder to reason about.

The docs changed too: the run-function section in README.md and docs/run.md now mention --deployment-id and the auto-selection behavior. A fix that only exists in code is half a fix.

Testing: the selection logic is the feature

The tests mock the Apps Script API at the HTTP boundary with nock, so they verify the actual URL and request body rather than an internal helper.

The core suite covers four behaviors, and the command suite covers the two CLI paths.

  1. Single API-executable deployment — the deployments list includes a head deployment and one EXECUTION_API deployment. The test asserts the request goes to /v1/scripts/api-deployment-id:run with devMode: false.
  2. Explicit deployment ID — the lookup endpoint is not called at all; the request uses the ID from the flag.
  3. No API-executable deployment — the call rejects with No API-executable deployment found instead of making a doomed scripts.run request.
  4. Multiple API-executable deployments — the call rejects with Multiple API-executable deployments found and asks for --deployment-id.
  5. Command-level resolution — run-function --nondev --json resolves the deployment and returns the mocked result.
  6. Command-level explicit ID — --deployment-id is passed through from the CLI to the core function.

The "multiple" test matters as much as the happy path. It is the case where a well-meaning auto-pick would create a subtle production bug: the command would work, exit zero, and run the wrong deployment.

Locally, npm test, npm run check, and npm run compile passed. On the PR, CI reported 8 checks passed. One caveat: these are unit tests against a mocked HTTP layer. They lock in the request shape and the selection rules, not Google's live behavior; the live evidence is the control experiment in the issue.

The change is 7 files, +186/−6, one commit — small enough to review in one sitting, which is exactly what you want for a fix in a tool people depend on.

What I learned

API field names are not documentation. scriptId in the scripts.run path does not mean "the project script ID" in every context. The discovery document and the execution API docs are the source of truth, and they describe a deployment ID. Reading the existing clasp code would never have revealed this, because the existing code repeated the same assumption.

A control experiment beats a theory. The issue's four-row table did more than a page of prose. Changing one variable at a time — path ID and devMode — isolated the bug immediately. I now try to reproduce API bugs with the smallest possible raw request before touching the client.

Ambiguity should fail loudly. It is tempting to pick the first deployment and move on. But a deployment ID determines which version of the code runs. If the tool cannot know the user's intent, the correct behavior is to stop and ask, with a message that includes the fix.

Error messages are part of the API. The old 404 said "function not found", which sent people looking at their function and their deployment instead of the request path. The new errors say what is missing and what command to run next. That is a product improvement, not just a string change.

Small, focused PRs get merged. The patch does one thing: resolve the right deployment ID for non-dev mode. It adds the flag, the selection rules, the errors, the tests, and the docs. It does not refactor authentication, rewrite the command, or change dev mode. @sqrrrl merged it two days after I opened it, and release-please included it in v3.5.0 the same day.

Tests are the specification. The selection rules are easy to describe in prose and easy to get subtly wrong in code. Pinning the zero-case, one-case, and many-case behaviors in tests made the implementation almost mechanical.

Try it

clasp run-function now has a documented path for non-dev executions:

# If the project has exactly one API-executable deployment clasp run-function myFunction --nondev # If it has several clasp run-function myFunction --nondev --deployment-id <deploymentId> 

If you hit Script function not found on an older clasp version while the function definitely exists, check whether you are passing a deployment ID to scripts.run. That error can be a resource-ID problem, not a missing-function problem.

Thanks to the issue author for the precise reproduction, and to the clasp maintainers for the quick review.

Links