Skip to content

Fix CodeFormatter strict mode error when processing a single file#2543

Merged
dpaulson45 merged 1 commit into
mainfrom
dpaul-fix-codeformatter-strictmode
May 29, 2026
Merged

Fix CodeFormatter strict mode error when processing a single file#2543
dpaulson45 merged 1 commit into
mainfrom
dpaul-fix-codeformatter-strictmode

Conversation

@dpaulson45
Copy link
Copy Markdown
Member

Problem

Invoke-CodeFormatterOnFiles.ps1 throws a PropertyNotFoundException on .Count when called through CodeFormatter.ps1 -Branch with exactly one changed .ps1/.psm1/.md file:

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.ps1 into Invoke-CodeFormatterOnFiles.ps1, the foreach assignment pattern was preserved:

powershell $filesToCheck = foreach ($path in $FilePaths) { ... }

In CodeFormatter.ps1, Set-StrictMode -Version Latest is active (line 14). Under strict mode, when the foreach yields a single FileInfo object, PowerShell assigns it as a scalar — not an array. A scalar FileInfo has no .Count property, so the guard check on line 47 throws.

Why it wasn't caught during the refactor: The original CodeFormatter.ps1 had separate code paths for single-file vs multi-file — the Get-ChildItem -Include call with -Recurse in 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 bare foreach assignment. The List always has a .Count property 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 Latest with 0, 1, and multiple files.

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>
Copilot AI review requested due to automatic review settings May 29, 2026 19:24
@dpaulson45 dpaulson45 requested a review from a team as a code owner May 29, 2026 19:24
Copy link
Copy Markdown
Contributor

Copilot AI left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

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 foreach assignment with System.Collections.Generic.List[object] accumulator
  • Simplify the empty-check guard to rely on .Count

@dpaulson45
Copy link
Copy Markdown
Member Author

/azp run

@azure-pipelines
Copy link
Copy Markdown

Azure Pipelines successfully started running 1 pipeline(s).

@dpaulson45 dpaulson45 merged commit 9566a2c into main May 29, 2026
8 checks passed
@dpaulson45 dpaulson45 deleted the dpaul-fix-codeformatter-strictmode branch May 29, 2026 20:33
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

3 participants