Skip to content

armgit / cua

Public Imported from GitHub
0

Scale computer-use 2.0 with open-source drivers, cross-OS sandboxes, and benchmarks for training, evaluation, and data generation.

fix(cua-driver): handle Add-Type failure on non-ASCII locales

injaneitycb213efb082d
diff --git a/.github/workflows/ci-cua-driver-installer-compat.yml b/.github/workflows/ci-cua-driver-installer-compat.yml
index c117adf683..d8b1b0f406 100644
--- a/.github/workflows/ci-cua-driver-installer-compat.yml
+++ b/.github/workflows/ci-cua-driver-installer-compat.yml
@@ -50,7 +50,7 @@ jobs:
"repos/$GITHUB_REPOSITORY/pulls/$PR_NUMBER/files?per_page=100" \
--jq '.[].filename')
if ! grep -Eq \
- '^(\.github/release-state/cua-driver-rs-(published-version|withdrawn-versions)|\.github/scripts/verify_cua_driver_release_archives\.py|\.github/scripts/tests/test_verify_cua_driver_release_archives\.py|\.github/workflows/(cd-rust-cua-driver|ci-cua-driver-installer-compat)\.yml|libs/cua-driver/scripts/(_install-common|_install-rust|install)\.sh|libs/cua-driver/scripts/(_install-common\.psm1|install\.ps1)|release-please-config\.json)$' \
+ '^(\.github/release-state/cua-driver-rs-(published-version|withdrawn-versions)|\.github/scripts/verify_cua_driver_release_archives\.py|\.github/scripts/tests/test_verify_cua_driver_release_archives\.py|\.github/workflows/(cd-rust-cua-driver|ci-cua-driver-installer-compat)\.yml|libs/cua-driver/scripts/(_install-common|_install-rust|install)\.sh|libs/cua-driver/scripts/(_install-common\.psm1|install\.ps1)|libs/cua-driver/scripts/tests/install-junction-fallback\.ps1|release-please-config\.json)$' \
<<< "$CHANGED_FILES"; then
SHOULD_RUN=false
fi
@@ -156,6 +156,11 @@ jobs:
grep -Fx com.trycua.driver
fi
+ - name: Validate the locale-safe junction rollback
+ if: runner.os == 'Windows'
+ shell: powershell
+ run: .\libs\cua-driver\scripts\tests\install-junction-fallback.ps1
+
- name: Run the current Windows installer against the release
if: runner.os == 'Windows'
shell: pwsh
diff --git a/libs/cua-driver/scripts/install.ps1 b/libs/cua-driver/scripts/install.ps1
index 3e88b6f7b2..41ee9a333d 100644
--- a/libs/cua-driver/scripts/install.ps1
+++ b/libs/cua-driver/scripts/install.ps1
@@ -327,8 +327,11 @@ function Get-AssetArchLabel($targetTriple) {
# create one as long as the source path is a real directory on a
# local NTFS volume.
+$script:JunctionTypeUnavailable = $false
+
function Add-JunctionSupportType {
if ("CuaDriverInstaller.Junction" -as [type]) { return }
+ if ($script:JunctionTypeUnavailable) { return }
$source = @'
using System;
@@ -536,7 +539,12 @@ namespace CuaDriverInstaller
}
'@
- Add-Type -TypeDefinition $source -Language CSharp
+ try {
+ Add-Type -TypeDefinition $source -Language CSharp -ErrorAction Stop
+ } catch {
+ $script:JunctionTypeUnavailable = $true
+ Write-WarningStep "P/Invoke junction support unavailable; using mklink fallback (PowerShell 5.1 Add-Type can fail on non-ASCII code pages)."
+ }
}
function Test-IsJunction([string]$path) {
@@ -548,14 +556,99 @@ function Test-IsJunction([string]$path) {
return (($item.Attributes -band [System.IO.FileAttributes]::ReparsePoint) -ne 0)
}
+function Invoke-MklinkJunction([string]$linkPath, [string]$targetPath) {
+ $cmd = "mklink /J `"$linkPath`" `"$targetPath`""
+ $prevEAP = $ErrorActionPreference
+ $ErrorActionPreference = 'Continue'
+ try {
+ $output = & $env:ComSpec /d /c $cmd 2>&1
+ $exitCode = $LASTEXITCODE
+ } finally {
+ $ErrorActionPreference = $prevEAP
+ }
+ if ($exitCode -ne 0) {
+ throw "mklink /J failed for $linkPath -> $targetPath (exit $exitCode): $($output -join ' ')"
+ }
+}
+
function Set-JunctionTarget([string]$linkPath, [string]$targetPath) {
Add-JunctionSupportType
- [CuaDriverInstaller.Junction]::SetTarget($linkPath, $targetPath)
+ if (-not $script:JunctionTypeUnavailable) {
+ [CuaDriverInstaller.Junction]::SetTarget($linkPath, $targetPath)
+ return
+ }
+
+ $parent = Split-Path -Parent $linkPath
+ if ($parent -and -not (Test-Path -LiteralPath $parent)) {
+ New-Item -ItemType Directory -Force -Path $parent | Out-Null
+ }
+ if (-not (Test-Path -LiteralPath $linkPath)) {
+ Invoke-MklinkJunction $linkPath $targetPath
+ return
+ }
+ if (-not (Test-IsJunction $linkPath)) {
+ throw "found existing non-junction directory at $linkPath; refusing to replace"
+ }
+
+ $suffix = [guid]::NewGuid().ToString("N")
+ $stagePath = Join-Path $parent ".cua-junction-stage-$suffix"
+ $backupPath = Join-Path $parent ".cua-junction-backup-$suffix"
+ try {
+ Invoke-MklinkJunction $stagePath $targetPath
+ [System.IO.Directory]::Move($linkPath, $backupPath)
+ try {
+ [System.IO.Directory]::Move($stagePath, $linkPath)
+ } catch {
+ $replaceError = $_.Exception.Message
+ try {
+ [System.IO.Directory]::Move($backupPath, $linkPath)
+ } catch {
+ throw "junction swap failed: $replaceError; previous junction remains at '$backupPath' because restoring '$linkPath' also failed: $($_.Exception.Message)"
+ }
+ throw "junction swap failed: $replaceError; restored previous junction at '$linkPath'"
+ }
+ [System.IO.Directory]::Delete($backupPath, $false)
+ } finally {
+ if (Test-Path -LiteralPath $stagePath) {
+ [System.IO.Directory]::Delete($stagePath, $false)
+ }
+ }
}
function Get-JunctionTarget([string]$linkPath) {
Add-JunctionSupportType
- return [CuaDriverInstaller.Junction]::GetTarget($linkPath)
+ if (-not $script:JunctionTypeUnavailable) {
+ return [CuaDriverInstaller.Junction]::GetTarget($linkPath)
+ }
+
+ try {
+ $item = Get-Item -LiteralPath $linkPath -Force -ErrorAction Stop
+ $targetProperty = $item.PSObject.Properties["Target"]
+ if ($targetProperty) {
+ $targets = @(@($targetProperty.Value) | Where-Object { -not [string]::IsNullOrWhiteSpace($_) })
+ if ($targets.Count -gt 0) { return [string]$targets[0] }
+ }
+
+ $prevEAP = $ErrorActionPreference
+ $ErrorActionPreference = 'Continue'
+ try {
+ $output = & $env:ComSpec /d /c "fsutil reparsepoint query `"$linkPath`"" 2>&1
+ $exitCode = $LASTEXITCODE
+ } finally {
+ $ErrorActionPreference = $prevEAP
+ }
+ if ($exitCode -ne 0) { return $null }
+ foreach ($line in @($output)) {
+ $text = [string]$line
+ $driveSeparator = $text.IndexOf(':\', [System.StringComparison]::Ordinal)
+ if ($driveSeparator -gt 0 -and [char]::IsLetter($text[$driveSeparator - 1])) {
+ return $text.Substring($driveSeparator - 1).Trim()
+ }
+ }
+ return $null
+ } catch {
+ return $null
+ }
}
# Ensure a junction at $linkPath points at $targetPath. Refuses to clobber
diff --git a/libs/cua-driver/scripts/tests/install-junction-fallback.ps1 b/libs/cua-driver/scripts/tests/install-junction-fallback.ps1
new file mode 100644
index 0000000000..11a7fd0bc6
--- /dev/null
+++ b/libs/cua-driver/scripts/tests/install-junction-fallback.ps1
@@ -0,0 +1,101 @@
+# Windows PowerShell 5.1 regression test for the install.ps1 junction fallback.
+# Run from the repository root with powershell.exe -File.
+
+Set-StrictMode -Version Latest
+$ErrorActionPreference = "Stop"
+
+if ($PSVersionTable.PSEdition -ne "Desktop" -or $PSVersionTable.PSVersion.Major -ne 5) {
+ throw "this test must run under Windows PowerShell 5.1"
+}
+
+$installer = Join-Path $PSScriptRoot "..\install.ps1"
+$tokens = $null
+$parseErrors = $null
+$ast = [System.Management.Automation.Language.Parser]::ParseFile(
+ $installer,
+ [ref]$tokens,
+ [ref]$parseErrors
+)
+if ($parseErrors.Count -ne 0) {
+ throw "install.ps1 parse failed: $($parseErrors -join '; ')"
+}
+
+$wanted = @("Test-IsJunction", "Get-JunctionTarget", "Set-JunctionTarget")
+foreach ($name in $wanted) {
+ $definition = $ast.Find({
+ param($node)
+ $node -is [System.Management.Automation.Language.FunctionDefinitionAst] -and
+ $node.Name -eq $name
+ }, $true)
+ if (-not $definition) { throw "function $name not found in install.ps1" }
+ . ([scriptblock]::Create($definition.Extent.Text))
+}
+
+function Add-JunctionSupportType {}
+$script:JunctionTypeUnavailable = $true
+$script:MklinkCalls = 0
+function Invoke-MklinkJunction([string]$linkPath, [string]$targetPath) {
+ $script:MklinkCalls++
+ if ($script:MklinkCalls -eq 1) {
+ throw "simulated replacement mklink failure"
+ }
+ $output = & $env:ComSpec /d /c "mklink /J `"$linkPath`" `"$targetPath`"" 2>&1
+ if ($LASTEXITCODE -ne 0) {
+ throw "restore mklink failed (exit $LASTEXITCODE): $($output -join ' ')"
+ }
+}
+
+$oldCodePage = (& $env:ComSpec /d /c chcp) -replace '[^0-9]', ''
+$root = Join-Path ([System.IO.Path]::GetTempPath()) ("cua-安装-junction-" + [guid]::NewGuid().ToString("N"))
+$oldTarget = Join-Path $root "旧版本"
+$newTarget = Join-Path $root "新版本"
+$link = Join-Path $root "current"
+
+try {
+ & $env:ComSpec /d /c "chcp 936 >nul"
+ if ($LASTEXITCODE -ne 0) { throw "could not activate code page 936" }
+
+ New-Item -ItemType Directory -Force -Path $oldTarget, $newTarget | Out-Null
+ $output = & $env:ComSpec /d /c "mklink /J `"$link`" `"$oldTarget`"" 2>&1
+ if ($LASTEXITCODE -ne 0) {
+ throw "fixture junction creation failed (exit $LASTEXITCODE): $($output -join ' ')"
+ }
+
+ $message = $null
+ try {
+ Set-JunctionTarget $link $newTarget
+ throw "Set-JunctionTarget unexpectedly succeeded"
+ } catch {
+ $message = $_.Exception.Message
+ }
+
+ if ($message -notlike "*simulated replacement mklink failure*") {
+ throw "replacement failure was not preserved: $message"
+ }
+ if (-not (Test-IsJunction $link)) {
+ throw "working junction was lost after staged replacement failure"
+ }
+ $actual = Get-JunctionTarget $link
+ if ([string]::IsNullOrWhiteSpace($actual)) {
+ $diagnostic = & $env:ComSpec /d /c "fsutil reparsepoint query `"$link`"" 2>&1
+ throw "PowerShell 5.1 fallback could not read the preserved target: $($diagnostic -join ' | ')"
+ }
+ if ([System.IO.Path]::GetFullPath($actual) -ne [System.IO.Path]::GetFullPath($oldTarget)) {
+ throw "junction target changed after staged replacement failure: expected '$oldTarget', got '$actual'"
+ }
+ if ($script:MklinkCalls -ne 1) {
+ throw "expected one failed staged replacement, got $script:MklinkCalls calls"
+ }
+
+ Write-Host "PASS: PowerShell 5.1 code-page-936 fallback preserves the prior junction"
+} finally {
+ if (Test-Path -LiteralPath $link) {
+ [System.IO.Directory]::Delete($link, $false)
+ }
+ if (Test-Path -LiteralPath $root) {
+ Remove-Item -LiteralPath $root -Recurse -Force
+ }
+ if ($oldCodePage) {
+ & $env:ComSpec /d /c "chcp $oldCodePage >nul"
+ }
+}