diff --git a/src/lib.rs b/src/lib.rs index bcb94d4..e9ded38 100644 --- a/src/lib.rs +++ b/src/lib.rs @@ -207,6 +207,10 @@ pub fn run_file(path: &Path, opts: &RunOptions) -> Result { let mut host = HostContext::for_run(Some(path), opts.host_caps(), opts.argv.clone()); host.set_entry_source(Some(path), &source); host.entry_metadata = module.metadata.clone(); + if let Some(root) = &opts.fs_root { + host.fs_root = Some(root.clone()); + host.cwd = root.clone(); + } let mut interp = Interpreter::new(Some(path), opts.trace_eval) .with_stdin(stdin_lines.clone()) .with_host(host); @@ -229,6 +233,10 @@ pub fn run_file(path: &Path, opts: &RunOptions) -> Result { let mut host = HostContext::for_run(Some(path), opts.host_caps(), opts.argv.clone()); host.set_entry_source(Some(path), &source); host.entry_metadata = module.metadata.clone(); + if let Some(root) = &opts.fs_root { + host.fs_root = Some(root.clone()); + host.cwd = root.clone(); + } let mut vm = Vm::new(Some(path)) .with_stdin(stdin_lines) .with_trace(opts.trace_eval) @@ -283,6 +291,7 @@ pub fn run_file_capture(path: &Path, opts: &RunOptions) -> Result { host.entry_metadata = module.metadata.clone(); if let Some(root) = &opts.fs_root { host.fs_root = Some(root.clone()); + host.cwd = root.clone(); } if let Some(lim) = opts.sleep_limit_ms { host.sleep_limit_ms = Some(lim); @@ -307,6 +316,7 @@ pub fn run_file_capture(path: &Path, opts: &RunOptions) -> Result { host.entry_metadata = module.metadata.clone(); if let Some(root) = &opts.fs_root { host.fs_root = Some(root.clone()); + host.cwd = root.clone(); } if let Some(lim) = opts.sleep_limit_ms { host.sleep_limit_ms = Some(lim); @@ -341,6 +351,7 @@ pub fn run_source(source: &str, opts: &RunOptions) -> Result { host.entry_metadata = module.metadata.clone(); if let Some(root) = &opts.fs_root { host.fs_root = Some(root.clone()); + host.cwd = root.clone(); } if let Some(lim) = opts.sleep_limit_ms { host.sleep_limit_ms = Some(lim); @@ -365,6 +376,7 @@ pub fn run_source(source: &str, opts: &RunOptions) -> Result { host.entry_metadata = module.metadata.clone(); if let Some(root) = &opts.fs_root { host.fs_root = Some(root.clone()); + host.cwd = root.clone(); } if let Some(lim) = opts.sleep_limit_ms { host.sleep_limit_ms = Some(lim); diff --git a/src/main.rs b/src/main.rs index e996cb3..3a4e722 100644 --- a/src/main.rs +++ b/src/main.rs @@ -78,6 +78,11 @@ enum Commands { #[arg(long = "bind", value_name = "KEY=VALUE")] binds: Vec, + /// Filesystem sandbox + process cwd root (default: entry file's directory). + /// Also read from env `MARQDO_FS_ROOT`. Use repo root when running `cli/…` entries. + #[arg(long = "fs-root", value_name = "DIR")] + fs_root: Option, + /// Emit diagnostics as JSON on stderr (machine-readable; AI/MLSP 面). #[arg(long)] json: bool, @@ -412,6 +417,7 @@ fn try_main(cli: Cli) -> Result { stdin_file, emit_result, binds, + fs_root, json: _, } => { let path = file.unwrap_or_else(|| PathBuf::from("index.mq.md")); @@ -423,11 +429,14 @@ fn try_main(cli: Cli) -> Result { for b in &binds { bind_pairs.push(marqdo::binding::parse_bind_kv(b)?); } + let env_fs_root = std::env::var_os("MARQDO_FS_ROOT").map(PathBuf::from); + let fs_root = fs_root.or(env_fs_root); let mut opts = if dump_all { RunOptions { stdin_lines: stdin_lines.clone(), emit_result: emit_result.clone(), binds: bind_pairs.clone(), + fs_root: fs_root.clone(), ..RunOptions::dump_all() } } else { @@ -442,6 +451,7 @@ fn try_main(cli: Cli) -> Result { stdin_lines, emit_result, binds: bind_pairs, + fs_root, ..RunOptions::default() } };