Skip to content

tests: a scripts/*.ps1 text writer must name -Encoding; first_look.ps1 shim-todo.txt is UTF-8 - #7818

Open
evilclanker9000 wants to merge 3 commits into
ItsNotRexed:mainfrom
evilclanker9000:deepseek/5-ps1-write-encoding
Open

evilclanker9000 wants to merge 3 commits into
ItsNotRexed:mainfrom
evilclanker9000:deepseek/5-ps1-write-encoding

Conversation

@evilclanker9000

Copy link
Copy Markdown
Contributor

Item: EGEN-PS1-WRITE-ENCODING (claimed in the coordination issue: #7694 (comment)). Nothing else is needed for it.

What changed and why

scripts/first_look.ps1:108 wrote shim-todo.txt with Set-Content and no -Encoding, so the bytes followed the PowerShell host instead of the content. Measured on this machine, same command and same input:

powershell.exe -NoProfile -Command "'aä' | Set-Content -LiteralPath $env:TEMP\x.txt"   # 61 e4 0d 0a
pwsh           -NoProfile -Command "'aä' | Set-Content -LiteralPath $env:TEMP\x.txt"   # 61 c3 a4 0d 0a

-Encoding utf8 writes 61 c3 a4 0d 0a on both (5.1 prepends a BOM, 7 does not; neither depends on the console code page). scripts/carve_all.ps1:62 and scripts/meta_to_xml.ps1:17,47 already name it, so this follows what those do.

  • scripts/first_look.ps1: -Encoding utf8 on that one write.
  • tests/test_ps1_write_encoding.py (new): fails for any Set-Content, Add-Content, Out-File or Export-Csv under scripts/ that passes no -Encoding. Its own case list checks that a pinned call, a comment, a quoted string, Tee-Object and [IO.File]::WriteAllText are not flagged.
  • AGENTS.md, CONTRIBUTING.md: the rule, next to the existing newline= one.

Test

Before (the first commit not applied), python -I tests/test_ps1_write_encoding.py:

.F
FAIL: test_every_script_writer_names_its_encoding
AssertionError: Lists differ: ['scripts/first_look.ps1:108'] != []
Ran 2 tests ... FAILED (failures=1)

After:

..
Ran 2 tests in 0.171s

OK

Also clean: python -I -m compileall -q make_game.py tools tests, python -I tests/test_docs_python_deps.py (3 tests, OK), python -I tests/test_docs_repeated_lines.py (2 tests, OK), python -I scripts/check_no_game_data.py on the four changed files, git diff --check.

GPU: none needed. CPU-only, Windows 11, CPython 3.12; the encoding measurement ran under Windows PowerShell 5.1 and PowerShell 7.5.

Notes

  • The scan covers scripts/ only: those are the build helpers a user runs by hand. tools/conformance/native/scripts/*.ps1 write ASCII .cpp fixtures with Set-Content -NoNewline, and a 5.1 -Encoding utf8 would put a BOM in front of a fixture, so they are left for their own change.
  • Tee-Object is left out because 5.1 has no -Encoding on it: (Get-Command Tee-Object).Parameters.ContainsKey('Encoding') is False there. scripts/first_look.ps1:24,101 still write UTF-16LE on 5.1 and UTF-8 on 7; pinning those means replacing Tee-Object, which this PR does not do.
  • [IO.File]::WriteAllText is left out: it writes UTF-8 without a BOM on every .NET, so it is already host-independent.
  • tests/test_tool_stdout_encoding.py already fails on this checkout before and after the branch, with the same 11 tool files (that is the open tools, workflows: reconfigure stdout error handling and pin text write newlines #7589); nothing here touches them.

AI-assisted.

…e page

Set-Content with no -Encoding writes the ANSI code page on Windows PowerShell 5.1 and UTF-8 on PowerShell 7, so the same run left byte-different reports on the two hosts the build runs on. carve_all.ps1 and meta_to_xml.ps1 already name -Encoding utf8.
Set-Content, Add-Content, Out-File and Export-Csv take the host default encoding: the ANSI code page on Windows PowerShell 5.1 and UTF-8 on 7. Measured: 'aae' | Set-Content wrote 61 e4 0d 0a on 5.1 and 61 c3 a4 0d 0a on 7 for the same line. The scan is line, comment and string aware, and its own case list checks that a pinned call, a comment, a string, Tee-Object (no -Encoding on 5.1) and [IO.File]::WriteAllText are not flagged.
…ine-ending one

The Python rule already says a writer names its line ending so the bytes do not depend on the platform; the PowerShell writers have the same choice between the ANSI code page (5.1) and UTF-8 (7), and tests/test_ps1_write_encoding.py now checks it.
@jebaczydow

Copy link
Copy Markdown

Read the diff (not run). The first_look.ps1 fix is right. Two things in the checker in tests/test_ps1_write_encoding.py:

  1. if "-Encoding" not in masked[match.start():]: looks for -Encoding from the writer to the end of the whole logical line, so a pinned command later in the same pipeline hides an unpinned writer: $x | Set-Content -LiteralPath $p -Value $l | Out-File -Encoding utf8 -LiteralPath $q passes the check for the Set-Content. The cases in test_detector_finds_the_unpinned_cmdlets do not include this shape. Searching only up to the next unquoted | would close it.
  2. The check is case-sensitive, but PowerShell parameter names are not, so -encoding utf8 is reported as unpinned (a false positive).

AI-assisted (Claude); code read only, nothing run.

@airp1ane

Copy link
Copy Markdown
Contributor

Tested 6b10cec on Windows 11, Python 3.15.2, NVIDIA RTX 4060 Ti (driver 621.24; CPU-only checks, no game files, no GPU/llvm-mc/MSVC used): pass.

Ran the test files this PR adds or changes, from a clean checkout of the PR head: test_ps1_write_encoding.py - all exit 0.

AI-assisted (Claude Code).

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.

3 participants