Skip to content

fix(functions): name the user's file in functions:serve errors - #300

Merged
Harshi-Shah-CS merged 6 commits into
v2-devfrom
CL-8791
Nov 20, 2026
Merged

Harshi-Shah-CS merged 6 commits into
v2-devfrom
CL-8791

Conversation

@Harshi-Shah-CS

Copy link
Copy Markdown
Contributor

Finishes CL-8791 (V2 bug bash, scenario 9.1), item 3: launch:functions:serve load and runtime errors did not name the user's file. Follows #299, which covered the rest of the ticket.

What

  • A function that fails to load stops serve with Could not load functions/<file>.js: <cause> (FunctionLoadError). V1 printed only the cause (error: init), so with many functions you could not tell which file to fix.
    • A syntax error adds where it is: Could not load functions/syn.js: Unexpected token '='... (functions/syn.js:2:8).
    • A thrown non-error value keeps its text instead of undefined.
  • A package that is not installed gives Could not load functions/api/needs.js: functions/api/needs.js imports "not-installed-pkg", which is not installed. Run npm install in the project. (MissingPackageError). This replaces a rollup warning with a long ../../ path followed by a base64 data: URL error.
    • It fires only when a real file imports the package statically, and it names the file that does the import. A require() from a CommonJS function is traced back through rollup's commonjs proxy to the function file.
    • A guarded dynamic import() of an optional package still only warns, and serve starts as before.
  • A handler that throws at request time logs a stack trace pointing at functions/<file>.js:<line>:<col>, not data:text/javascript;base64,...:1:28. Each function is bundled with a source map into a temporary .mjs file and imported by file URL. Serve turns on Node's source-map support once. The temporary directory is removed as soon as the functions are loaded, whether loading succeeds or fails.
  • AGENTS.md adds these to serve's approved differences from V1.

Notes for reviewers

  • loadDataURL keeps its name although it now imports a file: URL. Renaming it would also churn its compiled-build test (test/integration/compiled-load-data-url.test.ts), which still passes.
  • Jest maps stack traces itself, so the stack-trace test also checks that serve calls process.setSourceMapsEnabled(true). I checked the built CLI by hand. With the call, the trace reads functions/broken.js:2:9. Without it, the trace names the temporary bundle. It still maps correctly after the temporary files are deleted.
  • .talismanrc checksums are refreshed for the files this branch changed. The flagged lines are prose, test names and existing error wording, not secrets. The refreshes sit in the last commit of their group, so earlier commits in this PR carry a stale checksum. The PR is squash-merged, so those in-between states never land.

Tests

  • New cases in src/functions/cloud-functions.test.ts:
    • a load-time throw
    • a syntax error with its location
    • a non-error throw
    • a missing package from ESM and from CommonJS
    • a guarded optional dynamic import that still serves
    • temporary-bundle cleanup on success and on failure
    • the request-time stack trace
  • Full suite green: npm test, 197 suites, 3,949 tests.

#claude_code#
🤖 Generated with Claude Code

Harshi-Shah-CS and others added 5 commits November 19, 2026 22:25
A function that threw while being loaded stopped functions:serve with
only the cause ("error: init"), and one broken file stopped the whole
server without saying which file to fix.

Wrap each function's build and load in FunctionLoadError, which names
the file relative to the project and keeps the original error as its
cause: "Could not load functions/api/boom.js: init".

#claude_code#
🤖 Generated with [Claude Code](https://claude.com/claude-code)

Co-Authored-By: Claude <noreply@anthropic.com>
A function importing a package that was not installed printed a rollup
warning with a long ../../ path, then failed with "Failed to resolve
module specifier ... from data:text/javascript;base64,...", which names
neither the user's file nor what to do.

Turn rollup's UNRESOLVED_IMPORT warning into MissingPackageError:
"Could not load functions/api/needs.js: functions/api/needs.js imports
"not-installed-pkg", which is not installed. Run npm install in the
project." Other rollup warnings still go to the default handler. The
syntax-error test now also checks its "Could not load <file>:" prefix.

#claude_code#
🤖 Generated with [Claude Code](https://claude.com/claude-code)

Co-Authored-By: Claude <noreply@anthropic.com>
…e and line

Functions were bundled and imported from a base64 data: URL, so a
handler that threw at request time logged a stack trace pointing at
data:text/javascript;base64,...:1:28 instead of the user's file.

Write each bundle, with its source map, to a temporary .mjs file and
import it by file URL, and enable Node's source-map support so the trace
reads functions/broken.js:2:9. Jest maps stack traces on its own, so the
test also checks that serve turns source maps on; checked against the
built CLI, where the trace otherwise names the temporary bundle.

#claude_code#
🤖 Generated with [Claude Code](https://claude.com/claude-code)

Co-Authored-By: Claude <noreply@anthropic.com>
List the errors that now name the user's file: FunctionLoadError for a
function that fails to bundle or load, MissingPackageError for a package
that is not installed, and source-mapped stack traces from bundles
loaded by file URL instead of a data: URL.

Refresh the .talismanrc checksums for AGENTS.md and
src/functions/cloud-functions.test.ts, whose flagged lines are prose and
test names, not secrets.

#claude_code#
🤖 Generated with [Claude Code](https://claude.com/claude-code)

Co-Authored-By: Claude <noreply@anthropic.com>
- A missing package is refused only when a real file imports it
  statically. A guarded dynamic import of an optional package is left
  as a warning and serve starts, as before this branch.
- The importer is found through rollup's module graph, following the
  commonjs plugin's virtual proxies, so a require() of a missing package
  names the CommonJS function file instead of failing on the proxy's
  null-byte id.
- A thrown non-error value keeps its text ("plain string", not
  "undefined"), and a syntax error gives the file, line and column:
  "Could not load functions/syn.js: Unexpected token ... (functions/syn.js:2:8)".
- The temporary bundle directory is removed once the functions are
  loaded, whether loading succeeds or fails.
- Source maps are enabled once per serve, not once per function, and
  leave the sources out of the inline maps.
- The two importer walks share one helper.

Refresh the .talismanrc checksum for src/functions/cloud-functions.test.ts
(flagged lines are test names, not secrets).

#claude_code#
🤖 Generated with [Claude Code](https://claude.com/claude-code)

Co-Authored-By: Claude <noreply@anthropic.com>
@Harshi-Shah-CS
Harshi-Shah-CS requested review from a team as code owners November 19, 2026 17:40
@snyk-io

snyk-io Bot commented Nov 19, 2026 •

Copy link
Copy Markdown

✅ Snyk checks have passed. No issues have been found so far.

Status Scan Engine Critical High Medium Low Total (0)
✅ Open Source Security 0 0 0 0 0 issues
✅ Licenses 0 0 0 0 0 issues
✅ Code Security 0 0 0 0 0 issues

💻 Catch issues earlier using the plugins for VS Code, JetBrains IDEs, Visual Studio, and Eclipse.

@github-actions

Copy link
Copy Markdown

🔒 Security Scan Results

ℹ️ Note: Only vulnerabilities with available fixes (upgrades or patches) are counted toward thresholds.

Check Type Count (with fixes) Without fixes Threshold Result
🔴 Critical Severity 0 0 10 ✅ Passed
🟠 High Severity 0 2 25 ✅ Passed
🟡 Medium Severity 0 1 500 ✅ Passed
🔵 Low Severity 0 0 1000 ✅ Passed

⏱️ SLA Breach Summary

✅ No SLA breaches detected. All vulnerabilities are within acceptable time thresholds.

Severity Breaches (with fixes) Breaches (no fixes) SLA Threshold (with/no fixes) Status
🔴 Critical 0 0 15 / 30 days ✅ Passed
🟠 High 0 0 30 / 120 days ✅ Passed
🟡 Medium 0 0 90 / 365 days ✅ Passed
🔵 Low 0 0 180 / 365 days ✅ Passed

ℹ️ Vulnerabilities Without Available Fixes (Informational Only)

The following vulnerabilities were detected but do not have fixes available (no upgrade or patch). These are excluded from failure thresholds:

  • Critical without fixes: 0
  • High without fixes: 2
  • Medium without fixes: 1
  • Low without fixes: 0

✅ BUILD PASSED - All security checks passed

@rohan-agrawal-cs rohan-agrawal-cs 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.

Thanks, this is a clear improvement. Naming the file, the syntax-error location and the missing-package message all read well, and the guarded optional import() still serves. I checked it out, ran the cloud-functions suite, and ran the compiled CLI under plain Node to check the behavior outside Jest.

Should fix before merge

  1. Regression: prefix-only builtins (node:sqlite, node:test, node:sea) are now refused as "not installed". They served on v2-dev. One-line isBuiltin guard; see inline.
  2. macOS: mapped stack-trace paths get an extra /private prefix because the temp dir isn't realpathed. One-line fix; see inline. Jest can't catch it.

Nits

  • A load-time runtime throw doesn't get a location yet. The mapped frame is already in cause.stack.
  • sourceLocation should not let realpathSync replace the original error.
  • When the function file is the importer, the file name appears twice: Could not load functions/api/needs.js: functions/api/needs.js imports "...". It would read better to drop the inner path when importer === function file. Optional.

(Review drafted with Claude Code; each finding above was reproduced locally.)

Comment thread src/functions/cloud-functions.ts Resolved
Comment thread src/functions/cloud-functions.ts Resolved
Comment thread src/functions/cloud-functions.ts Resolved
Comment thread src/functions/cloud-functions.ts Resolved
From the review of #300:
- node:-only builtins (node:sqlite, node:test, node:sea) are not in
  builtinModules, so rollup reported them unresolved and serve refused
  them as missing packages. Skip anything isBuiltin() reports.
- On macOS the temp directory sits under the /var -> /private/var
  symlink, and mapped stack frames came out as /private/Users/...
  Realpath the bundle directory so frames name the real source path.
- A function that throws while loading gets the first stack frame
  inside the project as its location, as a syntax error does.
- A failing realpath while finding that location no longer replaces the
  original error; the message falls back to "Could not load <file>:".
- When the function file itself imports the missing package, the error
  is MissingPackageError on its own, without repeating the file name.

Refresh the .talismanrc checksums for the changed files (flagged lines
are test names and existing error wording, not secrets).

#claude_code#
🤖 Generated with [Claude Code](https://claude.com/claude-code)

Co-Authored-By: Claude <noreply@anthropic.com>
@github-actions

Copy link
Copy Markdown

🔒 Security Scan Results

ℹ️ Note: Only vulnerabilities with available fixes (upgrades or patches) are counted toward thresholds.

Check Type Count (with fixes) Without fixes Threshold Result
🔴 Critical Severity 0 0 10 ✅ Passed
🟠 High Severity 0 2 25 ✅ Passed
🟡 Medium Severity 0 1 500 ✅ Passed
🔵 Low Severity 0 0 1000 ✅ Passed

⏱️ SLA Breach Summary

✅ No SLA breaches detected. All vulnerabilities are within acceptable time thresholds.

Severity Breaches (with fixes) Breaches (no fixes) SLA Threshold (with/no fixes) Status
🔴 Critical 0 0 15 / 30 days ✅ Passed
🟠 High 0 0 30 / 120 days ✅ Passed
🟡 Medium 0 0 90 / 365 days ✅ Passed
🔵 Low 0 0 180 / 365 days ✅ Passed

ℹ️ Vulnerabilities Without Available Fixes (Informational Only)

The following vulnerabilities were detected but do not have fixes available (no upgrade or patch). These are excluded from failure thresholds:

  • Critical without fixes: 0
  • High without fixes: 2
  • Medium without fixes: 1
  • Low without fixes: 0

✅ BUILD PASSED - All security checks passed

@Harshi-Shah-CS

Copy link
Copy Markdown
Contributor Author

/ship 2b summary: @rohan-agrawal-cs, thanks for the review. Every point is addressed in 33a422a. I reproduced each one before changing it.

Fixed:

  • node:-only builtins (node:sqlite) are no longer refused as missing packages.
  • macOS stack frames no longer get the /private prefix; the bundle directory is realpathed.
  • A function that throws while loading now gets a (file:line:col) location from the first project stack frame.
  • A failing realpath while finding that location falls back to the plain message.
  • The repeated file name (from your review comment): when the function file itself imports the missing package, the error is functions/api/needs.js imports "pkg", which is not installed. Run npm install in the project. with no repeated Could not load functions/api/needs.js: prefix. A shared helper still gets the prefix that names the function.

Resolved: all 4 inline threads, each with a reply.
Left for human: nothing.

Full suite is green (197 suites, 3,953 tests). Next step: needs an approval to merge.

@Harshi-Shah-CS
Harshi-Shah-CS merged commit 8fb345a into v2-dev Nov 20, 2026
7 checks passed
@Harshi-Shah-CS
Harshi-Shah-CS deleted the CL-8791 branch November 20, 2026 05:37
SakshiKoli-CS added a commit that referenced this pull request Nov 20, 2026
Merges #296 (env:update skips unchanged settings) and #300 (functions:serve
names the user's file in its errors). The only conflict was .talismanrc:
v2-dev's version is kept and the checksums of every file changed on either
side are recomputed from the merged content.

#claude_code#
🤖 Generated with [Claude Code](https://claude.com/claude-code)

Co-Authored-By: Claude <noreply@anthropic.com>
Sign up for free to 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.

3 participants