Skip to content
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

Fix: findVisualStudio with pwsh constrained lang. mode #2449

Open
wants to merge 5 commits into
base: main
Choose a base branch
from

Conversation

lucascloarec
Copy link

'Add-Type' cannot be used in powershell with constrained language mode. Language mode does not exist in PowerShell 2.0 engine. To force powershell to use 2.0 engine allows the usage of msvc 2017 or newer in environment with constrained language mode.
See :
https://devblogs.microsoft.com/powershell/powershell-constrained-language-mode/
https://docs.microsoft.com/en-us/powershell/scripting/windows-powershell/starting-the-windows-powershell-2.0-engine?view=powershell-7.1

Checklist
  • npm install && npm test passes
  • tests are included
  • documentation is changed or added
  • commit message follows commit guidelines
Description of change

I added the option '-Version 2' in the powershell command which retrieves the installation data of Visual Studio 2017 (or newer). This option allows you to ignore the constrained language mode since the user does not necessarily have control over this parameter in his environment. This language mode does not allow the use of 'Add-Type', but this powershell applet is currently required to run C # code.

'Add-Type' cannot be used in powershell with constrained language mode. Language mode does not exist in PowerShell 2.0 engine. To force powershell to use 2.0 engine allows the usage of msvc 2017 or newer in environment with constrained language mode.
See : 
https://devblogs.microsoft.com/powershell/powershell-constrained-language-mode/
https://docs.microsoft.com/en-us/powershell/scripting/windows-powershell/starting-the-windows-powershell-2.0-engine?view=powershell-7.1
Copy link

@samuelmaddock samuelmaddock left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This solution also has problems when PS 2.0 isn't installed

C:\Users\smaddock>powershell.exe -Version 2
Encountered a problem reading the registry.  Cannot find registry key SOFTWARE\Microsoft\PowerShell\1\PowerShellEngine. The Windows PowerShell 2 engine is not installed on this computer.

@lucascloarec
Copy link
Author

This solution also has problems when PS 2.0 isn't installed

C:\Users\smaddock>powershell.exe -Version 2
Encountered a problem reading the registry.  Cannot find registry key SOFTWARE\Microsoft\PowerShell\1\PowerShellEngine. The Windows PowerShell 2 engine is not installed on this computer.

I modified the pull request so that the use of the '-Version 2' option is only done when necessary.
If the usage is needed, but version 2 is not installed, then the script will crash, it's not a regression since the initial command wouldn't have worked either.
If the use is not necessary because powershell's language mode is FullLanguage, then the '-Version 2' option is not added to the command.

@cclauss
Copy link
Contributor

cclauss commented Dec 6, 2023

Please rebase and fix git conflicts.

@jarig
Copy link
Contributor

jarig commented Dec 22, 2023

I found that Get-VSSetupInstance works well - https://github.com/microsoft/vssetup.powershell

Here is my gist, but it's very rough with few hard-codes there (to be generalized):
https://gist.github.com/jarig/0328a439765e68cd966d168677f6bb5a

Created pull request that is using VSModule #2957

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

Successfully merging this pull request may close these issues.

None yet

4 participants