Repository navigation
Studio: fix the Windows desktop setup log mojibake and double-printed steps - #8083
Conversation
… steps
The desktop 'Getting things ready...' log rendered as:
?? Unsloth Studio Setup
<52 replacement chars>
gpu
none (chat-only / GGUF)
gpu none (chat-only / GGUF)
Encoding. studio/setup.ps1 never set [Console]::OutputEncoding, so Windows
PowerShell 5.1 encoded redirected output with the OEM code page while the
desktop app decodes the pipe as UTF-8 (String::from_utf8_lossy in
src-tauri/src/install.rs). That corrupts two different ways: the sloth U+1F9A5
has no OEM representation so PowerShell substitutes one '?' per UTF-16
surrogate, and the rule U+2500 does have one, so it becomes a bare 0xC4 byte
that is invalid UTF-8 and surfaces as U+FFFD. Both entry scripts now set the
console encoding, $OutputEncoding, PYTHONUTF8 and PYTHONIOENCODING before the
first write, and Refresh-Environment can no longer reload the two Python vars
back over ours mid-run. The patch is ASCII-only: these files are UTF-8 without
a BOM and 5.1 parses those as ANSI.
Duplication. step/substep wrote through Write-Host AND a console-handle mirror.
The mirror's comment assumed Write-Host does not survive the process chain; it
does, because the CLI spawns setup.ps1 as -Command "& '...' *>&1"
(unsloth_cli/commands/studio.py), which merges the Information stream into
stdout deliberately. The sink is now resolved once and exactly one is used:
redirected writes to the console handle, interactive writes to Write-Host.
Splitting. step composed one logical line from two Write-Host calls using
-NoNewline, and a redirected consumer turns each Information record boundary
into a line break. Both scripts now emit one composed record; install.ps1 needs
this most, having no mirror to fall back on.
Rust children on Windows get PYTHONUTF8/PYTHONIOENCODING too, since install.rs,
update.rs and process.rs all decode their output as UTF-8. The readers stay
lossy on purpose -- strict decoding would turn display corruption into an
installation failure.
Tests: a Pester suite auto-discovered by the existing pester job, and a pytest
byte-level probe that runs real PowerShell in both the -File and -Command
launch shapes and asserts on raw bytes. Verified to fail against the unfixed
tree (9 Pester and 13 pytest failures) rather than merely passing.
… child Two holes in the previous commit, both on the exact path the desktop app takes. [Console]::OutputEncoding P/Invokes SetConsoleOutputCP, which needs a console handle. Under CREATE_NO_WINDOW there is none, so it throws, and it drops the cached writer BEFORE throwing while assigning OutputEncoding only after. Console .Out therefore rebuilt on the old code page. Swallowing the exception was not enough once redirected step/substep use Console.Out as their only sink, so the catch path now binds an explicit UTF-8 StreamWriter over OpenStandardOutput. build_update_command launches Python with -I, which implies -E, so that process ignores every PYTHON* variable and PYTHONUTF8/PYTHONIOENCODING never reached it. Pass -X utf8 as a switch instead. The env vars stay for its descendants. https://docs.python.org/3/using/cmdline.html#cmdoption-I
The no-console fallback repaired Console.Out only. Tauri pipes stderr through the same lossy UTF-8 decode (install.rs) and emits it to the same UI log, and InstallFailureContext builds the user-facing failure message from those lines, so a PowerShell error carrying a non-ASCII path still arrived as U+FFFD. install.ps1 also writes its Clear-TauriInstallError markers there.
|
@codex review |
1 similar comment
|
@codex review |
There was a problem hiding this comment.
💡 Codex Review
Here are some automated review suggestions for this pull request.
Reviewed commit: 1605703c7f
ℹ️ About Codex in GitHub
Your team has set up Codex to review pull requests in this repo. Reviews are triggered when you
- Open a pull request for review
- Mark a draft as ready
- Comment "@codex review".
If Codex has suggestions, it will comment; otherwise it will react with 👍.
Codex can also answer questions or update the PR. Try commenting "@codex address that feedback".
| $env:PYTHONUTF8 = '1' | ||
| $env:PYTHONIOENCODING = 'utf-8' |
There was a problem hiding this comment.
Restore the caller's Python encoding variables
When users follow the advertised irm https://unsloth.ai/install.ps1 | iex path, this function runs inside their existing interactive PowerShell process, so these process-wide assignments survive after the installer returns. Any existing PYTHONUTF8=0 or custom PYTHONIOENCODING is silently replaced, and every subsequent Python command launched from that terminal is forced to UTF-8; preserve the original values and restore them in a finally block after descendants no longer need the invariant.
Useful? React with 👍 / 👎.
There was a problem hiding this comment.
Not taking this one. The proposed remedy does not fire on the path it is raised about: on the interactive irm | iex path the function ends at & $UnslothExe studio -p 8888 (install.ps1:3760), which runs Studio in the foreground, so a finally would not run until the user stops the server.
That is already the documented stance in this file for the same class of mutation, twice. The PSModulePath block at the top (install.ps1:31-38) says it verbatim: "Not restored afterwards, deliberately... A try/finally would not change that for the case it is raised about: the interactive path ends by running Studio in the foreground, so the finally would not fire until the user stops the server." The working-directory relocation at install.ps1:1172-1179 cites the same reason. Two process-wide mutations already ship under that reasoning; this would be the third and it does not warrant diverging.
On impact: no crash, no data loss and no wrong content follows, and the forced value matches where CPython is heading, since PEP 686 is Final and makes UTF-8 mode the default in 3.15.
Worth noting studio/setup.ps1 is unaffected either way: it is always spawned as its own process (powershell.exe -File from install.rs, or -Command from the CLI), never dot-sourced into a user shell, so nothing there leaks.
|
Codex Review: Didn't find any major issues. 🚀 Reviewed commit: ℹ️ About Codex in GitHubYour team has set up Codex to review pull requests in this repo. Reviews are triggered when you
If Codex has suggestions, it will comment; otherwise it will react with 👍. Codex can also answer questions or update the PR. Try commenting "@codex address that feedback". |
Comments only, no code change. Verified with the PowerShell AST tokenizer for both .ps1 files and the Pester suite (token streams identical with Comment and NewLine excluded), comment_tools.py for the Python test, and a code-only diff for update.rs.
windows_update_command_uses_python_not_replaceable_console_stub asserts the exact argument vector, so adding -X utf8 broke it. The Windows cargo test job in studio-tauri-smoke.yml runs it; the Linux job skips it under cfg(windows), and cargo check type-checks tests without running them, so neither the org Linux run nor the staging check caught it.
|
Codex Review: Didn't find any major issues. 🎉 Reviewed commit: ℹ️ About Codex in GitHubYour team has set up Codex to review pull requests in this repo. Reviews are triggered when you
If Codex has suggestions, it will comment; otherwise it will react with 👍. Codex can also answer questions or update the PR. Try commenting "@codex address that feedback". |
Problem
Installing the Windows desktop build and letting it run "Getting things ready..." produces a corrupted, double-printed setup log:
Three defects, two root causes.
Encoding
studio/setup.ps1never set[Console]::OutputEncoding, so Windows PowerShell 5.1 encodes redirected output with the OEM code page, while the desktop app decodes the pipe as UTF-8 (String::from_utf8_lossy,studio/src-tauri/src/install.rs:495). That corrupts two different ways, and both are visible above:U+1F9A5has no OEM representation, so PowerShell itself substitutes one?per UTF-16 surrogate, giving??.$Rule = [string]::new([char]0x2500, 52)does have one, so it becomes 52 bare0xC4bytes, which are invalid UTF-8 and reach the user asU+FFFD.OutputEncoding,chcpand65001currently appear in no.ps1,.pyor.rsfile in the repo. Existing Windows CI masks this: everystudio-windows-*.ymlsetsPYTHONUTF8/PYTHONIOENCODINGat job scope, repairing the symptom externally.Duplication
stepandsubstepemitted throughWrite-HostandWrite-StudioStdoutMirror. The mirror's comment assumedWrite-Hostdoes not survive the process chain. It does: the CLI spawns setup as-Command "& 'setup.ps1' *>&1"(unsloth_cli/commands/studio.py:2789), which merges the Information stream into stdout deliberately. So both sinks landed. The banner is not mirrored, which is exactly why it appears once while everystepline appears twice.Splitting
stepcomposed one logical line from twoWrite-Hostcalls using-NoNewline. Once records are merged and redirected, each record boundary becomes a line break, which is what separatesgpufromnone (chat-only / GGUF).Fix
studio/setup.ps1,install.ps1: set[Console]::OutputEncoding,$OutputEncoding,PYTHONUTF8andPYTHONIOENCODINGbefore the first write. AssigningOutputEncodingrebuilds[Console]::Out, so ordering matters; only the console setter is wrapped, since that is the one that throws when the process has no console (CREATE_NO_WINDOW). The patch is ASCII-only because these files are UTF-8 without a BOM and 5.1 parses those as ANSI. No BOM is added: it would be a much wider packaging change for scripts that get concatenated and streamed throughirm | iex.studio/setup.ps1:Refresh-Environmentno longer reloadsPYTHONUTF8/PYTHONIOENCODINGfrom the registry. It runs repeatedly through a long setup, so a user withPYTHONUTF8=0would otherwise have every Python child after the first refresh go back to mojibake. Process scope only, the user's own value is untouched.studio/setup.ps1: the output sink is resolved once and exactly one is used. Redirected writes the console handle only; interactive writesWrite-Hostonly. This is correct for both launch shapes the product uses, and neither can lose its only sink.studio/setup.ps1,install.ps1: each step line is one composedWrite-Host.install.ps1needs this most, having no mirror to fall back on.studio/src-tauri/src/{install,update,process}.rs: forcePYTHONUTF8/PYTHONIOENCODINGon Windows children, since all three decode their output as UTF-8. The readers stay lossy on purpose; strict decoding would turn display corruption into an installation failure.Verification
A/B on
windows-latest, identical workflow, differing only in these source files. Code page 437 is forced in the child, because GitHub's runners already use a UTF-8-friendly code page and without that pin the encoding bug does not reproduce on CI at all.Before:
After:
cargo checkonwindows-latestpasses, which is the only place the new#[cfg(windows)]blocks get compiled.Tests
tests/studio_setup_ps1/Studio.Setup.Output.Tests.ps1: auto-discovered by the existingpesterjob instudio-windows-inference-smoke.yml, no workflow change needed. Covers one record perstep/substep, label and value on one line, and mutual exclusion of the two sinks across the VT, no-VT andNO_COLORbranches.tests/python/test_windows_setup_output_encoding.py: runs real PowerShell in both the-Fileand-Command ... *>&1shapes and asserts on raw bytes (decoding first would hide the regression), plus source contracts that run on Linux instudio-backend-ci.yml.Both suites were confirmed to fail against the unfixed tree, 10 Pester and 13 pytest failures, rather than merely passing.