Skip to content

fix(detect): resolve the dev port from every place it's actually pinned#5

Merged
irwansetiawan merged 2 commits into
mainfrom
fix/inline-port-env
Jul 12, 2026
Merged

fix(detect): resolve the dev port from every place it's actually pinned#5
irwansetiawan merged 2 commits into
mainfrom
fix/inline-port-env

Conversation

@irwansetiawan

@irwansetiawan irwansetiawan commented Jul 12, 2026

Copy link
Copy Markdown
Owner

Problem

In a real Next.js project whose dev script is "dev": "PORT=5100 next dev --turbopack", perchd started the server on 3000 while npm run dev serves on 5100. More broadly, perchd only looked at a -p/--port flag and a bare .env — it missed most of the places a project pins its dev port.

What determines the port (per framework)

Verified empirically (incl. that Next ignores PORT in .env files — it loads them but binds 3000 anyway):

Framework Real source of the dev port
Next.js -p/--port flag, or PORT env (shell / script prefix, not .env)
Vite / SvelteKit / Astro --port flag, or server.port in config — they ignore PORT
Nuxt --port flag, PORT/NUXT_PORT env, or devServer.port in config
generic Node PORT from the .env cascade

Fix

The JS detector now resolves the port from, first hit wins:

  1. -p/--port flag in the dev script
  2. inline PORT= / NUXT_PORT= env prefix in the dev script (incl. after cross-env)
  3. framework config fileserver.port (vite/astro/sveltekit) or devServer.port (nuxt), previously never read
  4. PORT= in the dotenv cascade (.env.local, .env.development, .env, …)

Sources a framework ignores aren't used to override its port. Detectors stay pure (static file reads, no spawn).

Tests

TDD throughout — 10 new cases. Full suite green (145 passed), typecheck clean. Verified against the real project: detector now returns port: 5100.

A dev script like `PORT=5100 next dev` set the port via an env-var prefix,
which portFromScript ignored (it only matched -p/--port flags) and
portFromEnv missed (it reads a bare .env, not the prefix). perchd fell
back to the framework default (Next.js 3000) while `npm run dev` served on
5100 — the port mismatch a user hit on a real Next project.

portFromScript now also matches a `PORT=NNNN` assignment (including after a
cross-env wrapper); an explicit -p/--port flag still takes precedence, as
next/vite resolve it.
Extends the port fix beyond the script prefix to every place a JS/TS
project actually pins its dev port, per-framework so we never report a
port the framework ignores:

- config-file port — `server.port` (vite/astro/sveltekit) and
  `devServer.port` (nuxt), which were never read before;
- `NUXT_PORT=` script prefix alongside `PORT=`;
- the dotenv cascade (.env.local/.env.development/.env) for plain-Node
  apps, not just a bare .env.

Precedence, first hit wins: script flag → script env prefix → config
file → dotenv. Verified empirically that Next ignores PORT in .env
files, so that source can't mask the real port for Next.
@irwansetiawan irwansetiawan changed the title fix(detect): read an inline PORT= prefix in the dev script fix(detect): resolve the dev port from every place it's actually pinned Jul 12, 2026
@irwansetiawan
irwansetiawan marked this pull request as ready for review July 12, 2026 23:41
@irwansetiawan
irwansetiawan merged commit 66bd732 into main Jul 12, 2026
4 checks passed
@irwansetiawan
irwansetiawan deleted the fix/inline-port-env branch July 12, 2026 23:41
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