close
Skip to content

Fix site creation assigning port 0 on first site#3422

Merged
sejas merged 2 commits into
trunkfrom
fix-port-zero-first-site-creation
May 11, 2026
Merged

Fix site creation assigning port 0 on first site#3422
sejas merged 2 commits into
trunkfrom
fix-port-zero-first-site-creation

Conversation

@sejas
Copy link
Copy Markdown
Member

@sejas sejas commented May 11, 2026

Related issues

How AI was used in this PR

Used an AI assistant to help trace the data flow between desktop main process and the CLI subprocess (SiteServer.createcreateSiteViaClireportKeyValuePair), draft the fix, and scaffold the test cases. I reviewed all changes manually, ran the local test suite, and verified the typecheck for the affected workspaces.

Proposed Changes

  • SiteServer.create registered a placeholder SiteDetails with port: 0 and never updated it after the CLI assigned a real port. The CLI only reported id and running back via reportKeyValuePair, so the placeholder leaked all the way to the renderer (visible as http://localhost:0) until the next app launch when SiteServer.fetchAll() reread cli.json. On a fresh machine, creating the very first site exposed the bug immediately.
  • CLI now emits port as a keyValuePair alongside id and running (apps/cli/commands/site/create.ts).
  • IPC schema accepts the new port key, parses it, and the success branch rejects when the CLI succeeds without a real (non-zero) port (apps/studio/src/modules/cli/lib/cli-site-creator.ts).
  • SiteServer.create writes the real port into details and, when the site is running, transitions to StartedSiteDetails with a correct http://localhost:<port> url, also updating server.server.url (apps/studio/src/site-server.ts).
  • Added unit tests covering port propagation and the new rejection paths.

Testing Instructions

Reproduce the bug on trunk (pre-fix):

  1. Quit Studio.
  2. Move the Studio config aside to simulate a fresh machine: mv ~/.studio ~/.studio.bak.
  3. Check out trunk, install, and run: npm install && npm start.
  4. Click Add site, pick an empty folder, and create the site.
  5. Inspect the resulting site URL in Site settings

Confirm the fix:

  1. Reset state again: rm -rf ~/.studio (or move the backup aside).
  2. Check out this branch, then npm install && npm start.
  3. Click Add site and create a site.
  4. The site URL now shows the real assigned port (e.g. http://localhost:8881), and opening the site in the browser works on the first try, with no restart needed.
  5. Try the Clone site flow — the cloned site also receives a real port (the updateSiteUrl call at ipc-handlers.ts:1058 no longer sees localhost:0).
  6. Restore your config when finished: mv ~/.studio.bak ~/.studio.

Before

port-zero.mp4

After

port-fix.mp4

Pre-merge Checklist

  • Have you checked for TypeScript, React or other console errors?

sejas added 2 commits May 11, 2026 10:58
SiteServer.create registered placeholder details with port 0 and never
updated it. CLI assigned a real port but only reported back id and running
via reportKeyValuePair, so the port stayed 0 in the desktop process and
leaked to the renderer (visible as http://localhost:0) until the next app
launch when fetchAll() reread cli.json. Most visible on a fresh machine
creating the first site.

CLI now emits port as a keyValuePair, the IPC schema accepts it, and
SiteServer.create writes the real port (and updated url) into details
before returning.
Guards against regressing to the placeholder port 0 leak:
- SiteServer.create copies CLI port into details and computes url when
  the site is running.
- createSiteViaCli accepts the new port keyValuePair and rejects when
  the CLI succeeds without reporting a real (non-zero) port or id.
@sejas sejas self-assigned this May 11, 2026
@sejas sejas requested a review from a team May 11, 2026 10:14
Copy link
Copy Markdown
Contributor

@epeicher epeicher left a comment

Choose a reason for hiding this comment

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

Thanks @sejas for the fix! I have tested it, and it works as expected, I no longer see the port 0 when creating the initial site. Changes also LGTM!

Before After Copy site
Image Image Image

@sejas sejas enabled auto-merge (squash) May 11, 2026 10:32
@wpmobilebot
Copy link
Copy Markdown
Collaborator

📊 Performance Test Results

Comparing 013ef3d vs trunk

app-size

Metric trunk 013ef3d Diff Change
App Size (Mac) 1408.82 MB 1408.83 MB +0.01 MB ⚪ 0.0%

site-editor

Metric trunk 013ef3d Diff Change
load 1522 ms 1499 ms 23 ms ⚪ 0.0%

site-startup

Metric trunk 013ef3d Diff Change
siteCreation 8090 ms 8093 ms +3 ms ⚪ 0.0%
siteStartup 4924 ms 4947 ms +23 ms ⚪ 0.0%

Results are median values from multiple test runs.

Legend: 🟢 Improvement (faster) | 🔴 Regression (slower) | ⚪ No change (<50ms diff)

@sejas sejas merged commit 4321154 into trunk May 11, 2026
13 of 14 checks passed
@sejas sejas deleted the fix-port-zero-first-site-creation branch May 11, 2026 10: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.

3 participants