Skip to content

The design language that makes your AI harness better at design.

JavaScript

Fix: live-server --background forwards its resolved root to the child (#1086) (#1089)

Abdul Wahabe7d0f583aaf7
diff --git a/crates/live/src/live_server.rs b/crates/live/src/live_server.rs
index bca4cd7dd..c7e834a5f 100644
--- a/crates/live/src/live_server.rs
+++ b/crates/live/src/live_server.rs
@@ -13,6 +13,7 @@ use crate::live_http::{
read_request_deadline, send_response, set_header_timeout, watch_close, Request, Response,
StreamResponse, Ticket, Turnstile, READ_REQUEST_DEADLINE,
};
+use crate::manifests::RootsManifest;
use crate::manual_edits::apply as manual_apply;
use crate::manual_edits::buffer as manual_buffer;
use crate::paths::{
@@ -81,6 +82,17 @@ Endpoints:
/status Durable recovery status (token-protected)
/health Health check";
+/// The detached child's argv. `--target` was consumed by `enter_live_root`;
+/// forward the resolved root so the child re-enters it instead of
+/// re-resolving from its new cwd (#1086).
+fn background_child_args(argv: &[String], roots: Option<&RootsManifest>) -> Vec<String> {
+ argv.iter()
+ .filter(|a| *a != "--background")
+ .cloned()
+ .chain(roots.map(|r| format!("--target={}", r.app_root)))
+ .collect()
+}
+
pub fn run(args: &[String], io: &mut Io) -> i32 {
let mut argv: Vec<String> = args.to_vec();
let roots = match enter_live_root(&mut argv, io) {
@@ -100,11 +112,7 @@ pub fn run(args: &[String], io: &mut Io) -> i32 {
}
if argv.iter().any(|a| a == "--background") {
- let child_args: Vec<String> = argv
- .iter()
- .filter(|a| *a != "--background")
- .cloned()
- .collect();
+ let child_args = background_child_args(&argv, roots.as_ref());
return match crate::server::spawn_detached_with_args(&cwd, &env, &child_args) {
Some(info) => {
println(io, &serde_json::to_string(&info).unwrap_or_default());
@@ -2988,3 +2996,42 @@ mod content_type_tests {
}
}
}
+
+#[cfg(test)]
+mod background_tests {
+ use super::*;
+
+ // #1086: from a static subdir of a workspace the parent falls back to the
+ // repo root, where a child resolving on its own would pick the sibling
+ // app. The child must land on the parent's root.
+ #[test]
+ fn background_child_re_enters_the_parents_root() {
+ let ws = std::env::temp_dir().join(format!("impeccable-bg-roots-{}", std::process::id()));
+ let _ = std::fs::remove_dir_all(&ws);
+ for (rel, body) in [
+ ("package.json", "{ \"name\": \"repro-root\", \"private\": true }\n"),
+ ("pnpm-workspace.yaml", "packages:\n - \"apps/*\"\n"),
+ ("apps/web/package.json", "{ \"name\": \"web\", \"private\": true }\n"),
+ ("apps/web/index.html", "<!doctype html><title>web</title>\n"),
+ ("static-mock/index.html", "<!doctype html><title>mock</title>\n"),
+ ] {
+ let abs = ws.join(rel);
+ std::fs::create_dir_all(abs.parent().unwrap()).unwrap();
+ std::fs::write(abs, body).unwrap();
+ }
+ std::fs::create_dir_all(ws.join(".git")).unwrap();
+ let env: Env = [("HOME".to_string(), "/nonexistent-home".to_string())]
+ .into_iter()
+ .collect();
+
+ let (mut parent, _) = Io::captured("", ws.join("static-mock"), env.clone());
+ let mut argv = vec!["--background".to_string()];
+ let roots = enter_live_root(&mut argv, &mut parent).unwrap();
+
+ let (mut child, _) = Io::captured("", parent.cwd.clone(), env);
+ let mut child_argv = background_child_args(&argv, roots.as_ref());
+ enter_live_root(&mut child_argv, &mut child).unwrap();
+ let _ = std::fs::remove_dir_all(&ws);
+ assert_eq!(child.cwd, parent.cwd);
+ }
+}
diff --git a/tests/oracle/cases/live-roots.mjs b/tests/oracle/cases/live-roots.mjs
index ac6ceca9e..81f9ed7ed 100644
--- a/tests/oracle/cases/live-roots.mjs
+++ b/tests/oracle/cases/live-roots.mjs
@@ -55,6 +55,11 @@ export default [
id: 'live-boot-full-cycle', verb: 'live', workspace: 'live-html', files: [...F, 'index.html', 'public/**'], normalize: PORT_NORM,
steps: [{ verb: 'live' }, { verb: 'live-inject', args: ['--check'] }, { verb: 'live-server', args: ['stop'] }],
},
+ {
+ id: 'live-server-background-static-subdir', verb: 'live-server', workspace: 'live-workspaces', cwd: 'static-mock', normalize: PORT_NORM,
+ setup: (ws) => { fakeGit(ws); rm(ws, 'apps/admin'); write(ws, 'static-mock/index.html', '<!doctype html><title>mock</title><main><h1>Mock</h1></main>\n'); },
+ steps: [{ args: ['--background'] }, { args: ['stop', '--keep-inject'] }],
+ },
// --- live-inject.mjs ---
{ id: 'live-inject-help', verb: 'live-inject', workspace: 'live-html', args: ['--help'] },
diff --git a/tests/oracle/golden/live-server-background-static-subdir.json b/tests/oracle/golden/live-server-background-static-subdir.json
new file mode 100644
index 000000000..32b3eb3ff
--- /dev/null
+++ b/tests/oracle/golden/live-server-background-static-subdir.json
@@ -0,0 +1,17 @@
+{
+ "steps": [
+ {
+ "stdout": "{\"pid\":<PID>,\"port\":<PORT>,\"token\":\"<UUID>\"}\n",
+ "stderr": "",
+ "exit": 0,
+ "signal": null
+ },
+ {
+ "stdout": "Stopped live server on port <PORT>.\n",
+ "stderr": "",
+ "exit": 0,
+ "signal": null
+ }
+ ],
+ "files": {}
+}