Skip to content

Variables defined with Set-Variable are not fully supported in the editor #2460

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
DmitrySokolov opened this issue Feb 5, 2020 · 1 comment
Labels
Area-Symbols & References Issue-Enhancement A feature request (enhancement). Up for Grabs Will shepherd PRs.

Comments

@DmitrySokolov
Copy link

Issue Type: Bug

Set-Variable MY_CONST 1 -Option Constant

Write-Host "MY_CONST = $($MY_CONST)"
  1. When cursor is on the fisrt MY_CONST, other $MY_CONST is not highlighted
  2. When typing $MY_<crtl+space> there is no suggestion for $MY_CONST

Extension version: 2020.1.0
VS Code version: Code 1.41.1 (26076a4de974ead31f97692a0d32f90d735645c0, 2019-12-18T14:58:56.166Z)
OS version: Windows_NT x64 10.0.18363

@ghost ghost added the Needs: Triage Maintainer attention needed! label Feb 5, 2020
@rjmholt rjmholt added Issue-Enhancement A feature request (enhancement). Area-Symbols & References labels Feb 5, 2020
@rjmholt
Copy link
Contributor

rjmholt commented Feb 5, 2020

Indeed using Set-Variable causes issues for PowerShell as well.

The ability to define a variable using a command is something particular to PowerShell and its dynamism.

There are simple cases, but in general it's impossible to analyse Set-Variable. For example:

$varName = 'y'
Set-Variable $x 1

$y

Or

$varName = Get-Content -Raw ./myVarName.txt
Set-Variable $varName 1

$y # Is this a reference of the variable defined above??

Or even better:

Set-Alias (Get-Content -Raw ./setVariableName.txt) Set-Variable

# Is this Set-Variable??
AssignVariable y 1

# Has $y been assigned??
$y 

(This last one is actually unaccounted for in PowerShell's own code, so I highly recommend against it.)

So given the pitfalls of using Set-Variable (it will cause the PowerShell compiler to deoptimise code, since it can no longer guarantee things about variables), and the general impossibility of analysing scripts that use any of the variable cmdlets, our ability to resolve references to variables affected by them is to basically "do what we can and hope for simple cases".

There's also the other argument that Set-Variable isn't really a variable reference; there's no variable on that line, because it's just a command taking a string. From a purely syntactic perspective, there are no variables there.

However, with all of this being said, I think that editors should try to be helpful where they can be. In that respect this is a bit like #261 (comment). So ideally I'd like the PowerShell extension to have this one day. But it's not currently on our agenda, as we drive toward other improvements in the extension's architecture.

@rjmholt rjmholt added the Up for Grabs Will shepherd PRs. label Feb 5, 2020
@SydneyhSmith SydneyhSmith removed the Needs: Triage Maintainer attention needed! label Feb 6, 2020
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Area-Symbols & References Issue-Enhancement A feature request (enhancement). Up for Grabs Will shepherd PRs.
Projects
None yet
Development

No branches or pull requests

3 participants