Skip to content

fix: await function template downloads in functions-create - #8465

Open
jherr wants to merge 1 commit into
mainfrom
fix/functions-create-await-downloads
Open

fix: await function template downloads in functions-create#8465
jherr wants to merge 1 commit into
mainfrom
fix/functions-create-await-downloads

Conversation

@jherr

@jherr jherr commented Sep 3, 2026

Copy link
Copy Markdown
Contributor

Two bugs in downloadFromURL (src/commands/functions/functions-create.ts), both on the netlify functions:create --url path.

Template downloads were never awaited. Files were written with res.body?.pipe(dest) — no await, so Promise.all resolved before any write had finished. The cp.exec('npm i', ...) that immediately follows could therefore run against a partially-written function directory. await pipeline(res.body, dest) closes the race.

A failed download was written to disk as the template. There was no res.ok check, so a 404 or 500 from GitHub had its error-page body piped into the function file and the command reported success. That's now an explicit throw, which the surrounding catch turns into the existing Error while retrieving ${downloadUrl} message.

 const res = await fetch(downloadUrl)
+if (!res.ok || !res.body) {
+  throw new Error(`HTTP ${res.status.toString()}: ${res.statusText}`)
+}
 const fileName = path.basename(name)
 const finalName = path.basename(fileName, '.js') === functionName ? `${nameToUse}.js` : fileName
 const dest = fs.createWriteStream(path.join(fnFolder, finalName))
-res.body?.pipe(dest)
+await pipeline(res.body, dest)

The node-fetch import is unchanged — node-fetch v3's res.body is a Node Readable typed NodeJS.ReadableStream | null, which satisfies PipelineSource, so this is purely the bug fix.

Verification

  • npm run typecheck, npm run lint, npm run format:check — clean
  • npm run test:unit — 499/500, matching the baseline on unmodified main exactly (the generate-autocompletion snapshot failure is pre-existing; I reproduced it on main at 97fd77d3c)

No package-lock.json change.


Split out of #8453, which bundled this with unrelated work.

🤖 Generated with Claude Code

`downloadFromURL` wrote each template file with `res.body?.pipe(dest)`,
which was never awaited. `Promise.all` therefore resolved before any
write had finished, and the `npm i` that follows ran against a possibly
incomplete function directory. `await pipeline(...)` closes the race.

A non-OK response was also piped straight to disk, silently writing an
error page where the template should be. That is now an explicit throw.

Co-Authored-By: Claude Opus 5 (1M context) <noreply@anthropic.com>
@jherr
jherr requested a review from a team as a code owner September 3, 2026 15:49
@coderabbitai

coderabbitai Bot commented Sep 3, 2026

Copy link
Copy Markdown
Contributor

Review Change Stack

No actionable comments were generated in the recent review. 🎉

ℹ️ Recent review info
⚙️ Run configuration

Configuration used: Organization UI

Review profile: CHILL

Plan: Team

Run ID: 2da71e25-cb14-445e-a465-f5740913ed36

📥 Commits

Reviewing files that changed from the base of the PR and between 97fd77d and 65c4504.

📒 Files selected for processing (1)
  • src/commands/functions/functions-create.ts
🔗 Linked repositories identified

CodeRabbit considers these linked repositories for cross-repo context during reviews:

  • netlify/blueprints (manual)

Included review availability: Your plan provides up to 4 included reviews per hour; 1 remains after this review.


📝 Summary

Summary by CodeRabbit

  • Bug Fixes
    • Improved reliability when downloading GitHub templates by detecting failed or empty responses.
    • Ensured template files finish writing before the download process completes.

Walkthrough

The template download flow now validates HTTP responses and requires a response body. It throws an HTTP error for failed or empty responses. It awaits pipeline to complete each file write.

Estimated code review effort: 2 (Simple) | ~10 minutes

Merge Risk: ⚪ Minimal · up to 65c45

URL-based function template downloads now reject invalid responses and wait for file writes to finish, preventing incomplete or error-content templates. The change is ready to merge.

Suggested reviewers: amun-sihra

🚥 Pre-merge checks | ✅ 5
✅ Passed checks (5 passed)
Check name Status Explanation
Description check ✅ Passed The description clearly explains the two download bugs, the implementation changes, and the verification results. It is directly related to the changeset.
Title check ✅ Passed The title clearly and concisely identifies the main change: awaiting function template downloads in functions-create.
Docstring Coverage ✅ Passed No functions found in the changed files to evaluate docstring coverage. Skipping docstring coverage check. Docstring coverage is scoped to functions touched by this diff. Analyzed 0 functions across 1…
Linked Issues check ✅ Passed Check skipped because no linked issues were found for this pull request.
Out of Scope Changes check ✅ Passed Check skipped because no linked issues were found for this pull request.
Full details: Docstring Coverage

Explanation

No functions found in the changed files to evaluate docstring coverage. Skipping docstring coverage check. Docstring coverage is scoped to functions touched by this diff. Analyzed 0 functions across 1 files.

✨ Finishing Touches
📝 Generate docstrings
  • Create stacked PR
  • Commit on current branch
🧪 Generate unit tests (beta)
  • Create PR with unit tests
  • Commit unit tests in branch fix/functions-create-await-downloads

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

@github-actions

github-actions Bot commented Sep 3, 2026

Copy link
Copy Markdown

📊 Benchmark results

Comparing with 97fd77d

  • Dependency count: 1,088 (no change)
  • Package size: 441 MB ⬆️ 0.00% increase vs. 97fd77d
  • Number of ts-expect-error directives: 346 (no change)

@pkg-pr-new

pkg-pr-new Bot commented Sep 3, 2026

Copy link
Copy Markdown

Open in StackBlitz

npm i https://pkg.pr.new/netlify-cli@8465

commit: 65c4504

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.

1 participant