pub use oxdock_diagnostics::Diagnostic;
use oxdock_diagnostics::flatten_chain;
use oxdock_macros::oxdock_embed;
oxdock_embed! {
name: UsageAssets,
script: {
INHERIT_ENV [CARGO_PKG_VERSION, CARGO_PKG_DESCRIPTION]
FUNC EMIT_BODY($is_net: BOOL): VOID {
LET $net_flags: STRING = """
--offline open no sockets (endpoint flags require the `net` feature)
"""
LET $tail: STRING = """
Endpoint flags (--listen/-p) require the `net` feature (rebuild with --features net).
"""
IF $is_net {
$net_flags = """
--listen <addr> expose a logical service port ([host:]port, repeatable)
-p <[host:]outer:inner> map outer port to an inner service port or name (repeatable; outer 0 is ephemeral; bare outer binds loopback, prefix 0.0.0.0: for all interfaces)
--offline open no sockets (conflicts with --listen/-p)
--remote TARGET=CMD bind a REMOTE target to a stdio transport command (repeatable)
"""
$tail = "Scripts declare logical endpoints (a port like 2251); the flags above map them to interfaces."
}
ECHO """
oxdock {{ env:CARGO_PKG_VERSION }} — {{ env:CARGO_PKG_DESCRIPTION }}
Usage: oxdock [OPTIONS] [--] [SCRIPT] [-- script-args...]
SCRIPT script file path (same as `--script <file>`); `-` reads stdin
--script <file|-> script file (relative resolves under the OxDock workspace root), or `-` for stdin
--shell run the script, then drop into an interactive shell (requires a TTY)
--dry-run print the transitive import closure (file:line CMD) instead of running
--mcp serve EXPORTed FUNCs as MCP tools over stdio (requires the `mcp` feature)
{{ $net_flags }}
--help, -h print this help and exit
--version, -V, -v print the version and exit
With no script given, reads the script from stdin (must be piped unless `--shell`).
Everything after SCRIPT is a verbatim script arg (read via `STD::ARGV()`); one leading `--` there is a separator and dropped, so `--dry-run` after the script stays data.
{{ $tail }}
"""
}
LET $p: PIPE
WITH_IO [stdout=$p] {
EMIT_BODY(true)
}
WITH_IO [stdin=$p] WRITE dist/usage-net.txt
WITH_IO [stdout=$p] {
EMIT_BODY(false)
}
WITH_IO [stdin=$p] WRITE dist/usage-lean.txt
},
out_dir: "usage-prebuilt",
}
pub fn usage() -> String {
#[cfg(feature = "net")]
let asset = "dist/usage-net.txt";
#[cfg(not(feature = "net"))]
let asset = "dist/usage-lean.txt";
let file = UsageAssets::get(asset).expect("usage must be embedded");
str::from_utf8(file.data.as_ref())
.expect("usage is UTF-8")
.to_string()
}
pub fn help_requested() -> Diagnostic {
Diagnostic::new("cli.help_requested", usage(), Diagnostic::make_fields(&[]))
}
pub fn version_requested() -> Diagnostic {
Diagnostic::new(
"cli.version_requested",
super::version(),
Diagnostic::make_fields(&[]),
)
}
pub fn feature_required(flag: &str, feature: &str) -> Diagnostic {
let message =
format!("{flag} requires the `{feature}` feature (rebuild with --features {feature})");
Diagnostic::new(
"cli.feature_required",
message,
Diagnostic::make_fields(&[("flag", flag), ("feature", feature)]),
)
}
pub fn offline_conflict() -> Diagnostic {
Diagnostic::new(
"cli.offline_conflict",
"--offline conflicts with --listen/-p (offline runs open no sockets)".to_string(),
Diagnostic::make_fields(&[]),
)
}
pub fn endpoint_duplicate_inner(endpoint: &str) -> Diagnostic {
let message = format!("virtual endpoint '{endpoint}' is mapped twice");
Diagnostic::new(
"cli.endpoint_duplicate_inner",
message,
Diagnostic::make_fields(&[("endpoint", endpoint)]),
)
}
pub fn endpoint_duplicate_outer(addr: &str) -> Diagnostic {
let message = format!("outer socket address {addr} is mapped twice");
Diagnostic::new(
"cli.endpoint_duplicate_outer",
message,
Diagnostic::make_fields(&[("address", addr)]),
)
}
pub fn listen_requires_address() -> Diagnostic {
Diagnostic::new(
"cli.listen_requires_address",
"--listen requires an address ([host:]port)".to_string(),
Diagnostic::make_fields(&[("flag", "--listen")]),
)
}
pub fn listen_port_numeric(raw: &str) -> Diagnostic {
let message = format!("--listen port must be numeric 1-65535, got {raw:?}");
Diagnostic::new(
"cli.listen_port_numeric",
message,
Diagnostic::make_fields(&[("flag", "--listen"), ("got", raw)]),
)
}
pub fn listen_port_zero() -> Diagnostic {
Diagnostic::new(
"cli.listen_port_zero",
"--listen port 0 has no stable service key (use -p 0:<endpoint> for an ephemeral outer port)"
.to_string(),
Diagnostic::make_fields(&[("flag", "--listen")]),
)
}
pub fn listen_address_invalid(raw: &str) -> Diagnostic {
let message = format!("--listen address must be [host:]port (try 0.0.0.0:2251), got {raw:?}");
Diagnostic::new(
"cli.listen_address_invalid",
message,
Diagnostic::make_fields(&[("flag", "--listen"), ("got", raw)]),
)
}
pub fn publish_requires_shape(raw: &str) -> Diagnostic {
let message = format!(
"-p requires [host:]outer:inner (try -p 2222:2251 or -p 127.0.0.1:1234:ssh-server), got {raw:?}"
);
Diagnostic::new(
"cli.publish_requires_shape",
message,
Diagnostic::make_fields(&[("flag", "-p"), ("got", raw)]),
)
}
pub fn publish_outer_invalid(raw: &str) -> Diagnostic {
let message = format!(
"-p outer address invalid in {raw:?} (expected a port 0-65535 or a [host:]port address)"
);
Diagnostic::new(
"cli.publish_outer_invalid",
message,
Diagnostic::make_fields(&[("flag", "-p"), ("got", raw)]),
)
}
#[cfg(feature = "net")]
pub fn publish_inner_invalid(raw: &str, reason: Diagnostic) -> Diagnostic {
let reason_text = flatten_chain(&reason);
let message = format!("-p inner endpoint invalid in {raw:?}: {reason_text}");
Diagnostic::new(
"cli.publish_inner_invalid",
message,
Diagnostic::make_fields(&[("flag", "-p"), ("got", raw), ("reason", &reason_text)]),
)
}
pub fn module_unlinked(module: &str, feature: &str) -> Diagnostic {
let message =
format!("module `{module}` is known but not linked (rebuild with --features {feature})");
Diagnostic::new(
"cli.module_unlinked",
message,
Diagnostic::make_fields(&[("module", module), ("feature", feature)]),
)
}
pub fn unexpected_flag(name: &str, long: bool) -> Diagnostic {
let prefix = if long { "--" } else { "-" };
let message = format!("unexpected flag: {prefix}{name}");
Diagnostic::new(
"cli.unexpected_flag",
message,
Diagnostic::make_fields(&[("flag", name)]),
)
}
pub fn script_flag_needs_path() -> Diagnostic {
Diagnostic::new(
"cli.script_flag_needs_path",
"--script requires a path".to_string(),
Diagnostic::make_fields(&[("flag", "--script")]),
)
}
pub fn script_not_local(text: &str) -> Diagnostic {
let message = format!("not a local path: {text:?} (pass a workspace path or `-` for stdin)");
Diagnostic::new(
"cli.script_not_local",
message,
Diagnostic::make_fields(&[("got", text)]),
)
}
pub fn arg_not_utf8() -> Diagnostic {
Diagnostic::new(
"cli.arg_not_utf8",
"argument must be UTF-8".to_string(),
Diagnostic::make_fields(&[]),
)
}
pub fn arg_lex_error(reason: &lexopt::Error) -> Diagnostic {
let reason_text = format!("{reason:#}");
Diagnostic::new(
"cli.arg_lex_error",
reason_text.clone(),
Diagnostic::make_fields(&[("reason", &reason_text)]),
)
}
pub fn remote_requires_value() -> Diagnostic {
Diagnostic::new(
"cli.remote_requires_value",
"--remote requires <target>=\"<command>\"".to_string(),
Diagnostic::make_fields(&[("flag", "--remote")]),
)
}
#[cfg(not(feature = "net"))]
pub fn remote_requires_shape(raw: &str) -> Diagnostic {
let message = format!("invalid --remote {raw:?}: expected <target>=\"<command>\"");
Diagnostic::new(
"cli.remote_requires_shape",
message,
Diagnostic::make_fields(&[("flag", "--remote"), ("got", raw)]),
)
}
pub fn script_stdin_missing() -> Diagnostic {
Diagnostic::new(
"cli.script_stdin_missing",
"no stdin detected; pass --script <file> or pipe a script into stdin (use --script - if explicit)"
.to_string(),
Diagnostic::make_fields(&[]),
)
}
pub fn guard_workspace_root(reason: &Diagnostic) -> Diagnostic {
let reason_text = flatten_chain(reason);
let message = format!("guard workspace root: {reason_text}");
Diagnostic::new(
"cli.guard_workspace_root",
message,
Diagnostic::make_fields(&[("reason", &reason_text)]),
)
}
pub fn guard_script_path(path: &str, reason: &Diagnostic) -> Diagnostic {
let reason_text = flatten_chain(reason);
let message = format!("guard script path {path}: {reason_text}");
Diagnostic::new(
"cli.guard_script_path",
message,
Diagnostic::make_fields(&[("path", path), ("reason", &reason_text)]),
)
}
pub fn read_script_at(path: &str, reason: &Diagnostic) -> Diagnostic {
let reason_text = flatten_chain(reason);
let message = format!("failed to read script at {path}: {reason_text}");
Diagnostic::new(
"cli.read_script_at",
message,
Diagnostic::make_fields(&[("path", path), ("reason", &reason_text)]),
)
}
pub fn read_script_stdin(reason: &std::io::Error) -> Diagnostic {
let reason_text = format!("{reason:#}");
let message = format!("failed to read script from stdin: {reason_text}");
Diagnostic::new(
"cli.read_script_stdin",
message,
Diagnostic::make_fields(&[("reason", &reason_text)]),
)
}
pub fn create_shell_temp_dir(reason: &Diagnostic) -> Diagnostic {
let reason_text = flatten_chain(reason);
let message = format!("failed to create shell temp dir: {reason_text}");
Diagnostic::new(
"cli.create_shell_temp_dir",
message,
Diagnostic::make_fields(&[("reason", &reason_text)]),
)
}
pub fn shell_needs_tty() -> Diagnostic {
Diagnostic::new(
"cli.shell_needs_tty",
"--shell requires a tty (no controlling tty available)".to_string(),
Diagnostic::make_fields(&[("flag", "--shell")]),
)
}
pub fn shell_unsupported_in_result() -> Diagnostic {
Diagnostic::new(
"cli.shell_unsupported_in_result",
"execute_with_result does not support --shell".to_string(),
Diagnostic::make_fields(&[]),
)
}
#[cfg(windows)]
pub fn shell_temp_missing_parent() -> Diagnostic {
Diagnostic::new(
"cli.shell_temp_missing_parent",
"temp path unexpectedly missing parent".to_string(),
Diagnostic::make_fields(&[]),
)
}
#[cfg(windows)]
pub fn determine_current_exe(reason: &std::io::Error) -> Diagnostic {
let reason_text = format!("{reason:#}");
let message = format!("determine current executable: {reason_text}");
Diagnostic::new(
"cli.determine_current_exe",
message,
Diagnostic::make_fields(&[("reason", &reason_text)]),
)
}
#[cfg(windows)]
pub fn guard_system_temp_dir(reason: &Diagnostic) -> Diagnostic {
let reason_text = flatten_chain(reason);
let message = format!("guard system temp dir: {reason_text}");
Diagnostic::new(
"cli.guard_system_temp_dir",
message,
Diagnostic::make_fields(&[("reason", &reason_text)]),
)
}
#[cfg(windows)]
pub fn construct_temp_shell_path(reason: &Diagnostic) -> Diagnostic {
let reason_text = flatten_chain(reason);
let message = format!("construct temp shell path: {reason_text}");
Diagnostic::new(
"cli.construct_temp_shell_path",
message,
Diagnostic::make_fields(&[("reason", &reason_text)]),
)
}
#[cfg(windows)]
pub fn copy_shell_runner(dest: &str, reason: &Diagnostic) -> Diagnostic {
let reason_text = flatten_chain(reason);
let message = format!("failed to copy shell runner to {dest}: {reason_text}");
Diagnostic::new(
"cli.copy_shell_runner",
message,
Diagnostic::make_fields(&[("dest", dest), ("reason", &reason_text)]),
)
}
#[cfg(windows)]
pub fn spawn_shell_from(dest: &str, reason: &Diagnostic) -> Diagnostic {
let reason_text = flatten_chain(reason);
let message = format!("failed to spawn shell from {dest}: {reason_text}");
Diagnostic::new(
"cli.spawn_shell_from",
message,
Diagnostic::make_fields(&[("dest", dest), ("reason", &reason_text)]),
)
}
#[cfg(feature = "net")]
pub fn remote_serve_runs_alone() -> Diagnostic {
Diagnostic::new(
"cli.remote_serve_runs_alone",
"--remote-serve runs alone (no --script, --shell, --remote, or script args)".to_string(),
Diagnostic::make_fields(&[("flag", "--remote-serve")]),
)
}
#[cfg(feature = "net")]
pub fn remote_serve_needs_stdio() -> Diagnostic {
Diagnostic::new(
"cli.remote_serve_needs_stdio",
"--remote-serve requires piped stdio (no TTY)".to_string(),
Diagnostic::make_fields(&[("flag", "--remote-serve")]),
)
}
#[cfg(feature = "net")]
pub fn fetch_tar_no_stage() -> Diagnostic {
Diagnostic::new(
"cli.serve_fetch_tar_no_stage",
"serve fetch tar arrived with no stage".to_string(),
Diagnostic::make_fields(&[]),
)
}
#[cfg(feature = "net")]
pub fn fetch_dir_escapes(path: &str, reason: &Diagnostic) -> Diagnostic {
let reason_text = flatten_chain(reason);
let message = format!("fetch dir {path:?} escapes staging: {reason_text}");
Diagnostic::new(
"cli.serve_fetch_dir_escapes",
message,
Diagnostic::make_fields(&[("path", path), ("reason", &reason_text)]),
)
}
#[cfg(feature = "net")]
pub fn fetch_file_escapes(path: &str, reason: &Diagnostic) -> Diagnostic {
let reason_text = flatten_chain(reason);
let message = format!("fetch file {path:?} escapes staging: {reason_text}");
Diagnostic::new(
"cli.serve_fetch_file_escapes",
message,
Diagnostic::make_fields(&[("path", path), ("reason", &reason_text)]),
)
}
#[cfg(feature = "net")]
pub fn fetch_parent_escapes(path: &str, reason: &Diagnostic) -> Diagnostic {
let reason_text = flatten_chain(reason);
let message = format!("fetch parent {path:?} escapes staging: {reason_text}");
Diagnostic::new(
"cli.serve_fetch_parent_escapes",
message,
Diagnostic::make_fields(&[("path", path), ("reason", &reason_text)]),
)
}
#[cfg(feature = "net")]
pub fn fetch_symlink_refused(path: &str, target: &str) -> Diagnostic {
let message = format!("fetch symlink {path:?} -> {target:?} refused: fetch entries are files");
Diagnostic::new(
"cli.serve_fetch_symlink_refused",
message,
Diagnostic::make_fields(&[("path", path), ("target", target)]),
)
}
#[cfg(feature = "net")]
pub fn serve_fetch_spill_write_failed(reason: &std::io::Error) -> Diagnostic {
let reason_text = format!("{reason:#}");
let message = format!("serve fetch spill write failed: {reason_text}");
Diagnostic::new(
"cli.serve_fetch_spill_write_failed",
message,
Diagnostic::make_fields(&[("reason", &reason_text)]),
)
}
#[cfg(feature = "net")]
pub fn serve_fetch_member_failed(path: &str, reason: &std::io::Error) -> Diagnostic {
let reason_text = format!("{reason:#}");
let message = format!("fetch member {path:?} failed: {reason_text}");
Diagnostic::new(
"cli.serve_fetch_member_failed",
message,
Diagnostic::make_fields(&[("path", path), ("reason", &reason_text)]),
)
}
#[cfg(feature = "net")]
pub fn serve_engine_panicked() -> Diagnostic {
Diagnostic::new(
"cli.serve_engine_panicked",
"serve engine thread panicked".to_string(),
Diagnostic::make_fields(&[]),
)
}
#[cfg(feature = "net")]
pub fn serve_handler_register_failed(
method: &str,
reason: &muxio_rpc_service_endpoint::error::RpcServiceEndpointError,
) -> Diagnostic {
let reason_text = format!("{reason:#}");
let message = format!("serve handler registration for {method} failed: {reason_text}");
Diagnostic::new(
"cli.serve_handler_register_failed",
message,
Diagnostic::make_fields(&[("method", method), ("reason", &reason_text)]),
)
}
#[cfg(feature = "net")]
pub fn serve_transfer_tempdir_failed(reason: &Diagnostic) -> Diagnostic {
let reason_text = flatten_chain(reason);
let message = format!("serve transfer tempdir failed: {reason_text}");
Diagnostic::new(
"cli.serve_transfer_tempdir_failed",
message,
Diagnostic::make_fields(&[("reason", &reason_text)]),
)
}
#[cfg(feature = "net")]
pub fn serve_fetch_descriptor_decode_failed(reason: &serde_json::Error) -> Diagnostic {
let reason_text = reason.to_string();
let message = format!("serve fetch descriptor decode failed: {reason_text}");
Diagnostic::new(
"cli.serve_fetch_descriptor_decode_failed",
message,
Diagnostic::make_fields(&[("reason", &reason_text)]),
)
}
#[cfg(feature = "net")]
pub fn serve_staging_tempdir_failed(reason: &Diagnostic) -> Diagnostic {
let reason_text = flatten_chain(reason);
let message = format!("serve staging tempdir failed: {reason_text}");
Diagnostic::new(
"cli.serve_staging_tempdir_failed",
message,
Diagnostic::make_fields(&[("reason", &reason_text)]),
)
}
#[cfg(feature = "net")]
pub fn serve_push_tempdir_failed(reason: &Diagnostic) -> Diagnostic {
let reason_text = flatten_chain(reason);
let message = format!("serve push tempdir failed: {reason_text}");
Diagnostic::new(
"cli.serve_push_tempdir_failed",
message,
Diagnostic::make_fields(&[("reason", &reason_text)]),
)
}
#[cfg(feature = "net")]
pub fn serve_push_spill_flush_failed(reason: &std::io::Error) -> Diagnostic {
let reason_text = format!("{reason:#}");
let message = format!("serve push spill flush failed: {reason_text}");
Diagnostic::new(
"cli.serve_push_spill_flush_failed",
message,
Diagnostic::make_fields(&[("reason", &reason_text)]),
)
}
#[cfg(feature = "net")]
pub fn serve_push_spill_hash_failed(reason: &std::io::Error) -> Diagnostic {
let reason_text = format!("{reason:#}");
let message = format!("serve push spill hash failed: {reason_text}");
Diagnostic::new(
"cli.serve_push_spill_hash_failed",
message,
Diagnostic::make_fields(&[("reason", &reason_text)]),
)
}
#[cfg(feature = "net")]
pub fn push_source_escapes(guest_src: &str, reason: &Diagnostic) -> Diagnostic {
let reason_text = flatten_chain(reason);
let message = format!("guest push source {guest_src:?} escapes staging: {reason_text}");
Diagnostic::new(
"cli.serve_push_source_escapes",
message,
Diagnostic::make_fields(&[("source", guest_src), ("reason", &reason_text)]),
)
}
#[cfg(feature = "net")]
pub fn push_source_unreadable(path: &str, reason: &Diagnostic) -> Diagnostic {
let reason_text = flatten_chain(reason);
let message = format!("guest push source {path:?} unreadable: {reason_text}");
Diagnostic::new(
"cli.serve_push_source_unreadable",
message,
Diagnostic::make_fields(&[("source", path), ("reason", &reason_text)]),
)
}
#[cfg(feature = "net")]
pub fn push_source_missing(guest_src: &str, reason: &Diagnostic) -> Diagnostic {
let reason_text = flatten_chain(reason);
let message = format!("guest push source {guest_src:?} missing: {reason_text}");
Diagnostic::new(
"cli.serve_push_source_missing",
message,
Diagnostic::make_fields(&[("source", guest_src), ("reason", &reason_text)]),
)
}
#[cfg(feature = "mcp")]
pub fn mcp_needs_stdio() -> Diagnostic {
Diagnostic::new(
"cli.mcp_needs_stdio",
"--mcp requires piped stdio transport".to_string(),
Diagnostic::make_fields(&[("flag", "--mcp")]),
)
}
#[cfg(feature = "mcp")]
pub fn mcp_needs_script_file() -> Diagnostic {
Diagnostic::new(
"cli.mcp_needs_script_file",
"--mcp requires a script file (stdin carries the JSON-RPC stream)".to_string(),
Diagnostic::make_fields(&[("flag", "--mcp")]),
)
}
#[cfg(feature = "mcp")]
pub fn mcp_runs_alone(conflicts: &[&str]) -> Diagnostic {
let joined = conflicts.join(", ");
let message = format!("--mcp runs alone (no {joined})");
Diagnostic::new(
"cli.mcp_runs_alone",
message,
Diagnostic::make_fields(&[("flag", "--mcp"), ("conflicts", &joined)]),
)
}
#[cfg(test)]
mod tests {
use super::*;
#[cfg(feature = "net")]
fn serde_err() -> serde_json::Error {
serde_json::from_slice::<serde_json::Value>(b"{bad}").unwrap_err()
}
fn sample_reason(text: &str) -> Diagnostic {
Diagnostic::new("t.reason", text.to_string(), Diagnostic::make_fields(&[]))
}
fn io_err() -> std::io::Error {
std::io::Error::new(std::io::ErrorKind::BrokenPipe, "boom")
}
#[test]
fn feature_message_shared_across_sites() {
for (flag, feature) in [
("--listen", "net"),
("-p", "net"),
("script module", "net"),
("--remote-serve", "net"),
("--mcp", "mcp"),
] {
let diag = feature_required(flag, feature);
assert_eq!(diag.key(), "cli.feature_required");
assert!(diag.message().contains(flag), "{diag}");
assert!(diag.message().contains(feature), "{diag}");
}
}
#[test]
fn mcp_feature_gate_names_mcp_not_net() {
let diag = feature_required("--mcp", "mcp");
let text = diag.message();
assert!(text.contains("`mcp`"), "{diag}");
assert!(text.contains("--features mcp"), "{diag}");
assert!(!text.contains("net"), "{diag}");
}
#[test]
fn endpoint_duplicates_name_the_key() {
let inner = endpoint_duplicate_inner("2251");
assert_eq!(inner.key(), "cli.endpoint_duplicate_inner");
let outer = endpoint_duplicate_outer("127.0.0.1:2222");
assert_eq!(outer.key(), "cli.endpoint_duplicate_outer");
}
#[test]
fn untrusted_input_cannot_crash_cli_errors() {
let evil = "bad\u{1f}flag".to_string();
let diag = feature_required(&evil, "net");
assert!(!diag.message().contains('\u{1f}'));
}
#[test]
fn net_shape_errors_carry_stable_keys() {
let cases: &[(&str, anyhow::Error)] = &[
(
"cli.listen_requires_address",
anyhow::Error::from(listen_requires_address()),
),
(
"cli.listen_port_numeric",
anyhow::Error::from(listen_port_numeric("abc")),
),
(
"cli.listen_port_zero",
anyhow::Error::from(listen_port_zero()),
),
(
"cli.listen_address_invalid",
anyhow::Error::from(listen_address_invalid("0.0.0.0:99999")),
),
(
"cli.publish_requires_shape",
anyhow::Error::from(publish_requires_shape("2251")),
),
(
"cli.publish_outer_invalid",
anyhow::Error::from(publish_outer_invalid("abc:2251")),
),
#[cfg(feature = "net")]
(
"cli.publish_inner_invalid",
anyhow::Error::from(publish_inner_invalid(
"2222:0",
Diagnostic::new(
"t.reason",
"bad endpoint".to_string(),
Diagnostic::make_fields(&[]),
),
)),
),
(
"cli.module_unlinked",
anyhow::Error::from(module_unlinked("NET", "net")),
),
(
"cli.unexpected_flag",
anyhow::Error::from(unexpected_flag("frobnicate", true)),
),
(
"cli.script_flag_needs_path",
anyhow::Error::from(script_flag_needs_path()),
),
(
"cli.script_not_local",
anyhow::Error::from(script_not_local("https://x")),
),
("cli.arg_not_utf8", anyhow::Error::from(arg_not_utf8())),
(
"cli.arg_lex_error",
anyhow::Error::from(arg_lex_error(&lexopt::Error::UnexpectedOption(
"--nope".to_string(),
))),
),
(
"cli.remote_requires_value",
anyhow::Error::from(remote_requires_value()),
),
#[cfg(not(feature = "net"))]
(
"cli.remote_requires_shape",
anyhow::Error::from(remote_requires_shape("prod")),
),
(
"cli.script_stdin_missing",
anyhow::Error::from(script_stdin_missing()),
),
(
"cli.shell_needs_tty",
anyhow::Error::from(shell_needs_tty()),
),
(
"cli.shell_unsupported_in_result",
anyhow::Error::from(shell_unsupported_in_result()),
),
#[cfg(windows)]
(
"cli.shell_temp_missing_parent",
anyhow::Error::from(shell_temp_missing_parent()),
),
#[cfg(feature = "net")]
(
"cli.remote_serve_runs_alone",
anyhow::Error::from(remote_serve_runs_alone()),
),
#[cfg(feature = "net")]
(
"cli.remote_serve_needs_stdio",
anyhow::Error::from(remote_serve_needs_stdio()),
),
#[cfg(feature = "net")]
(
"cli.serve_fetch_tar_no_stage",
anyhow::Error::from(fetch_tar_no_stage()),
),
#[cfg(feature = "net")]
(
"cli.serve_fetch_dir_escapes",
anyhow::Error::from(fetch_dir_escapes("a", &sample_reason("root"))),
),
#[cfg(feature = "net")]
(
"cli.serve_fetch_file_escapes",
anyhow::Error::from(fetch_file_escapes("a", &sample_reason("root"))),
),
#[cfg(feature = "net")]
(
"cli.serve_fetch_parent_escapes",
anyhow::Error::from(fetch_parent_escapes("a", &sample_reason("root"))),
),
#[cfg(feature = "net")]
(
"cli.serve_fetch_symlink_refused",
anyhow::Error::from(fetch_symlink_refused("a", "b")),
),
#[cfg(feature = "net")]
(
"cli.serve_fetch_spill_write_failed",
anyhow::Error::from(serve_fetch_spill_write_failed(&io_err())),
),
#[cfg(feature = "net")]
(
"cli.serve_fetch_member_failed",
anyhow::Error::from(serve_fetch_member_failed("a", &io_err())),
),
#[cfg(feature = "net")]
(
"cli.serve_engine_panicked",
anyhow::Error::from(serve_engine_panicked()),
),
#[cfg(feature = "net")]
(
"cli.serve_handler_register_failed",
anyhow::Error::from(serve_handler_register_failed(
"exec",
&muxio_rpc_service_endpoint::error::RpcServiceEndpointError::Handler(Box::new(
io_err(),
)),
)),
),
#[cfg(feature = "net")]
(
"cli.serve_transfer_tempdir_failed",
anyhow::Error::from(serve_transfer_tempdir_failed(&sample_reason("boom"))),
),
#[cfg(feature = "net")]
(
"cli.serve_fetch_descriptor_decode_failed",
anyhow::Error::from(serve_fetch_descriptor_decode_failed(&serde_err())),
),
#[cfg(feature = "net")]
(
"cli.serve_staging_tempdir_failed",
anyhow::Error::from(serve_staging_tempdir_failed(&sample_reason("boom"))),
),
#[cfg(feature = "net")]
(
"cli.serve_push_tempdir_failed",
anyhow::Error::from(serve_push_tempdir_failed(&sample_reason("boom"))),
),
#[cfg(feature = "net")]
(
"cli.serve_push_spill_flush_failed",
anyhow::Error::from(serve_push_spill_flush_failed(&io_err())),
),
#[cfg(feature = "net")]
(
"cli.serve_push_spill_hash_failed",
anyhow::Error::from(serve_push_spill_hash_failed(&io_err())),
),
#[cfg(feature = "net")]
(
"cli.serve_push_source_escapes",
anyhow::Error::from(push_source_escapes("a", &sample_reason("root"))),
),
#[cfg(feature = "net")]
(
"cli.serve_push_source_unreadable",
anyhow::Error::from(push_source_unreadable("a", &sample_reason("root"))),
),
#[cfg(feature = "net")]
(
"cli.serve_push_source_missing",
anyhow::Error::from(push_source_missing("a", &sample_reason("root"))),
),
#[cfg(feature = "mcp")]
(
"cli.mcp_needs_stdio",
anyhow::Error::from(mcp_needs_stdio()),
),
#[cfg(feature = "mcp")]
(
"cli.mcp_needs_script_file",
anyhow::Error::from(mcp_needs_script_file()),
),
#[cfg(feature = "mcp")]
(
"cli.mcp_runs_alone",
anyhow::Error::from(mcp_runs_alone(&["--shell"])),
),
(
"cli.guard_workspace_root",
anyhow::Error::from(guard_workspace_root(&sample_reason("boom"))),
),
(
"cli.guard_script_path",
anyhow::Error::from(guard_script_path("s.ox", &sample_reason("boom"))),
),
(
"cli.read_script_at",
anyhow::Error::from(read_script_at("s.ox", &sample_reason("boom"))),
),
(
"cli.read_script_stdin",
anyhow::Error::from(read_script_stdin(&io_err())),
),
(
"cli.create_shell_temp_dir",
anyhow::Error::from(create_shell_temp_dir(&sample_reason("boom"))),
),
#[cfg(windows)]
(
"cli.determine_current_exe",
anyhow::Error::from(determine_current_exe(&io_err())),
),
#[cfg(windows)]
(
"cli.guard_system_temp_dir",
anyhow::Error::from(guard_system_temp_dir(&sample_reason("boom"))),
),
#[cfg(windows)]
(
"cli.construct_temp_shell_path",
anyhow::Error::from(construct_temp_shell_path(&sample_reason("boom"))),
),
#[cfg(windows)]
(
"cli.copy_shell_runner",
anyhow::Error::from(copy_shell_runner("d.exe", &sample_reason("boom"))),
),
#[cfg(windows)]
(
"cli.spawn_shell_from",
anyhow::Error::from(spawn_shell_from("d.exe", &sample_reason("boom"))),
),
("cli.help_requested", anyhow::Error::from(help_requested())),
(
"cli.version_requested",
anyhow::Error::from(version_requested()),
),
];
let mut seen = std::collections::HashSet::new();
for (key, err) in cases {
let diag = err
.downcast_ref::<Diagnostic>()
.expect("CLI errors stay typed");
assert_eq!(&diag.key(), key, "{diag}");
assert!(seen.insert(*key), "duplicate key {key}");
}
}
#[test]
fn flag_shape_messages_name_the_flag() {
assert_eq!(
unexpected_flag("frobnicate", true).message(),
"unexpected flag: --frobnicate"
);
assert_eq!(unexpected_flag("z", false).message(), "unexpected flag: -z");
assert_eq!(
script_flag_needs_path().message(),
"--script requires a path"
);
assert!(
script_not_local("https://x/y")
.to_string()
.contains("\"https://x/y\"")
);
assert!(
arg_lex_error(&lexopt::Error::UnexpectedOption("--nope".to_string()))
.to_string()
.contains("--nope")
);
assert_eq!(
remote_requires_value().message(),
"--remote requires <target>=\"<command>\""
);
#[cfg(feature = "mcp")]
assert_eq!(
mcp_runs_alone(&["--shell", "--remote"]).message(),
"--mcp runs alone (no --shell, --remote)"
);
}
#[test]
fn startup_context_messages_carry_outer_text() {
assert_eq!(
guard_workspace_root(&sample_reason("boom")).message(),
"guard workspace root: boom"
);
assert_eq!(
guard_script_path("s.ox", &sample_reason("boom")).message(),
"guard script path s.ox: boom"
);
assert_eq!(
read_script_at("s.ox", &sample_reason("denied")).message(),
"failed to read script at s.ox: denied"
);
assert_eq!(
read_script_stdin(&io_err()).message(),
"failed to read script from stdin: boom"
);
assert_eq!(
create_shell_temp_dir(&sample_reason("boom")).message(),
"failed to create shell temp dir: boom"
);
}
#[cfg(windows)]
#[test]
fn windows_reexec_messages_carry_outer_text() {
assert_eq!(
determine_current_exe(&io_err()).message(),
"determine current executable: boom"
);
assert_eq!(
guard_system_temp_dir(&sample_reason("boom")).message(),
"guard system temp dir: boom"
);
assert_eq!(
construct_temp_shell_path(&sample_reason("boom")).message(),
"construct temp shell path: boom"
);
assert_eq!(
copy_shell_runner("d.exe", &sample_reason("boom")).message(),
"failed to copy shell runner to d.exe: boom"
);
assert_eq!(
spawn_shell_from("d.exe", &sample_reason("boom")).message(),
"failed to spawn shell from d.exe: boom"
);
}
#[cfg(feature = "mcp")]
#[test]
fn mcp_gate_messages() {
assert_eq!(
mcp_needs_stdio().message(),
"--mcp requires piped stdio transport"
);
assert_eq!(
mcp_needs_script_file().message(),
"--mcp requires a script file (stdin carries the JSON-RPC stream)"
);
}
#[cfg(feature = "net")]
#[test]
fn serve_staging_messages_name_the_member() {
let err = fetch_dir_escapes("a", &sample_reason("root"));
assert!(err.to_string().contains("\"a\""), "{err}");
assert!(err.to_string().contains("root"), "{err}");
let err = fetch_symlink_refused("a", "b");
assert_eq!(
err.message(),
"fetch symlink \"a\" -> \"b\" refused: fetch entries are files"
);
let err = push_source_unreadable("a", &sample_reason("denied"));
assert!(err.to_string().contains("denied"), "{err}");
assert_eq!(
serve_transfer_tempdir_failed(&sample_reason("boom")).message(),
"serve transfer tempdir failed: boom"
);
assert_eq!(
serve_fetch_descriptor_decode_failed(&serde_err()).message(),
"serve fetch descriptor decode failed: key must be a string at line 1 column 2"
);
assert_eq!(
serve_staging_tempdir_failed(&sample_reason("boom")).message(),
"serve staging tempdir failed: boom"
);
assert_eq!(
serve_push_tempdir_failed(&sample_reason("boom")).message(),
"serve push tempdir failed: boom"
);
assert_eq!(
serve_fetch_spill_write_failed(&io_err()).message(),
"serve fetch spill write failed: boom"
);
assert_eq!(
serve_fetch_member_failed("a", &io_err()).message(),
"fetch member \"a\" failed: boom"
);
assert_eq!(
serve_push_spill_flush_failed(&io_err()).message(),
"serve push spill flush failed: boom"
);
assert_eq!(
serve_push_spill_hash_failed(&io_err()).message(),
"serve push spill hash failed: boom"
);
}
#[test]
fn net_shape_messages_name_the_flag_and_value() {
assert_eq!(
listen_requires_address().message(),
"--listen requires an address ([host:]port)"
);
assert!(
publish_requires_shape("2251")
.to_string()
.contains("\"2251\"")
);
#[cfg(feature = "net")]
{
let err = publish_inner_invalid(
"2222:0",
Diagnostic::new(
"t.reason",
"bad endpoint".to_string(),
Diagnostic::make_fields(&[]),
),
);
let text = err.to_string();
assert!(text.contains("\"2222:0\""), "{text}");
assert!(text.contains("bad endpoint"), "{text}");
}
assert_eq!(
module_unlinked("NET", "net").message(),
"module `NET` is known but not linked (rebuild with --features net)"
);
}
}