Skip to main content

oxdock_cli/
errors.rs

1//! CLI user-facing errors behind single constructors with stable keys.
2//!
3//! This module owns every CLI template and key (`cli.*`). It builds on
4//! the generic [`oxdock_diagnostics::Diagnostic`] transport but owns its
5//! wording: changing CLI text happens here, never in the leaf crate.
6//! DSL diagnostics stay owned by the DSL crates.
7
8pub use oxdock_diagnostics::Diagnostic;
9use oxdock_diagnostics::flatten_chain;
10use oxdock_macros::oxdock_embed;
11
12// CLI usage bodies, one file per Cargo feature variant: statically
13// compiled assets cannot branch on Cargo features, so the script
14// renders both variants (version line included, through inherited
15// Cargo metadata) and Rust `#[cfg]` picks the live one.
16oxdock_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
71/// Human-readable CLI usage, printed for `--help`/`-h`. The full
72/// text, version line included, is rendered once at compile time by
73/// the `UsageAssets` script above. Feature selection stays in Rust
74/// `#[cfg]`: the DSL has no feature namespace, so each variant lives
75/// in its own asset file.
76pub 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
87/// `--help`/`-h`: not a failure, but surfaces through the parse
88/// result so `run()` can print usage and exit 0 without the parser
89/// exiting the process itself (public library API). Message is the
90/// full usage text byte for byte.
91pub fn help_requested() -> Diagnostic {
92    Diagnostic::new("cli.help_requested", usage(), Diagnostic::make_fields(&[]))
93}
94
95/// `--version`/`-V`/`-v`: same contract as `--help`. Message is the
96/// version line byte for byte.
97pub fn version_requested() -> Diagnostic {
98    Diagnostic::new(
99        "cli.version_requested",
100        super::version(),
101        Diagnostic::make_fields(&[]),
102    )
103}
104
105/// A flag needing a Cargo feature this build lacks. Takes the flag and
106/// the feature that would provide it; both ride as structured fields.
107pub 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
117/// `--offline` combined with socket flags.
118pub 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
126/// Same virtual endpoint mapped twice.
127pub 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
136/// Same outer socket address mapped twice.
137pub 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
146/// `--listen` without a value: an address is required.
147pub 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
155/// `--listen` with a non-numeric bare port.
156pub 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
165/// `--listen` port `0`: an exposed ephemeral port has no stable
166/// service key (that shape belongs to `-p 0:<endpoint>`).
167pub 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
176/// `--listen` with an unparsable socket address.
177pub 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
186/// `-p` without the `outer:inner` shape, or with an empty inner.
187pub 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
198/// `-p` with an unparsable outer address.
199pub 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/// `-p` inner endpoint rejected by the endpoint grammar. Takes the
211/// underlying `Diagnostic` and inlines its `flatten_chain` text so
212/// call sites stay `format!`-free. The inner is a leaf parse-surface
213/// failure (sourceless inline), so rendering the bare `Diagnostic`
214/// matches the retired anyhow-chain bytes exactly. Net-only: lean
215/// builds store the raw inner text and reject later with the feature
216/// error.
217#[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
228/// Script imports a module the CLI knows but this build did not link.
229/// Takes the module name and the Cargo feature that would link it.
230pub 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
240/// Unknown `--long` / `-s` flag. Takes the bare name plus whether it
241/// was long (`--name`) or short (`-n`); the template owns the dashes.
242pub 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
252/// `--script` without a value.
253pub 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
261/// SCRIPT positional that is a URL, not a workspace path.
262pub 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
271/// OS argument that is not valid UTF-8.
272pub 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
280/// lexopt plumbing failure. Takes the underlying failure;
281/// `{reason:#}` formatting lives here so call sites stay
282/// `format!`-free.
283pub 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
292/// `--remote` without a value.
293pub 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/// `--remote` without the `<target>="<command>"` shape (lean pure
302/// validator, kept live so the flag stays diagnosed without `net`).
303#[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
313/// No piped script on stdin and no `--shell` to justify emptiness.
314pub 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
323/// Workspace root discovery failed. Takes the underlying failure;
324/// the outer text matches the retired bare context byte for byte.
325pub 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
335/// Script path rejected by the workspace guard. Takes the path and
336/// the underlying failure; shared by both parse sites (one key).
337pub 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
347/// Script file unreadable. Takes the display path and the underlying
348/// failure; the outer text matches the retired bare context byte
349/// for byte.
350pub 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
360/// Piped script unreadable from stdin. Takes the underlying I/O
361/// failure; the text is inlined, so no source rides along. Shared
362/// by both read sites (one key).
363pub 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
373/// Shell snapshot directory failed to materialize. Takes the
374/// underlying failure; shared by both snapshot arms (one key).
375pub 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
385/// `--shell` without a controlling terminal.
386pub 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
394/// `--shell` through `execute_with_result`, which never supports it
395/// (embedder API misuse, not user input).
396pub 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/// Windows shell re-exec staging hit a temp path without a parent
405/// (internal invariant, not user input).
406#[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/// Windows shell re-exec could not locate the running executable.
416/// Takes the underlying I/O failure; the text is inlined, so no
417/// source rides along.
418#[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/// Windows shell re-exec could not guard the system temp dir.
430/// Takes the underlying failure.
431#[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/// Windows shell re-exec could not build the temp shell path.
443/// Takes the underlying failure.
444#[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/// Windows shell re-exec could not copy the runner binary. Takes
456/// the destination display path and the underlying failure.
457#[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/// Windows shell re-exec could not spawn the staged runner. Takes
469/// the destination display path and the spawn failure, which rides
470/// `Diagnostic` natively (spawn is `DiagnosticResult`).
471#[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/// `--remote-serve` combined with script/shell/remote inputs.
483#[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/// `--remote-serve` without piped stdio.
493#[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/// Fetch tar arrived with no staging section (guest protocol
503/// invariant, not user input).
504#[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/// Fetch tar dir member escapes staging. Takes the member path and the
514/// underlying failure; `flatten_chain` rendering lives here so call
515/// sites stay `format!`-free.
516#[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/// Fetch tar file member escapes staging.
528#[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/// Fetch tar parent directory escapes staging.
540#[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/// Fetch tar symlink member refused (content arrives resolved
552/// host-side; symlinks are never served).
553#[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/// Fetch spill write failure. Takes the underlying I/O failure; the
564/// text is inlined, so no source rides along. Sibling of
565/// `eval_remote_rewind_failed` for the serve feed loop (fetch tarball
566/// bytes into the spill stage).
567#[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/// Fetch member streaming I/O failure. Takes the member path and the
579/// underlying I/O failure; the text is inlined, so no source rides
580/// along. Shared by the prepare-staging member read/write sites (one
581/// key, one mode), mirroring `eval_unarchive_member_failed`.
582#[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/// Serve engine worker thread panicked (internal invariant).
594#[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/// Serve stream-handler registration failed. Takes the method name and
604/// the foreign endpoint failure; the text is inlined, so no source
605/// rides along. Sibling of the other serve I/O ctors (net module
606/// convention: cause text inlined without `with_source`).
607#[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/// Transfer staging tempdir failed. Takes the wrapped failure;
622/// `flatten_chain` rendering lives here.
623#[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/// Fetch descriptor decode failed. Takes the concrete serialization
635/// failure; rendering lives here.
636#[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/// Unpack staging tempdir failed. Takes the wrapped failure;
648/// `flatten_chain` rendering lives here.
649#[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/// Push staging tempdir failed. Takes the wrapped failure;
661/// `flatten_chain` rendering lives here.
662#[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/// Push spill flush failure. Takes the underlying I/O failure; the
674/// text is inlined, so no source rides along. Covers the packed-tar
675/// flush into the push spill before the hash pass.
676#[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/// Push spill hash failure. Takes the underlying I/O failure; the
688/// text is inlined, so no source rides along. Covers the re-read
689/// hash pass over the push spill.
690#[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/// Guest push source escapes staging. Takes the underlying failure;
702/// `flatten_chain` rendering lives here.
703#[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/// Guest push source unreadable. Takes the underlying failure;
715/// `flatten_chain` rendering lives here.
716#[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/// Guest push source missing from staging.
728#[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/// `--mcp` without piped stdio.
740#[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/// `--mcp` reading the script from stdin (stdin carries JSON-RPC).
750#[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/// `--mcp` combined with other run inputs. Takes the conflicting
760/// inputs as structured items; joining lives here.
761#[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    /// Sample keyed reason for serve-ctor tests: the ctors take
782    /// `&Diagnostic` and inline its `flatten_chain` text, so a keyed
783    /// sample pins the same bytes the old `anyhow!("...")` literals did.
784    fn sample_reason(text: &str) -> Diagnostic {
785        Diagnostic::new("t.reason", text.to_string(), Diagnostic::make_fields(&[]))
786    }
787
788    /// Sample I/O failure for the spill/hash/member/stdin ctors:
789    /// test-only `std::io::Error` construction (never `anyhow!`), so
790    /// the `{reason:#}` inline text pins to `"boom"`.
791    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            // Net-only: lean builds store the raw inner and reject with
864            // the feature error instead.
865            #[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}