1pub use oxdock_diagnostics::Diagnostic;
9use oxdock_diagnostics::flatten_chain;
10use oxdock_macros::oxdock_embed;
11
12oxdock_embed! {
17 name: UsageAssets,
18 script: {
19 INHERIT_ENV [CARGO_PKG_VERSION, CARGO_PKG_DESCRIPTION]
20 FUNC EMIT_BODY($is_net: BOOL): VOID {
21 LET $net_flags: STRING = """
22 --offline open no sockets (endpoint flags require the `net` feature)
23 """
24
25 LET $tail: STRING = """
26 Endpoint flags (--listen/-p) require the `net` feature (rebuild with --features net).
27 """
28
29 IF $is_net {
30 $net_flags = """
31 --listen <addr> expose a logical service port ([host:]port, repeatable)
32 -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)
33 --offline open no sockets (conflicts with --listen/-p)
34 --remote TARGET=CMD bind a REMOTE target to a stdio transport command (repeatable)
35 """
36 $tail = "Scripts declare logical endpoints (a port like 2251); the flags above map them to interfaces."
37 }
38
39 ECHO """
40 oxdock {{ env:CARGO_PKG_VERSION }} — {{ env:CARGO_PKG_DESCRIPTION }}
41
42 Usage: oxdock [OPTIONS] [--] [SCRIPT] [-- script-args...]
43 SCRIPT script file path (same as `--script <file>`); `-` reads stdin
44 --script <file|-> script file (relative resolves under the OxDock workspace root), or `-` for stdin
45 --shell run the script, then drop into an interactive shell (requires a TTY)
46 --dry-run print the transitive import closure (file:line CMD) instead of running
47 --mcp serve EXPORTed FUNCs as MCP tools over stdio (requires the `mcp` feature)
48 {{ $net_flags }}
49 --help, -h print this help and exit
50 --version, -V, -v print the version and exit
51
52 With no script given, reads the script from stdin (must be piped unless `--shell`).
53 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.
54 {{ $tail }}
55 """
56 }
57
58 LET $p: PIPE
59 WITH_IO [stdout=$p] {
60 EMIT_BODY(true)
61 }
62 WITH_IO [stdin=$p] WRITE dist/usage-net.txt
63 WITH_IO [stdout=$p] {
64 EMIT_BODY(false)
65 }
66 WITH_IO [stdin=$p] WRITE dist/usage-lean.txt
67 },
68 out_dir: "usage-prebuilt",
69}
70
71pub fn usage() -> String {
77 #[cfg(feature = "net")]
78 let asset = "dist/usage-net.txt";
79 #[cfg(not(feature = "net"))]
80 let asset = "dist/usage-lean.txt";
81 let file = UsageAssets::get(asset).expect("usage must be embedded");
82 str::from_utf8(file.data.as_ref())
83 .expect("usage is UTF-8")
84 .to_string()
85}
86
87pub fn help_requested() -> Diagnostic {
92 Diagnostic::new("cli.help_requested", usage(), Diagnostic::make_fields(&[]))
93}
94
95pub fn version_requested() -> Diagnostic {
98 Diagnostic::new(
99 "cli.version_requested",
100 super::version(),
101 Diagnostic::make_fields(&[]),
102 )
103}
104
105pub fn feature_required(flag: &str, feature: &str) -> Diagnostic {
108 let message =
109 format!("{flag} requires the `{feature}` feature (rebuild with --features {feature})");
110 Diagnostic::new(
111 "cli.feature_required",
112 message,
113 Diagnostic::make_fields(&[("flag", flag), ("feature", feature)]),
114 )
115}
116
117pub fn offline_conflict() -> Diagnostic {
119 Diagnostic::new(
120 "cli.offline_conflict",
121 "--offline conflicts with --listen/-p (offline runs open no sockets)".to_string(),
122 Diagnostic::make_fields(&[]),
123 )
124}
125
126pub fn endpoint_duplicate_inner(endpoint: &str) -> Diagnostic {
128 let message = format!("virtual endpoint '{endpoint}' is mapped twice");
129 Diagnostic::new(
130 "cli.endpoint_duplicate_inner",
131 message,
132 Diagnostic::make_fields(&[("endpoint", endpoint)]),
133 )
134}
135
136pub fn endpoint_duplicate_outer(addr: &str) -> Diagnostic {
138 let message = format!("outer socket address {addr} is mapped twice");
139 Diagnostic::new(
140 "cli.endpoint_duplicate_outer",
141 message,
142 Diagnostic::make_fields(&[("address", addr)]),
143 )
144}
145
146pub fn listen_requires_address() -> Diagnostic {
148 Diagnostic::new(
149 "cli.listen_requires_address",
150 "--listen requires an address ([host:]port)".to_string(),
151 Diagnostic::make_fields(&[("flag", "--listen")]),
152 )
153}
154
155pub fn listen_port_numeric(raw: &str) -> Diagnostic {
157 let message = format!("--listen port must be numeric 1-65535, got {raw:?}");
158 Diagnostic::new(
159 "cli.listen_port_numeric",
160 message,
161 Diagnostic::make_fields(&[("flag", "--listen"), ("got", raw)]),
162 )
163}
164
165pub fn listen_port_zero() -> Diagnostic {
168 Diagnostic::new(
169 "cli.listen_port_zero",
170 "--listen port 0 has no stable service key (use -p 0:<endpoint> for an ephemeral outer port)"
171 .to_string(),
172 Diagnostic::make_fields(&[("flag", "--listen")]),
173 )
174}
175
176pub fn listen_address_invalid(raw: &str) -> Diagnostic {
178 let message = format!("--listen address must be [host:]port (try 0.0.0.0:2251), got {raw:?}");
179 Diagnostic::new(
180 "cli.listen_address_invalid",
181 message,
182 Diagnostic::make_fields(&[("flag", "--listen"), ("got", raw)]),
183 )
184}
185
186pub fn publish_requires_shape(raw: &str) -> Diagnostic {
188 let message = format!(
189 "-p requires [host:]outer:inner (try -p 2222:2251 or -p 127.0.0.1:1234:ssh-server), got {raw:?}"
190 );
191 Diagnostic::new(
192 "cli.publish_requires_shape",
193 message,
194 Diagnostic::make_fields(&[("flag", "-p"), ("got", raw)]),
195 )
196}
197
198pub fn publish_outer_invalid(raw: &str) -> Diagnostic {
200 let message = format!(
201 "-p outer address invalid in {raw:?} (expected a port 0-65535 or a [host:]port address)"
202 );
203 Diagnostic::new(
204 "cli.publish_outer_invalid",
205 message,
206 Diagnostic::make_fields(&[("flag", "-p"), ("got", raw)]),
207 )
208}
209
210#[cfg(feature = "net")]
218pub fn publish_inner_invalid(raw: &str, reason: Diagnostic) -> Diagnostic {
219 let reason_text = flatten_chain(&reason);
220 let message = format!("-p inner endpoint invalid in {raw:?}: {reason_text}");
221 Diagnostic::new(
222 "cli.publish_inner_invalid",
223 message,
224 Diagnostic::make_fields(&[("flag", "-p"), ("got", raw), ("reason", &reason_text)]),
225 )
226}
227
228pub fn module_unlinked(module: &str, feature: &str) -> Diagnostic {
231 let message =
232 format!("module `{module}` is known but not linked (rebuild with --features {feature})");
233 Diagnostic::new(
234 "cli.module_unlinked",
235 message,
236 Diagnostic::make_fields(&[("module", module), ("feature", feature)]),
237 )
238}
239
240pub fn unexpected_flag(name: &str, long: bool) -> Diagnostic {
243 let prefix = if long { "--" } else { "-" };
244 let message = format!("unexpected flag: {prefix}{name}");
245 Diagnostic::new(
246 "cli.unexpected_flag",
247 message,
248 Diagnostic::make_fields(&[("flag", name)]),
249 )
250}
251
252pub fn script_flag_needs_path() -> Diagnostic {
254 Diagnostic::new(
255 "cli.script_flag_needs_path",
256 "--script requires a path".to_string(),
257 Diagnostic::make_fields(&[("flag", "--script")]),
258 )
259}
260
261pub fn script_not_local(text: &str) -> Diagnostic {
263 let message = format!("not a local path: {text:?} (pass a workspace path or `-` for stdin)");
264 Diagnostic::new(
265 "cli.script_not_local",
266 message,
267 Diagnostic::make_fields(&[("got", text)]),
268 )
269}
270
271pub fn arg_not_utf8() -> Diagnostic {
273 Diagnostic::new(
274 "cli.arg_not_utf8",
275 "argument must be UTF-8".to_string(),
276 Diagnostic::make_fields(&[]),
277 )
278}
279
280pub fn arg_lex_error(reason: &lexopt::Error) -> Diagnostic {
284 let reason_text = format!("{reason:#}");
285 Diagnostic::new(
286 "cli.arg_lex_error",
287 reason_text.clone(),
288 Diagnostic::make_fields(&[("reason", &reason_text)]),
289 )
290}
291
292pub fn remote_requires_value() -> Diagnostic {
294 Diagnostic::new(
295 "cli.remote_requires_value",
296 "--remote requires <target>=\"<command>\"".to_string(),
297 Diagnostic::make_fields(&[("flag", "--remote")]),
298 )
299}
300
301#[cfg(not(feature = "net"))]
304pub fn remote_requires_shape(raw: &str) -> Diagnostic {
305 let message = format!("invalid --remote {raw:?}: expected <target>=\"<command>\"");
306 Diagnostic::new(
307 "cli.remote_requires_shape",
308 message,
309 Diagnostic::make_fields(&[("flag", "--remote"), ("got", raw)]),
310 )
311}
312
313pub fn script_stdin_missing() -> Diagnostic {
315 Diagnostic::new(
316 "cli.script_stdin_missing",
317 "no stdin detected; pass --script <file> or pipe a script into stdin (use --script - if explicit)"
318 .to_string(),
319 Diagnostic::make_fields(&[]),
320 )
321}
322
323pub fn guard_workspace_root(reason: &Diagnostic) -> Diagnostic {
326 let reason_text = flatten_chain(reason);
327 let message = format!("guard workspace root: {reason_text}");
328 Diagnostic::new(
329 "cli.guard_workspace_root",
330 message,
331 Diagnostic::make_fields(&[("reason", &reason_text)]),
332 )
333}
334
335pub fn guard_script_path(path: &str, reason: &Diagnostic) -> Diagnostic {
338 let reason_text = flatten_chain(reason);
339 let message = format!("guard script path {path}: {reason_text}");
340 Diagnostic::new(
341 "cli.guard_script_path",
342 message,
343 Diagnostic::make_fields(&[("path", path), ("reason", &reason_text)]),
344 )
345}
346
347pub fn read_script_at(path: &str, reason: &Diagnostic) -> Diagnostic {
351 let reason_text = flatten_chain(reason);
352 let message = format!("failed to read script at {path}: {reason_text}");
353 Diagnostic::new(
354 "cli.read_script_at",
355 message,
356 Diagnostic::make_fields(&[("path", path), ("reason", &reason_text)]),
357 )
358}
359
360pub fn read_script_stdin(reason: &std::io::Error) -> Diagnostic {
364 let reason_text = format!("{reason:#}");
365 let message = format!("failed to read script from stdin: {reason_text}");
366 Diagnostic::new(
367 "cli.read_script_stdin",
368 message,
369 Diagnostic::make_fields(&[("reason", &reason_text)]),
370 )
371}
372
373pub fn create_shell_temp_dir(reason: &Diagnostic) -> Diagnostic {
376 let reason_text = flatten_chain(reason);
377 let message = format!("failed to create shell temp dir: {reason_text}");
378 Diagnostic::new(
379 "cli.create_shell_temp_dir",
380 message,
381 Diagnostic::make_fields(&[("reason", &reason_text)]),
382 )
383}
384
385pub fn shell_needs_tty() -> Diagnostic {
387 Diagnostic::new(
388 "cli.shell_needs_tty",
389 "--shell requires a tty (no controlling tty available)".to_string(),
390 Diagnostic::make_fields(&[("flag", "--shell")]),
391 )
392}
393
394pub fn shell_unsupported_in_result() -> Diagnostic {
397 Diagnostic::new(
398 "cli.shell_unsupported_in_result",
399 "execute_with_result does not support --shell".to_string(),
400 Diagnostic::make_fields(&[]),
401 )
402}
403
404#[cfg(windows)]
407pub fn shell_temp_missing_parent() -> Diagnostic {
408 Diagnostic::new(
409 "cli.shell_temp_missing_parent",
410 "temp path unexpectedly missing parent".to_string(),
411 Diagnostic::make_fields(&[]),
412 )
413}
414
415#[cfg(windows)]
419pub fn determine_current_exe(reason: &std::io::Error) -> Diagnostic {
420 let reason_text = format!("{reason:#}");
421 let message = format!("determine current executable: {reason_text}");
422 Diagnostic::new(
423 "cli.determine_current_exe",
424 message,
425 Diagnostic::make_fields(&[("reason", &reason_text)]),
426 )
427}
428
429#[cfg(windows)]
432pub fn guard_system_temp_dir(reason: &Diagnostic) -> Diagnostic {
433 let reason_text = flatten_chain(reason);
434 let message = format!("guard system temp dir: {reason_text}");
435 Diagnostic::new(
436 "cli.guard_system_temp_dir",
437 message,
438 Diagnostic::make_fields(&[("reason", &reason_text)]),
439 )
440}
441
442#[cfg(windows)]
445pub fn construct_temp_shell_path(reason: &Diagnostic) -> Diagnostic {
446 let reason_text = flatten_chain(reason);
447 let message = format!("construct temp shell path: {reason_text}");
448 Diagnostic::new(
449 "cli.construct_temp_shell_path",
450 message,
451 Diagnostic::make_fields(&[("reason", &reason_text)]),
452 )
453}
454
455#[cfg(windows)]
458pub fn copy_shell_runner(dest: &str, reason: &Diagnostic) -> Diagnostic {
459 let reason_text = flatten_chain(reason);
460 let message = format!("failed to copy shell runner to {dest}: {reason_text}");
461 Diagnostic::new(
462 "cli.copy_shell_runner",
463 message,
464 Diagnostic::make_fields(&[("dest", dest), ("reason", &reason_text)]),
465 )
466}
467
468#[cfg(windows)]
472pub fn spawn_shell_from(dest: &str, reason: &Diagnostic) -> Diagnostic {
473 let reason_text = flatten_chain(reason);
474 let message = format!("failed to spawn shell from {dest}: {reason_text}");
475 Diagnostic::new(
476 "cli.spawn_shell_from",
477 message,
478 Diagnostic::make_fields(&[("dest", dest), ("reason", &reason_text)]),
479 )
480}
481
482#[cfg(feature = "net")]
484pub fn remote_serve_runs_alone() -> Diagnostic {
485 Diagnostic::new(
486 "cli.remote_serve_runs_alone",
487 "--remote-serve runs alone (no --script, --shell, --remote, or script args)".to_string(),
488 Diagnostic::make_fields(&[("flag", "--remote-serve")]),
489 )
490}
491
492#[cfg(feature = "net")]
494pub fn remote_serve_needs_stdio() -> Diagnostic {
495 Diagnostic::new(
496 "cli.remote_serve_needs_stdio",
497 "--remote-serve requires piped stdio (no TTY)".to_string(),
498 Diagnostic::make_fields(&[("flag", "--remote-serve")]),
499 )
500}
501
502#[cfg(feature = "net")]
505pub fn fetch_tar_no_stage() -> Diagnostic {
506 Diagnostic::new(
507 "cli.serve_fetch_tar_no_stage",
508 "serve fetch tar arrived with no stage".to_string(),
509 Diagnostic::make_fields(&[]),
510 )
511}
512
513#[cfg(feature = "net")]
517pub fn fetch_dir_escapes(path: &str, reason: &Diagnostic) -> Diagnostic {
518 let reason_text = flatten_chain(reason);
519 let message = format!("fetch dir {path:?} escapes staging: {reason_text}");
520 Diagnostic::new(
521 "cli.serve_fetch_dir_escapes",
522 message,
523 Diagnostic::make_fields(&[("path", path), ("reason", &reason_text)]),
524 )
525}
526
527#[cfg(feature = "net")]
529pub fn fetch_file_escapes(path: &str, reason: &Diagnostic) -> Diagnostic {
530 let reason_text = flatten_chain(reason);
531 let message = format!("fetch file {path:?} escapes staging: {reason_text}");
532 Diagnostic::new(
533 "cli.serve_fetch_file_escapes",
534 message,
535 Diagnostic::make_fields(&[("path", path), ("reason", &reason_text)]),
536 )
537}
538
539#[cfg(feature = "net")]
541pub fn fetch_parent_escapes(path: &str, reason: &Diagnostic) -> Diagnostic {
542 let reason_text = flatten_chain(reason);
543 let message = format!("fetch parent {path:?} escapes staging: {reason_text}");
544 Diagnostic::new(
545 "cli.serve_fetch_parent_escapes",
546 message,
547 Diagnostic::make_fields(&[("path", path), ("reason", &reason_text)]),
548 )
549}
550
551#[cfg(feature = "net")]
554pub fn fetch_symlink_refused(path: &str, target: &str) -> Diagnostic {
555 let message = format!("fetch symlink {path:?} -> {target:?} refused: fetch entries are files");
556 Diagnostic::new(
557 "cli.serve_fetch_symlink_refused",
558 message,
559 Diagnostic::make_fields(&[("path", path), ("target", target)]),
560 )
561}
562
563#[cfg(feature = "net")]
568pub fn serve_fetch_spill_write_failed(reason: &std::io::Error) -> Diagnostic {
569 let reason_text = format!("{reason:#}");
570 let message = format!("serve fetch spill write failed: {reason_text}");
571 Diagnostic::new(
572 "cli.serve_fetch_spill_write_failed",
573 message,
574 Diagnostic::make_fields(&[("reason", &reason_text)]),
575 )
576}
577
578#[cfg(feature = "net")]
583pub fn serve_fetch_member_failed(path: &str, reason: &std::io::Error) -> Diagnostic {
584 let reason_text = format!("{reason:#}");
585 let message = format!("fetch member {path:?} failed: {reason_text}");
586 Diagnostic::new(
587 "cli.serve_fetch_member_failed",
588 message,
589 Diagnostic::make_fields(&[("path", path), ("reason", &reason_text)]),
590 )
591}
592
593#[cfg(feature = "net")]
595pub fn serve_engine_panicked() -> Diagnostic {
596 Diagnostic::new(
597 "cli.serve_engine_panicked",
598 "serve engine thread panicked".to_string(),
599 Diagnostic::make_fields(&[]),
600 )
601}
602
603#[cfg(feature = "net")]
608pub fn serve_handler_register_failed(
609 method: &str,
610 reason: &muxio_rpc_service_endpoint::error::RpcServiceEndpointError,
611) -> Diagnostic {
612 let reason_text = format!("{reason:#}");
613 let message = format!("serve handler registration for {method} failed: {reason_text}");
614 Diagnostic::new(
615 "cli.serve_handler_register_failed",
616 message,
617 Diagnostic::make_fields(&[("method", method), ("reason", &reason_text)]),
618 )
619}
620
621#[cfg(feature = "net")]
624pub fn serve_transfer_tempdir_failed(reason: &Diagnostic) -> Diagnostic {
625 let reason_text = flatten_chain(reason);
626 let message = format!("serve transfer tempdir failed: {reason_text}");
627 Diagnostic::new(
628 "cli.serve_transfer_tempdir_failed",
629 message,
630 Diagnostic::make_fields(&[("reason", &reason_text)]),
631 )
632}
633
634#[cfg(feature = "net")]
637pub fn serve_fetch_descriptor_decode_failed(reason: &serde_json::Error) -> Diagnostic {
638 let reason_text = reason.to_string();
639 let message = format!("serve fetch descriptor decode failed: {reason_text}");
640 Diagnostic::new(
641 "cli.serve_fetch_descriptor_decode_failed",
642 message,
643 Diagnostic::make_fields(&[("reason", &reason_text)]),
644 )
645}
646
647#[cfg(feature = "net")]
650pub fn serve_staging_tempdir_failed(reason: &Diagnostic) -> Diagnostic {
651 let reason_text = flatten_chain(reason);
652 let message = format!("serve staging tempdir failed: {reason_text}");
653 Diagnostic::new(
654 "cli.serve_staging_tempdir_failed",
655 message,
656 Diagnostic::make_fields(&[("reason", &reason_text)]),
657 )
658}
659
660#[cfg(feature = "net")]
663pub fn serve_push_tempdir_failed(reason: &Diagnostic) -> Diagnostic {
664 let reason_text = flatten_chain(reason);
665 let message = format!("serve push tempdir failed: {reason_text}");
666 Diagnostic::new(
667 "cli.serve_push_tempdir_failed",
668 message,
669 Diagnostic::make_fields(&[("reason", &reason_text)]),
670 )
671}
672
673#[cfg(feature = "net")]
677pub fn serve_push_spill_flush_failed(reason: &std::io::Error) -> Diagnostic {
678 let reason_text = format!("{reason:#}");
679 let message = format!("serve push spill flush failed: {reason_text}");
680 Diagnostic::new(
681 "cli.serve_push_spill_flush_failed",
682 message,
683 Diagnostic::make_fields(&[("reason", &reason_text)]),
684 )
685}
686
687#[cfg(feature = "net")]
691pub fn serve_push_spill_hash_failed(reason: &std::io::Error) -> Diagnostic {
692 let reason_text = format!("{reason:#}");
693 let message = format!("serve push spill hash failed: {reason_text}");
694 Diagnostic::new(
695 "cli.serve_push_spill_hash_failed",
696 message,
697 Diagnostic::make_fields(&[("reason", &reason_text)]),
698 )
699}
700
701#[cfg(feature = "net")]
704pub fn push_source_escapes(guest_src: &str, reason: &Diagnostic) -> Diagnostic {
705 let reason_text = flatten_chain(reason);
706 let message = format!("guest push source {guest_src:?} escapes staging: {reason_text}");
707 Diagnostic::new(
708 "cli.serve_push_source_escapes",
709 message,
710 Diagnostic::make_fields(&[("source", guest_src), ("reason", &reason_text)]),
711 )
712}
713
714#[cfg(feature = "net")]
717pub fn push_source_unreadable(path: &str, reason: &Diagnostic) -> Diagnostic {
718 let reason_text = flatten_chain(reason);
719 let message = format!("guest push source {path:?} unreadable: {reason_text}");
720 Diagnostic::new(
721 "cli.serve_push_source_unreadable",
722 message,
723 Diagnostic::make_fields(&[("source", path), ("reason", &reason_text)]),
724 )
725}
726
727#[cfg(feature = "net")]
729pub fn push_source_missing(guest_src: &str, reason: &Diagnostic) -> Diagnostic {
730 let reason_text = flatten_chain(reason);
731 let message = format!("guest push source {guest_src:?} missing: {reason_text}");
732 Diagnostic::new(
733 "cli.serve_push_source_missing",
734 message,
735 Diagnostic::make_fields(&[("source", guest_src), ("reason", &reason_text)]),
736 )
737}
738
739#[cfg(feature = "mcp")]
741pub fn mcp_needs_stdio() -> Diagnostic {
742 Diagnostic::new(
743 "cli.mcp_needs_stdio",
744 "--mcp requires piped stdio transport".to_string(),
745 Diagnostic::make_fields(&[("flag", "--mcp")]),
746 )
747}
748
749#[cfg(feature = "mcp")]
751pub fn mcp_needs_script_file() -> Diagnostic {
752 Diagnostic::new(
753 "cli.mcp_needs_script_file",
754 "--mcp requires a script file (stdin carries the JSON-RPC stream)".to_string(),
755 Diagnostic::make_fields(&[("flag", "--mcp")]),
756 )
757}
758
759#[cfg(feature = "mcp")]
762pub fn mcp_runs_alone(conflicts: &[&str]) -> Diagnostic {
763 let joined = conflicts.join(", ");
764 let message = format!("--mcp runs alone (no {joined})");
765 Diagnostic::new(
766 "cli.mcp_runs_alone",
767 message,
768 Diagnostic::make_fields(&[("flag", "--mcp"), ("conflicts", &joined)]),
769 )
770}
771
772#[cfg(test)]
773mod tests {
774 use super::*;
775
776 #[cfg(feature = "net")]
777 fn serde_err() -> serde_json::Error {
778 serde_json::from_slice::<serde_json::Value>(b"{bad}").unwrap_err()
779 }
780
781 fn sample_reason(text: &str) -> Diagnostic {
785 Diagnostic::new("t.reason", text.to_string(), Diagnostic::make_fields(&[]))
786 }
787
788 fn io_err() -> std::io::Error {
792 std::io::Error::new(std::io::ErrorKind::BrokenPipe, "boom")
793 }
794
795 #[test]
796 fn feature_message_shared_across_sites() {
797 for (flag, feature) in [
798 ("--listen", "net"),
799 ("-p", "net"),
800 ("script module", "net"),
801 ("--remote-serve", "net"),
802 ("--mcp", "mcp"),
803 ] {
804 let diag = feature_required(flag, feature);
805 assert_eq!(diag.key(), "cli.feature_required");
806 assert!(diag.message().contains(flag), "{diag}");
807 assert!(diag.message().contains(feature), "{diag}");
808 }
809 }
810
811 #[test]
812 fn mcp_feature_gate_names_mcp_not_net() {
813 let diag = feature_required("--mcp", "mcp");
814 let text = diag.message();
815 assert!(text.contains("`mcp`"), "{diag}");
816 assert!(text.contains("--features mcp"), "{diag}");
817 assert!(!text.contains("net"), "{diag}");
818 }
819
820 #[test]
821 fn endpoint_duplicates_name_the_key() {
822 let inner = endpoint_duplicate_inner("2251");
823 assert_eq!(inner.key(), "cli.endpoint_duplicate_inner");
824
825 let outer = endpoint_duplicate_outer("127.0.0.1:2222");
826 assert_eq!(outer.key(), "cli.endpoint_duplicate_outer");
827 }
828
829 #[test]
830 fn untrusted_input_cannot_crash_cli_errors() {
831 let evil = "bad\u{1f}flag".to_string();
832 let diag = feature_required(&evil, "net");
833 assert!(!diag.message().contains('\u{1f}'));
834 }
835
836 #[test]
837 fn net_shape_errors_carry_stable_keys() {
838 let cases: &[(&str, anyhow::Error)] = &[
839 (
840 "cli.listen_requires_address",
841 anyhow::Error::from(listen_requires_address()),
842 ),
843 (
844 "cli.listen_port_numeric",
845 anyhow::Error::from(listen_port_numeric("abc")),
846 ),
847 (
848 "cli.listen_port_zero",
849 anyhow::Error::from(listen_port_zero()),
850 ),
851 (
852 "cli.listen_address_invalid",
853 anyhow::Error::from(listen_address_invalid("0.0.0.0:99999")),
854 ),
855 (
856 "cli.publish_requires_shape",
857 anyhow::Error::from(publish_requires_shape("2251")),
858 ),
859 (
860 "cli.publish_outer_invalid",
861 anyhow::Error::from(publish_outer_invalid("abc:2251")),
862 ),
863 #[cfg(feature = "net")]
866 (
867 "cli.publish_inner_invalid",
868 anyhow::Error::from(publish_inner_invalid(
869 "2222:0",
870 Diagnostic::new(
871 "t.reason",
872 "bad endpoint".to_string(),
873 Diagnostic::make_fields(&[]),
874 ),
875 )),
876 ),
877 (
878 "cli.module_unlinked",
879 anyhow::Error::from(module_unlinked("NET", "net")),
880 ),
881 (
882 "cli.unexpected_flag",
883 anyhow::Error::from(unexpected_flag("frobnicate", true)),
884 ),
885 (
886 "cli.script_flag_needs_path",
887 anyhow::Error::from(script_flag_needs_path()),
888 ),
889 (
890 "cli.script_not_local",
891 anyhow::Error::from(script_not_local("https://x")),
892 ),
893 ("cli.arg_not_utf8", anyhow::Error::from(arg_not_utf8())),
894 (
895 "cli.arg_lex_error",
896 anyhow::Error::from(arg_lex_error(&lexopt::Error::UnexpectedOption(
897 "--nope".to_string(),
898 ))),
899 ),
900 (
901 "cli.remote_requires_value",
902 anyhow::Error::from(remote_requires_value()),
903 ),
904 #[cfg(not(feature = "net"))]
905 (
906 "cli.remote_requires_shape",
907 anyhow::Error::from(remote_requires_shape("prod")),
908 ),
909 (
910 "cli.script_stdin_missing",
911 anyhow::Error::from(script_stdin_missing()),
912 ),
913 (
914 "cli.shell_needs_tty",
915 anyhow::Error::from(shell_needs_tty()),
916 ),
917 (
918 "cli.shell_unsupported_in_result",
919 anyhow::Error::from(shell_unsupported_in_result()),
920 ),
921 #[cfg(windows)]
922 (
923 "cli.shell_temp_missing_parent",
924 anyhow::Error::from(shell_temp_missing_parent()),
925 ),
926 #[cfg(feature = "net")]
927 (
928 "cli.remote_serve_runs_alone",
929 anyhow::Error::from(remote_serve_runs_alone()),
930 ),
931 #[cfg(feature = "net")]
932 (
933 "cli.remote_serve_needs_stdio",
934 anyhow::Error::from(remote_serve_needs_stdio()),
935 ),
936 #[cfg(feature = "net")]
937 (
938 "cli.serve_fetch_tar_no_stage",
939 anyhow::Error::from(fetch_tar_no_stage()),
940 ),
941 #[cfg(feature = "net")]
942 (
943 "cli.serve_fetch_dir_escapes",
944 anyhow::Error::from(fetch_dir_escapes("a", &sample_reason("root"))),
945 ),
946 #[cfg(feature = "net")]
947 (
948 "cli.serve_fetch_file_escapes",
949 anyhow::Error::from(fetch_file_escapes("a", &sample_reason("root"))),
950 ),
951 #[cfg(feature = "net")]
952 (
953 "cli.serve_fetch_parent_escapes",
954 anyhow::Error::from(fetch_parent_escapes("a", &sample_reason("root"))),
955 ),
956 #[cfg(feature = "net")]
957 (
958 "cli.serve_fetch_symlink_refused",
959 anyhow::Error::from(fetch_symlink_refused("a", "b")),
960 ),
961 #[cfg(feature = "net")]
962 (
963 "cli.serve_fetch_spill_write_failed",
964 anyhow::Error::from(serve_fetch_spill_write_failed(&io_err())),
965 ),
966 #[cfg(feature = "net")]
967 (
968 "cli.serve_fetch_member_failed",
969 anyhow::Error::from(serve_fetch_member_failed("a", &io_err())),
970 ),
971 #[cfg(feature = "net")]
972 (
973 "cli.serve_engine_panicked",
974 anyhow::Error::from(serve_engine_panicked()),
975 ),
976 #[cfg(feature = "net")]
977 (
978 "cli.serve_handler_register_failed",
979 anyhow::Error::from(serve_handler_register_failed(
980 "exec",
981 &muxio_rpc_service_endpoint::error::RpcServiceEndpointError::Handler(Box::new(
982 io_err(),
983 )),
984 )),
985 ),
986 #[cfg(feature = "net")]
987 (
988 "cli.serve_transfer_tempdir_failed",
989 anyhow::Error::from(serve_transfer_tempdir_failed(&sample_reason("boom"))),
990 ),
991 #[cfg(feature = "net")]
992 (
993 "cli.serve_fetch_descriptor_decode_failed",
994 anyhow::Error::from(serve_fetch_descriptor_decode_failed(&serde_err())),
995 ),
996 #[cfg(feature = "net")]
997 (
998 "cli.serve_staging_tempdir_failed",
999 anyhow::Error::from(serve_staging_tempdir_failed(&sample_reason("boom"))),
1000 ),
1001 #[cfg(feature = "net")]
1002 (
1003 "cli.serve_push_tempdir_failed",
1004 anyhow::Error::from(serve_push_tempdir_failed(&sample_reason("boom"))),
1005 ),
1006 #[cfg(feature = "net")]
1007 (
1008 "cli.serve_push_spill_flush_failed",
1009 anyhow::Error::from(serve_push_spill_flush_failed(&io_err())),
1010 ),
1011 #[cfg(feature = "net")]
1012 (
1013 "cli.serve_push_spill_hash_failed",
1014 anyhow::Error::from(serve_push_spill_hash_failed(&io_err())),
1015 ),
1016 #[cfg(feature = "net")]
1017 (
1018 "cli.serve_push_source_escapes",
1019 anyhow::Error::from(push_source_escapes("a", &sample_reason("root"))),
1020 ),
1021 #[cfg(feature = "net")]
1022 (
1023 "cli.serve_push_source_unreadable",
1024 anyhow::Error::from(push_source_unreadable("a", &sample_reason("root"))),
1025 ),
1026 #[cfg(feature = "net")]
1027 (
1028 "cli.serve_push_source_missing",
1029 anyhow::Error::from(push_source_missing("a", &sample_reason("root"))),
1030 ),
1031 #[cfg(feature = "mcp")]
1032 (
1033 "cli.mcp_needs_stdio",
1034 anyhow::Error::from(mcp_needs_stdio()),
1035 ),
1036 #[cfg(feature = "mcp")]
1037 (
1038 "cli.mcp_needs_script_file",
1039 anyhow::Error::from(mcp_needs_script_file()),
1040 ),
1041 #[cfg(feature = "mcp")]
1042 (
1043 "cli.mcp_runs_alone",
1044 anyhow::Error::from(mcp_runs_alone(&["--shell"])),
1045 ),
1046 (
1047 "cli.guard_workspace_root",
1048 anyhow::Error::from(guard_workspace_root(&sample_reason("boom"))),
1049 ),
1050 (
1051 "cli.guard_script_path",
1052 anyhow::Error::from(guard_script_path("s.ox", &sample_reason("boom"))),
1053 ),
1054 (
1055 "cli.read_script_at",
1056 anyhow::Error::from(read_script_at("s.ox", &sample_reason("boom"))),
1057 ),
1058 (
1059 "cli.read_script_stdin",
1060 anyhow::Error::from(read_script_stdin(&io_err())),
1061 ),
1062 (
1063 "cli.create_shell_temp_dir",
1064 anyhow::Error::from(create_shell_temp_dir(&sample_reason("boom"))),
1065 ),
1066 #[cfg(windows)]
1067 (
1068 "cli.determine_current_exe",
1069 anyhow::Error::from(determine_current_exe(&io_err())),
1070 ),
1071 #[cfg(windows)]
1072 (
1073 "cli.guard_system_temp_dir",
1074 anyhow::Error::from(guard_system_temp_dir(&sample_reason("boom"))),
1075 ),
1076 #[cfg(windows)]
1077 (
1078 "cli.construct_temp_shell_path",
1079 anyhow::Error::from(construct_temp_shell_path(&sample_reason("boom"))),
1080 ),
1081 #[cfg(windows)]
1082 (
1083 "cli.copy_shell_runner",
1084 anyhow::Error::from(copy_shell_runner("d.exe", &sample_reason("boom"))),
1085 ),
1086 #[cfg(windows)]
1087 (
1088 "cli.spawn_shell_from",
1089 anyhow::Error::from(spawn_shell_from("d.exe", &sample_reason("boom"))),
1090 ),
1091 ("cli.help_requested", anyhow::Error::from(help_requested())),
1092 (
1093 "cli.version_requested",
1094 anyhow::Error::from(version_requested()),
1095 ),
1096 ];
1097 let mut seen = std::collections::HashSet::new();
1098 for (key, err) in cases {
1099 let diag = err
1100 .downcast_ref::<Diagnostic>()
1101 .expect("CLI errors stay typed");
1102 assert_eq!(&diag.key(), key, "{diag}");
1103 assert!(seen.insert(*key), "duplicate key {key}");
1104 }
1105 }
1106
1107 #[test]
1108 fn flag_shape_messages_name_the_flag() {
1109 assert_eq!(
1110 unexpected_flag("frobnicate", true).message(),
1111 "unexpected flag: --frobnicate"
1112 );
1113 assert_eq!(unexpected_flag("z", false).message(), "unexpected flag: -z");
1114 assert_eq!(
1115 script_flag_needs_path().message(),
1116 "--script requires a path"
1117 );
1118 assert!(
1119 script_not_local("https://x/y")
1120 .to_string()
1121 .contains("\"https://x/y\"")
1122 );
1123 assert!(
1124 arg_lex_error(&lexopt::Error::UnexpectedOption("--nope".to_string()))
1125 .to_string()
1126 .contains("--nope")
1127 );
1128 assert_eq!(
1129 remote_requires_value().message(),
1130 "--remote requires <target>=\"<command>\""
1131 );
1132 #[cfg(feature = "mcp")]
1133 assert_eq!(
1134 mcp_runs_alone(&["--shell", "--remote"]).message(),
1135 "--mcp runs alone (no --shell, --remote)"
1136 );
1137 }
1138
1139 #[test]
1140 fn startup_context_messages_carry_outer_text() {
1141 assert_eq!(
1142 guard_workspace_root(&sample_reason("boom")).message(),
1143 "guard workspace root: boom"
1144 );
1145 assert_eq!(
1146 guard_script_path("s.ox", &sample_reason("boom")).message(),
1147 "guard script path s.ox: boom"
1148 );
1149 assert_eq!(
1150 read_script_at("s.ox", &sample_reason("denied")).message(),
1151 "failed to read script at s.ox: denied"
1152 );
1153 assert_eq!(
1154 read_script_stdin(&io_err()).message(),
1155 "failed to read script from stdin: boom"
1156 );
1157 assert_eq!(
1158 create_shell_temp_dir(&sample_reason("boom")).message(),
1159 "failed to create shell temp dir: boom"
1160 );
1161 }
1162
1163 #[cfg(windows)]
1164 #[test]
1165 fn windows_reexec_messages_carry_outer_text() {
1166 assert_eq!(
1167 determine_current_exe(&io_err()).message(),
1168 "determine current executable: boom"
1169 );
1170 assert_eq!(
1171 guard_system_temp_dir(&sample_reason("boom")).message(),
1172 "guard system temp dir: boom"
1173 );
1174 assert_eq!(
1175 construct_temp_shell_path(&sample_reason("boom")).message(),
1176 "construct temp shell path: boom"
1177 );
1178 assert_eq!(
1179 copy_shell_runner("d.exe", &sample_reason("boom")).message(),
1180 "failed to copy shell runner to d.exe: boom"
1181 );
1182 assert_eq!(
1183 spawn_shell_from("d.exe", &sample_reason("boom")).message(),
1184 "failed to spawn shell from d.exe: boom"
1185 );
1186 }
1187
1188 #[cfg(feature = "mcp")]
1189 #[test]
1190 fn mcp_gate_messages() {
1191 assert_eq!(
1192 mcp_needs_stdio().message(),
1193 "--mcp requires piped stdio transport"
1194 );
1195 assert_eq!(
1196 mcp_needs_script_file().message(),
1197 "--mcp requires a script file (stdin carries the JSON-RPC stream)"
1198 );
1199 }
1200
1201 #[cfg(feature = "net")]
1202 #[test]
1203 fn serve_staging_messages_name_the_member() {
1204 let err = fetch_dir_escapes("a", &sample_reason("root"));
1205 assert!(err.to_string().contains("\"a\""), "{err}");
1206 assert!(err.to_string().contains("root"), "{err}");
1207 let err = fetch_symlink_refused("a", "b");
1208 assert_eq!(
1209 err.message(),
1210 "fetch symlink \"a\" -> \"b\" refused: fetch entries are files"
1211 );
1212 let err = push_source_unreadable("a", &sample_reason("denied"));
1213 assert!(err.to_string().contains("denied"), "{err}");
1214 assert_eq!(
1215 serve_transfer_tempdir_failed(&sample_reason("boom")).message(),
1216 "serve transfer tempdir failed: boom"
1217 );
1218 assert_eq!(
1219 serve_fetch_descriptor_decode_failed(&serde_err()).message(),
1220 "serve fetch descriptor decode failed: key must be a string at line 1 column 2"
1221 );
1222 assert_eq!(
1223 serve_staging_tempdir_failed(&sample_reason("boom")).message(),
1224 "serve staging tempdir failed: boom"
1225 );
1226 assert_eq!(
1227 serve_push_tempdir_failed(&sample_reason("boom")).message(),
1228 "serve push tempdir failed: boom"
1229 );
1230 assert_eq!(
1231 serve_fetch_spill_write_failed(&io_err()).message(),
1232 "serve fetch spill write failed: boom"
1233 );
1234 assert_eq!(
1235 serve_fetch_member_failed("a", &io_err()).message(),
1236 "fetch member \"a\" failed: boom"
1237 );
1238 assert_eq!(
1239 serve_push_spill_flush_failed(&io_err()).message(),
1240 "serve push spill flush failed: boom"
1241 );
1242 assert_eq!(
1243 serve_push_spill_hash_failed(&io_err()).message(),
1244 "serve push spill hash failed: boom"
1245 );
1246 }
1247
1248 #[test]
1249 fn net_shape_messages_name_the_flag_and_value() {
1250 assert_eq!(
1251 listen_requires_address().message(),
1252 "--listen requires an address ([host:]port)"
1253 );
1254 assert!(
1255 publish_requires_shape("2251")
1256 .to_string()
1257 .contains("\"2251\"")
1258 );
1259 #[cfg(feature = "net")]
1260 {
1261 let err = publish_inner_invalid(
1262 "2222:0",
1263 Diagnostic::new(
1264 "t.reason",
1265 "bad endpoint".to_string(),
1266 Diagnostic::make_fields(&[]),
1267 ),
1268 );
1269 let text = err.to_string();
1270 assert!(text.contains("\"2222:0\""), "{text}");
1271 assert!(text.contains("bad endpoint"), "{text}");
1272 }
1273 assert_eq!(
1274 module_unlinked("NET", "net").message(),
1275 "module `NET` is known but not linked (rebuild with --features net)"
1276 );
1277 }
1278}