Skip to content

Global variables are erroneously reported as "never used" #1300

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
SetTrend opened this issue Jul 31, 2019 · 4 comments
Open

Global variables are erroneously reported as "never used" #1300

SetTrend opened this issue Jul 31, 2019 · 4 comments

Comments

@SetTrend
Copy link

System Details Output

### VSCode version: 1.36.1 2213894ea0415ee8c85c5eea0d0ff81ecc191529 x64

### VSCode extensions:
[email protected]
[email protected]
[email protected]
[email protected]
[email protected]
[email protected]


### PSES version:

### PowerShell version:

Name                           Value
----                           -----
PSVersion                      5.1.16299.1146
PSEdition                      Desktop
PSCompatibleVersions           {1.0, 2.0, 3.0, 4.0...}
BuildVersion                   10.0.16299.1146
CLRVersion                     4.0.30319.42000
WSManStackVersion              3.0
PSRemotingProtocolVersion      2.3
SerializationVersion           1.1.0.1

Issue Description

PowerShell extension reports global variables as assigned but not used when they are not used from within a .psm1 module file. However, global variables may be used by scripts outside that module importing it.

Expected Behaviour

Global variables should never be reported as being unused.

Actual Behaviour

PowerShell extension reports global variables as assigned but not used when they are not used from within a .psm1 module file.

global variables error

@rjmholt rjmholt transferred this issue from PowerShell/vscode-powershell Jul 31, 2019
@bergmeister
Copy link
Collaborator

I agree with you that global variables should be included from the analysis as that's their nature and there is a separate rule that discourages them anyway, so I'd be happy to accept a PR with such a change

@NapAlot
Copy link

NapAlot commented Jan 7, 2021

I have this issue as well. it seems that The analyzer should be aware of $Global:var as being in the host script outside the function as $var.

image

@NapAlot
Copy link

NapAlot commented Jan 7, 2021

I agree with you that global variables should be included from the analysis as that's their nature and there is a separate rule that discourages them anyway, so I'd be happy to accept a PR with such a change

Global variables are discouraged? What should be used in place of them?

@NapAlot
Copy link

NapAlot commented Jan 7, 2021

I was able to resolve my issues by setting my $global: to $script:

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

No branches or pull requests

3 participants