Skip to content

Commit b07ac96

Browse files
Improve file handling in Invoke-DbatoolsFormatter
Added checks to skip directories and non-PowerShell files, improved error handling for file read/write operations, and ensured only valid content is processed. These changes enhance robustness and prevent errors when processing invalid or unreadable files.
1 parent 1f17830 commit b07ac96

File tree

1 file changed

+39
-5
lines changed

1 file changed

+39
-5
lines changed

public/Invoke-DbatoolsFormatter.ps1

Lines changed: 39 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -68,7 +68,7 @@ function Invoke-DbatoolsFormatter {
6868
'PSUseConsistentIndentation',
6969
'PSUseConsistentWhitespace'
7070
)
71-
Rules = @{
71+
Rules = @{
7272
PSPlaceOpenBrace = @{
7373
Enable = $true
7474
OnSameLine = $true
@@ -115,7 +115,29 @@ function Invoke-DbatoolsFormatter {
115115
Stop-Function -Message "Cannot find or resolve $p" -Continue
116116
}
117117

118-
$originalContent = Get-Content -Path $realPath -Raw -Encoding UTF8
118+
# Skip directories and non-PowerShell files
119+
if (Test-Path -Path $realPath -PathType Container) {
120+
Write-Message -Level Verbose "Skipping directory: $realPath"
121+
continue
122+
}
123+
124+
if ($realPath -notmatch '\.ps1$|\.psm1$|\.psd1$') {
125+
Write-Message -Level Verbose "Skipping non-PowerShell file: $realPath"
126+
continue
127+
}
128+
129+
try {
130+
$originalContent = Get-Content -Path $realPath -Raw -Encoding UTF8
131+
} catch {
132+
Stop-Function -Message "Unable to read file $realPath : $($_.Exception.Message)" -Continue
133+
}
134+
135+
# If Get-Content failed, originalContent might be null or empty
136+
if (-not $originalContent) {
137+
Write-Message -Level Verbose "Skipping empty or unreadable file: $realPath"
138+
continue
139+
}
140+
119141
$content = $originalContent
120142

121143
if ($OSEOL -eq "`r`n") {
@@ -134,8 +156,16 @@ function Invoke-DbatoolsFormatter {
134156
# Use custom settings instead of CodeFormattingOTBS
135157
$content = Invoke-Formatter -ScriptDefinition $content -Settings $customSettings -ErrorAction Stop
136158
} catch {
137-
Write-Message -Level Warning "Unable to format $p"
159+
Write-Message -Level Warning "Unable to format $realPath : $($_.Exception.Message)"
160+
continue
161+
}
162+
163+
# Ensure $content is a string before processing
164+
if (-not $content -or $content -isnot [string]) {
165+
Write-Message -Level Warning "Formatter returned unexpected content type for $realPath"
166+
continue
138167
}
168+
139169
#match the ending indentation of CBH with the starting one, see #4373
140170
$CBH = $CBHRex.Match($content).Value
141171
if ($CBH) {
@@ -170,8 +200,12 @@ function Invoke-DbatoolsFormatter {
170200

171201
# Only write the file if there are actual changes
172202
if ($finalContent -ne $originalContent) {
173-
Write-Message -Level Verbose "Formatting changes detected in $realPath"
174-
[System.IO.File]::WriteAllText($realPath, $finalContent, $Utf8NoBomEncoding)
203+
try {
204+
Write-Message -Level Verbose "Formatting changes detected in $realPath"
205+
[System.IO.File]::WriteAllText($realPath, $finalContent, $Utf8NoBomEncoding)
206+
} catch {
207+
Stop-Function -Message "Unable to write file $realPath : $($_.Exception.Message)" -Continue
208+
}
175209
} else {
176210
Write-Message -Level Verbose "No formatting changes needed for $realPath"
177211
}

0 commit comments

Comments
 (0)