pub use oxdock_diagnostics::{Diagnostic, DiagnosticResult, flatten_chain};
use oxdock_fs::{
GuardedPath, LazyGuardedTempDir, PathResolver, WorkspaceFs, discover_workspace_root,
env as oxdock_env, init_temp_gc,
};
#[cfg(windows)]
use oxdock_process::CommandBuilder;
use oxdock_process::{DefaultProcessManager, SharedInput};
use std::env;
use std::io::{self, IsTerminal, Read};
use std::sync::{Arc, Mutex};
use oxdock_core::ExecIo;
pub use oxdock_core::{
Engine, EngineOutput, ExecState, ExitRequest, FuncKind, FuncMeta, FuncParam, HostModule,
HostRegistration, NativeFn, OxDockFn, OxDockType, PureFn, PushManifestSink, StepCtx,
TypeDescriptor, Value, dry_run_closure, exit_code_of_diag, parse_script,
parse_script_with_modules, run_steps, run_steps_with_context, run_steps_with_context_result,
run_steps_with_lazy_snapshot_and_modules_and_entry, run_steps_with_manager_with_modules,
run_steps_with_manager_with_modules_and_entry, run_steps_without_snapshot,
};
pub use oxdock_parser::{Guard, Step, StepKind};
pub use oxdock_process::shell_program;
use std::collections::BTreeMap;
mod endpoints;
pub use endpoints::EndpointFlags;
mod errors;
pub use errors::{
endpoint_duplicate_inner, endpoint_duplicate_outer, feature_required, offline_conflict, usage,
};
#[cfg(feature = "mcp")]
mod mcp;
#[cfg(feature = "net")]
pub use endpoints::build_registry;
#[cfg(feature = "net")]
use oxdock_net_plugin::EndpointRegistry;
#[cfg(feature = "net")]
mod serve;
fn cli_host_modules() -> Vec<HostModule<DefaultProcessManager>> {
cli_host_modules_with(None)
}
#[cfg(feature = "net")]
fn cli_host_modules_for_serve() -> Vec<HostModule<DefaultProcessManager>> {
cli_host_modules_with(None)
}
#[cfg(feature = "net")]
fn remote_session_module_hash(entries: &[(String, Vec<String>)]) -> String {
oxdock_net_plugin::module_hash(entries)
}
fn cli_host_modules_with(
#[cfg(feature = "net")] registry: Option<&Arc<EndpointRegistry>>,
#[cfg(not(feature = "net"))] registry: Option<&Arc<()>>,
) -> Vec<HostModule<DefaultProcessManager>> {
#[cfg(any(feature = "net", feature = "markdown"))]
let mut modules: Vec<HostModule<DefaultProcessManager>> = Vec::new();
#[cfg(not(any(feature = "net", feature = "markdown")))]
let modules: Vec<HostModule<DefaultProcessManager>> = Vec::new();
#[cfg(feature = "net")]
{
let net_module = match registry {
Some(registry) => oxdock_net_plugin::module_with_endpoints(Arc::clone(registry)),
None => oxdock_net_plugin::module(),
};
modules.push(net_module);
#[cfg(feature = "ssh")]
{
let ssh_module = match registry {
Some(registry) => oxdock_ssh_plugin::module_with_endpoints(Arc::clone(registry)),
None => oxdock_ssh_plugin::module(),
};
modules.push(ssh_module);
}
}
#[cfg(feature = "markdown")]
modules.push(oxdock_markdown_plugin::module());
let _ = registry;
modules
}
fn cli_host_types() -> Vec<&'static TypeDescriptor> {
#[cfg(feature = "net")]
let net: &[&'static TypeDescriptor] = &[oxdock_net_plugin::NetListenerTag::descriptor()];
#[cfg(not(feature = "net"))]
let net: &[&'static TypeDescriptor] = &[];
#[cfg(feature = "ssh")]
let ssh: &[&'static TypeDescriptor] = &[
oxdock_ssh_plugin::SshServerTag::descriptor(),
oxdock_ssh_plugin::SshSessionTag::descriptor(),
];
#[cfg(not(feature = "ssh"))]
let ssh: &[&'static TypeDescriptor] = &[];
net.iter().chain(ssh.iter()).copied().collect()
}
fn run_cli_steps(
workspace_root: &GuardedPath,
steps: &[Step],
io: ExecIo,
#[cfg(feature = "net")] registry: Option<&Arc<EndpointRegistry>>,
#[cfg(not(feature = "net"))] registry: Option<&Arc<()>>,
entry_file: Option<GuardedPath>,
) -> DiagnosticResult<oxdock_core::exec::LazyRunOutput> {
run_steps_with_lazy_snapshot_and_modules_and_entry(
workspace_root,
steps,
io,
cli_host_modules_with(registry),
cli_host_types(),
entry_file,
)
}
fn parse_cli_script(script: &str) -> DiagnosticResult<Vec<Step>> {
parse_script_with_modules(script, cli_module_table()).map_err(|e| {
if let Some(unlinked) = unlinked_module_error(script) {
return unlinked;
}
e
})
}
fn unlinked_modules() -> Vec<(&'static str, &'static str)> {
let modules: &[(&'static str, &'static str)] = &[
#[cfg(not(feature = "net"))]
("NET", "net"),
#[cfg(not(feature = "ssh"))]
("SSH", "ssh"),
];
modules.to_vec()
}
fn unlinked_module_error(script: &str) -> Option<oxdock_diagnostics::Diagnostic> {
let unlinked = unlinked_modules();
if unlinked.is_empty() {
return None;
}
let toks = oxdock_parser::lex_str(script).ok()?;
for (module, feature) in unlinked {
if imports_module(&toks, module) {
return Some(errors::module_unlinked(module, feature));
}
}
None
}
fn imports_module(toks: &[oxdock_parser::Tok], module: &str) -> bool {
use oxdock_parser::{GroupDelim, TokKind};
let mut i = 0;
while i < toks.len() {
match &toks[i].kind {
TokKind::Word(w) if w == "IMPORT" => {
let mut j = i + 1;
while j < toks.len()
&& matches!(
toks[j].kind,
TokKind::Newline | TokKind::Comment(_) | TokKind::Semicolon
)
{
j += 1;
}
if let Some(next) = toks.get(j)
&& let TokKind::Group {
delim: GroupDelim::Bracket,
inner,
..
} = &next.kind
&& inner
.iter()
.any(|t| matches!(&t.kind, TokKind::Word(w) if w == module))
{
return true;
}
}
TokKind::Group { inner, .. } if imports_module(inner, module) => {
return true;
}
_ => {}
}
i += 1;
}
false
}
fn cli_module_table() -> oxdock_parser::ModuleTable {
let modules = cli_host_modules();
if modules.is_empty() {
return oxdock_core::std_module_table();
}
let mut engine = Engine::new();
for module in modules {
engine.register_module(module);
}
engine.module_table()
}
fn entry_file_of(script: &ScriptSource) -> Option<GuardedPath> {
match script {
ScriptSource::Path(path) => Some(path.clone()),
ScriptSource::Stdin => None,
}
}
fn print_dry_run(
steps: &[Step],
script: &ScriptSource,
workspace_root: &GuardedPath,
) -> DiagnosticResult<()> {
let table = cli_module_table();
let entry_file = entry_file_of(script);
{
let resolver = PathResolver::new_lazy(workspace_root.clone())?;
let fs: Box<dyn WorkspaceFs> = Box::new(resolver);
oxdock_core::validate_steps_with_fs_modules_and_entry(
fs,
steps,
cli_host_modules_with(None),
cli_host_types(),
entry_file.clone(),
)?;
}
let (label, base) = match script {
ScriptSource::Path(path) => (
path.display(),
path.parent().unwrap_or_else(|| workspace_root.clone()),
),
ScriptSource::Stdin => ("<stdin>".to_string(), workspace_root.clone()),
};
let resolver = PathResolver::new(workspace_root.as_path(), workspace_root.as_path())?;
let listing = dry_run_closure(steps, &label, &base, &resolver, &table)?;
for item in listing {
println!("{}:{} {}", item.file, item.step.line, item.step.kind);
}
Ok(())
}
pub fn run() -> DiagnosticResult<()> {
init_temp_gc();
let workspace_root = discover_workspace_root().map_err(|e| errors::guard_workspace_root(&e))?;
let mut args = std::env::args().skip(1);
let opts = match Options::parse(&mut args, &workspace_root) {
Ok(opts) => opts,
Err(err) if err.key() == "cli.version_requested" => {
println!("{err}");
return Ok(());
}
Err(err) if err.key() == "cli.help_requested" => {
print!("{err}");
return Ok(());
}
Err(err) => return Err(err),
};
#[cfg(feature = "net")]
if opts.remote_serve {
if opts.shell
|| !opts.remotes.is_empty()
|| !matches!(opts.script, ScriptSource::Stdin)
|| !opts.script_args.is_empty()
{
return Err(errors::remote_serve_runs_alone());
}
return serve::serve();
}
#[cfg(not(feature = "net"))]
if opts.remote_serve {
return Err(errors::feature_required("--remote-serve", "net"));
}
if opts.mcp {
#[cfg(feature = "mcp")]
{
if opts.shell || opts.remote_serve || !opts.remotes.is_empty() {
return Err(errors::mcp_runs_alone(&[
"--shell",
"--remote-serve",
"--remote",
]));
}
if !opts.endpoints.listens.is_empty() || !opts.endpoints.publishes.is_empty() {
return Err(errors::mcp_runs_alone(&["--listen/-p"]));
}
if opts.dry_run {
return Err(errors::mcp_runs_alone(&["--dry-run"]));
}
if !opts.script_args.is_empty() {
return Err(errors::mcp_runs_alone(&["script args"]));
}
return mcp::serve_mcp(opts, workspace_root);
}
#[cfg(not(feature = "mcp"))]
{
return Err(errors::feature_required("--mcp", "mcp"));
}
}
execute(opts, workspace_root)
}
pub fn process_exit_code(err: &Diagnostic) -> i32 {
exit_code_of_diag(err)
.map(|code| code.clamp(0, 255) as i32)
.unwrap_or(1)
}
#[derive(Debug, Clone)]
pub enum ScriptSource {
Path(GuardedPath),
Stdin,
}
fn reject_url_script(text: &str) -> DiagnosticResult<()> {
if text.contains("://") {
return Err(errors::script_not_local(text));
}
Ok(())
}
#[derive(Debug, Clone)]
pub struct Options {
pub script: ScriptSource,
pub shell: bool,
pub dry_run: bool,
pub endpoints: EndpointFlags,
pub remote_serve: bool,
pub mcp: bool,
pub remotes: Vec<(String, String)>,
pub script_args: Vec<String>,
}
impl Options {
pub fn parse(
args: &mut impl Iterator<Item = String>,
workspace_root: &GuardedPath,
) -> DiagnosticResult<Self> {
use lexopt::Arg::{Long, Short, Value};
let mut script: Option<ScriptSource> = None;
let mut shell = false;
let mut dry_run = false;
let mut endpoints = EndpointFlags::default();
let mut remote_serve = false;
let mut mcp = false;
#[cfg_attr(not(feature = "net"), allow(unused_mut))]
let mut remotes: Vec<(String, String)> = Vec::new();
let mut script_args: Vec<String> = Vec::new();
let drain_tail =
|parser: &mut lexopt::Parser, strip_leading: bool| -> DiagnosticResult<Vec<String>> {
let mut raw = parser.raw_args().map_err(|e| errors::arg_lex_error(&e))?;
let Some(first_os) = raw.next() else {
return Ok(Vec::new());
};
let first = value_string(first_os)?;
if strip_leading && first == "--" {
raw.map(value_string).collect()
} else {
std::iter::once(Ok(first))
.chain(raw.map(value_string))
.collect()
}
};
let mut parser = lexopt::Parser::from_args(args.by_ref());
while let Some(arg) = parser.next().map_err(|e| errors::arg_lex_error(&e))? {
match arg {
Long("script") => {
let path = value_string(
parser
.value()
.map_err(|_| errors::script_flag_needs_path())?,
)?;
if path.is_empty() {
return Err(errors::script_flag_needs_path());
}
if path == "-" {
script = Some(ScriptSource::Stdin);
} else {
reject_url_script(&path)?;
script = Some(ScriptSource::Path(
workspace_root
.join_script(&path)
.map_err(|e| errors::guard_script_path(&path, &e))?,
));
}
script_args = drain_tail(&mut parser, true)?;
break;
}
Long("shell") => {
shell = true;
}
Long("dry-run") => {
dry_run = true;
}
Long("listen") => {
let raw = value_string(
parser
.value()
.map_err(|_| errors::listen_requires_address())?,
)?;
#[cfg(not(feature = "net"))]
{
let _ = endpoints::parse_listen_arg(&raw);
return Err(errors::feature_required("--listen/-p", "net"));
}
#[cfg(feature = "net")]
endpoints.listens.push(endpoints::parse_listen_arg(&raw)?);
}
Short('p') => {
let raw = value_string(
parser
.value()
.map_err(|_| errors::publish_requires_shape(""))?,
)?;
#[cfg(not(feature = "net"))]
{
let _ = endpoints::parse_publish_arg(&raw);
return Err(errors::feature_required("--listen/-p", "net"));
}
#[cfg(feature = "net")]
endpoints
.publishes
.push(endpoints::parse_publish_arg(&raw)?);
}
Long("offline") => {
endpoints.offline = true;
}
Long("remote-serve") => {
remote_serve = true;
}
Long("mcp") => {
mcp = true;
}
Long("remote") => {
let raw = value_string(
parser
.value()
.map_err(|_| errors::remote_requires_value())?,
)?;
#[cfg(not(feature = "net"))]
{
let _ = oxdock_net_plugin_stub_parse_remote(&raw);
return Err(errors::feature_required("--remote", "net"));
}
#[cfg(feature = "net")]
remotes.push(oxdock_net_plugin::parse_remote_arg(&raw)?);
}
Long("help") | Short('h') => {
return Err(errors::help_requested());
}
Long("version") | Short('V') | Short('v') => {
return Err(errors::version_requested());
}
Value(value) => {
let text = value_string(value)?;
if text.is_empty() {
continue;
}
if text == "--" {
script = Some(ScriptSource::Stdin);
script_args = drain_tail(&mut parser, false)?;
break;
}
if text == "-" {
script = Some(ScriptSource::Stdin);
script_args = drain_tail(&mut parser, true)?;
break;
}
if text.starts_with('-') {
script = Some(ScriptSource::Stdin);
let mut tail = drain_tail(&mut parser, false)?;
tail.insert(0, text);
script_args = tail;
break;
}
reject_url_script(&text)?;
script = Some(ScriptSource::Path(
workspace_root
.join_script(&text)
.map_err(|e| errors::guard_script_path(&text, &e))?,
));
script_args = drain_tail(&mut parser, true)?;
break;
}
Long(other) => {
return Err(errors::unexpected_flag(other, true));
}
Short(other) => {
return Err(errors::unexpected_flag(&other.to_string(), false));
}
}
}
let script = script.unwrap_or(ScriptSource::Stdin);
Ok(Self {
script,
shell,
dry_run,
endpoints,
remote_serve,
mcp,
remotes,
script_args,
})
}
}
fn value_string(value: std::ffi::OsString) -> DiagnosticResult<String> {
value.into_string().map_err(|_| errors::arg_not_utf8())
}
pub fn version() -> String {
format!("oxdock {}", env!("CARGO_PKG_VERSION"))
}
pub fn execute(opts: Options, workspace_root: GuardedPath) -> DiagnosticResult<()> {
init_temp_gc();
execute_with_shell_runner(opts, workspace_root, run_shell, true)
}
pub struct ExecutionResult {
pub snapshot: Arc<LazyGuardedTempDir>,
pub final_cwd: GuardedPath,
pub bindings: BTreeMap<String, Value>,
}
impl ExecutionResult {
pub fn has_snapshot(&self) -> bool {
self.snapshot.is_materialized()
}
pub fn snapshot_path(&self) -> Option<&GuardedPath> {
self.snapshot.get()
}
}
pub fn execute_with_result(
opts: Options,
workspace_root: GuardedPath,
) -> DiagnosticResult<ExecutionResult> {
if opts.shell {
return Err(errors::shell_unsupported_in_result());
}
let script = read_script(&opts.script)?;
let mut final_cwd = workspace_root.clone();
let snapshot = Arc::new(LazyGuardedTempDir::new());
if !script.trim().is_empty() {
let steps = parse_cli_script(&script)?;
if opts.dry_run {
print_dry_run(&steps, &opts.script, &workspace_root)?;
return Ok(ExecutionResult {
snapshot,
final_cwd,
bindings: BTreeMap::new(),
});
}
#[cfg(feature = "net")]
let registry = build_registry(&opts.endpoints)?;
#[cfg(not(feature = "net"))]
check_no_net_endpoints(&opts.endpoints)?;
#[cfg(not(feature = "net"))]
check_no_net_remotes(&opts.remotes)?;
#[cfg(feature = "net")]
let inventory = {
let pairs = opts.remotes.clone();
let inventory = oxdock_net_plugin::RemoteInventory::build(pairs)?;
inventory.log_resolutions();
inventory.validate_script(&steps)?;
inventory
};
let entry_file = entry_file_of(&opts.script);
#[cfg(feature = "net")]
let io_cfg = {
let mut io_cfg = ExecIo::new();
io_cfg.set_script_args(opts.script_args.clone());
register_remote_runners(&mut io_cfg, &inventory);
io_cfg
};
#[cfg(not(feature = "net"))]
let io_cfg = {
let mut io_cfg = ExecIo::new();
io_cfg.set_script_args(opts.script_args.clone());
io_cfg
};
#[cfg(feature = "net")]
let output = run_cli_steps(&workspace_root, &steps, io_cfg, Some(®istry), entry_file)?;
#[cfg(not(feature = "net"))]
let output = run_cli_steps(&workspace_root, &steps, io_cfg, None, entry_file)?;
final_cwd = output.final_cwd;
return Ok(ExecutionResult {
snapshot: output.snapshot,
final_cwd,
bindings: output.bindings,
});
}
Ok(ExecutionResult {
snapshot,
final_cwd,
bindings: BTreeMap::new(),
})
}
#[cfg(feature = "net")]
fn register_remote_runners(io: &mut ExecIo, inventory: &oxdock_net_plugin::RemoteInventory) {
use std::sync::Arc;
let entries = surface_entries();
let module_hash = oxdock_net_plugin::module_hash(&entries);
let modules: Vec<String> = entries.iter().map(|(name, _)| name.clone()).collect();
for target in inventory.targets() {
let Some(argv) = inventory.argv(&target) else {
continue;
};
let config = oxdock_net_plugin::SessionConfig {
argv: argv.to_vec(),
oxdock_version: env!("CARGO_PKG_VERSION").to_string(),
modules: modules.clone(),
module_hash: module_hash.clone(),
};
io.set_remote_runner_for_target(
target,
Arc::new(oxdock_net_plugin::StdioSession::new(config)),
);
}
}
#[cfg(feature = "net")]
fn surface_entries() -> Vec<(String, Vec<String>)> {
let mut engine = Engine::new();
for module in cli_host_modules() {
engine.register_module(module);
}
let table = engine.module_table();
let mut entries: Vec<(String, Vec<String>)> = table
.modules
.into_iter()
.map(|(name, module_funcs)| {
let mut func_names: Vec<String> = module_funcs
.map(|surface| surface.func_names.into_iter().collect())
.unwrap_or_default();
func_names.sort();
(name, func_names)
})
.collect();
entries.sort();
entries
}
#[cfg(feature = "net")]
fn report_ephemeral_publishes(flags: &EndpointFlags, registry: &Arc<EndpointRegistry>) {
use oxdock_net_plugin::{EndpointKey, Protocol, parse_endpoint_ref};
for (outer, inner) in &flags.publishes {
if outer.port() != 0 {
continue;
}
let Ok((protocol, endpoint)) = parse_endpoint_ref(inner, "-p") else {
continue;
};
let key = EndpointKey {
protocol: protocol.unwrap_or(Protocol::Tcp),
endpoint,
};
match registry.resolve_endpoint(&key) {
Ok(addr) => eprintln!("oxdock: published {addr} -> {inner}"),
Err(_) => eprintln!("oxdock: published <unbound> -> {inner}"),
}
}
}
#[cfg(not(feature = "net"))]
fn oxdock_net_plugin_stub_parse_remote(raw: &str) -> DiagnosticResult<(String, String)> {
match raw.split_once('=') {
Some((target, command)) if !target.trim().is_empty() && !command.trim().is_empty() => {
Ok((target.trim().to_string(), command.to_string()))
}
_ => Err(errors::remote_requires_shape(raw)),
}
}
#[cfg(not(feature = "net"))]
fn check_no_net_remotes(remotes: &[(String, String)]) -> DiagnosticResult<()> {
if !remotes.is_empty() {
return Err(errors::feature_required("--remote", "net"));
}
Ok(())
}
#[cfg(not(feature = "net"))]
fn check_no_net_endpoints(flags: &EndpointFlags) -> DiagnosticResult<()> {
if !flags.listens.is_empty() || !flags.publishes.is_empty() {
return Err(errors::feature_required("--listen/-p", "net"));
}
Ok(())
}
fn read_script(source: &ScriptSource) -> DiagnosticResult<String> {
match source {
ScriptSource::Path(path) => {
let resolver = PathResolver::new(path.root(), path.root())?;
resolver
.read_to_string(path)
.map_err(|e| errors::read_script_at(&path.display().to_string(), &e))
}
ScriptSource::Stdin => {
let mut buf = String::new();
io::stdin()
.lock()
.read_to_string(&mut buf)
.map_err(|e| errors::read_script_stdin(&e))?;
Ok(buf)
}
}
}
fn execute_with_shell_runner<F>(
opts: Options,
workspace_root: GuardedPath,
shell_runner: F,
require_tty: bool,
) -> DiagnosticResult<()>
where
F: FnOnce(&GuardedPath, &GuardedPath) -> DiagnosticResult<()>,
{
#[cfg(windows)]
maybe_reexec_shell_to_temp(&opts)?;
let script = match &opts.script {
ScriptSource::Path(_) => read_script(&opts.script)?,
ScriptSource::Stdin => {
let stdin = io::stdin();
if stdin.is_terminal() {
if opts.shell {
String::new()
} else {
return Err(errors::script_stdin_missing());
}
} else {
let mut buf = String::new();
stdin
.lock()
.read_to_string(&mut buf)
.map_err(|e| errors::read_script_stdin(&e))?;
buf
}
}
};
let mut final_cwd = workspace_root.clone();
let mut snapshot = Arc::new(LazyGuardedTempDir::new());
let mut fs: Option<Box<dyn WorkspaceFs>> = None;
if !script.trim().is_empty() {
let steps = parse_cli_script(&script)?;
if opts.dry_run {
print_dry_run(&steps, &opts.script, &workspace_root)?;
return Ok(());
}
#[cfg(feature = "net")]
let registry = build_registry(&opts.endpoints)?;
#[cfg(not(feature = "net"))]
check_no_net_endpoints(&opts.endpoints)?;
#[cfg(not(feature = "net"))]
check_no_net_remotes(&opts.remotes)?;
#[cfg(feature = "net")]
report_ephemeral_publishes(&opts.endpoints, ®istry);
#[cfg(feature = "net")]
let inventory = {
let inventory = oxdock_net_plugin::RemoteInventory::build(opts.remotes.clone())?;
inventory.log_resolutions();
inventory.validate_script(&steps)?;
inventory
};
let mut stdin_handle: Option<SharedInput> = None;
if let ScriptSource::Path(_) = opts.script {
let stdin = io::stdin();
if !stdin.is_terminal() {
stdin_handle = Some(Arc::new(Mutex::new(stdin)));
}
}
let mut io_cfg = ExecIo::new();
io_cfg.set_stdin(stdin_handle);
io_cfg.set_script_args(opts.script_args.clone());
#[cfg(feature = "net")]
register_remote_runners(&mut io_cfg, &inventory);
let entry_file = entry_file_of(&opts.script);
#[cfg(feature = "net")]
let output = run_cli_steps(&workspace_root, &steps, io_cfg, Some(®istry), entry_file)?;
#[cfg(not(feature = "net"))]
let output = run_cli_steps(&workspace_root, &steps, io_cfg, None, entry_file)?;
final_cwd = output.final_cwd;
snapshot = output.snapshot;
fs = Some(output.fs);
}
if opts.shell {
if require_tty && !has_controlling_tty() {
return Err(errors::shell_needs_tty());
}
match fs.as_ref() {
Some(fs) => {
if fs.is_snapshot_pending() {
snapshot
.materialize()
.map_err(|e| errors::create_shell_temp_dir(&e))?;
}
final_cwd = fs.concretize_cwd(&final_cwd);
}
None => {
snapshot
.materialize()
.map_err(|e| errors::create_shell_temp_dir(&e))?;
final_cwd = snapshot
.get()
.cloned()
.expect("shell snapshot materialized above");
}
}
return shell_runner(&final_cwd, &workspace_root);
}
Ok(())
}
#[cfg(test)]
fn execute_for_test<F>(
opts: Options,
workspace_root: GuardedPath,
shell_runner: F,
) -> DiagnosticResult<()>
where
F: FnOnce(&GuardedPath, &GuardedPath) -> DiagnosticResult<()>,
{
execute_with_shell_runner(opts, workspace_root, shell_runner, false)
}
fn has_controlling_tty() -> bool {
#[cfg(unix)]
{
io::stdin().is_terminal() || io::stderr().is_terminal()
}
#[cfg(windows)]
{
io::stdin().is_terminal() || io::stderr().is_terminal()
}
#[cfg(not(any(unix, windows)))]
{
false
}
}
#[cfg(windows)]
fn maybe_reexec_shell_to_temp(opts: &Options) -> DiagnosticResult<()> {
if !opts.shell {
return Ok(());
}
if std::env::var(oxdock_env::oxdock::SHELL_REEXEC)
.ok()
.as_deref()
== Some("1")
{
return Ok(());
}
let self_path = std::env::current_exe().map_err(|e| errors::determine_current_exe(&e))?;
let base_temp = GuardedPath::new_root(std::env::temp_dir().as_path())
.map_err(|e| errors::guard_system_temp_dir(&e))?;
let ts = std::time::SystemTime::now()
.duration_since(std::time::UNIX_EPOCH)
.unwrap_or_default()
.as_millis();
let temp_file = base_temp
.join(&format!("oxdock-shell-{ts}-{}.exe", std::process::id()))
.map_err(|e| errors::construct_temp_shell_path(&e))?;
let temp_root_guard = temp_file
.parent()
.ok_or_else(errors::shell_temp_missing_parent)?;
let resolver_temp = PathResolver::new(temp_root_guard.as_path(), temp_root_guard.as_path())?;
let dest = temp_file;
#[allow(clippy::disallowed_types)]
let source = oxdock_fs::UnguardedPath::external(self_path);
resolver_temp
.copy_file_from_unguarded(&source, &dest)
.map_err(|e| errors::copy_shell_runner(&dest.display().to_string(), &e))?;
let mut cmd = CommandBuilder::new(dest.as_path());
cmd.args(std::env::args_os().skip(1));
cmd.env(oxdock_env::oxdock::SHELL_REEXEC, "1");
cmd.spawn()
.map_err(|e| errors::spawn_shell_from(&dest.display().to_string(), &e))?;
std::process::exit(0);
}
pub fn run_script(workspace_root: &GuardedPath, steps: &[Step]) -> DiagnosticResult<()> {
run_steps_with_context(workspace_root, workspace_root, steps)
}
fn shell_banner(cwd: &GuardedPath, workspace_root: &GuardedPath) -> String {
#[cfg(windows)]
let cwd_disp = oxdock_fs::command_path(cwd).as_ref().display().to_string();
#[cfg(windows)]
let workspace_disp = oxdock_fs::command_path(workspace_root)
.as_ref()
.display()
.to_string();
#[cfg(not(windows))]
let cwd_disp = cwd.display().to_string();
#[cfg(not(windows))]
let workspace_disp = workspace_root.display().to_string();
let pkg = env::var(oxdock_env::cargo::PKG_NAME)
.unwrap_or_else(|_| oxdock_env::oxdock::FALLBACK_APP_NAME.to_string());
indoc::formatdoc! {"
{pkg} shell workspace
cwd: {cwd_disp}
source: workspace root at {workspace_disp}
lifetime: temporary directory created for this shell session; it disappears when you exit
creation: temp workspace starts empty unless your script copies files into it
WARNING: This shell still runs on your host filesystem and is **not** isolated!
"}
}
fn run_shell(cwd: &GuardedPath, workspace_root: &GuardedPath) -> DiagnosticResult<()> {
oxdock_process::spawn_interactive_shell(cwd, workspace_root, &shell_banner(cwd, workspace_root))
}
#[cfg(test)]
mod tests {
use super::*;
use anyhow::Result;
use indoc::indoc;
use oxdock_fs::PathResolver;
use std::cell::{Cell, RefCell};
#[cfg_attr(
miri,
ignore = "GuardedPath::tempdir relies on OS tempdirs; blocked under Miri isolation"
)]
#[test]
fn shell_runner_receives_final_workdir() -> Result<()> {
let workspace = GuardedPath::tempdir()?;
let workspace_root = workspace.as_guarded_path().clone();
let script_path = workspace_root.join("script.ox")?;
let resolver = PathResolver::new(workspace_root.as_path(), workspace_root.as_path())?;
let script = indoc! {"
WRITE temp.txt 123
WORKDIR sub
"};
resolver.write_file(&script_path, script.as_bytes())?;
let opts = Options {
script: ScriptSource::Path(script_path),
shell: true,
dry_run: false,
endpoints: EndpointFlags::default(),
remote_serve: false,
mcp: false,
remotes: Vec::new(),
script_args: Vec::new(),
};
let observed = Cell::new(false);
execute_for_test(opts, workspace_root.clone(), |cwd, _| {
assert!(
cwd.as_path().ends_with("sub"),
"final cwd should end in WORKDIR target, got {}",
cwd.display()
);
let temp_root = GuardedPath::new_root(cwd.root())?;
let sub_dir = temp_root.join("sub")?;
assert_eq!(
cwd.as_path(),
sub_dir.as_path(),
"shell runner cwd should match guarded sub dir"
);
let temp_file = temp_root.join("temp.txt")?;
let temp_resolver = PathResolver::new(temp_root.as_path(), temp_root.as_path())?;
let contents = temp_resolver.read_to_string(&temp_file)?;
assert!(
contents.contains("123"),
"expected WRITE command to materialize temp file"
);
observed.set(true);
Ok(())
})?;
assert!(
observed.into_inner(),
"shell runner closure should have been invoked"
);
Ok(())
}
#[cfg_attr(
miri,
ignore = "GuardedPath::tempdir relies on OS tempdirs; blocked under Miri isolation"
)]
#[test]
fn options_parse_requires_script_path_value() {
let workspace = GuardedPath::tempdir().expect("tempdir");
let mut args = vec!["--script".to_string()].into_iter();
let err = Options::parse(&mut args, workspace.as_guarded_path())
.expect_err("expected missing path error");
assert!(err.to_string().contains("--script requires a path"));
}
#[cfg_attr(
miri,
ignore = "GuardedPath::tempdir relies on OS tempdirs; blocked under Miri isolation"
)]
#[test]
fn options_parse_script_flag_takes_dash_leading_path() {
let workspace = GuardedPath::tempdir().expect("tempdir");
let workspace_root = workspace.as_guarded_path().clone();
let mut args = vec!["--script".to_string(), "--weird.ox".to_string()].into_iter();
let opts = Options::parse(&mut args, &workspace_root).expect("parse");
match opts.script {
ScriptSource::Path(path) => assert_eq!(
path,
workspace_root.join("--weird.ox").expect("script path")
),
ScriptSource::Stdin => panic!("expected path script"),
}
assert!(opts.script_args.is_empty());
}
#[cfg_attr(
miri,
ignore = "GuardedPath::tempdir relies on OS tempdirs; blocked under Miri isolation"
)]
#[test]
fn options_parse_script_path_and_shell() {
let workspace = GuardedPath::tempdir().expect("tempdir");
let workspace_root = workspace.as_guarded_path().clone();
let script_path = workspace_root.join("script.txt").expect("script path");
let resolver = PathResolver::new(workspace_root.as_path(), workspace_root.as_path())
.expect("resolver");
resolver
.write_file(&script_path, b"WRITE out.txt hi")
.expect("write script");
let mut args = vec![
"--shell".to_string(),
"--script".to_string(),
"script.txt".to_string(),
]
.into_iter();
let opts = Options::parse(&mut args, &workspace_root).expect("parse");
assert!(opts.shell);
match opts.script {
ScriptSource::Path(path) => assert_eq!(path, script_path),
ScriptSource::Stdin => panic!("expected path script"),
}
assert!(opts.script_args.is_empty());
let mut args = vec![
"--script".to_string(),
"script.txt".to_string(),
"--shell".to_string(),
]
.into_iter();
let opts = Options::parse(&mut args, &workspace_root).expect("parse");
assert!(
!opts.shell,
"--shell after the script is data, not the interpreter flag"
);
assert_eq!(opts.script_args, vec!["--shell"]);
}
#[cfg_attr(
miri,
ignore = "GuardedPath::tempdir relies on OS tempdirs; blocked under Miri isolation"
)]
#[test]
fn options_parse_mcp_flag() {
let workspace = GuardedPath::tempdir().expect("tempdir");
let workspace_root = workspace.as_guarded_path().clone();
let mut args = vec!["--mcp".to_string(), "tools.oxdock".to_string()].into_iter();
let opts = Options::parse(&mut args, &workspace_root).expect("parse");
assert!(opts.mcp);
match opts.script {
ScriptSource::Path(path) => assert_eq!(
path,
workspace_root.join("tools.oxdock").expect("script path")
),
ScriptSource::Stdin => panic!("expected path script"),
}
}
#[cfg_attr(
miri,
ignore = "GuardedPath::tempdir relies on OS tempdirs; blocked under Miri isolation"
)]
#[test]
fn options_parse_mcp_defaults_off() {
let workspace = GuardedPath::tempdir().expect("tempdir");
let workspace_root = workspace.as_guarded_path().clone();
let mut args = vec!["script.txt".to_string()].into_iter();
let opts = Options::parse(&mut args, &workspace_root).expect("parse");
assert!(!opts.mcp);
}
#[cfg_attr(
miri,
ignore = "GuardedPath::tempdir relies on OS tempdirs; blocked under Miri isolation"
)]
#[test]
fn options_parse_positional_script_path() {
let workspace = GuardedPath::tempdir().expect("tempdir");
let workspace_root = workspace.as_guarded_path().clone();
let mut args = vec!["script.txt".to_string()].into_iter();
let opts = Options::parse(&mut args, &workspace_root).expect("parse");
assert!(!opts.shell);
match opts.script {
ScriptSource::Path(path) => assert_eq!(
path,
workspace_root.join("script.txt").expect("script path")
),
ScriptSource::Stdin => panic!("expected path script"),
}
}
#[cfg_attr(
miri,
ignore = "GuardedPath::tempdir relies on OS tempdirs; blocked under Miri isolation"
)]
#[test]
fn options_parse_positional_dash_reads_stdin() {
let workspace = GuardedPath::tempdir().expect("tempdir");
let mut args = vec!["-".to_string()].into_iter();
let opts = Options::parse(&mut args, workspace.as_guarded_path()).expect("parse");
assert!(matches!(opts.script, ScriptSource::Stdin));
}
#[cfg_attr(
miri,
ignore = "GuardedPath::tempdir relies on OS tempdirs; blocked under Miri isolation"
)]
#[test]
fn options_parse_second_positional_is_script_arg() {
let workspace = GuardedPath::tempdir().expect("tempdir");
let workspace_root = workspace.as_guarded_path().clone();
let mut args = vec![
"a.ox".to_string(),
"--script".to_string(),
"b.ox".to_string(),
]
.into_iter();
let opts = Options::parse(&mut args, &workspace_root).expect("parse");
match &opts.script {
ScriptSource::Path(path) => assert_eq!(
path,
&workspace_root.join("a.ox").expect("script path"),
"first positional fixes the script"
),
ScriptSource::Stdin => panic!("expected path script"),
}
assert_eq!(opts.script_args, vec!["--script", "b.ox"]);
let mut args = vec!["a.ox".to_string(), "b.ox".to_string()].into_iter();
let opts = Options::parse(&mut args, &workspace_root).expect("parse");
assert_eq!(opts.script_args, vec!["b.ox"]);
}
#[cfg_attr(
miri,
ignore = "GuardedPath::tempdir relies on OS tempdirs; blocked under Miri isolation"
)]
#[test]
fn options_parse_absolute_script_outside_execution_workspace_loads() {
let workspace = GuardedPath::tempdir().expect("tempdir");
let outside = GuardedPath::tempdir().expect("outside");
let target = outside
.as_guarded_path()
.as_path()
.join("step.ox")
.to_string_lossy()
.into_owned();
let mut args = vec!["--script".to_string(), target.clone()].into_iter();
let opts = Options::parse(&mut args, workspace.as_guarded_path()).expect("parse");
match opts.script {
ScriptSource::Path(path) => {
assert_eq!(path.as_path().to_string_lossy(), target);
assert_eq!(path.root(), outside.as_guarded_path().as_path());
}
ScriptSource::Stdin => panic!("absolute script must not parse as stdin"),
}
}
#[cfg_attr(
miri,
ignore = "GuardedPath::tempdir relies on OS tempdirs; blocked under Miri isolation"
)]
#[test]
fn options_parse_relative_escape_stays_rejected() {
let workspace = GuardedPath::tempdir().expect("tempdir");
let mut args = vec!["--script".to_string(), "../escape.ox".to_string()].into_iter();
let err =
Options::parse(&mut args, workspace.as_guarded_path()).expect_err("escape must fail");
assert!(err.to_string().contains("guard script path"), "{err:?}");
}
#[cfg_attr(
miri,
ignore = "GuardedPath::tempdir relies on OS tempdirs; blocked under Miri isolation"
)]
#[test]
fn options_parse_rejects_unknown_flags() {
let workspace = GuardedPath::tempdir().expect("tempdir");
let mut args = vec!["--frobnicate".to_string()].into_iter();
let err = Options::parse(&mut args, workspace.as_guarded_path())
.expect_err("expected unknown flag error");
assert!(err.to_string().contains("unexpected flag"), "{err:?}");
}
#[test]
fn usage_describes_positional_script_and_help() {
let text = usage();
assert!(text.contains("Usage: oxdock"), "{text}");
assert!(text.contains("SCRIPT"), "{text}");
assert!(text.contains("--script"), "{text}");
assert!(text.contains("--help"), "{text}");
assert!(text.contains("--offline"), "{text}");
#[cfg(feature = "net")]
{
assert!(text.contains("--listen"), "{text}");
assert!(text.contains("-p <[host:]outer:inner>"), "{text}");
}
#[cfg(not(feature = "net"))]
{
assert!(!text.contains("--listen <addr>"), "{text}");
assert!(!text.contains("-p <[host:]outer:inner>"), "{text}");
assert!(text.contains("require the `net` feature"), "{text}");
}
assert!(text.contains(env!("CARGO_PKG_DESCRIPTION")), "{text}");
}
#[cfg(feature = "net")]
#[cfg_attr(
miri,
ignore = "GuardedPath::tempdir relies on OS tempdirs; blocked under Miri isolation"
)]
#[test]
fn options_parse_endpoint_flags() {
let workspace = GuardedPath::tempdir().expect("tempdir");
let mut args = vec![
"--listen".to_string(),
"0.0.0.0:2251".to_string(),
"-p".to_string(),
"2222:demo-proxy".to_string(),
"-p".to_string(),
"0:2252".to_string(),
"-".to_string(),
]
.into_iter();
let opts = Options::parse(&mut args, workspace.as_guarded_path()).expect("parse");
assert_eq!(opts.endpoints.listens.len(), 1);
assert_eq!(opts.endpoints.publishes.len(), 2);
assert!(!opts.endpoints.offline);
assert!(matches!(opts.script, ScriptSource::Stdin));
}
#[cfg_attr(
miri,
ignore = "GuardedPath::tempdir relies on OS tempdirs; blocked under Miri isolation"
)]
#[test]
fn options_parse_offline_flag() {
let workspace = GuardedPath::tempdir().expect("tempdir");
let mut args = vec!["--offline".to_string(), "-".to_string()].into_iter();
let opts = Options::parse(&mut args, workspace.as_guarded_path()).expect("parse");
assert!(opts.endpoints.offline);
}
#[cfg(feature = "net")]
#[cfg_attr(
miri,
ignore = "GuardedPath::tempdir relies on OS tempdirs; blocked under Miri isolation"
)]
#[test]
fn options_parse_rejects_bad_endpoint_flags() {
let workspace = GuardedPath::tempdir().expect("tempdir");
for args in [
vec!["--listen"],
vec!["--listen", "0.0.0.0:0"],
vec!["-p"],
vec!["-p", "2222"],
vec!["-p", "2222:0"],
] {
let mut args = args.into_iter().map(str::to_string);
Options::parse(&mut args, workspace.as_guarded_path())
.expect_err("bad endpoint flag must fail");
}
}
#[cfg_attr(
miri,
ignore = "GuardedPath::tempdir relies on OS tempdirs; blocked under Miri isolation"
)]
#[test]
fn options_parse_empty_values_hold_token_boundaries() {
let workspace = GuardedPath::tempdir().expect("tempdir");
let mut args = vec![
"--script".to_string(),
"".to_string(),
"--shell".to_string(),
]
.into_iter();
let err = Options::parse(&mut args, workspace.as_guarded_path())
.expect_err("empty script path must fail");
assert!(
err.to_string().contains("--script requires a path"),
"{err:?}"
);
let mut args = vec!["".to_string(), "-".to_string()].into_iter();
let opts = Options::parse(&mut args, workspace.as_guarded_path()).expect("parse");
assert!(matches!(opts.script, ScriptSource::Stdin));
}
#[cfg(feature = "net")]
#[cfg_attr(
miri,
ignore = "GuardedPath::tempdir relies on OS tempdirs; blocked under Miri isolation"
)]
#[test]
fn options_parse_accepts_equals_and_attached_forms() {
let workspace = GuardedPath::tempdir().expect("tempdir");
let workspace_root = workspace.as_guarded_path().clone();
let mut args = vec![
"--listen=0.0.0.0:2251".to_string(),
"-p2222:demo-proxy".to_string(),
"--".to_string(),
"script.ox".to_string(),
]
.into_iter();
let opts = Options::parse(&mut args, &workspace_root).expect("parse");
assert_eq!(opts.endpoints.listens.len(), 1);
assert_eq!(opts.endpoints.publishes.len(), 1);
match opts.script {
ScriptSource::Path(path) => {
assert_eq!(path, workspace_root.join("script.ox").expect("script path"))
}
ScriptSource::Stdin => panic!("expected path script after --"),
}
assert!(opts.script_args.is_empty());
}
#[cfg_attr(
miri,
ignore = "GuardedPath::tempdir relies on OS tempdirs; blocked under Miri isolation"
)]
#[test]
fn options_parse_routes_verbatim_tail_to_script_args() {
let workspace = GuardedPath::tempdir().expect("tempdir");
let workspace_root = workspace.as_guarded_path().clone();
let mut args = vec![
"my.ox".to_string(),
"--dry-run".to_string(),
"--listen".to_string(),
"0.0.0.0:2251".to_string(),
]
.into_iter();
let opts = Options::parse(&mut args, &workspace_root).expect("parse");
assert_eq!(
opts.script_args,
vec!["--dry-run", "--listen", "0.0.0.0:2251"]
);
let mut args = vec![
"my.ox".to_string(),
"--".to_string(),
"--dry-run".to_string(),
]
.into_iter();
let opts = Options::parse(&mut args, &workspace_root).expect("parse");
assert_eq!(opts.script_args, vec!["--dry-run"]);
let mut args = vec![
"--script".to_string(),
"my.ox".to_string(),
"--".to_string(),
"a".to_string(),
"--".to_string(),
"b".to_string(),
]
.into_iter();
let opts = Options::parse(&mut args, &workspace_root).expect("parse");
assert_eq!(opts.script_args, vec!["a", "--", "b"]);
}
#[cfg_attr(
miri,
ignore = "GuardedPath::tempdir relies on OS tempdirs; blocked under Miri isolation"
)]
#[test]
fn options_parse_help_after_script_is_data() {
let workspace = GuardedPath::tempdir().expect("tempdir");
let workspace_root = workspace.as_guarded_path().clone();
let mut args = vec!["my.ox".to_string(), "--help".to_string()].into_iter();
let opts = Options::parse(&mut args, &workspace_root).expect("parse");
assert_eq!(opts.script_args, vec!["--help"]);
let mut args = vec!["my.ox".to_string(), "--version".to_string()].into_iter();
let opts = Options::parse(&mut args, &workspace_root).expect("parse");
assert_eq!(opts.script_args, vec!["--version"]);
}
#[cfg_attr(
miri,
ignore = "GuardedPath::tempdir relies on OS tempdirs; blocked under Miri isolation"
)]
#[test]
fn options_parse_dash_leading_after_separator_is_stdin_args() {
let workspace = GuardedPath::tempdir().expect("tempdir");
let workspace_root = workspace.as_guarded_path().clone();
let mut args = vec!["--".to_string(), "--foo".to_string()].into_iter();
let opts = Options::parse(&mut args, &workspace_root).expect("parse");
assert!(matches!(opts.script, ScriptSource::Stdin));
assert_eq!(opts.script_args, vec!["--foo"]);
let mut args = vec!["--".to_string(), "script.ox".to_string()].into_iter();
let opts = Options::parse(&mut args, &workspace_root).expect("parse");
match opts.script {
ScriptSource::Path(path) => {
assert_eq!(path, workspace_root.join("script.ox").expect("script path"))
}
ScriptSource::Stdin => panic!("expected path script"),
}
assert!(opts.script_args.is_empty());
let mut args = vec!["--".to_string(), "--".to_string(), "a".to_string()].into_iter();
let opts = Options::parse(&mut args, &workspace_root).expect("parse");
assert!(matches!(opts.script, ScriptSource::Stdin));
assert_eq!(opts.script_args, vec!["a"]);
}
#[cfg_attr(
miri,
ignore = "GuardedPath::tempdir relies on OS tempdirs; blocked under Miri isolation"
)]
#[test]
fn options_parse_preserves_inner_separators_in_tail() {
let workspace = GuardedPath::tempdir().expect("tempdir");
let workspace_root = workspace.as_guarded_path().clone();
let mut args = vec![
"--".to_string(),
"--foo".to_string(),
"--".to_string(),
"--bar".to_string(),
]
.into_iter();
let opts = Options::parse(&mut args, &workspace_root).expect("parse");
assert!(matches!(opts.script, ScriptSource::Stdin));
assert_eq!(opts.script_args, vec!["--foo", "--", "--bar"]);
let mut args = vec![
"my.ox".to_string(),
"a".to_string(),
"--".to_string(),
"b".to_string(),
]
.into_iter();
let opts = Options::parse(&mut args, &workspace_root).expect("parse");
assert_eq!(opts.script_args, vec!["a", "--", "b"]);
}
#[cfg_attr(
miri,
ignore = "GuardedPath::tempdir relies on OS tempdirs; blocked under Miri isolation"
)]
#[test]
fn options_parse_version_returns_version_without_exiting() {
let workspace = GuardedPath::tempdir().expect("tempdir");
for flag in ["--version", "-V", "-v"] {
let mut args = vec![flag.to_string()].into_iter();
let err = Options::parse(&mut args, workspace.as_guarded_path())
.expect_err("version flag must not parse as options");
assert_eq!(err.key(), "cli.version_requested");
assert_eq!(err.to_string(), version());
}
}
#[cfg_attr(
miri,
ignore = "GuardedPath::tempdir relies on OS tempdirs; blocked under Miri isolation"
)]
#[test]
fn options_parse_help_returns_usage_error_without_exiting() {
let workspace = GuardedPath::tempdir().expect("tempdir");
for flag in ["--help", "-h"] {
let mut args = vec![flag.to_string()].into_iter();
let err = Options::parse(&mut args, workspace.as_guarded_path())
.expect_err("help flag must not parse as options");
assert_eq!(err.key(), "cli.help_requested");
assert_eq!(err.to_string(), usage());
}
}
#[cfg_attr(
miri,
ignore = "GuardedPath::tempdir relies on OS tempdirs; blocked under Miri isolation"
)]
#[test]
fn options_parse_rejects_remote_script_with_input_intact() {
let workspace = GuardedPath::tempdir().expect("tempdir");
for args in [
vec!["https://example.com/process.oxfile".to_string()],
vec![
"--script".to_string(),
"https://example.com/process.oxfile".to_string(),
],
] {
let err = Options::parse(&mut args.into_iter(), workspace.as_guarded_path())
.expect_err("remote script must not parse as a path");
let rendered = format!("{err:#}");
assert!(
rendered.contains("not a local path")
&& rendered.contains("https://example.com/process.oxfile"),
"rejection names the intact input: {rendered}"
);
assert!(
!rendered.contains("https:/example.com"),
"scheme must never come out mangled: {rendered}"
);
}
}
#[test]
fn execute_with_result_runs_script() {
let workspace = GuardedPath::tempdir().expect("tempdir");
let workspace_root = workspace.as_guarded_path().clone();
let script_path = workspace_root.join("script.txt").expect("script path");
let resolver = PathResolver::new(workspace_root.as_path(), workspace_root.as_path())
.expect("resolver");
resolver
.write_file(&script_path, b"WRITE out.txt hi")
.expect("write script");
let opts = Options {
script: ScriptSource::Path(script_path),
shell: false,
dry_run: false,
endpoints: EndpointFlags::default(),
remote_serve: false,
mcp: false,
remotes: Vec::new(),
script_args: Vec::new(),
};
let result = execute_with_result(opts, workspace_root).expect("execute");
let snapshot = result
.snapshot_path()
.expect("default WRITE materializes the snapshot");
assert_eq!(snapshot, &result.final_cwd);
let temp_resolver = PathResolver::new(snapshot.root(), snapshot.root()).expect("resolver");
let out = snapshot.join("out.txt").expect("out path");
let contents = temp_resolver.read_to_string(&out).expect("read out");
assert_eq!(contents.trim(), "hi");
}
#[cfg_attr(
miri,
ignore = "GuardedPath::tempdir relies on OS tempdirs; blocked under Miri isolation"
)]
#[test]
fn execute_with_result_routes_script_args_to_argv() {
let workspace = GuardedPath::tempdir().expect("tempdir");
let workspace_root = workspace.as_guarded_path().clone();
let script_path = workspace_root.join("script.txt").expect("script path");
let resolver = PathResolver::new(workspace_root.as_path(), workspace_root.as_path())
.expect("resolver");
let script = indoc! {r#"
IMPORT [STD]
LET $args: LIST<STRING> = ARGV()
LET $n: INT = LEN($args)
ASSERT_EQ $n 2
ASSERT_EQ $args.0 "--dry-run"
ASSERT_EQ $args.1 "serve"
WRITE out.txt "ok"
"#};
resolver
.write_file(&script_path, script.as_bytes())
.expect("write script");
let mut args = vec![
"script.txt".to_string(),
"--".to_string(),
"--dry-run".to_string(),
"serve".to_string(),
]
.into_iter();
let opts = Options::parse(&mut args, &workspace_root).expect("parse");
assert_eq!(opts.script_args, vec!["--dry-run", "serve"]);
let result = execute_with_result(opts, workspace_root).expect("execute");
let snapshot = result
.snapshot_path()
.expect("WRITE materializes the snapshot");
let temp_resolver = PathResolver::new(snapshot.root(), snapshot.root()).expect("resolver");
let out = snapshot.join("out.txt").expect("out path");
assert_eq!(
temp_resolver.read_to_string(&out).expect("read").trim(),
"ok"
);
}
#[cfg_attr(
miri,
ignore = "GuardedPath::tempdir relies on OS tempdirs; blocked under Miri isolation"
)]
#[test]
fn execute_with_result_local_only_creates_no_snapshot() {
let workspace = GuardedPath::tempdir().expect("tempdir");
let workspace_root = workspace.as_guarded_path().clone();
let script_path = workspace_root.join("script.txt").expect("script path");
let resolver = PathResolver::new(workspace_root.as_path(), workspace_root.as_path())
.expect("resolver");
resolver
.write_file(&script_path, b"WORKSPACE LOCAL\nWRITE out.txt hi")
.expect("write script");
let opts = Options {
script: ScriptSource::Path(script_path),
shell: false,
dry_run: false,
endpoints: EndpointFlags::default(),
remote_serve: false,
mcp: false,
remotes: Vec::new(),
script_args: Vec::new(),
};
let result = execute_with_result(opts, workspace_root.clone()).expect("execute");
assert!(
!result.has_snapshot(),
"WORKSPACE LOCAL-only script must not create a snapshot tempdir"
);
assert!(result.snapshot_path().is_none());
let out = workspace_root.join("out.txt").expect("out path");
let contents = resolver.read_to_string(&out).expect("read out");
assert_eq!(contents.trim(), "hi");
assert_eq!(result.final_cwd.root(), workspace_root.as_path());
}
#[cfg_attr(
miri,
ignore = "GuardedPath::tempdir relies on OS tempdirs; blocked under Miri isolation"
)]
#[test]
fn execute_with_result_empty_script_creates_no_snapshot() {
let workspace = GuardedPath::tempdir().expect("tempdir");
let workspace_root = workspace.as_guarded_path().clone();
let script_path = workspace_root.join("empty.txt").expect("script path");
let resolver = PathResolver::new(workspace_root.as_path(), workspace_root.as_path())
.expect("resolver");
resolver
.write_file(&script_path, b"")
.expect("write script");
let opts = Options {
script: ScriptSource::Path(script_path),
shell: false,
dry_run: false,
endpoints: EndpointFlags::default(),
remote_serve: false,
mcp: false,
remotes: Vec::new(),
script_args: Vec::new(),
};
let result = execute_with_result(opts, workspace_root.clone()).expect("execute");
assert!(
!result.has_snapshot(),
"empty script must not create a snapshot tempdir"
);
assert_eq!(result.final_cwd, workspace_root);
}
#[cfg_attr(
miri,
ignore = "GuardedPath::tempdir relies on OS tempdirs; blocked under Miri isolation"
)]
#[test]
fn execute_for_test_invokes_shell_runner() -> Result<()> {
let workspace = GuardedPath::tempdir()?;
let workspace_root = workspace.as_guarded_path().clone();
let script_path = workspace_root.join("empty.txt")?;
let resolver = PathResolver::new(workspace_root.as_path(), workspace_root.as_path())?;
resolver.write_file(&script_path, b"")?;
let opts = Options {
script: ScriptSource::Path(script_path),
shell: true,
dry_run: false,
endpoints: EndpointFlags::default(),
remote_serve: false,
mcp: false,
remotes: Vec::new(),
script_args: Vec::new(),
};
let called = RefCell::new(None::<(String, String)>);
execute_for_test(opts, workspace_root.clone(), |cwd, workspace| {
called.replace(Some((cwd.display(), workspace.display())));
assert!(
cwd.exists(),
"shell cwd must exist on disk, got {}",
cwd.display()
);
Ok(())
})?;
let seen = called.borrow().clone().expect("shell runner called");
assert_eq!(seen.1, workspace_root.display());
Ok(())
}
#[cfg(feature = "ssh")]
#[cfg_attr(
miri,
ignore = "loopback TCP plus threads plus a Tokio runtime; also GuardedPath::tempdir"
)]
#[test]
fn ssh_feature_serves_and_closes() -> Result<()> {
let workspace = GuardedPath::tempdir()?;
let workspace_root = workspace.as_guarded_path().clone();
let script_path = workspace_root.join("ssh-serve.ox")?;
let resolver = PathResolver::new(workspace_root.as_path(), workspace_root.as_path())?;
let script = indoc! {"
IMPORT [STD, SSH]
LET $m: SSH_SERVE_INFO = SSH_SERVE(\"cli-ssh-svc\", {username: \"test\", password: \"test123\"})
SSH_CLOSE($m.server)
"};
resolver.write_file(&script_path, script.as_bytes())?;
let opts = Options {
script: ScriptSource::Path(script_path),
shell: false,
dry_run: false,
endpoints: EndpointFlags {
publishes: vec![(
std::net::SocketAddr::from(([127, 0, 0, 1], 0)),
"cli-ssh-svc".to_string(),
)],
..EndpointFlags::default()
},
remote_serve: false,
mcp: false,
remotes: Vec::new(),
script_args: Vec::new(),
};
execute_with_result(opts, workspace_root)?;
Ok(())
}
#[cfg(feature = "net")]
#[cfg_attr(
miri,
ignore = "loopback TCP plus GuardedPath::tempdir; blocked under Miri isolation"
)]
#[test]
fn net_module_listens_and_closes() -> Result<()> {
let workspace = GuardedPath::tempdir()?;
let workspace_root = workspace.as_guarded_path().clone();
let script_path = workspace_root.join("net-listen.ox")?;
let resolver = PathResolver::new(workspace_root.as_path(), workspace_root.as_path())?;
let script = indoc! {"
IMPORT [STD, NET]
LET $l: NET_LISTEN_INFO = NET_LISTEN(\"23501\", {})
NET_CLOSE($l.listener)
"};
resolver.write_file(&script_path, script.as_bytes())?;
let opts = Options {
script: ScriptSource::Path(script_path),
shell: false,
dry_run: false,
endpoints: EndpointFlags {
publishes: vec![(
std::net::SocketAddr::from(([127, 0, 0, 1], 0)),
"23501".to_string(),
)],
..EndpointFlags::default()
},
remote_serve: false,
mcp: false,
remotes: Vec::new(),
script_args: Vec::new(),
};
execute_with_result(opts, workspace_root)?;
Ok(())
}
#[cfg(feature = "net")]
#[cfg_attr(
miri,
ignore = "loopback TCP plus GuardedPath::tempdir; blocked under Miri isolation"
)]
#[test]
fn net_port_addr_observe_publish_mapping() -> Result<()> {
let workspace = GuardedPath::tempdir()?;
let workspace_root = workspace.as_guarded_path().clone();
let script_path = workspace_root.join("net-port.ox")?;
let resolver = PathResolver::new(workspace_root.as_path(), workspace_root.as_path())?;
let script = indoc! {"
IMPORT [STD, NET]
LET $port: INT = NET_PORT(\"routed-svc\")
WRITE port.txt \"{{ $port }}\"
LET $addr: STRING = NET_ADDR(\"routed-svc\")
WRITE addr.txt \"{{ $addr }}\"
"};
resolver.write_file(&script_path, script.as_bytes())?;
let opts = Options {
script: ScriptSource::Path(script_path),
shell: false,
dry_run: false,
endpoints: EndpointFlags {
publishes: vec![(
std::net::SocketAddr::from(([0, 0, 0, 0], 0)),
"routed-svc".to_string(),
)],
..EndpointFlags::default()
},
remote_serve: false,
mcp: false,
remotes: Vec::new(),
script_args: Vec::new(),
};
let result = execute_with_result(opts, workspace_root)?;
let snapshot = result
.snapshot_path()
.expect("WRITE materializes the snapshot");
let snapshot_resolver = PathResolver::new(snapshot.root(), snapshot.root())?;
let port_path = snapshot.join("port.txt")?;
let port_text = snapshot_resolver.read_to_string(&port_path)?;
let port: u16 = port_text
.trim()
.parse()
.map_err(|_| anyhow::anyhow!("expected a resolved port, got {port_text:?}"))?;
assert_ne!(port, 0, "ephemeral outer port must resolve");
let addr_path = snapshot.join("addr.txt")?;
let addr_text = snapshot_resolver.read_to_string(&addr_path)?;
assert!(
addr_text.trim().ends_with(&format!(":{port}")),
"dial string must carry the resolved port, got {addr_text:?}"
);
Ok(())
}
#[cfg(not(feature = "ssh"))]
#[cfg_attr(
miri,
ignore = "GuardedPath::tempdir relies on OS tempdirs; blocked under Miri isolation"
)]
#[test]
fn ssh_scripts_rejected_without_feature() -> Result<()> {
let workspace = GuardedPath::tempdir()?;
let workspace_root = workspace.as_guarded_path().clone();
let script_path = workspace_root.join("ssh-serve.ox")?;
let resolver = PathResolver::new(workspace_root.as_path(), workspace_root.as_path())?;
let script = indoc! {"
IMPORT [STD, SSH]
LET $m: SSH_SERVE_INFO = SSH_SERVE(\"23301\", {username: \"test\", password: \"test123\"})
SSH_CLOSE($m.server)
"};
resolver.write_file(&script_path, script.as_bytes())?;
let opts = Options {
script: ScriptSource::Path(script_path),
shell: false,
dry_run: false,
endpoints: EndpointFlags::default(),
remote_serve: false,
mcp: false,
remotes: Vec::new(),
script_args: Vec::new(),
};
let err = match execute_with_result(opts, workspace_root) {
Ok(_) => panic!("SSH names must be unknown without the feature"),
Err(err) => err,
};
assert!(err.to_string().contains("SSH"), "{err}");
assert_eq!(err.key(), "cli.module_unlinked", "{err}");
Ok(())
}
#[cfg(not(feature = "net"))]
#[cfg_attr(
miri,
ignore = "GuardedPath::tempdir relies on OS tempdirs; blocked under Miri isolation"
)]
#[test]
fn net_flags_rejected_without_feature() {
let workspace = GuardedPath::tempdir().expect("tempdir");
for args in [
vec!["--listen", "0.0.0.0:2251", "-"],
vec!["-p", "2222:2251", "-"],
vec!["--listen=0.0.0.0:2251", "-"],
] {
let mut args = args.into_iter().map(str::to_string);
let err = Options::parse(&mut args, workspace.as_guarded_path())
.expect_err("endpoint flag must fail without net");
assert!(err.to_string().contains("`net` feature"), "{err:?}");
assert_eq!(err.key(), "cli.feature_required");
}
let mut args = vec!["--offline".to_string(), "-".to_string()].into_iter();
let opts = Options::parse(&mut args, workspace.as_guarded_path()).expect("parse");
assert!(opts.endpoints.offline);
}
#[cfg(not(feature = "net"))]
#[cfg_attr(
miri,
ignore = "GuardedPath::tempdir relies on OS tempdirs; blocked under Miri isolation"
)]
#[test]
fn net_scripts_rejected_without_feature() -> Result<()> {
let workspace = GuardedPath::tempdir()?;
let workspace_root = workspace.as_guarded_path().clone();
let script_path = workspace_root.join("net-listen.ox")?;
let resolver = PathResolver::new(workspace_root.as_path(), workspace_root.as_path())?;
let script = indoc! {"
IMPORT [STD, NET]
LET $l: NET_LISTEN_INFO = NET_LISTEN(\"23501\", {})
LET $body: STRING = NET_FETCH(\"https://example.com/x\")
NET_CLOSE($l.listener)
"};
resolver.write_file(&script_path, script.as_bytes())?;
let opts = Options {
script: ScriptSource::Path(script_path),
shell: false,
dry_run: false,
endpoints: EndpointFlags::default(),
remote_serve: false,
mcp: false,
remotes: Vec::new(),
script_args: Vec::new(),
};
let err = match execute_with_result(opts, workspace_root) {
Ok(_) => panic!("NET names must be unknown without the feature"),
Err(err) => err,
};
assert!(err.to_string().contains("NET"), "{err}");
assert_eq!(err.key(), "cli.module_unlinked", "{err}");
Ok(())
}
#[test]
fn imports_module_ignores_comments_strings_and_other_brackets() {
use oxdock_parser::lex_str;
let toks = lex_str("IMPORT [STD, NET]\n").expect("lex");
assert!(super::imports_module(&toks, "NET"));
assert!(!super::imports_module(&toks, "SSH"));
let toks = lex_str("IF true {\nIMPORT [SSH]\n}\n").expect("lex");
assert!(super::imports_module(&toks, "SSH"));
let toks = lex_str("IMPORT [\"./a.oxdock\" AS LIB]\n").expect("lex");
assert!(!super::imports_module(&toks, "NET"));
let toks = lex_str("IMPORT [\"./NET.oxdock\" AS LIB]\n").expect("lex");
assert!(!super::imports_module(&toks, "NET"));
let toks = lex_str("// IMPORT [NET]\nECHO hi\n").expect("lex");
assert!(!super::imports_module(&toks, "NET"));
let toks = lex_str("ECHO \"IMPORT [NET]\"\n").expect("lex");
assert!(!super::imports_module(&toks, "NET"));
let toks = lex_str("WITH_IO [stdout=$p] ECHO hi\n").expect("lex");
assert!(!super::imports_module(&toks, "NET"));
}
#[cfg(not(feature = "net"))]
#[test]
fn unlinked_module_error_needs_a_real_import() {
let err =
super::unlinked_module_error("IMPORT [STD, NET]\n").expect("real import must map");
assert_eq!(err.key(), "cli.module_unlinked");
assert!(super::unlinked_module_error("// IMPORT [NET]\nECHO hi\n").is_none());
assert!(super::unlinked_module_error("ECHO \"IMPORT [NET]\"\n").is_none());
assert!(super::unlinked_module_error("ECHO hi\n").is_none());
}
#[test]
fn process_exit_code_relays_exit_requests() {
use std::sync::Arc as StdArc;
let exit = oxdock_diagnostics::Diagnostic::new(
"t.exit",
"exit 3".to_string(),
oxdock_diagnostics::Diagnostic::make_fields(&[]),
)
.with_source(StdArc::new(ExitRequest::new(3)));
assert_eq!(process_exit_code(&exit), 3);
let relayed = oxdock_diagnostics::Diagnostic::new(
"t.exit",
"exit 3".to_string(),
oxdock_diagnostics::Diagnostic::make_fields(&[]),
)
.with_source(StdArc::new(ExitRequest::new(3)))
.with_contexts(vec!["REMOTE 'prod' exited with code 3".to_string()]);
assert_eq!(process_exit_code(&relayed), 3);
let keyed = oxdock_core::exec::diagnostics::eval_exit_requested(3);
assert_eq!(process_exit_code(&keyed), 3);
let keyed_wrapped = oxdock_core::exec::diagnostics::eval_exit_requested(3)
.with_contexts(vec!["step 1".to_string()]);
assert_eq!(process_exit_code(&keyed_wrapped), 3);
assert_eq!(process_exit_code(&errors::shell_needs_tty()), 1);
assert_eq!(
process_exit_code(
&oxdock_diagnostics::Diagnostic::new(
"t.exit",
"exit 300".to_string(),
oxdock_diagnostics::Diagnostic::make_fields(&[]),
)
.with_source(StdArc::new(ExitRequest::new(300)))
),
255,
"codes clamp to the portable range"
);
assert_eq!(
process_exit_code(
&oxdock_diagnostics::Diagnostic::new(
"t.exit",
"exit -1".to_string(),
oxdock_diagnostics::Diagnostic::make_fields(&[]),
)
.with_source(StdArc::new(ExitRequest::new(-1)))
),
0,
"negative codes clamp to zero"
);
}
}
#[cfg(all(test, windows))]
mod windows_shell_tests {
use super::*;
#[test]
fn command_path_strips_verbatim_prefix() -> DiagnosticResult<()> {
let temp = GuardedPath::tempdir()?;
let converted = oxdock_fs::command_path(temp.as_guarded_path());
let as_str = converted.as_ref().display().to_string();
assert!(
!as_str.starts_with(r"\\?\"),
"expected non-verbatim path, got {as_str}"
);
Ok(())
}
}