You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
Fix Powershell Execution Policy & Language Mode Logic (#2096)
* Set Execution Policy to Bypass.
For #2095
Also includes whitespace changes. I should probably just make a PR that does all of that sometime. You can hide the whitespace changes in the GH diff view.
See my read up there. Which I copied below.
# Context
There are language modes, which constrain powershell language features, and then execution policies, which constrain running scripts in general. I have had to set flags to enable running scripts by default from a powershell terminal, but there is no documentation indicating anything but that the language mode default is full language, or unrestricted. I also tried a sandbox and it was Full by default.
'FullLanguage is the default language mode for default sessions on all versions of Windows.'
https://learn.microsoft.com/en-us/powershell/module/microsoft.powershell.core/about/about_language_modes?view=powershell-7.4&viewFallbackFrom=powershell-7.3#:~:text=The%20FullLanguage%20mode%20permits%20all%20language%20elements%20in%20the%20session.%20FullLanguage%20is%20the%20default%20language%20mode%20for%20default%20sessions%20on%20all%20versions%20of%20Windows.
What causes our error to trigger is not Get-ExecutionPolicy but ExecutionContext.SessionState.LanguageMode.
We should try to set the execution policy to bypass instead of restricted in our code as it prompts a dialogue for permission by default on unrestricted which could be the cause of unknown failures/timeouts, which is an awesome insight from @jacdavis. Even if the script is signed.
Sadly this option does not seem to exist to bypass language mode like how you can for 1 script via execution policy.
The error that spawned this issue triggers by checking the language mode:
Your machine policy ConstrainedLanguage disables PowerShell language features that may be needed to install .NET. Read more at: https://learn.microsoft.com/powershell/module/microsoft.powershell.core/about/about_language_modes?view=powershell-7.3. If you cannot safely and confidently change the execution policy, try setting a custom existingDotnetPath following our instructions here: https://github.com/dotnet/vscode-dotnet-runtime/blob/main/Documentation/troubleshooting-runtime.md.
There is code I wrote that just fails it if ExecutionContext.SessionState.LanguageMode is set to ConstrainedLanguage or NoLanguage.
vscode-dotnet-runtime/vscode-dotnet-runtime-library/src/Acquisition/AcquisitionInvoker.ts
Line 195 in ef5299b
if(languageMode === 'ConstrainedLanguage' || languageMode === 'NoLanguage')
I dont think those 2 to 4 daily people you see with that error are going to go away. The code is failing on them because it sees their language mode is restricted which is not by default and is set by a SysAdmin. If the install script team knows if we can run the script in a way with a constrained language mode, then we could do that, but that sounds impossible.
* Fix the error message about language mode and handle new error
The language mode error said it was for execution policy which is very confusing.
Now there are 2 separate errors and it has a clear message for both.
* Handle Each Error Better
* Consider that ubuntu 22 04 now supports .net 9
* Fix linting issue
* Improve Test Logic
* Fix Architecture Logic & Remove Old Code
The ubuntu 22 04 version sometimes has 9.0 but older patches of it may not. CI machines are stuck on this logic and fail depending on the machine instance version so we need to work with either case for now. Also, the architecture should not be null in the local state, we want the data to be in sync after the install is made, so I've updated this logic.
I removed the test language change that was there as well.
* Fix the version compare in the linux test
* Dont try to call powershell on non win os
* Url doesn't need specific PowerShell version in it.
Co-authored-by: Michael Yanni <[email protected]>
* Fix url formation
Co-authored-by: Michael Yanni <[email protected]>
---------
Co-authored-by: Michael Yanni <[email protected]>
assert.equal(result[0].version,Number(distroVersion)>22.04 ? '9.0.1xx' : '8.0.1xx','The SDK did not recommend the version it was supposed to, which should be N.0.1xx based on surface level distro knowledge. If a new version is available, this test may need to be updated to the newest version.');
625
+
assert.equal(result[0].version,Number(distroVersion.version)>=22.04 ? '9.0.1xx' : '8.0.1xx',`The SDK did not recommend the version (it said ${result[0].version}) it was supposed to, which should be N.0.1xx based on surface level distro knowledge, version ${distroVersion.version}. If a new version is available, this test may need to be updated to the newest version.`);
privatenoPowershellError=`powershell.exe is not discoverable on your system. Is PowerShell added to your PATH and correctly installed? Please visit: https://learn.microsoft.com/powershell/scripting/install/installing-powershell-on-windows.
41
45
You will need to restart VS Code after these changes. If PowerShell is still not discoverable, try setting a custom existingDotnetPath following our instructions here: https://github.com/dotnet/vscode-dotnet-runtime/blob/main/Documentation/troubleshooting-runtime.md.`
constbadPolicyError=newEventBasedError('PowershellBadExecutionPolicy',`Your powershell execution policy does not allow script execution, so we can't automate the installation.
87
+
Please read more at https://go.microsoft.com/fwlink/?LinkID=135170`);
constbadModeError=newEventBasedError('PowershellBadLanguageMode',`Your Language Mode disables PowerShell language features needed to install .NET. Read more at: https://learn.microsoft.com/powershell/module/microsoft.powershell.core/about/about_language_modes.
94
+
If you cannot change this flag, try setting a custom existingDotnetPath via the instructions here: https://github.com/dotnet/vscode-dotnet-runtime/blob/main/Documentation/troubleshooting-runtime.md.`);
// Need to escape apostrophes with two apostrophes
@@ -163,52 +231,41 @@ You will need to restart VS Code after these changes. If PowerShell is still not
163
231
* @remarks Some users have reported not having powershell.exe or having execution policy that fails property evaluation functions in powershell install scripts.
164
232
* We use this function to throw better errors if powershell is not configured correctly.
consterr=Error(`Your machine policy ${languageMode} disables PowerShell language features that may be needed to install .NET. Read more at: https://learn.microsoft.com/powershell/module/microsoft.powershell.core/about/about_language_modes?view=powershell-7.3.
199
-
If you cannot safely and confidently change the execution policy, try setting a custom existingDotnetPath following our instructions here: https://github.com/dotnet/vscode-dotnet-runtime/blob/main/Documentation/troubleshooting-runtime.md.`);
200
-
error=err;
201
-
}
202
259
}
203
-
catch(err: any)
260
+
catch(err: any)
204
261
{
205
-
if(!knownError)
262
+
if(!knownError)
206
263
{
207
264
error=newError(`${this.noPowershellError} More details: ${(errasError).message}`);
0 commit comments