Skip to content

Commit

Permalink
feat: print Listening on messages on stderr instead of stdout (#25491)
Browse files Browse the repository at this point in the history
Fixes #25114

---------

Signed-off-by: Leo Kettmeir <crowlkats@toaxl.com>
Co-authored-by: Bartek Iwańczuk <biwanczuk@gmail.com>
Co-authored-by: crowlkats <crowlkats@toaxl.com>
Co-authored-by: Nathan Whitaker <17734409+nathanwhit@users.noreply.github.com>
  • Loading branch information
4 people authored Sep 14, 2024
1 parent af2d992 commit 597f2d8
Show file tree
Hide file tree
Showing 6 changed files with 30 additions and 44 deletions.
6 changes: 3 additions & 3 deletions ext/http/00_serve.ts
Original file line number Diff line number Diff line change
Expand Up @@ -646,7 +646,7 @@ function serve(arg1, arg2) {
options.onListen(listener.addr);
} else {
// deno-lint-ignore no-console
console.log(`Listening on ${path}`);
console.error(`Listening on ${path}`);
}
});
}
Expand Down Expand Up @@ -695,7 +695,7 @@ function serve(arg1, arg2) {
const host = formatHostName(addr.hostname);

// deno-lint-ignore no-console
console.log(`Listening on ${scheme}${host}:${addr.port}/`);
console.error(`Listening on ${scheme}${host}:${addr.port}/`);
}
};

Expand Down Expand Up @@ -871,7 +871,7 @@ function registerDeclarativeServer(exports) {
const host = formatHostName(hostname);

// deno-lint-ignore no-console
console.debug(
console.error(
`%cdeno serve%c: Listening on %chttp://${host}:${port}/%c${nThreads}`,
"color: green",
"color: inherit",
Expand Down
34 changes: 9 additions & 25 deletions runtime/js/99_main.js
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
// Remove Intl.v8BreakIterator because it is a non-standard API.
delete Intl.v8BreakIterator;

import * as internalConsole from "ext:deno_console/01_console.js";
import { core, internals, primordials } from "ext:core/mod.js";
const ops = core.ops;
import {
Expand Down Expand Up @@ -578,36 +579,19 @@ function bootstrapMainRuntime(runtimeOptions, warmup = false) {

if (mode === executionModes.serve) {
if (serveIsMain && serveWorkerCount) {
// deno-lint-ignore no-console
const origLog = console.log;
// deno-lint-ignore no-console
const origError = console.error;
const prefix = `[serve-worker-0 ]`;
// deno-lint-ignore no-console
console.log = (...args) => {
return origLog(prefix, ...new primordials.SafeArrayIterator(args));
};
// deno-lint-ignore no-console
console.error = (...args) => {
return origError(prefix, ...new primordials.SafeArrayIterator(args));
};
// deno-lint-ignore no-global-assign
console = new internalConsole.Console((msg, level) =>
core.print("[serve-worker-0 ] " + msg, level > 1)
);
} else if (serveWorkerCount !== null) {
// deno-lint-ignore no-console
const origLog = console.log;
// deno-lint-ignore no-console
const origError = console.error;
const base = `serve-worker-${serveWorkerCount + 1}`;
// 15 = "serve-worker-nn".length, assuming
// serveWorkerCount < 100
const prefix = `[${StringPrototypePadEnd(base, 15, " ")}]`;
// deno-lint-ignore no-console
console.log = (...args) => {
return origLog(prefix, ...new primordials.SafeArrayIterator(args));
};
// deno-lint-ignore no-console
console.error = (...args) => {
return origError(prefix, ...new primordials.SafeArrayIterator(args));
};
// deno-lint-ignore no-global-assign
console = new internalConsole.Console((msg, level) =>
core.print(`${prefix} ` + msg, level > 1)
);
}
}

Expand Down
12 changes: 7 additions & 5 deletions tests/integration/serve_tests.rs
Original file line number Diff line number Diff line change
Expand Up @@ -61,11 +61,13 @@ impl ServeClientBuilder {
fn new() -> Self {
Self(
util::deno_cmd()
.env("NO_COLOR", "1")
.current_dir(util::testdata_path())
.arg("serve")
.arg("--port")
.arg("0")
.stdout_piped(),
.stdout_piped()
.stderr_piped(),
None,
)
}
Expand Down Expand Up @@ -106,12 +108,12 @@ impl ServeClient {
fn output(self) -> String {
let mut child = self.child.borrow_mut();
child.kill().unwrap();
let mut stdout = child.stdout.take().unwrap();
let mut stderr = child.stderr.take().unwrap();
child.wait().unwrap();

let mut output_buf = self.output_buf.borrow_mut();

stdout.read_to_end(&mut output_buf).unwrap();
stderr.read_to_end(&mut output_buf).unwrap();

String::from_utf8(std::mem::take(&mut *output_buf)).unwrap()
}
Expand All @@ -128,7 +130,7 @@ impl ServeClient {
let mut buffer = self.output_buf.borrow_mut();
let mut temp_buf = [0u8; 64];
let mut child = self.child.borrow_mut();
let stdout = child.stdout.as_mut().unwrap();
let stderr = child.stderr.as_mut().unwrap();
let port_regex = regex::bytes::Regex::new(r":(\d+)").unwrap();

let start = std::time::Instant::now();
Expand All @@ -141,7 +143,7 @@ impl ServeClient {
String::from_utf8_lossy(&buffer)
);
}
let read = stdout.read(&mut temp_buf).unwrap();
let read = stderr.read(&mut temp_buf).unwrap();
buffer.extend_from_slice(&temp_buf[..read]);
if let Some(p) = port_regex
.captures(&buffer)
Expand Down
6 changes: 3 additions & 3 deletions tests/integration/watcher_tests.rs
Original file line number Diff line number Diff line change
Expand Up @@ -1359,16 +1359,16 @@ async fn test_watch_serve() {
.piped_output()
.spawn()
.unwrap();
let (mut stdout_lines, mut stderr_lines) = child_lines(&mut child);
let (mut _stdout_lines, mut stderr_lines) = child_lines(&mut child);

wait_contains("Listening on", &mut stdout_lines).await;
wait_contains("Listening on", &mut stderr_lines).await;
// Note that we start serving very quickly, so we specifically want to wait for this message
wait_contains(r#"Watching paths: [""#, &mut stderr_lines).await;

file_to_watch.write(file_content);

wait_contains("serving", &mut stderr_lines).await;
wait_contains("Listening on", &mut stdout_lines).await;
wait_contains("Listening on", &mut stderr_lines).await;

check_alive_then_kill(child);
}
Expand Down
4 changes: 2 additions & 2 deletions tests/testdata/serve/parallel.ts
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
console.log("starting serve");
console.error("starting serve");
export default {
fetch(_req: Request) {
console.log("serving request");
console.error("serving request");
return new Response("deno serve parallel");
},
};
12 changes: 6 additions & 6 deletions tests/unit/serve_test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -792,8 +792,8 @@ Deno.test(
async function httpServerDefaultOnListenCallback() {
const ac = new AbortController();

const consoleLog = console.log;
console.log = (msg) => {
const consoleError = console.error;
console.error = (msg) => {
try {
const match = msg.match(
/Listening on http:\/\/(localhost|0\.0\.0\.0):(\d+)\//,
Expand All @@ -818,7 +818,7 @@ Deno.test(

await server.finished;
} finally {
console.log = consoleLog;
console.error = consoleError;
}
},
);
Expand Down Expand Up @@ -875,8 +875,8 @@ Deno.test({ permissions: { net: true } }, async function ipv6Hostname() {
const ac = new AbortController();
let url = "";

const consoleLog = console.log;
console.log = (msg) => {
const consoleError = console.error;
console.error = (msg) => {
try {
const match = msg.match(/Listening on (http:\/\/(.*?):(\d+)\/)/);
assert(!!match, `Didn't match ${msg}`);
Expand All @@ -897,7 +897,7 @@ Deno.test({ permissions: { net: true } }, async function ipv6Hostname() {
assert(new URL(url), `Not a valid URL "${url}"`);
await server.shutdown();
} finally {
console.log = consoleLog;
console.error = consoleError;
}
});

Expand Down

0 comments on commit 597f2d8

Please sign in to comment.