-
Notifications
You must be signed in to change notification settings - Fork 391
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
New rule for ForEach-Object -Parallel that helps users pinpoint problems with their variable scoping #1410
Comments
I think your tests are missing a case for assigning |
@felixfbecker thank you, added a test :-) |
You could publish this as a custom rule, which can be written in PowerShell. Otherwise, I'd rather create a new rule to keep it separate from |
I was hoping to generate some built-in user education through a rule like this. I am aware the same kind of problems are also present in You have a point with the user being able to declare a variable with the same name in the new scope. There is nothing to prevent that, and it's a harder situation to check against. And I am guessing would 99% of the time be non-intentional. I have no real fix for that. If we go forward with this idea, I agree this should be a separate rule in PsScriptAnalyzer, or a custom (powershell based) rule. I just think we can reuse ideas from the rule I mentioned, because it already solves some issues with detecting variables, built in variables, scoping etc. If we can find another way, even outside of PsScriptAnalyzer, how we can help users find out about the difference between |
I think a rule like this definitely belongs into core PSScriptAnalyzer, as it can be immensely useful in catching errors before execution. Especially in the scenario where you're trying to parallelize a |
Maybe this new rule should be applied to a couple of cmdlets/parameter pairs. |
Yes, I might, but it will have to wait until next week. I am also keeping an eye on PowerShell/PowerShell#11811 |
@Jawz84 |
Thanks Christoph. Working on it. |
Summary of the new feature
In response to questions seen on Twitter, like this one: https://twitter.com/_bateskevin/status/1225536662769405952?s=20
As a PowerShell 7 user, I would like to be warned when I use variables incorrectly with
ForEach-Object -Parallel
, so I don't have to wonder why I get an error related to a non-initialized variable.Example with incorrect variable usage:
I would like PSScriptAnalyzer to warn me that
$myVar
is not going to work. (I need to use $using:myVar)Proposed technical implementation details (optional)
A quick proof of context:
What is the latest version of PSScriptAnalyzer at the point of writing
1.18.3
The text was updated successfully, but these errors were encountered: