Skip to content

feat(complete): add --install flag to source completions from a file - #751

Merged
NuroDev merged 11 commits into
mainfrom
NuroDev/shell-completion-caching-6d4c50
Nov 30, 2026
Merged

NuroDev merged 11 commits into
mainfrom
NuroDev/shell-completion-caching-6d4c50

Conversation

@NuroDev

@NuroDev NuroDev commented Nov 23, 2026 •

Copy link
Copy Markdown
Member

Summary

Putting eval "$(cf complete zsh)" in a shell rc cost ~250ms per new shell. This removes that cost in two ways:

  • perf(complete): cf complete <shell> no longer loads the command catalogue before printing the init script. tab's init scripts are static (commands are resolved at TAB time through cf complete -- <words…>), so the load was wasted work. ~380ms → ~110ms on a local build.
  • feat(complete): new cf complete <shell> --install writes the script to completions/cf.<ext> (bash, zsh, fish, ps1) under cf's config directory: ~/.config/cloudflare on Linux, ~/Library/Preferences/cloudflare on macOS. The path goes to stdout, and how to load it goes to stderr: an rc line that skips a missing file for bash, zsh and PowerShell, or a symlink into fish's autoloaded completions directory. Shell startup then never spawns cf.
cf complete zsh --install
# add to ~/.zshrc (Linux path shown):
if [ -f ~/.config/cloudflare/completions/cf.zsh ]; then source ~/.config/cloudflare/completions/cf.zsh; fi

Design notes

  • Staleness: the script only changes when tab changes its template. Each TAB callback rewrites installed scripts whose content differs from the running cf's output. It compares content rather than versions, so a global and a project-pinned cf with the same tab version never fight over the file. Missing files are never created, so deleting the file uninstalls.
  • Atomic writes: temp file + rename, so a shell starting mid-write or a crash never sources a truncated script.
  • Capturing the script: tab has no API that returns the script as a string (checked 0.0.22 and the latest 0.0.23), so renderScript briefly swaps console.log around the synchronous t.setup() call. It throws on an empty render, so a tab change there can't make the refresh blank installed scripts.
  • Quoting: the printed command is quoted for each shell (POSIX '\'', fish \'/\\, PowerShell ''). On Windows, bash/zsh lines use forward slashes.
  • Lazy loading: the TAB path imports only node:fs plus modules it already loaded. The install branch imports output.js and log.js dynamically.

Testing

  • pnpm check passes.
  • New completions-install.test.ts covers rendering parity with t.setup, install path, refresh of stale vs. missing files, rc-line quoting, and cf complete with and without --install, plus TAB-time refresh.
  • Manually loaded the installed zsh, bash and fish scripts in real shells (_cf registered, complete -p cf, fish complete -c cf). PowerShell was not run.
  • Full suite: 2 failures in telemetry/dispatcher.test.ts that come from the agent env vars set in the local shell (AI_AGENT, CLAUDECODE). They pass with those unset and are unrelated to this change.

🤖 Generated with Claude Code

NuroDev and others added 2 commits November 23, 2026 14:54
The script tab prints is static; commands resolve at TAB time through
`cf complete -- <words…>`.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
`cf complete <shell> --install` writes the init script to
~/.config/cloudflare/completions/cf.<ext> so the shell rc can source it
instead of spawning cf on every startup. TAB callbacks rewrite installed
scripts whose content differs from the running cf's.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
@pkg-pr-new

pkg-pr-new Bot commented Nov 23, 2026 •

Copy link
Copy Markdown
npm i https://pkg.pr.new/cf@751

commit: 4bd4f2e

@NuroDev NuroDev self-assigned this Nov 23, 2026
@NuroDev NuroDev changed the title feat(complete): add --install to source completions from a file feat(complete): add --install flag to source completions from a file Nov 23, 2026
@ask-bonk

ask-bonk Bot commented Nov 23, 2026

Copy link
Copy Markdown
Contributor

LGTM

github run

@github-actions

github-actions Bot commented Nov 23, 2026 •

Copy link
Copy Markdown
Contributor

cf --help startup

mean stddev min max
PR 279.5 ms 4.2 ms 274.1 ms 288.8 ms
base 277.4 ms 3.5 ms 273.4 ms 284.5 ms

Δ (PR vs base): ↑ +2.1 ms (+0.8%)

Measured with hyperfine on ubuntu-latest (3 warmup runs, 20 timed runs). CI numbers carry ±a few ms of runner jitter; treat small deltas as noise.

The usage hint, splash, and first-run tip suggested commands that only
print the script.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
@ask-bonk

ask-bonk Bot commented Nov 25, 2026

Copy link
Copy Markdown
Contributor

LGTM

github run

@NuroDev
NuroDev marked this pull request as ready for review November 25, 2026 23:00
@NuroDev
NuroDev requested a review from a team as a code owner November 25, 2026 23:00
@NuroDev
NuroDev requested a review from jamesopstad November 25, 2026 23:00
@ask-bonk

ask-bonk Bot commented Nov 25, 2026

Copy link
Copy Markdown
Contributor

LGTM

github run

@clanky-the-clanker clanky-the-clanker Bot left a comment •

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Superseded by the review of c48bce88.

This review, of d35883cb

💬 Approved with comments — No blocking or warning-level issues; two INFO notes on Windows rc-line paths for bash/zsh and a possible leftover temp file when a write fails.

Findings: 2 info (2 are inline comments on the diff)

The decision is advisory: this review is a comment and never approves, blocks or requests changes on the pull request.


📋 Code Quality: 💬 2 info

Sound and well-tested change; two minor INFO notes in install.ts.

Recommendation: 💬 Optional Nits

  • [INFO] getSourceLine prints a native Windows backslash path inside a POSIX source '…' line for bash/zsh (packages/cli/src/commands/completions/install.ts:60)
  • [INFO] writeScript can leave a stray .tmp file behind when the write or rename fails (packages/cli/src/commands/completions/install.ts:69)
View full review
  • Windows paths in the bash/zsh rc line (packages/cli/src/commands/completions/install.ts:60): on Windows, getScriptPath returns a backslash path. It is printed as is inside a POSIX source '…' line, which may not resolve in every bash/zsh environment on Windows. The tests only cover POSIX-style paths for these shells.
  • Leftover temp file (packages/cli/src/commands/completions/install.ts:69-70): if renameSync (or a partial writeFileSync) fails, cf.<ext>.<pid>.tmp stays behind. On the TAB path the error is swallowed, and the PID is new on every press, so repeated failures can pile up orphan files.

✅ All other checks passed

🚀 Release: ✅ LGTM

Two changesets cover the new --install flag (minor) and the startup change (patch), which matches the repository's changeset rules. The new installer writes to the user's config directory, but the write is reversible: deleting the file uninstalls it, and TAB refresh never recreates a missing file.


🔒 Security: ✅ LGTM

The installed script content comes from tab's static output, the rc line is quoted for each shell, and writes go only to the user's own config directory. shell is limited to the SHELLS choices.


📖 AGENTS.md: ✅ LGTM

No issues found.


⚡ Performance: ✅ LGTM

No issues found.


📝 Documentation: ✅ LGTM

No issues found.


Reviewed commit: d35883cb

NuroDev and others added 3 commits November 25, 2026 23:05
Each TAB callback runs under a new pid, so a target that keeps failing
would otherwise collect a temp file per press.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
bash and zsh on Windows run under MSYS2 or Cygwin, which accept C:/
paths.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
…-6d4c50' into NuroDev/shell-completion-caching-6d4c50
@ask-bonk

ask-bonk Bot commented Nov 25, 2026

Copy link
Copy Markdown
Contributor

LGTM!

github run

@clanky-the-clanker clanky-the-clanker Bot left a comment •

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Superseded by the review of 8c6daaac.

This review, of c48bce88

✅ Approved — Both earlier INFO notes (Windows path in the bash/zsh rc line, leftover temp file) are fixed and tested; no new issues.

The decision is advisory: this review is a comment and never approves, blocks or requests changes on the pull request.


✅ All checks passed

🚀 Release: ✅ LGTM

Two changesets cover the change: a minor bump for the new --install flag and a patch bump for the startup improvement. The install is opt-in and reversible: deleting the file uninstalls it, and the TAB refresh never recreates a missing file.


🔒 Security: ✅ LGTM

The script content comes from tab's static output. shell is limited to the SHELLS choices, writes go only to the user's own config directory, and the rc line is quoted for each shell.


📖 AGENTS.md: ✅ LGTM

No issues found.


📋 Code Quality: ✅ LGTM

  • Windows rc line: for bash/zsh, getSourceLine now replaces sep with / (install.ts:68). This does nothing on POSIX, and a win32-only test covers it.
  • Temp file cleanup: writeScript calls rmSync(tempPath, { force: true }) before rethrowing (install.ts:80-84). A test forces the rename to fail and checks that no temp file is left.

⚡ Performance: ✅ LGTM

On TAB, the refresh only reads small files and rewrites a script when its content differs. Printing the init script no longer loads the command catalogue.


📝 Documentation: ✅ LGTM

No issues found.


Reviewed commit: c48bce88

@jamesopstad jamesopstad 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.

Note that these comments are mostly from an AI review.

NuroDev and others added 3 commits November 29, 2026 19:44
…ions

bash, zsh, and PowerShell rc lines now check the script exists, so a removed
script does not print an error in every new shell. fish autoloads
completions on first use, so its hint links the script into
$__fish_config_dir/completions instead of sourcing it from config.fish. The
bash and fish examples use --install too.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
renderScript now throws when tab prints nothing, so a future tab that stops
printing through a single console.log cannot blank every installed script.
Tests check each shell's script registers its completions, independent of
how the output is captured.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
@ask-bonk

ask-bonk Bot commented Nov 29, 2026

Copy link
Copy Markdown
Contributor

LGTM!

github run

@clanky-the-clanker clanky-the-clanker Bot left a comment •

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Superseded by the review of 4bd4f2e9.

This review, of 8c6daaac

💬 Approved with comments — The fixes from the discussion are in place and tested. The one remaining note is INFO only: the documented install path is Linux-specific.

Findings: 1 info (1 is an inline comment on the diff)

The decision is advisory: this review is a comment and never approves, blocks or requests changes on the pull request.


📝 Documentation: 💬 1 info

The documented install path applies to Linux only

Recommendation: 💬 Optional Nits

  • [INFO] architecture.md hard-codes the Linux config path for installed completion scripts; macOS uses ~/Library/Preferences/cloudflare. (docs/architecture.md:199)
View full review

docs/architecture.md:199 gives the install path as ~/.config/cloudflare/completions/cf.<ext>. The code builds the path from getCfConfigPath(), which is ~/Library/Preferences/cloudflare on macOS. The changeset's example is framed as "the printed line, e.g.", so it is less of a problem there.


✅ All other checks passed

🚀 Release: ✅ LGTM

Two changesets cover the change: a minor bump for the new --install flag and a patch bump for the faster init-script output. The install is opt-in and reversible: deleting the file uninstalls it, and the TAB refresh never recreates a missing file.


🔒 Security: ✅ LGTM

No issues found. The script content is tab's static output, shell is limited to the SHELLS choices, files are written only to the user's own config directory, and the printed commands are quoted for each shell.


📖 AGENTS.md: ✅ LGTM

No issues found.


📋 Code Quality: ✅ LGTM

The changes since the last review are correct:

  • rc lines now check that the file exists.
  • The fish hint now creates a symlink.
  • renderScript throws on an empty render (install.ts:53-56).
  • getScriptPath now runs inside the refresh try (install.ts:129).
  • Telemetry now records --install through safeFlags.

The Windows forward-slash fix and the temp-file cleanup still hold.


⚡ Performance: ✅ LGTM

No issues found.


Reviewed commit: 8c6daaac

Comment thread docs/architecture.md Resolved
cf's config directory is ~/Library/Preferences/cloudflare on macOS.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
@ask-bonk

ask-bonk Bot commented Nov 29, 2026

Copy link
Copy Markdown
Contributor

LGTM

github run

@clanky-the-clanker clanky-the-clanker Bot left a comment

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

✅ Approved — The last note, the Linux-only install path in architecture.md, is fixed. There are no open findings, and the code still holds.

The decision is advisory: this review is a comment and never approves, blocks or requests changes on the pull request.


✅ All checks passed

🚀 Release: ✅ LGTM

There is a minor changeset for the new --install flag and a patch changeset for the faster init-script output. The install is opt-in and reversible.


🔒 Security: ✅ LGTM

No issues found. The script content is tab's static output, shell is limited to the SHELLS choices, files are written only to the user's own config directory, and the printed commands are quoted for each shell.


📖 AGENTS.md: ✅ LGTM

No issues found.


📋 Code Quality: ✅ LGTM

Still correct:

  • renderScript restores console.log and throws on an empty render.
  • writeScript writes a temp file and renames it into place, and removes the temp file on failure.
  • refreshInstalledScripts never creates a missing file and swallows its own errors.
  • The tests cover rc-line quoting for each shell, including the Windows forward-slash case.

⚡ Performance: ✅ LGTM

The TAB-time refresh only does a few small reads and renders only for shells whose script exists. It writes only when the content differs. Skipping the catalogue load for init scripts cuts startup work.


📝 Documentation: ✅ LGTM

docs/architecture.md now describes the install location as completions/cf.<ext> under cf's config directory (getCfConfigPath()). The changeset example names the macOS path.


Reviewed commit: 4bd4f2e9

@jamesopstad jamesopstad 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 Ben!

@NuroDev
NuroDev added this pull request to the merge queue Nov 30, 2026
Merged via the queue into main with commit bb6dd45 Nov 30, 2026
13 checks passed
@NuroDev
NuroDev deleted the NuroDev/shell-completion-caching-6d4c50 branch November 30, 2026 10:32
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.

2 participants