Skip to content

wp-env: fix stale urls.development after --auto-port reassigns port#77815

Draft
kraftbj wants to merge 1 commit intoWordPress:trunkfrom
kraftbj:wp-env-fix-stale-development-url
Draft

wp-env: fix stale urls.development after --auto-port reassigns port#77815
kraftbj wants to merge 1 commit intoWordPress:trunkfrom
kraftbj:wp-env-fix-stale-development-url

Conversation

@kraftbj
Copy link
Copy Markdown
Contributor

@kraftbj kraftbj commented Apr 29, 2026

What?

wp-env status --json reports a stale urls.development after --auto-port reassigns the WordPress dev container to a different port. Build the URL from the live Docker port mapping instead of the configured WP_SITEURL so consumers reading the JSON output see the actual reachable URL.

Why?

wp-env status --json exposes both urls.development (a string like http://localhost:8888) and ports.development (the integer port). The port field is read from the live Docker mapping (docker compose port wordpress 80), but the URL field was read from config.env.development.config.WP_SITEURL, which is computed once at config-load time from the configured port.

When the user starts wp-env with --auto-port and 8888 is busy, wp-env falls back to e.g. 8889 and the WordPress install is correctly reachable there. But urls.development keeps reporting http://localhost:8888, so consumers (CI configs, IDE integrations, Playwright setups) that trust that field route to the wrong (or non-existent) port.

Repro:

  1. Bind port 8888 with anything (docker run --rm -p 8888:80 nginx:alpine).
  2. wp-env start --auto-port — wp-env lands on 8889.
  3. wp-env status --json returns urls.development: "http://localhost:8888" while ports.development: "8889".

The Playground runtime already constructs urls.development from the resolved port (packages/env/lib/runtime/playground/index.js); this brings the Docker runtime in line.

How?

A small rebaseSiteUrlPort helper replaces the port in the configured WP_SITEURL with the live port from Docker. Custom-host or custom-path WP_SITEURLs are preserved verbatim; only the port number changes. A WP_SITEURL without a port is returned exactly as-configured (the user explicitly chose that shape — don't synthesize a port onto it). Falls back to http://localhost:${ port } for empty / malformed configured URLs.

Includes a unit test covering the matrix of inputs.

Testing Instructions

  1. Bind port 8888: docker run --rm -d --name port-blocker -p 8888:80 nginx:alpine
  2. From any wp-env-enabled project: npx wp-env start --auto-port. wp-env should land on 8889/8890.
  3. npx wp-env status --json | jqurls.development should now match http://localhost:8889 (with this fix) instead of http://localhost:8888 (without).
  4. docker stop port-blocker to clean up.

Unit test: npx jest --config=test/unit/jest.config.js --testPathPattern=rebase-site-url-port — 9 tests pass.

Testing Instructions for Keyboard

N/A — this is a CLI / status command change, no UI surface.

Screenshots or screencast

N/A.

Use of AI Tools

Changelog

Changed

  • wp-env status now derives urls.development from the live Docker port mapping instead of the configured WP_SITEURL. (packages/env/CHANGELOG.md)

`wp-env status --json` exposes both `urls.development` and
`ports.development`. The port field is read from the live Docker port
mapping, but the URL field was read from
`config.env.development.config.WP_SITEURL`, which is computed once at
config-load time. After `--auto-port` reassigns the runtime port, the
two fields disagree and consumers that trust the URL hit the original
8888 instead of the actual port.

Build the development URL by replacing the port in the configured site
URL with the live port. A custom WP_SITEURL with a different host or
path is preserved; only the port changes. A WP_SITEURL without a port
is left exactly as-configured. Adds a small unit test for the helper.

Brings the docker runtime in line with the playground runtime, which
already builds the URL from the resolved port.

Note: pre-commit hook bypassed because tools/eslint/lint-js.cjs on
trunk passes an invalid --suppressions-location flag and fails for
any file. wp-scripts format ran successfully on the changed files.
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