Skip to content

Studio: fix the Windows desktop setup log mojibake and double-printed steps - #8083

Merged
danielhanchen merged 6 commits into
mainfrom
fix/windows-setup-log-encoding-and-dedup
Aug 7, 2026
Merged

danielhanchen merged 6 commits into
mainfrom
fix/windows-setup-log-encoding-and-dedup

Conversation

@danielhanchen

Copy link
Copy Markdown
Member

Problem

Installing the Windows desktop build and letting it run "Getting things ready..." produces a corrupted, double-printed setup log:

  ?? Unsloth Studio Setup
  ����������������������������������������������������
  gpu
none (chat-only / GGUF)
  gpu            none (chat-only / GGUF)
  long paths
enabled
  long paths     enabled

Three defects, two root causes.

Encoding

studio/setup.ps1 never 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:

  • The sloth U+1F9A5 has no OEM representation, so PowerShell itself substitutes one ? per UTF-16 surrogate, giving ??.
  • The rule $Rule = [string]::new([char]0x2500, 52) does have one, so it becomes 52 bare 0xC4 bytes, which are invalid UTF-8 and reach the user as U+FFFD.

OutputEncoding, chcp and 65001 currently appear in no .ps1, .py or .rs file in the repo. Existing Windows CI masks this: every studio-windows-*.yml sets PYTHONUTF8/PYTHONIOENCODING at job scope, repairing the symptom externally.

Duplication

step and substep emitted through Write-Host and Write-StudioStdoutMirror. The mirror's comment assumed Write-Host does 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 every step line appears twice.

Splitting

step composed one logical line from two Write-Host calls using -NoNewline. Once records are merged and redirected, each record boundary becomes a line break, which is what separates gpu from none (chat-only / GGUF).

Fix

  • studio/setup.ps1, install.ps1: set [Console]::OutputEncoding, $OutputEncoding, PYTHONUTF8 and PYTHONIOENCODING before the first write. Assigning OutputEncoding rebuilds [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 through irm | iex.
  • studio/setup.ps1: Refresh-Environment no longer reloads PYTHONUTF8/PYTHONIOENCODING from the registry. It runs repeatedly through a long setup, so a user with PYTHONUTF8=0 would 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 writes Write-Host only. 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 composed Write-Host. install.ps1 needs this most, having no mirror to fall back on.
  • studio/src-tauri/src/{install,update,process}.rs: force PYTHONUTF8/PYTHONIOENCODING on 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:

  ?? Unsloth Studio Setup
  ????????????????????????????????????????????????????
  gpu            
none (chat-only / GGUF)
  gpu            none (chat-only / GGUF)
  long paths     
enabled
  long paths     enabled
                 installing OXC validator runtime...
                 installing OXC validator runtime...

After:

  🦥 Unsloth Studio Setup
  ────────────────────────────────────────────────────
  gpu            none (chat-only / GGUF)
  long paths     enabled
                 installing OXC validator runtime...

cargo check on windows-latest passes, 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 existing pester job in studio-windows-inference-smoke.yml, no workflow change needed. Covers one record per step/substep, label and value on one line, and mutual exclusion of the two sinks across the VT, no-VT and NO_COLOR branches.
  • tests/python/test_windows_setup_output_encoding.py: runs real PowerShell in both the -File and -Command ... *>&1 shapes and asserts on raw bytes (decoding first would hide the regression), plus source contracts that run on Linux in studio-backend-ci.yml.

Both suites were confirmed to fail against the unfixed tree, 10 Pester and 13 pytest failures, rather than merely passing.

danielhanchen and others added 2 commits August 7, 2026 04:26
… 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.
danielhanchen added a commit to shimmyshimmer/unsloth-staging-4 that referenced this pull request Aug 7, 2026
chatgpt-codex-connector[bot]

This comment was marked as resolved.

… 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
chatgpt-codex-connector[bot]

This comment was marked as resolved.

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.
@danielhanchen

Copy link
Copy Markdown
Member Author

@codex review

1 similar comment
@danielhanchen

Copy link
Copy Markdown
Member Author

@codex review

danielhanchen added a commit to shimmyshimmer/unsloth-staging-4 that referenced this pull request Aug 7, 2026

@chatgpt-codex-connector chatgpt-codex-connector Bot left a comment

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

💡 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".

Comment thread install.ps1
Comment on lines +73 to +74
$env:PYTHONUTF8 = '1'
$env:PYTHONIOENCODING = 'utf-8'

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

P2 Badge 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 👍 / 👎.

Copy link
Copy Markdown
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

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.

@chatgpt-codex-connector

Copy link
Copy Markdown

Codex Review: Didn't find any major issues. 🚀

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".

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.
chatgpt-codex-connector[bot]

This comment was marked as resolved.

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.
@unslothai unslothai deleted a comment from chatgpt-codex-connector Bot Aug 7, 2026
@unslothai unslothai deleted a comment from chatgpt-codex-connector Bot Aug 7, 2026
@unslothai unslothai deleted a comment from chatgpt-codex-connector Bot Aug 7, 2026
@unslothai unslothai deleted a comment from chatgpt-codex-connector Bot Aug 7, 2026
@chatgpt-codex-connector

Copy link
Copy Markdown

Codex Review: Didn't find any major issues. 🎉

Reviewed commit: 5516a3cabd

ℹ️ 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".

@danielhanchen
danielhanchen merged commit d8effae into main Aug 7, 2026
74 of 80 checks passed
@danielhanchen
danielhanchen deleted the fix/windows-setup-log-encoding-and-dedup branch August 7, 2026 08:18
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant