Skip to content

[Playground CLI] Return a readable error and hint when given port is already in use#3238

Merged
mho22 merged 5 commits intotrunkfrom
return-cli-error-when-port-already-in-use
Feb 23, 2026
Merged

[Playground CLI] Return a readable error and hint when given port is already in use#3238
mho22 merged 5 commits intotrunkfrom
return-cli-error-when-port-already-in-use

Conversation

@mho22
Copy link
Copy Markdown
Collaborator

@mho22 mho22 commented Feb 3, 2026

Motivation for the change, related issues

Based on this issue

The goal was to ignore stack traces, only print error messages in the CLI output style.

Implementation details

Added a onError option in ServerOptions that will be called once a error happens in app.listen(..).

Testing Instructions

CLI

or

Run the following command in two separate terminals :

node --no-warnings --loader=./packages/meta/src/node-es-module-loader/loader.mts ./packages/playground/cli/src/cli.ts server
WordPress Playground CLI

PHP 8.3  WordPress latest
Extensions intl

Running Blueprint 100%

Ready! WordPress is running on http://127.0.0.1:9400 (1 worker)
WordPress Playground CLI

PHP 8.3  WordPress latest
Extensions intl

Error: listen EADDRINUSE: address already in use :::9400

@mho22 mho22 force-pushed the return-cli-error-when-port-already-in-use branch from 870218a to dbc18b9 Compare February 3, 2026 15:18
@mho22 mho22 added [Type] Bug An existing feature does not function as intended [Type] UI / UX / User Experience [Package][@wp-playground] CLI labels Feb 3, 2026
@mho22 mho22 changed the title Return an error when given port is already in use [Playground CLI] Return an error when given port is already in use Feb 3, 2026
@fellyph
Copy link
Copy Markdown
Collaborator

fellyph commented Feb 3, 2026

I would like to have some guidance for users who are not familiar with the CLI yet, showing the flag --port as an alternative.

Can we set a message like:

Error: listen EADDRINUSE: address already in use :::9400. 

Use the --port flag to specify a different port. 

What do you think, @brandonpayton?

@mho22
Copy link
Copy Markdown
Collaborator Author

mho22 commented Feb 3, 2026

@fellyph @brandonpayton What do you think ?
Capture d’écran 2026-02-03 à 18 09 21

I’m not convinced about the newLine parameter :

printInfo(message: string, newLine = false): void {
	if (this.isQuiet) return;

	if (newLine) this.writeStream.write('\n');

	this.writeStream.write(`${this.cyan('Info:')} ${message}\n`);
}

Do you have any insights on it?

@mho22 mho22 changed the title [Playground CLI] Return an error when given port is already in use [Playground CLI] Return an readable error and hint when given port is already in use Feb 3, 2026
@mho22 mho22 changed the title [Playground CLI] Return an readable error and hint when given port is already in use [Playground CLI] Return a readable error and hint when given port is already in use Feb 3, 2026
@mho22
Copy link
Copy Markdown
Collaborator Author

mho22 commented Feb 4, 2026

I also prepared code to face these use cases :

If no port is mentioned and port 9400 is already in use, we run on a random free port. No error, hint or stack trace returned.
If a port is mentioned and that port is already in use, we return the error and hint without stack trace.

I'll create a new pull request with it once this one is merged.

@brandonpayton
Copy link
Copy Markdown
Member

brandonpayton commented Feb 9, 2026

I also prepared code to face these use cases :

If no port is mentioned and port 9400 is already in use, we run on a random free port. No error, hint or stack trace returned.
If a port is mentioned and that port is already in use, we return the error and hint without stack trace.

I'll create a new pull request with it once this one is merged.

@mho22 I think this is a good idea that could just be done in this PR. If we do this, we won't even need to tell users about the --port option because we only fail on a busy port when the --port option was used.

@brandonpayton
Copy link
Copy Markdown
Member

I’m not convinced about the newLine parameter :

I'm not convinced either. It seems to make sense that you'd only want to print an extra newline char when the info message itself will be printed, but it feels a bit entangled to have the function ask "Should I print an extra newline?"

Maybe printing info in this PR will be going away anyway, if we always auto-select an available port when --port is not specified.

Comment thread packages/playground/cli/src/run-cli.ts
@mho22
Copy link
Copy Markdown
Collaborator Author

mho22 commented Feb 9, 2026

@brandonpayton I removed the printInfo and implemented a isPortInUse function. I think we're good.

@brandonpayton
Copy link
Copy Markdown
Member

@brandonpayton I removed the printInfo and implemented a isPortInUse function. I think we're good.

@mho22 It looks like this PR will pick a random available port when the --port arg is included and the specified port is unavailable. Is that right? Since there is a default 9400 value for the port yargs declaration, I don't currently see a way to tell whether or not the port arg was provided.

What we want is the following:

If no port is mentioned and port 9400 is already in use, we run on a random free port. No error, hint or stack trace returned.
If a port is mentioned and that port is already in use, we return the error and hint without stack trace.

@mho22
Copy link
Copy Markdown
Collaborator Author

mho22 commented Feb 11, 2026

This is probably not very clear, I admit.

const selectedPort = args.command === 'server' ? (args.port ?? 9400) : 0;

...

port: args.port
	? args.port
	: !(await isPortInUse(selectedPort))
		? selectedPort
		: 0,
onError: (error: NodeJS.ErrnoException) =>
	cliOutput.printError(error.message),

If there's a args.port then use it [ and if it is already in use it will fail and return an error without stack trace ], else if the selectedPort is already in use, use port 0 [ random one in this case ].

screenshot

@brandonpayton
Copy link
Copy Markdown
Member

brandonpayton commented Feb 11, 2026

If there's a args.port then use it [ and if it is already in use it will fail and return an error without stack trace ], else if the selectedPort is already in use, use port 0 [ random one in this case ].

@mho22 Since we declare a default value of 9400 in the yargs declarations, won't yargs always assign to args.port? I might be missing something, but if not...

It would probably be good to add a couple of run-cli test for this, using parseOptionsAndRunCLI() which exercises yargs and will confirm the behaviors.

@mho22
Copy link
Copy Markdown
Collaborator Author

mho22 commented Feb 11, 2026

@brandonpayton I had to remove the default: 9400 to make this work.

It would probably be good to add a couple of run-cli test for this, using parseOptionsAndRunCLI() which exercises yargs and will confirm the behaviors.

On it.

@mho22
Copy link
Copy Markdown
Collaborator Author

mho22 commented Feb 16, 2026

@brandonpayton I've added tests and they ran successfully. 👍.

Copy link
Copy Markdown
Member

@brandonpayton brandonpayton left a comment

Choose a reason for hiding this comment

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

Thanks, @mho22! This looks good.

Before merging, would you please update the --port args declared descriptions to mention the port defaults to 9400 when available? It seems like it would be good to still document this, even though we had to remove the yargs default.

@mho22 mho22 force-pushed the return-cli-error-when-port-already-in-use branch from f076643 to aa0bc30 Compare February 23, 2026 10:48
@mho22 mho22 merged commit 3b54ff0 into trunk Feb 23, 2026
86 of 87 checks passed
@mho22 mho22 deleted the return-cli-error-when-port-already-in-use branch February 23, 2026 11:32
@fellyph
Copy link
Copy Markdown
Collaborator

fellyph commented Feb 24, 2026

@mho22, this check also works for npx @wp-playground/cli start ?

@mho22 mho22 restored the return-cli-error-when-port-already-in-use branch February 24, 2026 12:38
@mho22
Copy link
Copy Markdown
Collaborator Author

mho22 commented Feb 24, 2026

@fellyph I tried the following command and it worked as expected :

node --no-warnings --loader=./packages/meta/src/node-es-module-loader/loader.mts ./packages/playground/cli/src/cli.ts start
Capture d’écran 2026-02-24 à 13 52 28

@mho22 mho22 deleted the return-cli-error-when-port-already-in-use branch February 24, 2026 12:53
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

Projects

None yet

Development

Successfully merging this pull request may close these issues.

3 participants