Skip to content

Commit 4bb854b

Browse files
authored
tools: Extract Dart SDK to temp directory before moving to final location (flutter#191263)
Extract the downloaded Dart SDK zip/tar into a temporary staging directory first, and only move/rename it to `bin/cache/dart-sdk` once extraction succeeds. This prevents the cache directory from being left in a partially populated or corrupted state if download or extraction is interrupted. Fixes flutter#15552 ## Pre-launch Checklist - [x] I read the [Contributor Guide] and followed the process outlined there for submitting PRs. - [x] I read the [AI contribution guidelines] and understand my responsibilities, or I am not using AI tools. - [x] I read the [Tree Hygiene] wiki page, which explains my responsibilities. - [x] I read and followed the [Flutter Style Guide], including [Features we expect every widget to implement]. - [x] I signed the [CLA]. - [x] I listed at least one issue that this PR fixes in the description above. - [x] I updated/added relevant documentation (doc comments with `///`). - [x] I added new tests to check the change I am making, or this PR is [test-exempt]. - [x] I followed the [breaking change policy] and added [Data Driven Fixes] where supported. - [x] All existing and new tests are passing. <!-- Links --> [Contributor Guide]: https://github.com/flutter/flutter/blob/main/docs/contributing/Tree-hygiene.md#overview [AI contribution guidelines]: https://github.com/flutter/flutter/blob/main/docs/contributing/Tree-hygiene.md#ai-contribution-guidelines [Tree Hygiene]: https://github.com/flutter/flutter/blob/main/docs/contributing/Tree-hygiene.md [test-exempt]: https://github.com/flutter/flutter/blob/main/docs/contributing/Tree-hygiene.md#tests [Flutter Style Guide]: https://github.com/flutter/flutter/blob/main/docs/contributing/Style-guide-for-Flutter-repo.md [Features we expect every widget to implement]: https://github.com/flutter/flutter/blob/main/docs/contributing/Style-guide-for-Flutter-repo.md#features-we-expect-every-widget-to-implement [CLA]: https://cla.developers.google.com/ [flutter/tests]: https://github.com/flutter/tests [breaking change policy]: https://github.com/flutter/flutter/blob/main/docs/contributing/Tree-hygiene.md#handling-breaking-changes [Discord]: https://github.com/flutter/flutter/blob/main/docs/contributing/Chat.md [Data Driven Fixes]: https://github.com/flutter/flutter/blob/main/docs/contributing/Data-driven-Fixes.md
1 parent 27f8676 commit 4bb854b

2 files changed

Lines changed: 80 additions & 28 deletions

File tree

‎bin/internal/update_dart_sdk.ps1‎

Lines changed: 44 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -70,20 +70,11 @@ if ($env:FLUTTER_HOST_ARCH -eq "arm64") {
7070
}
7171
$dartSdkUrl = "$dartSdkBaseUrl/flutter_infra_release/flutter/$engineVersion/$dartZipName"
7272

73-
if ((Test-Path $dartSdkPath) -or (Test-Path $dartSdkLicense)) {
74-
# Move old SDK to a new location instead of deleting it in case it is still in use (e.g. by IntelliJ).
75-
$oldDartSdkSuffix = 1
76-
while (Test-Path "$cachePath\$oldDartSdkPrefix$oldDartSdkSuffix") { $oldDartSdkSuffix++ }
77-
78-
if (Test-Path $dartSdkPath) {
79-
Rename-Item $dartSdkPath "$oldDartSdkPrefix$oldDartSdkSuffix"
80-
}
81-
82-
if (Test-Path $dartSdkLicense) {
83-
Rename-Item $dartSdkLicense "$oldDartSdkPrefix$oldDartSdkSuffix.LICENSE.md"
84-
}
73+
$dartSdkPathTemp = "$cachePath\dart-sdk.tmp"
74+
if (Test-Path $dartSdkPathTemp) {
75+
Remove-Item $dartSdkPathTemp -Recurse -Force
8576
}
86-
New-Item $dartSdkPath -force -type directory | Out-Null
77+
New-Item $dartSdkPathTemp -force -type directory | Out-Null
8778
$dartSdkZip = "$cachePath\$dartZipName"
8879

8980
Try {
@@ -106,27 +97,63 @@ Catch {
10697
If (Get-Command 7z -errorAction SilentlyContinue) {
10798
Write-Host "Expanding downloaded archive with 7z..."
10899
# The built-in unzippers are painfully slow. Use 7-Zip, if available.
109-
& 7z x $dartSdkZip "-o$cachePath" -bd | Out-Null
100+
& 7z x $dartSdkZip "-o$dartSdkPathTemp" -bd | Out-Null
110101
} ElseIf (Get-Command 7za -errorAction SilentlyContinue) {
111102
Write-Host "Expanding downloaded archive with 7za..."
112103
# Use 7-Zip's standalone version 7za.exe, if available.
113-
& 7za x $dartSdkZip "-o$cachePath" -bd | Out-Null
104+
& 7za x $dartSdkZip "-o$dartSdkPathTemp" -bd | Out-Null
114105
} ElseIf (Get-Command Microsoft.PowerShell.Archive\Expand-Archive -errorAction SilentlyContinue) {
115106
Write-Host "Expanding downloaded archive with PowerShell..."
116107
# Use PowerShell's built-in unzipper, if available (requires PowerShell 5+).
117108
$global:ProgressPreference='SilentlyContinue'
118-
Microsoft.PowerShell.Archive\Expand-Archive $dartSdkZip -DestinationPath $cachePath
109+
Microsoft.PowerShell.Archive\Expand-Archive $dartSdkZip -DestinationPath $dartSdkPathTemp
119110
} Else {
120111
Write-Host "Expanding downloaded archive with Windows..."
121112
# As last resort: fall back to the Windows GUI.
122113
$shell = New-Object -com shell.application
123114
$zip = $shell.NameSpace($dartSdkZip)
124115
foreach($item in $zip.items()) {
125-
$shell.Namespace($cachePath).copyhere($item)
116+
$shell.Namespace($dartSdkPathTemp).copyhere($item)
126117
}
127118
}
128119

129120
Remove-Item $dartSdkZip
121+
122+
if (-not (Test-Path "$dartSdkPathTemp\dart-sdk")) {
123+
Remove-Item $dartSdkPathTemp -Recurse -Force -ErrorAction SilentlyContinue
124+
Write-Error "Dart SDK extraction failed: '$dartSdkPathTemp\dart-sdk' not found."
125+
exit 1
126+
}
127+
128+
# Move old SDK to a new location instead of deleting it in case it is still in use (e.g. by IntelliJ).
129+
if ((Test-Path $dartSdkPath) -or (Test-Path $dartSdkLicense)) {
130+
$oldDartSdkSuffix = 1
131+
while (Test-Path "$cachePath\$oldDartSdkPrefix$oldDartSdkSuffix") { $oldDartSdkSuffix++ }
132+
133+
if (Test-Path $dartSdkPath) {
134+
Rename-Item $dartSdkPath "$oldDartSdkPrefix$oldDartSdkSuffix" -ErrorAction Stop
135+
}
136+
137+
if (Test-Path $dartSdkLicense) {
138+
Rename-Item $dartSdkLicense "$oldDartSdkPrefix$oldDartSdkSuffix.LICENSE.md" -ErrorAction Stop
139+
}
140+
}
141+
142+
# The unzip might have extracted LICENSE.dart_sdk_archive.md to the temp dir
143+
$tempLicense = "$dartSdkPathTemp\LICENSE.dart_sdk_archive.md"
144+
if (Test-Path $tempLicense) {
145+
if (Test-Path $dartSdkLicense) {
146+
Remove-Item $dartSdkLicense -Force -ErrorAction Stop
147+
}
148+
Move-Item $tempLicense $dartSdkLicense -ErrorAction Stop
149+
}
150+
151+
# Move the extracted SDK to the final location
152+
try {
153+
Move-Item "$dartSdkPathTemp\dart-sdk" $dartSdkPath -ErrorAction Stop
154+
} finally {
155+
Remove-Item $dartSdkPathTemp -Recurse -Force -ErrorAction SilentlyContinue
156+
}
130157
$engineVersion | Out-File $engineStamp -Encoding ASCII
131158

132159
# Try to delete all old SDKs and license files.

‎bin/internal/update_dart_sdk.sh‎

Lines changed: 36 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -133,15 +133,11 @@ if [ ! -f "$ENGINE_STAMP" ] || [ "$ENGINE_VERSION" != "$(< "$ENGINE_STAMP")" ];
133133
DART_SDK_BASE_URL="${FLUTTER_STORAGE_BASE_URL:-https://storage.googleapis.com}${ENGINE_REALM:+/$ENGINE_REALM}"
134134
DART_SDK_URL="$DART_SDK_BASE_URL/flutter_infra_release/flutter/$ENGINE_VERSION/$DART_ZIP_NAME"
135135

136-
# if the sdk path exists, copy it to a temporary location
137-
if [ -d "$DART_SDK_PATH" ]; then
138-
rm -rf "$DART_SDK_PATH_OLD"
139-
mv "$DART_SDK_PATH" "$DART_SDK_PATH_OLD"
140-
fi
136+
# Create a temporary directory for extraction to ensure atomicity
137+
DART_SDK_PATH_TEMP="$FLUTTER_ROOT/bin/cache/dart-sdk.tmp"
138+
rm -rf -- "$DART_SDK_PATH_TEMP"
139+
mkdir -m 755 -p -- "$DART_SDK_PATH_TEMP"
141140

142-
# install the new sdk
143-
rm -rf -- "$DART_SDK_PATH"
144-
mkdir -m 755 -p -- "$DART_SDK_PATH"
145141
DART_SDK_ZIP="$FLUTTER_ROOT/bin/cache/$DART_ZIP_NAME"
146142

147143
# Conditionally set verbose flag for LUCI
@@ -172,20 +168,49 @@ if [ ! -f "$ENGINE_STAMP" ] || [ "$ENGINE_VERSION" != "$(< "$ENGINE_STAMP")" ];
172168
>&2 echo " https://flutter.dev/community/china"
173169
>&2 echo
174170
rm -f -- "$DART_SDK_ZIP"
171+
rm -rf -- "$DART_SDK_PATH_TEMP"
175172
exit 1
176173
}
177-
unzip -o -q "$DART_SDK_ZIP" -d "$FLUTTER_ROOT/bin/cache" || {
174+
unzip -o -q "$DART_SDK_ZIP" -d "$DART_SDK_PATH_TEMP" || {
178175
>&2 echo
179176
>&2 echo "It appears that the downloaded file is corrupt; please try again."
180177
>&2 echo "If this problem persists, please report the problem at:"
181178
>&2 echo " https://github.com/flutter/flutter/issues/new?template=01_activation.yml"
182179
>&2 echo
183180
rm -f -- "$DART_SDK_ZIP"
181+
rm -rf -- "$DART_SDK_PATH_TEMP"
184182
exit 1
185183
}
186184
rm -f -- "$DART_SDK_ZIP"
187-
$FIND "$DART_SDK_PATH" -type d -exec chmod 755 {} +
188-
$FIND "$DART_SDK_PATH" -type f $IS_USER_EXECUTABLE -exec chmod a+x,a+r {} +
185+
186+
if [ ! -d "$DART_SDK_PATH_TEMP/dart-sdk" ]; then
187+
>&2 echo "Dart SDK extraction failed: '$DART_SDK_PATH_TEMP/dart-sdk' not found."
188+
rm -rf -- "$DART_SDK_PATH_TEMP"
189+
exit 1
190+
fi
191+
192+
# The unzip might have extracted LICENSE.dart_sdk_archive.md to the temp dir
193+
if [ -f "$DART_SDK_PATH_TEMP/LICENSE.dart_sdk_archive.md" ]; then
194+
mv "$DART_SDK_PATH_TEMP/LICENSE.dart_sdk_archive.md" "$FLUTTER_ROOT/bin/cache/LICENSE.dart_sdk_archive.md"
195+
fi
196+
197+
$FIND "$DART_SDK_PATH_TEMP/dart-sdk" -type d -exec chmod 755 {} +
198+
$FIND "$DART_SDK_PATH_TEMP/dart-sdk" -type f $IS_USER_EXECUTABLE -exec chmod a+x,a+r {} +
199+
200+
# Move old SDK to a temporary location in case it is still in use (e.g. by an IDE).
201+
if [ -d "$DART_SDK_PATH" ]; then
202+
rm -rf "$DART_SDK_PATH_OLD"
203+
mv "$DART_SDK_PATH" "$DART_SDK_PATH_OLD"
204+
fi
205+
206+
# Move the extracted SDK to the final location
207+
mv "$DART_SDK_PATH_TEMP/dart-sdk" "$DART_SDK_PATH" || {
208+
>&2 echo "Failed to move Dart SDK to final destination."
209+
rm -rf -- "$DART_SDK_PATH_TEMP"
210+
exit 1
211+
}
212+
rm -rf -- "$DART_SDK_PATH_TEMP"
213+
189214
echo "$ENGINE_VERSION" > "$ENGINE_STAMP"
190215

191216
# delete any temporary sdk path

0 commit comments

Comments
 (0)