PowerShell / PowerShell/PSScriptAnalyzer
Rule Idea: Warn on `[xml](Get-Content <File>)` pattern
Nobody has claimed this yet.
- Dominant language
- C#
- Stars
- 2.2k
- Forks
- 414
- Avg merge
- 13h 1m
- Merged PRs (30d)
- 2
Description
Summary of the new feature
Warn on Pattern: [xml](Get-Content <File>)
Suggest Pattern: $a = [xml]::new(); $a.Load(<File>)
This pattern is currently in wide use, but it has some problems and should be discouraged.
-
This pattern, due to using Get-Content, has problems if the (perfectly valid) xml file being parsed is not utf8 encoded. See: https://github.com/PowerShell/PowerShell/issues/14505 & https://stackoverflow.com/questions/65263942/how-to-load-or-read-an-xml-file-using-convertto-xml-and-select-xml/65264118#65264118
-
This pattern is very inefficient. I know that PSSA does not generally worry about this - but since the encoding issue above exists, feels worth it to mention. Testing against a ~1mb file shows
[xml](Get-Content <File>)as ~12x slower, and against a 170mb file as 22x slower (loading took 46 seconds!).
What is the latest version of PSScriptAnalyzer at the point of writing
PSSA 1.19.1
Powershell Versions
This currently affects both Windows Powershell 5.1 and Powershell 7.
Contributor guide
First steps
- Read the whole issue, then the project's contributing guide.
- Comment on the issue to say you are picking it up — it saves two people doing the same work.
- Fork the repository and make your change on a branch.
- Open a pull request that references the issue number.
Research direction
The issue targets PSScriptAnalyzer and the PowerShell pattern [xml](Get-Content <File>). Begin by finding the analyzer's existing rule implementations and tests for pattern-based diagnostics; done means the pattern is warned on and the suggested XML-loading form is reported consistently on Windows PowerShell 5.1 and PowerShell 7.
Written by the indexing model from the issue text.
Assessment
- Tech stack
- powershell
- Domain
- tooling
- Issue type
- Feature
- Difficulty
- 3/5
- Estimated time
- 1-2 days
- Activity status
- Stale
- Clarity
- Mostly clear
- Newbie friendliness
- 38/100