Skip to content

Studio: route every Windows installer line through the UTF-8 stdout sink - #8148

Merged
danielhanchen merged 10 commits into
mainfrom
fix/windows-setup-output-sink
Aug 8, 2026
Merged

danielhanchen merged 10 commits into
mainfrom
fix/windows-setup-output-sink

Conversation

@danielhanchen

@danielhanchen danielhanchen commented Aug 8, 2026 •

Copy link
Copy Markdown
Member

The Windows desktop setup log renders the banner as ?? Unsloth Studio Setup over a rule of replacement characters, and on some hosts the banner does not arrive at all.

Tauri spawns PowerShell 5.1 with CREATE_NO_WINDOW (install.rs). The preamble added in #8083 sets [Console]::OutputEncoding to UTF-8 and, if that setter throws, rebinds [Console]::Out and [Console]::Error to UTF-8 writers. step and substep write there and come out clean. Write-Host does not use those writers, so the banner, the footer and every warning and error line in the installer path sat outside the sink #8083 built. install.ps1 was further behind: it has the UTF-8 preamble but no IsOutputRedirected probe and no mirror at all, and it is the only script the desktop bundle ships as a resource, so it is what a first install runs.

I captured the bytes on a real windows-latest runner under that exact spawn shape, and the result is worth recording because it refines the mechanism:

  • As shipped on a modern runner, before and after are both clean UTF-8. A CREATE_NO_WINDOW child there still gets a console (GetConsoleOutputCP 437, GetConsoleWindow 0), so the OutputEncoding setter succeeds (437 to 65001) and Write-Host is UTF-8 too. Forcing the code page back to OEM 437 after the preamble does not reproduce it either. So the ?? and bare 0xC4 transcode is what a script without the preamble does, which is the pre-Studio: fix the Windows desktop setup log mojibake and double-printed steps #8083 script users still had on disk while updating, not what main does today.
  • With no console at all, the state install.rs's own comment assumes CREATE_NO_WINDOW produces, the difference is stark. On main, Write-Host throws HostException: GetConsoleScreenBufferInfo, The Win32 internal error "The handle is invalid" 0x6, the script aborts with exit 1, and 2 bytes reach stdout: the banner never arrives and the setup log shows a PowerShell stack trace instead. With this change, 191 bytes of correct UTF-8, sloth and rule intact, with [Console]::OutputEncoding left at 1252, proving the setter threw and the catch branch bound the writers.

So this fixes a real Windows-only failure, and a harder one than mojibake: on a console-less host the banner is currently lost entirely.

This routes all of it through one sink.

  • Write-StudioLine in both scripts: [Console]::Out when stdout is redirected, Write-Host with colour when it is not. Same either/or shape as Write-StudioStdoutMirror, defined before the first line either script can print.
  • 164 call sites rewritten in studio/setup.ps1, 155 in install.ps1 including its own step and substep, which had no mirror.
  • The banner and footer keep the sloth and the U+2500 rule. Through [Console]::Out they encode correctly, and Enable-StudioVirtualTerminal already returns false when redirected, so no ANSI leaks into the log.

Write-Host survives only inside Write-StudioLine itself, inside the interactive branches of step and substep that sit behind the redirect early-return, and inside the launcher script install.ps1 generates, which is a separate process.

A source-contract test enforces that. It masks comments, strings and here-strings before scanning, and names the offending file and line in the failure.

Five Windows CI steps used to run & ./install.ps1 *>&1 | Tee-Object. *>&1 existed to fold the Information stream into the pipeline, so with the lines now going to fd 1 they would reach the step log but not logs/install.log. Those steps now spawn install.ps1 as a child process, which also means setup.ps1's output reaches the tee'd log for the first time. The greps against that log for falling back to source build and for the winget package IDs go from effectively dead to live; I traced each one and they should stay green, but the no-vs-cpu leg is the one to watch on the first run.

Testing:

  • pytest tests/python/test_windows_setup_output_encoding.py 35 passed. Byte-level cases run the scripts under both -File and -Command "& '<script>' *>&1" and assert the banner and footer arrive as valid UTF-8 containing U+1F9A5 and U+2500, exactly once each.
  • Invoke-Pester tests/studio_setup_ps1 57 passed, covering Write-StudioLine in both modes.
  • Both scripts parse clean under Parser::ParseFile.
  • Binding parity checked against Write-Host for quoted strings starting with -, empty strings with -ForegroundColor, $null, arrays and parenthesized expressions, and for emitting nothing on the success stream.
  • Reintroducing one stray Write-Host in the banner fails the contract test as expected.

The OEM transcode itself only reproduces on 5.1, so the Windows runners are the real gate here.

The desktop setup log rendered "?? Unsloth Studio Setup" over a rule of
replacement characters. Tauri spawns Windows PowerShell 5.1 with
CREATE_NO_WINDOW (install.rs), so the [Console]::OutputEncoding setter
throws and both entry scripts rebind [Console]::Out to a UTF-8 writer.
step/substep already write only through that writer when stdout is
redirected, so they came out right. Every other line did not: Write-Host
is written by 5.1's console host with its own writer on the OEM code
page, and U+1F9A5 has no OEM form while U+2500 becomes a bare 0xC4, which
from_utf8_lossy turns into U+FFFD. The banner and the footer are not
steps, so they kept arriving as mojibake, and install.ps1 had neither the
IsOutputRedirected probe nor a mirror at all.

Add Write-StudioLine above the first write in studio/setup.ps1 and
install.ps1: console handle when redirected, Write-Host when interactive,
since it is the only writer that colorizes. Rewrite 164 call sites in
setup.ps1 and 155 in install.ps1 onto it, including install.ps1's own
step/substep. Write-Host now survives only inside helpers that have
already ruled out the redirected sink, and the launcher script install.ps1
generates keeps its own, since it runs as a separate process.

No behaviour change for an interactive console user: same text, same
colors, same single record per line.

test_windows_setup_output_encoding.py gains byte-level coverage that the
real banner and footer, sliced out of setup.ps1, survive both launch
shapes as valid UTF-8 exactly once, plus a source contract that runs on
Linux and names any file:line that reaches for Write-Host outside the
allow-list. Studio.Setup.Output.Tests.ps1 covers Write-StudioLine in both
modes and pins install.ps1's copy to setup.ps1's.

Harnesses that splice these scripts apart now stub or dot-source
Write-StudioLine: two PowerShell harnesses, one Python harness, and the
VC++ redist leg of studio-windows-inference-smoke.

pytest tests/python tests/test_installer_*.py: 1077 passed (2 pre-existing
sandbox failures unrelated to this change). All 16 tests/studio harnesses
and 57 Pester cases pass. Both scripts parse clean.
pre-commit-ci Bot and others added 3 commits August 8, 2026 07:58
The byte-level cases in this file run with a console attached, and a GitHub
runner gives a CREATE_NO_WINDOW child one, so the UTF-8 setter succeeds there
and every version of these scripts emits a clean banner. Those cases cannot
tell this fix from what preceded it.

Add cases that call FreeConsole() in the child first, which is the state
install.rs's own comment assumes CREATE_NO_WINDOW produces. There Write-Host
has no screen buffer to query, throws, and takes the script down: 2 bytes of
stdout and exit 1 rather than the banner. The probe is assembled entirely out
of text sliced from the script under test and spawned with install.rs's own
interpreter, flags and creation flags.

No Windows job ran this file, so its byte-level half was only ever exercised
under pwsh 7 on the Linux Backend CI leg, which is UTF-8 by default. Add it to
the cross-platform parity matrix, which already has a windows-latest row and
already triggers on install.ps1 and studio/setup.ps1.
A platform-gated case that stopped running on the row it exists for still
reports green with -q alone.
It is what turns the Write-Host throw into a dead script rather than a
skipped line, so restating it would be assuming the result.

@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: cc2c965d0a

ℹ️ 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
Write-Host " moved aside: $candidate" -ForegroundColor Yellow
Write-Host " A running 'unsloth studio' process usually holds a file open here." -ForegroundColor Yellow
Write-Host " Close Unsloth Studio and re-run the installer to reverse the move." -ForegroundColor Yellow
Write-StudioLine "[WARN] Moving the existing environment aside stopped partway -- files are in both places." -ForegroundColor Yellow

Copy link
Copy Markdown

Choose a reason for hiding this comment

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

P1 Badge Stub the new sink in the partial-rollback harness

When test_rollback_keeps_state_when_the_move_stops_partway runs its partial case, it extracts Start-StudioVenvRollback but defines only substep, not Write-StudioLine. With $ErrorActionPreference = "Stop", this new call raises a command-not-found error before either rollback path is printed; the harness suppresses that error and then fails its assertion that both paths appear in the output. This test is included in the Windows cross-platform parity job, so the Windows CI row will remain red until the harness stubs or extracts the new sink.

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.

Genuine, fixed in fa50191. One correction to the mechanism: a missing command raises CommandNotFoundException from command discovery, which is terminating for the statement regardless of $ErrorActionPreference, not a non-terminating error. Here it lands inside a catch, so the harness wrote 0 bytes to stderr and exited 0, which is why it was invisible. The stated consequence is also not what happens at head: the test passes today because str(existing) is a prefix substring of state['dir'] and matches the dir= line. So the stub alone would have been a no-op on the outcome; I also tightened the assertion to 'still in place: ' and 'moved aside: ' so the check cannot pass on a prefix match. Without the stub: AssertionError, 1 failed. With it: 13 passed.

Comment thread studio/setup.ps1
}
Write-Host "[ERROR] $Path already exists and is not marked as an Unsloth-owned $Label." -ForegroundColor Red
Write-Host " Move it aside or choose an empty UNSLOTH_STUDIO_HOME before re-running." -ForegroundColor Yellow
Write-StudioLine "[ERROR] $Path already exists and is not marked as an Unsloth-owned $Label." -ForegroundColor Red

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 Stub the sink in the ownership-guard harness

When tests/studio/test_path_probe_access_denied.ps1 exercises an existing unowned custom directory, its extracted Assert-StudioOwnedOrAbsent harness defines step, substep, and Exit-SetupFailure but not Write-StudioLine. Under the suite's ErrorActionPreference = "Stop", this new call throws command-not-found before Exit-SetupFailure; the test catches that exception and treats any throw as success, so the ownership guard now passes for the wrong reason and no longer verifies that the intended failure path is reached.

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.

Genuine, fixed in fa50191, and this one had the consequence exactly right. Without the stub the throw is CommandNotFoundException,Assert-StudioOwnedOrAbsent rather than EXIT-SETUP, so the check passed on the wrong exception and the fatal path went untested. Stub added and the check pinned to $thrownBy -eq 'EXIT-SETUP' instead of 'something threw'. Without: FAIL. With: 126 PASS, 0 FAIL. Your two items prompted an exhaustive sweep of all 25 harnesses that splice these scripts, which turned up two more live breaks you did not flag: tests/python/test_windows_installer_concurrency_guard.py (uncaught, rc=1) and tests/studio_setup_ps1/Studio.Setup.Vs2026.Tests.ps1 (breaks on any host without cmake). Both fixed in the same commit.

The Write-Host rewrite left four spliced-source harnesses reaching
Write-StudioLine without defining it. An undefined command is a terminating
error, so each one either aborted or was swallowed by the harness's own catch,
and the test kept passing while no longer testing anything.

- test_windows_python_venv_hardening.py, partial-rollback case: the five-line
  split-move warning was lost. The assertion that "both halves are named" only
  stayed green because $existing is a prefix of the rollback dir, so it matched
  the dir= line instead. Pin it to the warning text.
- test_path_probe_access_denied.ps1, ownership guard: the catch scored the
  command-not-found as the intended failure and never reached Exit-SetupFailure.
  Pin the check to the EXIT-SETUP message.
- test_windows_installer_concurrency_guard.py: the decision block prints before
  Exit-InstallFailure, so on Windows the active case aborted at exit 1 and never
  produced RESULT:blocked.
- Studio.Setup.Vs2026.Tests.ps1: on a host without cmake,
  Ensure-BuildToolsForLlamaSourceBuild hits the sink first and the no-op case
  fails on the throw.

Also stub the three remaining harnesses that splice sink-calling helpers but do
not reach the sink on the paths they exercise today, so the next case added to
them cannot reintroduce this.
@danielhanchen

Copy link
Copy Markdown
Member Author

@codex review

@chatgpt-codex-connector

Copy link
Copy Markdown

Codex Review: Didn't find any major issues. Swish!

Reviewed commit: fa50191449

ℹ️ 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 07df950 into main Aug 8, 2026
49 of 77 checks passed
@danielhanchen
danielhanchen deleted the fix/windows-setup-output-sink branch August 8, 2026 13:47
shimmyshimmer added a commit that referenced this pull request Aug 30, 2026
install.ps1 conflicted: main took the same save-and-restore approach for its
own handoff variables while this branch was open. Kept every one of main's
env vars and restores, and re-applied this branch's two changes on top --
SKIP_STUDIO_BASE is saved and restored too, and the try opens before the
first mutation so the --with-llama-cpp-dir bail restores as well. Every
capture now sits above the try, so a finally reached early cannot read an
unset flag as "there was nothing here".

Two things the merge broke rather than conflicted on. main moved setup.ps1's
override resolver ahead of where this branch printed the elevation warning,
and that resolver creates and write-probes the custom root -- the exact
folder the warning tells you not to create. The block moves back above it.
And the warnings still called Write-Host, which main routed through the UTF-8
sink in #8148: under CREATE_NO_WINDOW the desktop app renders those as
U+FFFD, which is the flow this branch exists for.
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