Skip to content

Set-StrictMode turns missing JSON fields into crashes

versions · case

bothhard-errorscriptcifirst-partyrepro: verifiedstrictmode-contract
Affectspowershell 51, pwsh 7
Fails asPROPERTYNOTFOUND
Mechanismstrictmode contract
Safe fixpsobject probe

A script parses a JSON manifest and reads optional fields ($entry.tag, $entry.artifacts.$arch). It works for months — then dies with PropertyNotFoundException the first time a manifest omits an optional field, because the script also sets Set-StrictMode -Version Latest.

Terminal window
Set-StrictMode -Version Latest
$entry = '{"name":"x"}' | ConvertFrom-Json
$entry.tag
# PropertyNotFoundException — without StrictMode this quietly yields $null

StrictMode changes the CONTRACT of property access: missing note-properties go from “$null” to “throw”. Optional-field patterns written under default mode become latent crashes when someone adds StrictMode later (usually to catch the dq-regex class of bug — the two traps travel together).

Terminal window
if ($entry.PSObject.Properties.Name -contains 'tag') { $entry.tag }

Probe PSObject.Properties.Name before dereferencing optional fields; the referenced fix wraps every optional manifest access this way.