astral-sh / rye

a Hassle-Free Python Experience
https://rye.astral.sh
MIT License
13.6k stars 466 forks source link

`rye` mangles/strips escape sequences when run in `cmd.exe` with stdout redirected #1092

Closed mataha closed 2 months ago

mataha commented 4 months ago

I've been hacking together an improvement to rye-up.com/get that would enable Windows users to install rye in the same way Linux and macOS users can (piping curl output to shell), though I've hit a wall - no matter what I do, the executable is unable to produce correct output with stdout redirected.

Steps to Reproduce

git clone -b feat/windows/install --single-branch https://github.com/mataha/rye
cd rye
::: Notice I'm redirecting stdout to `nul` here - this is necessary as `cmd.exe`
::: prints every command executed when evaluating piped input, prompts included
curl -fsSL "file:///%CD%/scripts/install.cmd" | cmd /d/x/k >nul

Expected Result

image

Actual Result

As expected - text is on stdout, which is redirected to nul (though the prompt remains visible):

image

Redirecting the binary's output to stderr makes the entire text get mangled: ```cmd ::: This makes invoking the binary roughly equivalent to `rye self install >&2` set "RYE_INSTALL_OPTIONS=>&2" ```

image

With the following change all ANSI escape sequences are stripped except those in prompts (last line): ```diff diff --git a/rye/src/cli/rye.rs b/rye/src/cli/rye.rs index 72d7ca357ff3..733710bad41e 100644 --- a/rye/src/cli/rye.rs +++ b/rye/src/cli/rye.rs @@ -525,6 +525,7 @@ fn perform_install( }, }; + let _guard = crate::tui::redirect_to_stderr(true); echo!("{}", style("Welcome to Rye!").bold()); if matches!(mode, InstallMode::AutoInstall) { ```

image

I have no idea what's going on; why is dialoguer able to work correctly in every instance? Admittedly I don't believe this is a bug in rye itself, but I have to start somewhere.

Version Info

rye 0.34.0
commit: 0.33.0+14 (761e4b583 2024-05-18)
platform: windows (x86_64)
self-python: cpython@3.12.3
symlink support: true
uv enabled: true
mataha commented 2 months ago

Not applicable anymore - figured how to run rye without butchering stdout.