Fix CodeFormatter strict mode error when processing a single file#2543
Merged
Conversation
Use a typed List[object] instead of bare foreach assignment so .Count is always available under Set-StrictMode -Version Latest. Previously, a single FileInfo object lacks a .Count property in strict mode, causing a PropertyNotFoundException when CodeFormatter.ps1 processes exactly one changed file. Co-authored-by: Copilot <223556219+Copilot@users.noreply.github.com>
Contributor
There was a problem hiding this comment.
Pull request overview
Fixes a strict-mode PropertyNotFoundException in Invoke-CodeFormatterOnFiles.ps1 triggered when exactly one file is processed. The foreach assignment yielded a scalar FileInfo (no .Count) under Set-StrictMode -Version Latest; switching to a typed List[object] guarantees .Count regardless of element count.
Changes:
- Replace
foreachassignment withSystem.Collections.Generic.List[object]accumulator - Simplify the empty-check guard to rely on
.Count
lusassl-msft
approved these changes
May 29, 2026
Member
Author
|
/azp run |
|
Azure Pipelines successfully started running 1 pipeline(s). |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Problem
Invoke-CodeFormatterOnFiles.ps1throws aPropertyNotFoundExceptionon.Countwhen called throughCodeFormatter.ps1 -Branchwith exactly one changed.ps1/.psm1/.mdfile:PropertyNotFoundException: .build\Invoke-CodeFormatterOnFiles.ps1:47 Line | 47 | if ($null -eq $filesToCheck -or $filesToCheck.Count -eq 0) { | The property 'Count' cannot be found on this object.Root Cause
This was introduced in b04eb8f (Refactor CodeFormatter: extract Invoke-CodeFormatterOnFiles). When the formatting logic was extracted from
CodeFormatter.ps1intoInvoke-CodeFormatterOnFiles.ps1, theforeachassignment pattern was preserved:powershell $filesToCheck = foreach ($path in $FilePaths) { ... }In
CodeFormatter.ps1,Set-StrictMode -Version Latestis active (line 14). Under strict mode, when theforeachyields a singleFileInfoobject, PowerShell assigns it as a scalar — not an array. A scalarFileInfohas no.Countproperty, so the guard check on line 47 throws.Why it wasn't caught during the refactor: The original
CodeFormatter.ps1had separate code paths for single-file vs multi-file — theGet-ChildItem -Includecall with-Recursein the non-optimized path always returned an array. When the logic was extracted into a standalone function, the caller controls the input, and the function lost that implicit array guarantee. The strict mode interaction only manifests when the branch diff has exactly one qualifying file, which is an uncommon but valid scenario.Fix
Use a typed
List[object]with explicit.Add()calls instead of bareforeachassignment. TheListalways has a.Countproperty regardless of how many items it contains.`powershell
$filesToCheck = New-Object System.Collections.Generic.List[object]
foreach ($path in $FilePaths) {
if (Test-Path -Path $path) {
$filesToCheck.Add((Get-Item -Path $path))
} else {
Write-Warning "File not found, skipping: $path"
}
}
if ($filesToCheck.Count -eq 0) { ... }
`
Tested under
Set-StrictMode -Version Latestwith 0, 1, and multiple files.