question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

`Set-StrictMode 2`: False positive for references to the `Count` property

See original GitHub issue

Prerequisites

Steps to reproduce

Set-StrictMode -Version Latest
$TempFile = New-TemporaryFile
$TempPath = Join-Path $Env:Temp ('Folder_' + [Guid]::NewGuid())
$TempFolder = New-Item -ItemType Directory -Path $TempPath
Copy-Item -Path $TempFile -Destination $TempFolder
$TempFolder |Should -Exist
(Get-ChildItem $TempFolder).Count |Should -Be 1

Expected behavior

No Error

Actual behavior

PropertyNotFoundException: The property ‘Count’ cannot be found on this object. Verify that the property exists.

Error details

With Set-StrictMode -Off the (Get-ChildItem $TempFolder).Count return 1 which shows that the property does exist

Environment data

Name                           Value
----                           -----
PSVersion                      7.2.0
PSEdition                      Core
GitCommitId                    7.2.0
OS                             Microsoft Windows 10.0.22000
Platform                       Win32NT
PSCompatibleVersions           {1.0, 2.0, 3.0, 4.0…}
PSRemotingProtocolVersion      2.3
SerializationVersion           1.1.0.1
WSManStackVersion              3.0

Issue Analytics

  • State:open
  • Created 2 years ago
  • Comments:13 (4 by maintainers)

github_iconTop GitHub Comments

4reactions
vexx32commented, Nov 17, 2021

That makes sense to me. If strict mode is intended to by design restrict usage of otherwise ubiquitous properties provided by the engine, that should be well documented. Given that it’s not (as far as I’m aware?), I would be inclined to assume it’s more of an accident of implementation and we should see if it’s feasible to rectify the issue.

2reactions
mklement0commented, Dec 7, 2021

Note that this problematic behavior was first reported in

#15526 also discusses the inconsistency, but argues for the opposite resolution; to borrow from my comment there:

Engine-supplied members such as .Count, .Length, scalar [...], .psobject, ForEach(), … now have an official name: intrinsic members.

It makes much more sense to make them consistently available, irrespective of strict mode.

This is already true for most of them, but inexplicably not for .Count and .Length ([pscustomobject] being the only exception), despite their tremendous usefulness in unified handling of scalars and collections.

Simplest repro:

# .Count fails on all types without their own .Count property  *except* [pscustomobject]`
# Applies analogously to .Length
& { Set-StrictMode -Version 2; [pscustomobject] @{ foo=1 }.Count; 'foo'.Count; $null.Count }

@iRon7, your [string[]] $StringArray parameter example can be reduced to $null.Count, because unbound reference-type parameters (without default values) are $null.

Read more comments on GitHub >

github_iconTop Results From Across the Web

Objects with no '.Count' Property - use of @() (array ...
Count property unexpectedly fails when Set-StrictMode -Version 2 or higher is in effect, which is a long-standing bug reported in GitHub ...
Read more >
Set-StrictMode (Microsoft.PowerShell.Core)
Uninitialized variables are assumed to have a value of 0 (zero) or $Null , depending on type; References to non-existent properties return $Null;...
Read more >
The Poster of the Plethora of PowerShell Pitfalls - Simple Talk
Arithmetic operators produce unexpected results with arrays (precedence issue). Issue: Simple arithmetic operations with an array seem wrong.
Read more >
Strict mode @ strict_mode @ StarRocks Docs
Strict mode is an optional property that you can configure for data loads. It affects the loading behavior and the final loaded data....
Read more >
Class PgxConfigBuilder
Examples: A value of 0.0 means graphs get freed as soon as their reference count becomes zero. That is, all sessions which loaded...
Read more >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found