-
-
Notifications
You must be signed in to change notification settings - Fork 1.5k
Fix #6515 "Expand-7zipArchive won't remove top folder if $ExtractDir depth exceeds 2" - Idea 3 #6518
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
base: develop
Are you sure you want to change the base?
Conversation
WalkthroughUpdates non-tar extraction cleanup in lib/decompress.ps1: resolves absolute paths, derives the extracted top-level directory, compares against destination contents to decide if it’s safe to remove, switches move operation to explicit -from/-to parameters, conditionally deletes the top-level folder, and preserves existing tar handling and log cleanup. Changes
Sequence Diagram(s)sequenceDiagram
autonumber
participant C as Caller
participant D as decompress.ps1
participant Z as 7-Zip
participant FS as FileSystem
C->>D: Expand-7zipArchive(archive, dest, ExtractDir)
D->>D: Resolve full paths<br/>(dest, ExtractDir, topPath)
D->>Z: Extract archive to temp/extract path
Z-->>D: Extraction complete
D->>FS: Inspect dest to detect existing dirs/files
D->>D: Compute shouldRemoveTop flag
D->>FS: Move contents (-from topPath -to dest)
alt shouldRemoveTop == true
D->>FS: Remove topPath
else
D->>D: Skip removal
end
D-->>C: Return
note over D,FS: Tar handling and log cleanup remain as before
Estimated code review effort🎯 3 (Moderate) | ⏱️ ~20 minutes Poem
Pre-merge checks and finishing touches❌ Failed checks (1 warning)
✅ Passed checks (4 passed)
✨ Finishing touches🧪 Generate unit tests (beta)
Thanks for using CodeRabbit! It's free for OSS, and your support helps us grow. If you like it, consider giving us a shout-out. Comment |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Actionable comments posted: 1
🧹 Nitpick comments (1)
lib/decompress.ps1 (1)
152-154: Consider logging cleanup failures.Using
-ErrorAction 'Ignore'silently suppresses all errors during directory removal. While this prevents installation failures due to cleanup issues (locked files, permissions), it also hides potential problems from users.Consider using
SilentlyContinueinstead and checking if the directory still exists afterward, logging a warning if cleanup failed:# Remove leftovers after movedir if ($DeleteExtractDirTopPath) { - Remove-Item -Path $ExtractDirTopPath -Recurse -Force -ErrorAction 'Ignore' + Remove-Item -Path $ExtractDirTopPath -Recurse -Force -ErrorAction 'SilentlyContinue' + if (Test-Path $ExtractDirTopPath) { + warn "Failed to remove temporary extraction directory: $(friendly_path $ExtractDirTopPath)" + } }
📜 Review details
Configuration used: CodeRabbit UI
Review profile: CHILL
Plan: Pro
📒 Files selected for processing (1)
lib/decompress.ps1(1 hunks)
🔇 Additional comments (2)
lib/decompress.ps1 (2)
126-138: LGTM: Path resolution logic is correct.The path manipulation correctly derives both the full extraction path and the top-level directory path. The split regex
[\\/]handles both Windows and Unix-style separators, and filtering empty strings ensures trailing slashes don't cause issues.
150-150: movedir supports named parameters
Themovedirfunction is defined with parameters($from, $to), so using-fromand-toworks as intended.
| $DeleteExtractDirTopPath = [bool]( | ||
| ([System.IO.DirectoryInfo]($ExtractDirTopPath)).'Name' -notin [System.IO.Directory]::GetDirectories( | ||
| $ExtractDirPath | ||
| ).ForEach{ | ||
| $_.Split( | ||
| [System.IO.Path]::DirectorySeparatorChar | ||
| )[-1] | ||
| } | ||
| ) |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Add error handling for directory enumeration.
If $ExtractDirPath doesn't exist or is inaccessible, GetDirectories() will throw an exception and halt the extraction. Consider wrapping this in a try-catch or checking path existence first.
Apply this diff to add error handling:
)
)
- # Check if $ExtractDirPath contains a directory with the same name as $ExtractDirTopPath
- $DeleteExtractDirTopPath = [bool](
- ([System.IO.DirectoryInfo]($ExtractDirTopPath)).'Name' -notin [System.IO.Directory]::GetDirectories(
- $ExtractDirPath
- ).ForEach{
- $_.Split(
- [System.IO.Path]::DirectorySeparatorChar
- )[-1]
- }
- )
+ # Check if $ExtractDirPath contains a directory with the same name as $ExtractDirTopPath
+ $DeleteExtractDirTopPath = $true
+ if (Test-Path -Path $ExtractDirPath -PathType Container) {
+ $DeleteExtractDirTopPath = [bool](
+ ([System.IO.DirectoryInfo]($ExtractDirTopPath)).'Name' -notin [System.IO.Directory]::GetDirectories(
+ $ExtractDirPath
+ ).ForEach{
+ $_.Split(
+ [System.IO.Path]::DirectorySeparatorChar
+ )[-1]
+ }
+ )
+ }
# Move content of $ExtractDir to destination📝 Committable suggestion
‼️ IMPORTANT
Carefully review the code before committing. Ensure that it accurately replaces the highlighted code, contains no missing lines, and has no issues with indentation. Thoroughly test & benchmark the code to ensure it meets the requirements.
| $DeleteExtractDirTopPath = [bool]( | |
| ([System.IO.DirectoryInfo]($ExtractDirTopPath)).'Name' -notin [System.IO.Directory]::GetDirectories( | |
| $ExtractDirPath | |
| ).ForEach{ | |
| $_.Split( | |
| [System.IO.Path]::DirectorySeparatorChar | |
| )[-1] | |
| } | |
| ) | |
| # Check if $ExtractDirPath contains a directory with the same name as $ExtractDirTopPath | |
| $DeleteExtractDirTopPath = $true | |
| if (Test-Path -Path $ExtractDirPath -PathType Container) { | |
| $DeleteExtractDirTopPath = [bool]( | |
| ([System.IO.DirectoryInfo]($ExtractDirTopPath)).'Name' -notin [System.IO.Directory]::GetDirectories( | |
| $ExtractDirPath | |
| ).ForEach{ | |
| $_.Split( | |
| [System.IO.Path]::DirectorySeparatorChar | |
| )[-1] | |
| } | |
| ) | |
| } | |
| # Move content of $ExtractDir to destination |
🤖 Prompt for AI Agents
In lib/decompress.ps1 around lines 140 to 148, the call to
[System.IO.Directory]::GetDirectories($ExtractDirPath) can throw if
$ExtractDirPath doesn't exist or is inaccessible; wrap the directory enumeration
in a check/try-catch: first verify Test-Path -Path $ExtractDirPath -PathType
Container and only enumerate if true, otherwise set the result to an empty
array; alternatively wrap the GetDirectories call in try { ... } catch { log the
error (or set empty array) } so $DeleteExtractDirTopPath calculation never
throws and proceeds safely.
Description
Fix #6515 by simply deleting what's left after $ExtractDir has been moved, unless there is a directory name conflict as in #6011.
This is a third idea to a fix, also see
Motivation and Context
Closes #6515.
How Has This Been Tested?
Checklist:
developbranch.Summary by CodeRabbit
Bug Fixes
Refactor