Skip to content

Fix NullReferenceException in manifest rules on .psd1 files with non-string keys - #2306

Open
David Dashti (Dashtid) wants to merge 1 commit into
PowerShell:mainfrom
Dashtid:fix/manifest-rules-non-string-key
Open

David Dashti (Dashtid) wants to merge 1 commit into
PowerShell:mainfrom
Dashtid:fix/manifest-rules-non-string-key

Conversation

@Dashtid

Copy link
Copy Markdown

PR Summary

PSAvoidUsingDeprecatedManifestFields, PSMissingModuleManifestField and PSUseToExportFieldsInManifest no longer throw a NullReferenceException on a .psd1 whose top-level hashtable has a non-string key, such as @{ 0 = 'OK' }. Helper.IsModuleManifest now returns false when such a key makes GetMapFromHashtableAst return null, which also fixes the same error in PSDSCUseIdenticalMandatoryParametersForDSC. Each manifest rule's tests check a data file with integer keys.

PR Checklist

Claude helped me write this and the issue, and I reviewed every line before sending.

IsModuleManifest threw a NullReferenceException on a .psd1 whose
top-level hashtable has a key that is not a string constant, such as
the data file @{ 0 = 'OK' }: GetMapFromHashtableAst returns null for
such a key, and the map was used without a check. The manifest rules
that call it, PSAvoidUsingDeprecatedManifestFields,
PSMissingModuleManifestField and PSUseToExportFieldsInManifest,
reported this as a rule error, and so did
PSDSCUseIdenticalMandatoryParametersForDSC.

Return false in that case, as the method did when 2f69abf added it.

Copilot AI left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

🟢 Approval recommended

No active review issues are reported for the change addressing non-string keys in PowerShell data files.

No open issues 🎉

What changed in this PR

Fixes #2305 by preventing manifest rules from throwing NullReferenceException when a .psd1 data file contains non-string top-level keys.

Changes:

  • Adds a null guard to Helper.IsModuleManifest, so these data files are not classified as module manifests.
  • Adds an integer-key fixture and regression tests confirming all three manifest rules produce neither errors nor diagnostics.
File Description
Tests/​Rules/​UseToExportFieldsInManifest.tests.ps1 Adds regression coverage for the export-fields rule.
Tests/​Rules/​TestManifest/​PowerShellDataFileNonStringKey.psd1 Adds a shared data-file fixture with integer keys.
Tests/​Rules/​AvoidUsingDeprecatedManifestFields.tests.ps1 Adds regression coverage for the deprecated-fields rule.
Tests/​Rules/​AvoidUnloadableModuleOrMissingRequiredFieldInManifest.tests.ps1 Adds regression coverage for the missing-manifest-field rule.
Engine/​Helper.cs Returns false when hashtable-to-map conversion returns null.

Review effort: Balanced

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

2 participants