Skip to content

AvoidGlobalVars should only trigger on assignment #2100

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

Open
ThomasNieto opened this issue May 6, 2025 · 0 comments
Open

AvoidGlobalVars should only trigger on assignment #2100

ThomasNieto opened this issue May 6, 2025 · 0 comments

Comments

@ThomasNieto
Copy link
Contributor

PowerShell classes requires variables to be initialized in a method before reference. This causes issues with global variables without scoping as the parser will throw a Variable is not assigned in the method error. To get around this the variable must be scoped but this causes PSScriptAnalyzer to throw a violation. The rule should only trigger when assigning a variable to the global scope not reading.

Steps to reproduce

class Test {
  [void] Abc() {
    if ($global:PSEdition -eq 'core') { write-host core }
  }
}

Expected behavior

No warnings

Actual behavior

RuleName                            Severity     ScriptName Line  Message
--------                            --------     ---------- ----  -------
PSAvoidGlobalVars                   Warning                 3     Found global variable 'global:PSEdition'.

If an unexpected error was thrown then please report the full error details using e.g. $error[0] | Select-Object *

Environment data

7.6-preview4

1.24.0
@ThomasNieto ThomasNieto changed the title AvoidGlobalVars should not alarm if only read AvoidGlobalVars trigger only on assignment May 6, 2025
@ThomasNieto ThomasNieto changed the title AvoidGlobalVars trigger only on assignment AvoidGlobalVars should only trigger on assignment May 6, 2025
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

1 participant