node-gyp icon indicating copy to clipboard operation
node-gyp copied to clipboard

Fix: findVisualStudio with pwsh constrained lang. mode

Open lucascloarec opened this issue 4 years ago • 7 comments

'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
  • [x] npm install && npm test passes
  • [ ] tests are included
  • [ ] documentation is changed or added
  • [x] 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.

lucascloarec avatar Jul 02 '21 09:07 lucascloarec

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.

lucascloarec avatar Sep 14 '22 14:09 lucascloarec

Please rebase and fix git conflicts.

cclauss avatar Dec 06 '23 06:12 cclauss

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

jarig avatar Dec 22 '23 21:12 jarig