Skip to content

Commit 0ed388c

Browse files
authored
fix: build_data_writer.ps1 encoding and ACLs (#3604)
Windows Powershell APIs have several quirks that can make a file problematic if typical Linux semantics are assumed * BOM header bytes can be written, which break regular UTF8 parsing * The file can be left open, which prevents later deletion of it * The files can be written with limited permissions, leading to permission errors later. To fix, use alternative Powershell APIs for writing the text file. Also explicitly set permissions on the output. I came across this when Windows starting running the build data tests in non-zip mode, where the original file permissions are kept.
1 parent 659b87b commit 0ed388c

File tree

2 files changed

+21
-8
lines changed

2 files changed

+21
-8
lines changed

CHANGELOG.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -92,6 +92,7 @@ END_UNRELEASED_TEMPLATE
9292
fixing missing transitive dependencies when extras contain hyphens
9393
(e.g., `sqlalchemy[postgresql-psycopg2binary]`).
9494
([#3587](https://github.com/bazel-contrib/rules_python/issues/3587))
95+
* (binaries/tests) Stamped build data generated by Windows actions is readable
9596

9697
{#v0-0-0-added}
9798
### Added
Lines changed: 20 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -1,17 +1,29 @@
11
$OutputPath = $env:OUTPUT
2-
3-
Add-Content -Path $OutputPath -Value "TARGET $env:TARGET"
4-
Add-Content -Path $OutputPath -Value "CONFIG_MODE $env:CONFIG_MODE"
5-
Add-Content -Path $OutputPath -Value "STAMPED $env:STAMPED"
2+
$Lines = @(
3+
"TARGET $env:TARGET",
4+
"CONFIG_MODE $env:CONFIG_MODE",
5+
"STAMPED $env:STAMPED"
6+
)
67

78
$VersionFilePath = $env:VERSION_FILE
8-
if (-not [string]::IsNullOrEmpty($VersionFilePath)) {
9-
Get-Content -Path $VersionFilePath | Add-Content -Path $OutputPath
9+
if (-not [string]::IsNullOrEmpty($VersionFilePath) -and (Test-Path $VersionFilePath)) {
10+
$Lines += Get-Content -Path $VersionFilePath
1011
}
1112

1213
$InfoFilePath = $env:INFO_FILE
13-
if (-not [string]::IsNullOrEmpty($InfoFilePath)) {
14-
Get-Content -Path $InfoFilePath | Add-Content -Path $OutputPath
14+
if (-not [string]::IsNullOrEmpty($InfoFilePath) -and (Test-Path $InfoFilePath)) {
15+
$Lines += Get-Content -Path $InfoFilePath
1516
}
1617

18+
# Use .NET to write file to avoid PowerShell encoding/locking quirks
19+
# We use UTF8 without BOM for compatibility with how the bash script writes (and
20+
# what consumers expect).
21+
$Utf8NoBom = New-Object System.Text.UTF8Encoding $False
22+
[System.IO.File]::WriteAllLines($OutputPath, $Lines, $Utf8NoBom)
23+
24+
$Acl = Get-Acl $OutputPath
25+
$AccessRule = New-Object System.Security.AccessControl.FileSystemAccessRule("Everyone", "Read", "Allow")
26+
$Acl.SetAccessRule($AccessRule)
27+
Set-Acl $OutputPath $Acl
28+
1729
exit 0

0 commit comments

Comments
 (0)