# Mechanical replacement recipe (WinuxCmd 2.0, #1488) How to move one applet from its C++ implementation to the rust2 library inside `winuxcmd.exe`. Distilled from the first real replacement (`true` + `false`) and hardened by the waves that followed, including the grep "split-engine" wire (#1660). The rules it enforces: **mechanical replacement, the old implementation is deleted in the same change (no long-lived dual track), and every replaced command passes three gates** — GNU oracle, existing tests, byte equality with the previous exe. > Status at the 2.0 freeze (2026-11-14): 50 applets wired on main, 32 more > with a green library gate waiting for a wiring wave, and grep/sed/find in > flight (#1660/#1635/#1634). `DOCS/2.0-inventory.tsv` is the authoritative > per-command state. ## 1. Decisions fixed by the pilot wave (one-time, already done) | decision | choice | why | |---|---|---| | link shape | **staticlib** (`winuxcmd_ffi.lib`, crate `winuxcmd-ffi` with `crate-type = ["cdylib", "staticlib"]`) | #1487 / niubash#727 traced the FFI breakage to the DLL boundary (load-time CRT stdio binding in a separately linked module). A staticlib is folded into winuxcmd.exe by the same link: one image, one CRT, no load-time binding. The cdylib stays for host experiments only. | | CRT | Rust built with `-C target-feature=+crt-static` | rustc's MSVC default is `/defaultlib:msvcrt` (/MD); winuxcmd.exe is `/MT` (`USE_STATIC_CRT=ON`). With the flag the archive asks for `libcmt`. Verified: no LNK4098 in the link. With `USE_STATIC_CRT=OFF` CMake drops the flag. | | ABI | `winuxcmd_run` (v1) plus the additive streaming entry `winuxcmd_run_stream` (v2); argv/envp/cwd as UTF-8 C strings, stdout/stderr returned as Rust-owned buffers, `catch_unwind` -> 125 | #1488 "ABI 面只传显式句柄/缓冲". C mirror: `src/shared/rust2_abi.h` (change together with `lib.rs`, bump `WINUXCMD_RUST2_ABI_VERSION`). ABI version is 2 since the streaming adoption (#1556). | | C++/Rust boundary | C++ keeps the `REGISTER_COMMAND` registration (help/man/completion metadata) and the dispatcher's shared pre-handler machinery (sole `--help`/`--version` interception, rewrite hooks, option policy). The **handler body** becomes one call into Rust. | The product's help/version text is WinuxCmd-branded for all 176 applets; moving it per command would break gate 3 and fork the help system. What gets deleted is the C++ code that implements the command. | | bridge | `src/bridge/rust2_bridge.cppm`, module `rust2_bridge`, `winux::rust2::run_command(name, ctx.raw_args)` | The only C++ that touches the ABI. Snapshots env (`GetEnvironmentStringsW`, skipping `=C:` pseudo-vars) and cwd (`GetCurrentDirectoryW`), no exceptions (winuxcmd builds without /EHsc), drains stdout/stderr through `safePrint`/`safeErrorPrint` so capture handles and console handling stay on the one output exit. | | split engine | when a command keeps one C++ face, Rust exports a **capability predicate** and the C++ handler branches; the retained face is deleted only where unreachable | #1660: `grep -P` stays on PCRE2, so Rust exports `winuxcmd_grep_uses_perl` (additive, ABI stays 2) and the handler routes non-Perl to rust2, `-P` to the retained C++ face. `egrep`/`fgrep` route unconditionally (upstream wrappers are `exec grep -E/-F`; `egrep -P` is a matcher conflict rc 2, never Perl — the 1.x body running Perl there was a standing oracle deviation). | | CMake | `WINUXCMD_ENABLE_RUST2` (default ON iff `cargo` is found), list `WINUXCMD_RUST2_COMMANDS`, custom command `cargo rustc --release --locked -p winuxcmd-ffi --lib --crate-type staticlib` with `CARGO_TARGET_DIR=/rust2-target`, imported static lib + pinned native libs | A machine without Rust configures to OFF and still builds; it then ships **without** the listed commands (their sources and the bridge are filtered out). That is the price of "no dual track"; there is no C++ fallback body. | Native libs pinned in CMake (`advapi32 kernel32 ntdll userenv ws2_32 dbghelp`) come from `cargo rustc --release -p winuxcmd-ffi --lib --crate-type staticlib -- -C target-feature=+crt-static --print native-static-libs`. Re-run it and update the list if a rust2 change makes the link fail with LNK2019. Symbol check: `dumpbin /LINKERMEMBER:1 winuxcmd_ffi.lib` lists `winuxcmd_run`, `winuxcmd_run_stream`, `winuxcmd_free_buf`, `winuxcmd_abi_version`, `winuxcmd_has_command` unmangled (plus any per-command capability predicate, e.g. `winuxcmd_grep_uses_perl`). Cost observed: winuxcmd.exe (Debug) 20,887,552 -> 21,923,328 bytes (+1.0 MB, Rust std incl. backtrace support; one-time, shared by later commands). First cargo build of the workspace is seconds (std only, no external crates). ## 2. Per-command steps (repeat for each applet) Preconditions — stop and fix first if any fails: - The command is in `winux-cmds::registry()` and `bash rust2/gate/run-gate.sh ` is green. - The inventory row is `deferred(#1488)` with `engine=rust2-ready` (or the branch you are wiring is the one that lands the library). - Read the dispatcher's behavior for it (`src/core/dispatcher.cppm` `behavior_for()`, `src/core/command_context.cppm` `option_policy_for_command()`): rewrite hooks, abbreviation expansion, sole-argument help/version, and the macro's `make_context` parse all run **before** the handler. The Rust function receives `ctx.raw_args` after those steps. If C++ can reject an argument first (an option parse error), that diagnostic stays C++ — decide whether the C++ option policy must become permissive for this command. ### 2.1 Baseline (before touching anything) Build (`powershell -ExecutionPolicy Bypass -File scripts/build-with-vs.ps1`), copy `build-vs/winuxcmd.exe` to a scratch dir **keeping the name `winuxcmd.exe`** (the multicall routes by argv[0]; a copy named `winuxcmd-old.exe` answers `command not found: winuxcmd-old`), run the full `ctest` in `build-vs`, save the failing set. Keep that baseline exe: gate c compares against it byte-for-byte. ### 2.2 Library check Re-run the rust2 gate for the command and re-read its `note`/`win` rows: a `note` row can hide a pilot divergence. Fix the Rust side first, in its own commit. ### 2.3 Wire shape A — one engine (the default) 1. Edit `src/commands/.cpp`: keep the `REGISTER_COMMAND` metadata byte-identical (help/man text is generated from it), add `import rust2_bridge;`, replace the handler body with `return winux::rust2::run_command("", ctx.raw_args);`, delete every helper, include and static the old body used. 2. Append `` to `WINUXCMD_RUST2_COMMANDS` in `CMakeLists.txt` (union — never replace the existing set). 3. If the command reads stdin, see §5 before choosing the entry. 4. Build, then run the gates (§3). 5. Record: update `rust2/GATE-REPORT.md`, flip the inventory row to `rust2-wired` with the PR/commit anchor, commit (Rust fix / wiring / docs as separate commits). ### 2.4 Wire shape B — split engine (#1660 grep) Use it when exactly one face of the command genuinely belongs to C++ (today: `grep -P` and its PCRE2 dependency; the decision is recorded in `DOCS/2.0-regex-engine-decision.md` §6). 1. Export a capability predicate from `winuxcmd-ffi` (`winuxcmd__uses_`), add it to `src/shared/rust2_abi.h`, and expose it from the bridge. Additive only — do not bump the ABI for a new symbol. 2. In the handler, parse only enough to decide the face (for grep: the effective matcher after option clustering; `egrep`/`fgrep` force `-E`/`-F`), then call the streaming rust2 entry for the non-C++ face and the retained C++ function for the other. The retained code is moved to a named function; everything unreachable from it is deleted. 3. Add the command to the **bridge drain policy** when it has a command-specific write-error status (grep family keeps GNU `EXIT_TROUBLE 2`; the generic broken-pipe policy must not rewrite it). 4. Keep the C++ surface honest: `egrep`/`fgrep` must not accept a Perl matcher conflict — upstream wrappers are `exec grep -E/-F`. 5. Gates add one row class: **split smoke** — every retained-face invocation byte-equal old vs new, plus at least one non-face invocation proving the Rust path. Evidence: `rust2/target/wire-i18n/` (#1660: 10/10 -P split smoke byte-equal). ### 2.5 Record the retirement If the 1.x body carried WinuxCmd-only options, list each one in `DOCS/2.0-MIGRATION.md` §3 with its GNU source citation, and re-pin the affected ctest to the GNU contract (the e7ddc432 precedent: echo `-u`/`-r`, fold `-c`, fmt `-m/-P/-X/-x/-T`). ## 3. The three gates **Gate a + c with the existing harness, zero new code** — point the gate's two Windows sides at the new and the old product exe: ```sh cd rust2 RUST_BIN=D:/repo//build-vs/winuxcmd.exe \ CPP_BIN=/winuxcmd.exe \ bash gate/run-gate.sh ``` The "rust" side is now the wired product: `strict`/`exit` rows gate it byte-for-byte against WSL coreutils 9.4 (gate a); the ledger column `r=c` compares new vs old exe rc + stdout (gate c). Every `r=c:no` row must be explained, or the replacement is not mechanical. **stderr and extra argument shapes**: run a matrix probe that records rc/stdout/stderr for GNU, old exe and new exe, then compare (`rust2/gate/wire/cmd-matrix.sh` + `compare.sh`; one matrix block per command, new rows appended in the same change). GNU side runs under WSL (`MSYS_NO_PATHCONV=1 wsl bash