Skip to content

Cannot use serve --open with --port 0 #2599

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
koalp opened this issue Mar 18, 2025 · 1 comment · May be fixed by #2630
Open

Cannot use serve --open with --port 0 #2599

koalp opened this issue Mar 18, 2025 · 1 comment · May be fixed by #2630
Labels
C-bug Category: A bug, incorrect or unintended behavior Command-serve Command: serve

Comments

@koalp
Copy link

koalp commented Mar 18, 2025

Problem

When using the mdbook serve command with --port 0 and --open options, the opened page is wrong, as the port is 0 instead of the random port attributed by the system.

Image

Steps

  1. mdbook init abc --force
  2. mdbook serve abc --port 0 --open

Possible Solution(s)

The serving_url should contain the bound port instead of the asked port (0)

let serving_url = format!("http://{address}");

Maybe using something like bind_ephemeral could be used. It would maybe require to move the creation of the warp server in a separate function run before the thread spawn.

mdBook/src/cmd/serve.rs

Lines 88 to 90 in 3a8faba

let thread_handle = std::thread::spawn(move || {
serve(build_dir, sockaddr, reload_tx, &file_404);
});

Notes

No response

Version

mdbook v0.4.47
@koalp koalp added the C-bug Category: A bug, incorrect or unintended behavior label Mar 18, 2025
@ehuss ehuss added the Command-serve Command: serve label Mar 18, 2025
szabgab added a commit to szabgab/mdBook that referenced this issue Apr 2, 2025
@szabgab
Copy link
Contributor

szabgab commented Apr 2, 2025

I can confirm the problem on Linux and sending a PR fixing it.

@szabgab szabgab linked a pull request Apr 2, 2025 that will close this issue
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
C-bug Category: A bug, incorrect or unintended behavior Command-serve Command: serve
Projects
None yet
Development

Successfully merging a pull request may close this issue.

3 participants