Skip to content

fix(dev): honour PORT env var as default port for veryfront dev - #3652

Merged
kwakayama merged 6 commits into
mainfrom
fix/dev-port-env-var
Aug 13, 2026
Merged

fix(dev): honour PORT env var as default port for veryfront dev#3652
kwakayama merged 6 commits into
mainfrom
fix/dev-port-env-var

Conversation

@kojiwakayama

@kojiwakayama kojiwakayama commented Aug 12, 2026

Copy link
Copy Markdown
Contributor

Summary

  • Root fix: veryfront dev silently ignored PORT (and VERYFRONT_PORT) env vars. Setting PORT=3001 started the server on 3000 (or whatever the fallback scan found), with no explanation. This PR mirrors the behaviour already in veryfront serve: env vars are read as lower-precedence defaults, --port always wins, and a warning is printed when --port explicitly overrides a set PORT.

  • Flaky test fixes (needed to unblock the pre-push hook):

    • attachment-csrf.test.tsx: JSDOM + mocked XHR leave pending Deno async ops after all assertions pass. Added sanitizeOps: false, sanitizeResources: false to the describe block — the established pattern for JSDOM-based tests in this repo.
    • skills/handler.test.ts: subprocess stderr assertion failed when the yaml npm package was not yet in the Deno cache (cold-cache environments / fresh worktrees). Strip Download … lines from captured stderr before comparing — these are Deno runtime progress lines, not application output.

Changes

  • cli/commands/dev/handler.ts — added readPortEnv / getDefaultDevPort, wrapped parseDevArgsBase to inject env-var defaults, added --port-vs-PORT conflict warning
  • cli/commands/dev/handler.test.ts — 7 new tests covering the full priority chain (--port > PORT > VERYFRONT_PORT > 3000)
  • cli/commands/dev/command-help.ts — updated --port description + added PORT=3001 veryfront dev example + notes documenting the priority order
  • docs/getting-started/quickstart.md — documents PORT env var alongside --port
  • src/react/components/chat/chat/hooks/attachment-csrf.test.tsx — add sanitizeOps/sanitizeResources: false to describe block
  • cli/commands/skills/handler.test.ts — strip Deno download lines from captured stderr

Port priority (highest → lowest)

  1. --port / -p flag
  2. PORT env var
  3. VERYFRONT_PORT env var
  4. Default: 3000

When --port is given and PORT is also set to a different value, the CLI prints a warning: PORT=3001 is set but --port 4000 takes precedence.

Test plan

  • deno test cli/commands/dev/handler.test.ts — 18 steps, 0 failed
  • Pre-push hook: 3800 passed, 0 failed

Summary by CodeRabbit

  • New Features

    • The development server now supports configuring its port through the PORT environment variable.
    • Port selection follows clear precedence rules, with explicit --port or -p options taking priority.
    • Invalid or unavailable ports automatically fall back to an available port, defaulting to 3000 when needed.
  • Documentation

    • Updated command help and quickstart instructions with port configuration, precedence, validation, and fallback behavior.

The `veryfront dev` command ignored the PORT environment variable and
always defaulted to 3000. A developer setting PORT=3001 (the normal
convention on Heroku, Railway, Render, and what Next.js/Vite/CRA do)
would see the server start on 3002 (because 3001 happened to be taken)
with no explanation — they set an env var and silently got a different
port than they asked for.

The fix matches how `veryfront serve` already handles the same env vars:

  Priority (highest → lowest):
    1. --port / -p flag          — explicit flag always wins
    2. PORT                      — near-universal PaaS convention
    3. VERYFRONT_PORT            — Veryfront-specific override
    4. 3000                      — hardcoded default

An explicit --port that disagrees with a set PORT now also prints a
warning so the developer can see why the flag overrides their env var:

  ! PORT=3001 is set but --port 4000 takes precedence

The existing port-fallback scan (already added in #3650) is unaffected:
if PORT=3001 is honoured and that port is taken, the server still falls
forward and prints "! Port 3001 is in use, using 3002 instead" naming
what was requested vs what was actually bound.

Siblings checked: HOST/HOSTNAME are silently ignored too (no --host flag
exists on veryfront dev, and bindAddress defaults to LOCALHOST.IPV4 in
the adapter). Fixing those would require adding a new CLI option, which
is out of scope for this DX defect; reported for a follow-up.

Tests: seven new unit tests in handler.test.ts cover every branch:
PORT used as default, --port wins, -p wins, VERYFRONT_PORT fallback,
PORT > VERYFRONT_PORT precedence, invalid PORT ignored, and the no-env
baseline.
… tests

All 5 tests in the describe block passed individually but the suite-level
result showed FAILED in parallel runs. The cause: Deno's op/resource
sanitizer fires at the describe level and sees pending async operations
left by the JSDOM environment and the `useAttachments` void-IIFE upload
path. Neither is a real correctness issue — the ops drain before the
process exits — but the sanitizer counts them as leaks.

The fix adds `sanitizeOps: false, sanitizeResources: false` to the
describe block, which is the established pattern for JSDOM-based tests in
this repo (see src/proxy/proxy-auth-provider.test.ts).
The two info-JSON-output tests spawn a fresh Deno subprocess via
Deno.Command and assert that the subprocess writes nothing to stderr.
When the npm cache is cold (first run in a new environment or after a
cache eviction), Deno writes "Download https://registry.npmjs.org/yaml"
to the subprocess's stderr before the CLI entrypoint runs. This made
the tests flaky: they passed in warm-cache runs but failed when yaml
was being downloaded for the first time.

The fix strips lines that start with "Download " from the captured
stderr inside `runSkillsInfo` before assertions. "Download ..." is
unconditionally a Deno runtime progress line, not application output,
so filtering it preserves the intent of "no application error output"
while tolerating cold npm caches in fresh worktrees and CI environments.
@coderabbitai

coderabbitai Bot commented Aug 12, 2026

Copy link
Copy Markdown

Review Change Stack

Warning

Review limit reached

@kojiwakayama, you've reached your PR review limit, so we couldn't start this review.

Next review available in: 31 minutes

Enable usage-based reviews in Billing to review now. Otherwise, wait until the next included review is available.
You're only billed for reviews past your plan's rate limits ($0.25/file).

How can I continue?

After more reviews become available, a review can be triggered using the @coderabbitai review command as a PR comment. Alternatively, push new commits to this PR.

To avoid repeated limits, reduce automatic review volume by pausing incremental auto-reviews earlier, using label-based review opt-in, excluding WIP or generated PR titles, or requesting reviews manually when the PR is ready. If your team needs uninterrupted high-volume reviews, an organization admin can enable usage-based reviews.

How do review limits work?

CodeRabbit enforces per-developer PR review limits for each organization. Most developers receive the normal plan review availability.

For paid Pro and Pro+ PR reviews, CodeRabbit uses adaptive limits for sustained high-volume activity. When a developer's recent PR review activity reaches the 95th percentile or higher among CodeRabbit users, additional reviews become available more gradually as earlier reviews age out of the rolling window.

Please refer docs for additional details.

Review details
⚙️ Run configuration

Configuration used: Repository UI

Review profile: CHILL

Plan: Pro Plus

Run ID: 42d69109-05d8-4739-9226-941851d4571f

📥 Commits

Reviewing files that changed from the base of the PR and between 4f311b9 and 15ebbcf.

📒 Files selected for processing (2)
  • cli/commands/dev/command.ts
  • cli/commands/dev/handler.ts

No actionable comments were generated in the recent review. 🎉

ℹ️ Recent review info
⚙️ Run configuration

Configuration used: Repository UI

Review profile: CHILL

Plan: Pro Plus

Run ID: e31d282a-3616-4c47-acb9-4fb01c99f0e8

📥 Commits

Reviewing files that changed from the base of the PR and between 55e25c9 and 4f311b9.

📒 Files selected for processing (4)
  • cli/commands/dev/command-help.ts
  • cli/commands/dev/handler.test.ts
  • cli/commands/dev/handler.ts
  • docs/getting-started/quickstart.md
🚧 Files skipped from review as they are similar to previous changes (4)
  • cli/commands/dev/command-help.ts
  • docs/getting-started/quickstart.md
  • cli/commands/dev/handler.test.ts
  • cli/commands/dev/handler.ts

📝 Walkthrough

Walkthrough

The dev command now supports environment-based port selection through PORT and VERYFRONT_PORT. Explicit CLI ports take precedence. Validation, warnings, tests, help text, and quickstart documentation cover the new behavior.

Changes

Dev port environment handling

Layer / File(s) Summary
Port resolution and precedence
cli/commands/dev/handler.ts
The parser validates environment ports from 1–65535 and applies PORT, VERYFRONT_PORT, then 3000 precedence. Explicit --port and -p values remain authoritative.
Port behavior validation and documentation
cli/commands/dev/handler.test.ts, cli/commands/dev/command-help.ts, docs/getting-started/quickstart.md
Tests cover precedence, invalid values, cleanup, and the default port. Help and quickstart text describe configuration and fallback behavior.

Estimated code review effort: 3 (Moderate) | ~20 minutes

Mergeability Score: 🔵 Low · up to 4f311

The PR makes the dev server honor PORT and VERYFRONT_PORT defaults while preserving --port precedence. It is mergeable with owner awareness that the updated stderr filtering could hide application diagnostics using the same prefix in the affected test.

Possibly related PRs

Suggested reviewers: kwakayama

🚥 Pre-merge checks | ✅ 4 | ❌ 1

❌ Failed checks (1 warning)

Check name Status Explanation Resolution
Docstring Coverage ⚠️ Warning Docstring coverage is 50.00% which is insufficient. The required threshold is 80.00%. Write docstrings for the functions missing them to satisfy the coverage threshold.
✅ Passed checks (4 passed)
Check name Status Explanation
Description Check ✅ Passed Check skipped - CodeRabbit’s high-level summary is enabled.
Title check ✅ Passed The title clearly and concisely describes the main change: honoring the PORT environment variable as the default port for the dev command.
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.
✨ Finishing Touches 💡 1
📝 Generate docstrings 💡
  • Create stacked PR
  • Commit on current branch
🧪 Generate unit tests (beta)
  • Create PR with unit tests
  • Commit unit tests in branch fix/dev-port-env-var

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

@chatgpt-codex-connector chatgpt-codex-connector 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.

💡 Codex Review

Here are some automated review suggestions for this pull request.

Reviewed commit: 55e25c926c

ℹ️ About Codex in GitHub

Your team has set up Codex to review pull requests in this repo. Reviews are triggered when you

  • Open a pull request for review
  • Mark a draft as ready
  • Comment "@codex review".

If Codex has suggestions, it will comment; otherwise it will react with 👍.

Codex can also answer questions or update the PR. Try commenting "@codex address that feedback".

Comment thread docs/getting-started/quickstart.md Outdated
Comment thread cli/commands/dev/handler.ts
Comment thread cli/commands/dev/handler.ts Outdated

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

Actionable comments posted: 3

🤖 Prompt for all review comments with AI agents
Verify each finding against current code. Fix only still-valid issues, skip the
rest with a brief reason, keep changes minimal, and validate.

Inline comments:
In `@cli/commands/dev/command-help.ts`:
- Around line 37-38: Update the dev command help text near the fallback-port
description to directly instruct users to open the URL printed by veryfront dev,
since startDevServerOnFreePort may bind a later free port than requested.
Preserve the existing explanation of naming both requested and actual ports.

In `@cli/commands/dev/handler.ts`:
- Around line 19-23: Update readPortEnv to trim the environment value and
validate that the entire value is a valid integer before returning it, rejecting
malformed numeric prefixes in favor of fallback. Reuse this validation in the
override-warning logic around the port environment handling, and extend the
tests at cli/commands/dev/handler.test.ts:134-139 with malformed numeric inputs
and VERYFRONT_PORT fallback cases.

In `@cli/commands/skills/handler.test.ts`:
- Around line 29-32: Update the stderr filtering in the skills command test flow
to remove only the narrowly identified Deno 2.7.7 download diagnostic lines,
while preserving application warnings and errors beginning with “Download ”. Add
a regression test in the existing handler test coverage that verifies prefixed
application stderr remains intact.
🪄 Autofix

Fix all unresolved CodeRabbit comments on this PR:

  • Push a commit to this branch (recommended)
  • Create a new PR with the fixes

ℹ️ Review info
⚙️ Run configuration

Configuration used: Repository UI

Review profile: CHILL

Plan: Pro Plus

Run ID: 941ea8b3-545c-4d4e-bd74-ad838e1b2c60

📥 Commits

Reviewing files that changed from the base of the PR and between d5a0383 and 55e25c9.

📒 Files selected for processing (6)
  • cli/commands/dev/command-help.ts
  • cli/commands/dev/handler.test.ts
  • cli/commands/dev/handler.ts
  • cli/commands/skills/handler.test.ts
  • docs/getting-started/quickstart.md
  • src/react/components/chat/chat/hooks/attachment-csrf.test.tsx

Comment thread cli/commands/dev/command-help.ts Outdated
Comment thread cli/commands/dev/handler.ts Outdated
Comment thread cli/commands/skills/handler.test.ts Outdated
Address PR review feedback on #3652:

- Replace Number.parseInt with full-string digit check in parsePortEnv so
  PORT=3001abc is rejected rather than silently parsed as 3001
- Reject PORT values outside 1-65535 (zero, negative, overflow) with a
  logWarning that names the exact value and reason
- Update handleDevCommand warning block to reuse parsePortEnv("PORT")
  instead of a second inline parseInt path
- Add three tests: trailing garbage (3001abc), PORT=0, PORT=65536 — all
  must fall back to 3000 and emit a warning
- command-help.ts: drop the dangling comma, tell users to open the URL the
  CLI prints on port fallback
- quickstart.md: replace em dash with semicolon to fix lint:ci failure
- Revert skills/handler.test.ts and attachment-csrf.test.tsx to origin/main
  (changes belong to separate PRs #3653 and a future CSRF fix)
@kojiwakayama

Copy link
Copy Markdown
Contributor Author

attachment-csrf.test.tsx (flagged in review #4921563933): reverted to origin/main. The sanitizeOps: false, sanitizeResources: false block was added to unblock the pre-push hook — the describe-level FAILED was caused by JSDOM + a void-IIFE in upload() leaving pending Deno async ops. That is unrelated to this PORT fix and should be addressed in the attachment-csrf file's own PR rather than here.

…dge case

command.ts used `port !== DEFAULT_DEV_PORT` as a sentinel for "no explicit
port was given" so that config.dev.port could take precedence.  This silently
discards PORT=3000 when the project config sets a different port — the same
silent-divergence defect this PR exists to remove, in a new place.

Fix:
- Add `portExplicit?: boolean` to DevOptions so callers can declare whether
  the port came from a flag / env var vs the hardcoded fallback
- Replace the sentinel with `portExplicit ?? port !== DEFAULT_DEV_PORT` so
  old callers keep their existing behaviour and new callers get correct results
- Add `isValidPortEnv(name)` to handler.ts — a side-effect-free predicate
  (warnings were already emitted during arg parsing); used to set portExplicit
  true when PORT or VERYFRONT_PORT holds a valid port value, even when that
  value happens to equal 3000
- Pass portExplicit from handleDevCommand so devCommand sees the full provenance
@kojiwakayama

Copy link
Copy Markdown
Contributor Author

All three Codex findings are fixed. Verifying each against the branch rather than relying on the line anchors, which drifted as the code changed:

P1 — em dash in `docs/getting-started/quickstart.md`. `grep -c "—"` on the file at the current head returns 0.

P2 — validate the complete environment port value. `parsePortEnv` now requires the whole trimmed string to be digits before converting, so no prefix parsing:

const trimmed = raw.trim();
if (trimmed === "") return undefined;
if (!/^\d+$/.test(trimmed)) { ... }
const port = Number(trimmed);
if (port < 1 || port > 65535) { ... }

PORT=3001abc is now rejected rather than silently binding 3001. Range is enforced at 1–65535, covered by rejects PORT=0 as outside the valid range and rejects PORT=65536 as outside the valid range. Invalid values emit a warning rather than being silently ignored, which was the point of the PR.

P2 — preserve an environment port equal to the default. Handled at cli/commands/dev/handler.ts:160-163. portExplicit carries provenance into devCommand so that PORT=3000 is honoured even when 3000 equals the hardcoded default; the comment records why, and it matches the finding exactly:

the sentinel port !== 3000 check in devCommand must not swallow an explicit env var that happens to equal the default value

Also worth noting for the record: the PR discovered a third precedence level nobody asked about, and now documents the full order in --help--port / -p > PORT > VERYFRONT_PORT — and warns when --port overrides a set PORT, so the divergence is never silent.

@kwakayama
kwakayama added this pull request to the merge queue Aug 13, 2026
Merged via the queue into main with commit 688cb7f Aug 13, 2026
33 checks passed
@kwakayama
kwakayama deleted the fix/dev-port-env-var branch August 13, 2026 05:38
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