Skip to content
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Serve on a random port when user provides --port 0
Closes #2599
  • Loading branch information
szabgab committed Apr 2, 2025
commit 8159b9b5bd4b90d10999d188b2d45b2547069566
8 changes: 7 additions & 1 deletion src/cmd/serve.rs
Original file line number Diff line number Diff line change
Expand Up @@ -54,7 +54,13 @@ pub fn execute(args: &ArgMatches) -> Result<()> {
let hostname = args.get_one::<String>("hostname").unwrap();
let open_browser = args.get_flag("open");

let address = format!("{hostname}:{port}");
let mut address = format!("{hostname}:{port}");

if port == "0" {
let listener = std::net::TcpListener::bind(address).unwrap();
let port = listener.local_addr().unwrap().port();
address = format!("{hostname}:{port}");
}
Comment on lines +57 to +63
Copy link
Member

Choose a reason for hiding this comment

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

Why not doing this when starting the server?


let update_config = |book: &mut MDBook| {
book.config
Expand Down