GV

GeezerVison

The World Wide Web, restored to 800 pixels of glory.

Home|About|Gaming cabinet|Archives|How it works|Starter sites

On this site

  • Sign in
  • ActionsCI/CD and automation
  • CodespacesCloud development environments
  • All Features
  • Documentation
  • Training
  • Certifications
  • Build with Copilot
  • Customize Copilot
  • Integrations
  • Trending repos
  • GitHub Sponsors
  • Developer articles
  • Enterprises

This view

  • Original page
  • New address

GeezerVison is showing github.com — Add UseUsingScopeModifierInNewRunspaces rule by Jawz84 · Pull Request #1419 · PowerShell/PSScriptAnalyzer

Conversation

@Jawz84

@Jawz84 Jos Koelewijn (Jawz84) commented Feb 17, 2020 •
edited
Loading

Uh oh!

There was an error while loading. Please reload this page.

Copy link
Copy Markdown
Contributor

PR Summary

Fixes #1410

  • write tests
  • write documentation
  • draft rudimentary rule
  • tune rule until all tests for this rule pass (except the test for built-in vars)
  • adapt other rules that start failing
  • add support for built-in variable detection
  • expand functionality to other commands /situations where this check makes sense
    • added support for InlineScript, Start-(Thread)Job and Invoke-Command
  • finetune
  • implement suggested correction (Replace $var with $using:var)
    • add test for suggested correction
  • refactor to AstVisitor/AstVisitor2
  • add support for DSC Script Resource (SetScript, GetScript, TestScript)

PR Checklist

  • PR has a meaningful title
    • Use the present tense and imperative mood when describing your changes
  • Summarized changes
  • Change is not breaking
  • Make sure all .cs, .ps1 and .psm1 files have the correct copyright header
  • Make sure you've added a new test if existing tests do not effectively test the code changed and/or updated documentation
  • This PR is ready to merge and is not Work in Progress.
    • If the PR is work in progress, please add the prefix WIP: to the beginning of the title and remove the prefix when the PR is ready.

Sorry, something went wrong.

Uh oh!

There was an error while loading. Please reload this page.

All reactions
Jos Koelewijn added 4 commits February 16, 2020 16:36
Jos Koelewijn Jos Koelewijn
Add tests for AvoidUnInitializedVarsInNewRunspaces
8b9f115
Jos Koelewijn Jos Koelewijn
Add strings for AvoidUnInitializedVarsInNewRunspaces
0ab5fbf
Jos Koelewijn Jos Koelewijn
Add documentation
7182edb
Jos Koelewijn Jos Koelewijn
wrestling with Ast in C# - not working
068b1fc
bergmeister
Christoph Bergmeister (bergmeister) reviewed Feb 17, 2020
View reviewed changes
Comment thread Tests/Rules/AvoidUnInitializedVarsInNewRunspaces.tests.ps1 Resolved

Uh oh!

There was an error while loading. Please reload this page.

bergmeister
Christoph Bergmeister (bergmeister) reviewed Feb 17, 2020
View reviewed changes
Comment thread Tests/Rules/AvoidUnInitializedVarsInNewRunspaces.tests.ps1 Resolved

Uh oh!

There was an error while loading. Please reload this page.

bergmeister
Christoph Bergmeister (bergmeister) reviewed Feb 17, 2020
View reviewed changes
Comment thread Tests/Rules/AvoidUnInitializedVarsInNewRunspaces.tests.ps1 Resolved

Uh oh!

There was an error while loading. Please reload this page.

bergmeister
Christoph Bergmeister (bergmeister) reviewed Feb 17, 2020
View reviewed changes
Comment thread Rules/AvoidUnInitializedVarsInNewRunspaces.cs Resolved

Uh oh!

There was an error while loading. Please reload this page.

bergmeister
Christoph Bergmeister (bergmeister) reviewed Feb 17, 2020
View reviewed changes
Comment thread Rules/AvoidUnInitializedVarsInNewRunspaces.cs Resolved

Uh oh!

There was an error while loading. Please reload this page.

@bergmeister

Christoph Bergmeister (bergmeister) commented Feb 17, 2020

Copy link
Copy Markdown
Collaborator

You are on the right track. I suggest to continue focusing on the code of the rule itself. Don't worry about the rest like docs or some other test failures at the moment, they will be easy to fix at the end.

Jawz84 reacted with thumbs up emoji
All reactions
  • 👍 1 reaction

Sorry, something went wrong.

Uh oh!

There was an error while loading. Please reload this page.

@Jawz84
Jos Koelewijn (Jawz84) requested a review from Christoph Bergmeister (bergmeister) February 18, 2020 05:50
Jos Koelewijn added 7 commits February 20, 2020 09:44
Jos Koelewijn Jos Koelewijn
Add sensible warning message
adbeff0
Jos Koelewijn Jos Koelewijn
remove unnecessary boilerplate from test
23183d2
Jos Koelewijn Jos Koelewijn
clean up and finetune rule
83394d6
Jos Koelewijn Jos Koelewijn
Add RuleToTest parameter to Test-ScriptAnalyzer to aid with test driv…
99c2bea
…en rule development
Jos Koelewijn Jos Koelewijn
increment rule count to fix test
91e6689
Jos Koelewijn Jos Koelewijn
add reference to rule documentation
337ac20
Jos Koelewijn Jos Koelewijn
exclude built-in variables
406d7ec
@Jawz84

Jos Koelewijn (Jawz84) commented Feb 20, 2020 •
edited
Loading

Uh oh!

There was an error while loading. Please reload this page.

Copy link
Copy Markdown
Contributor Author

Christoph Bergmeister (@bergmeister) I've hit a milestone. At this point the code seems to be working as intended. I have added one minor feature which may need to be in a separate PR: I've added the option to use Test-ScriptAnalyzer with a -RuleToTest parameter, so I could easily make adjustments, run .\build.ps1, and run Test-ScriptAnalyzer -RuleToTest 'avoiduni' -ShowAll to check if the change made it worse or better :-).

Can we have a little brainstorm on where this rule may be applied to, other than just for Foreach-Object -Parallel? I'm thinking of maybe Invoke-Command, but other than that? [Edit: a list of commands where this could be applied to can be found here]

Would you like to start a review round at this point, or better after we have a clear picture of how to move forward?

All reactions

Sorry, something went wrong.

Uh oh!

There was an error while loading. Please reload this page.

@Jawz84

Jos Koelewijn (Jawz84) commented Feb 20, 2020

Copy link
Copy Markdown
Contributor Author

The failure in Ubuntu 1804 build is the same Authenticode glitch we saw earlier.

bergmeister reacted with thumbs up emoji
All reactions
  • 👍 1 reaction

Sorry, something went wrong.

Uh oh!

There was an error while loading. Please reload this page.

@Jawz84

Jos Koelewijn (Jawz84) commented Feb 21, 2020 •
edited
Loading

Uh oh!

There was an error while loading. Please reload this page.

Copy link
Copy Markdown
Contributor Author

// brainstorm

Other situations where this rule can be applied:

  • Start-Job/Start-ThreadJob -scriptBlock {$using:foo}
    Here the catch is, we need to be sure we check the right scriptblock. The rule does not apply to the -InitializationScript scriptblock parameter.

  • Invoke-Command -ComputerName "baz" -ScriptBlock {$using:foo}
    The catch here, is that the -ComputerName parameter has to be used, otherwise $using is prohibited.
    Also tricky; one can open a persistent session, and use subsequent invoke-command calls to that, where variables previous calls are still valid (more info here)

    $session = new-pssession -computername "baz"
    Invoke-Command -session $session -scriptblock {$foo = "foo" }
    Invoke-Command -session $session -scriptblock {Write-Output $foo} # works
  • workflow bar {$foo = 'foo'; inlinescript {$using:foo} }
    On Windows PowerShell only. (see get-help about_InlineScript)

mklement0 reacted with thumbs up emoji
All reactions
  • 👍 1 reaction

Sorry, something went wrong.

Uh oh!

There was an error while loading. Please reload this page.

@Jawz84

Jos Koelewijn (Jawz84) commented Feb 21, 2020

Copy link
Copy Markdown
Contributor Author

Michael Klement (@mklement0) thanks for creating the powershell-docs PR for documenting this. That gave me the quick overview I needed. If you have any suggestions, I'd love to hear them, thanks.

mklement0 reacted with thumbs up emoji
All reactions
  • 👍 1 reaction

Sorry, something went wrong.

Uh oh!

There was an error while loading. Please reload this page.

@mklement0

Michael Klement (mklement0) commented Feb 21, 2020

Copy link
Copy Markdown

Thanks, Jos Koelewijn (@Jawz84), a great rule to add, and it would be great if it covered the other $using: contexts too (as you outline above).

One pending PR to watch out for: PowerShell/PowerShell#11829, which is trying to eliminate overzealous $using: checks for nested script blocks, as described in PowerShell/PowerShell#11817

As for the terminology in the rule documentation: about_Remote_Variables refers to $using: as the Using scope modifier - it's not a directive.

Jawz84 reacted with thumbs up emoji
All reactions
  • 👍 1 reaction

Sorry, something went wrong.

Uh oh!

There was an error while loading. Please reload this page.

@Jawz84

Jos Koelewijn (Jawz84) commented Feb 21, 2020

Copy link
Copy Markdown
Contributor Author

Thanks for pointing these out Michael Klement (@mklement0) !

mklement0 reacted with thumbs up emoji
All reactions
  • 👍 1 reaction

Sorry, something went wrong.

Uh oh!

There was an error while loading. Please reload this page.

Jos Koelewijn added 3 commits February 24, 2020 07:57
Jos Koelewijn Jos Koelewijn
change using directive => scoope modifier
55b7316
Jos Koelewijn Jos Koelewijn
add tests for InlineScript, Invoke-Command and Start-(Thread)Job
ef4b79a
Jos Koelewijn Jos Koelewijn
add rule implementation for InlineScript, Invoke-Command and Start-(T…
36bb1fc
…hread)Job
@Jawz84 Jos Koelewijn (Jawz84) changed the title WIP: 1410 foreach parallel rule 1410 foreach parallel rule Feb 24, 2020
@Jawz84 Jos Koelewijn (Jawz84) changed the title 1410 foreach parallel rule WIP: 1410 foreach parallel rule Feb 24, 2020
@Jawz84

Jos Koelewijn (Jawz84) commented Feb 24, 2020

Copy link
Copy Markdown
Contributor Author

The rule is working now. I am not completely satisfied with what the implementation looks like, and I'm likely to do some cleanup and refactor. If Michael Klement (@mklement0) or Christoph Bergmeister (@bergmeister) have insights how to best convert this to cleaner code, I'm all ears.

Second question: when starting out, I named the rule AvoidUnInitializedVarsInNewRunspaces Is that the best, most descriptive name for this rule?

All reactions

Sorry, something went wrong.

Uh oh!

There was an error while loading. Please reload this page.

Jos Koelewijn and others added 13 commits March 13, 2020 10:38
Jos Koelewijn Jos Koelewijn
Add documentation that DSC Script resource is supported
b1bee39
@Jawz84 @rjmholt
change string[] to IReadOnlyList<string>
f6de07d
Co-Authored-By: Robert Holt <[email protected]>
@Jawz84 @rjmholt
extract scriptBlockPosition as a variable for readability
5f6bae0
Co-Authored-By: Robert Holt <[email protected]>
Jos Koelewijn Jos Koelewijn
put arguments on their own line for readability
bcaba83
Jos Koelewijn Jos Koelewijn
make visitor class a private, nested class in rule
1d6b55e
Jos Koelewijn Jos Koelewijn
change 'var' to type name for method calls
92f19c9
Jos Koelewijn Jos Koelewijn
fix indentation for nested visitor class
7943625
@Jawz84 @rjmholt
Remove explicit 'ToList()' for performance
a6acfd7
Co-Authored-By: Robert Holt <[email protected]>
Jos Koelewijn Jos Koelewijn
add check for strongly typed assignments
cee3ab7
Jos Koelewijn Jos Koelewijn
Merge remote-tracking branch 'refs/remotes/origin/1410_foreach_parall…
fe68244
…el_rule' into 1410_foreach_parallel_rule

resolve merge conflict
Jos Koelewijn Jos Koelewijn
Add todo comments
c4c4fde
Jos Koelewijn Jos Koelewijn
refactor FindAll predicates to static methods
490d8d9
to avoid closure allocation
Jos Koelewijn Jos Koelewijn
Refactor GetSessionName for performance.
6767a58
@Jawz84

Jos Koelewijn (Jawz84) commented Mar 14, 2020

Copy link
Copy Markdown
Contributor Author

Rob Holt (@rjmholt) I've been thinking about what to optimize, and especially: how to measure if it has worked. When I run tests, most are around 15ms, but some stand out and take longer:
image
I have tried Visual Studios profiling tool and the diagnostic tools, but none the wiser really. How do I visualise which codepaths make the Non-DSC function with the name SetScript {} test take 156ms instead of the more typical 15ms?

All reactions

Sorry, something went wrong.

Uh oh!

There was an error while loading. Please reload this page.

@Jawz84

Jos Koelewijn (Jawz84) commented Mar 18, 2020

Copy link
Copy Markdown
Contributor Author

Rob Holt (@rjmholt) did you have a chance to look at this yet? I'm starting to fear we might be accidentally waiting for each other

All reactions

Sorry, something went wrong.

Uh oh!

There was an error while loading. Please reload this page.

rjmholt
Rob Holt (rjmholt) reviewed Mar 18, 2020
View reviewed changes
Comment thread Rules/Strings.resx Resolved

Uh oh!

There was an error while loading. Please reload this page.

Comment thread Rules/UseUsingScopeModifierInNewRunspaces.cs Resolved

Uh oh!

There was an error while loading. Please reload this page.

Comment thread Rules/UseUsingScopeModifierInNewRunspaces.cs Resolved

Uh oh!

There was an error while loading. Please reload this page.

Comment thread Rules/UseUsingScopeModifierInNewRunspaces.cs Resolved

Uh oh!

There was an error while loading. Please reload this page.

Comment thread Rules/UseUsingScopeModifierInNewRunspaces.cs Resolved

Uh oh!

There was an error while loading. Please reload this page.

Comment thread Rules/UseUsingScopeModifierInNewRunspaces.cs Resolved

Uh oh!

There was an error while loading. Please reload this page.

Comment thread Rules/UseUsingScopeModifierInNewRunspaces.cs Resolved

Uh oh!

There was an error while loading. Please reload this page.

Comment thread Rules/UseUsingScopeModifierInNewRunspaces.cs Resolved

Uh oh!

There was an error while loading. Please reload this page.

Comment thread Rules/UseUsingScopeModifierInNewRunspaces.cs Resolved

Uh oh!

There was an error while loading. Please reload this page.

Comment thread Rules/UseUsingScopeModifierInNewRunspaces.cs Resolved

Uh oh!

There was an error while loading. Please reload this page.

Jos Koelewijn (Jawz84) and others added 4 commits March 19, 2020 20:26
@Jawz84 @rjmholt
use full type for string expression variable
c824fd7
Co-Authored-By: Robert Holt <[email protected]>
@Jawz84 @rjmholt
use full type name for foreach variable initialization
ab64ebf
Co-Authored-By: Robert Holt <[email protected]>
@Jawz84 @rjmholt
refactor diagnostic message out to local variable
d5b77a9
Co-Authored-By: Robert Holt <[email protected]>
Jos Koelewijn Jos Koelewijn
WIP: apply review suggestions
6c0e90a
refactoring FindVarsInAssignmentAsts to return a dictionary in progress
@Jawz84

Jos Koelewijn (Jawz84) commented Mar 19, 2020

Copy link
Copy Markdown
Contributor Author

Really enjoying your feedback Rob Holt (@rjmholt)! I am learning a lot.

All reactions

Sorry, something went wrong.

Uh oh!

There was an error while loading. Please reload this page.

Jos Koelewijn added 3 commits March 20, 2020 14:31
Jos Koelewijn Jos Koelewijn
apply review suggestions
fa7c065
refactoredFindVarsInAssignmentAsts to return a dictionary
Jos Koelewijn Jos Koelewijn
Merge remote-tracking branch 'refs/remotes/origin/1410_foreach_parall…
378fb16
…el_rule' into 1410_foreach_parallel_rule

fix merge conflict
Jos Koelewijn Jos Koelewijn
Fix CR/LF -> LF
c1247c0
@Jawz84

Jos Koelewijn (Jawz84) commented Mar 20, 2020

Copy link
Copy Markdown
Contributor Author

Rob Holt (@rjmholt) back over to you, all good here.

All reactions

Sorry, something went wrong.

Uh oh!

There was an error while loading. Please reload this page.

rjmholt
Rob Holt (rjmholt) approved these changes Mar 20, 2020
View reviewed changes
@Jawz84

Jos Koelewijn (Jawz84) commented Mar 24, 2020

Copy link
Copy Markdown
Contributor Author

Christoph Bergmeister (@bergmeister), Rob Holt (@rjmholt) thanks for all the reviews. Is there anything more I can do here? I am uncertain what the next step is. Is this blocked on review from James Truher (@JamesWTruher)?
Thanks in advance for enlightening me 🙂

All reactions

Sorry, something went wrong.

Uh oh!

There was an error while loading. Please reload this page.

bergmeister
Christoph Bergmeister (bergmeister) approved these changes Mar 24, 2020
View reviewed changes
@bergmeister

Christoph Bergmeister (bergmeister) commented Mar 24, 2020

Copy link
Copy Markdown
Collaborator

I think having at least one reviewer from Microsoft is good enough, this is our unwritten rule, so I'll therefore merge. Congrats and thanks for your hard work

Jawz84 and rjmholt reacted with heart emoji
All reactions
  • ❤️ 2 reactions

Sorry, something went wrong.

Uh oh!

There was an error while loading. Please reload this page.

@bergmeister
Christoph Bergmeister (bergmeister) merged commit 791ee64 into PowerShell:master Mar 24, 2020
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Reviewers

@bergmeister Christoph Bergmeister (bergmeister) bergmeister approved these changes
@JamesWTruher James Truher (JamesWTruher) Awaiting requested review from JamesWTruher
+1 more reviewer
@rjmholt Rob Holt (rjmholt) rjmholt approved these changes
Reviewers whose approvals may not affect merge requirements

Uh oh!

There was an error while loading. Please reload this page.

Assignees

No one assigned

Labels

Area - Rules Issue - New Rule

Projects

None yet

Milestone

No milestone

Development

Successfully merging this pull request may close these issues.

New rule for ForEach-Object -Parallel that helps users pinpoint problems with their variable scoping

Uh oh!

There was an error while loading. Please reload this page.

4 participants

@Jawz84 @bergmeister @mklement0 @rjmholt
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
You are visitor 01402520 — GeezerVison does not store the pages you look up.
Best viewed with Netscape Navigator 4.7 or Internet Explorer 6 at 800×600. No cookies required.