question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

Util.getWinDirs throws an exception if PowerShell is in Constrained Language Mode

See original GitHub issue

If PowerShell is in Constrained Language Mode, that is PS C:\> $ExecutionContext.SessionState.LanguageMode prints ConstrainedLanguage instead of FullLanguage, Util.getWinDirs throws an exception which can be observed by running dev.dirs.DirectoriesTest:

sbt:directories> testOnly dev.dirs.DirectoriesTest
[info] Total time: 0 sec
[info] Compiling 1 Java source to C:\Project\prj\directories-jvm\target\test-classes ...
[error] Test dev.dirs.DirectoriesTest.testBaseDirectories failed: java.lang.RuntimeException: Couldn't find pwsh.exe or powershell.exe on path or in default system locations, took 2.545 sec
[error]     at dev.dirs.Util.windowsFallback(Util.java:280)
[error]     at dev.dirs.Util.getWinDirs(Util.java:161)
[error]     at dev.dirs.BaseDirectories.<init>(BaseDirectories.java:274)
[error]     at dev.dirs.BaseDirectories.get(BaseDirectories.java:246)
[error]     at dev.dirs.DirectoriesTest.testBaseDirectories(DirectoriesTest.java:9)
[error]     ...
[error] Caused by: java.io.IOException: no output from process
[error]     at dev.dirs.Util.runCommands(Util.java:206)
[error]     at dev.dirs.Util.runWinCommands(Util.java:241)
[error]     at dev.dirs.Util.windowsFallback(Util.java:278)
[error]     ... 41 more
[error] Failed: Total 1, Failed 1, Errors 0, Passed 0
[error] Failed tests:
[error]         dev.dirs.DirectoriesTest
[info] Total time: 3 sec
[info]   root / Test / testOnly: 3 sec
[error] (Test / testOnly) sbt.TestsFailedException: Tests unsuccessful
[error] Total time: 3 s, completed 19.11.2020 14:08:57

The inner exception java.io.IOException: no output from process is thrown because the PowerShell process exits with only printing to stderr and nothing to stdout.

PowerShell exits with errors when trying to execute the SCRIPT_START_BASE64 script. The error can be reproduced in a PowerShell console by executing the first line of that script:

PS C:\> $ExecutionContext.SessionState.LanguageMode
FullLanguage
PS C:\> [Console]::OutputEncoding = [System.Text.Encoding]::UTF8

PS C:\> $ExecutionContext.SessionState.LanguageMode = "ConstrainedLanguage"

PS C:\> $ExecutionContext.SessionState.LanguageMode
ConstrainedLanguage
PS C:\> [Console]::OutputEncoding = [System.Text.Encoding]::UTF8
Die Eigenschaft kann nicht festgelegt werden. Das Festlegen der Eigenschaft wird in diesem Sprachmodus nur für Kerntypen unterstützt.
In Zeile:1 Zeichen:1
+ [Console]::OutputEncoding = [System.Text.Encoding]::UTF8
+ ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
    + CategoryInfo          : InvalidOperation: (:) [], RuntimeException
    + FullyQualifiedErrorId : PropertySetterNotSupportedInConstrainedLanguage

Note that [Console]::OutputEncoding = [System.Text.Encoding]::UTF8 does not exit with an error in FullLanguage mode.

That Util.getWinDirs throws an exception if PowerShell is in Constrained Language Mode is the reason why the current sbt 1.4 versions are not usable in such environments as noted in https://github.com/dirs-dev/directories-jvm/pull/40#issuecomment-729581072 and https://github.com/coursier/coursier/issues/1913.

A possible workaround to make sbt usable again would be removing this line.

Issue Analytics

  • State:closed
  • Created 3 years ago
  • Comments:15 (5 by maintainers)

github_iconTop GitHub Comments

2reactions
fthomascommented, Jan 4, 2021

FTR: sbt 1.4.6 is now using directories-jvm 23 via lm-coursier 2.0.5 / Coursier 2.0.8 and the fix with dowgrading the PowerShell version (https://github.com/dirs-dev/directories-jvm/commit/f976091da250fe3a85ca48df6041cf9852f79a09) seems to be working:

PS C:\> $ExecutionContext.SessionState.LanguageMode
ConstrainedLanguage
PS C:\> sbt csrCacheDirectory
...
[info] csrCacheDirectory
[info]  C:\Users\...\AppData\Local\Coursier\cache\v1

I can finally enjoy sbt 1.4 on Windows. Thanks @soc, @kalejami, @eed3si9n and @alexarchambault for fixing this issue and getting it into sbt!

2reactions
eed3si9ncommented, Nov 20, 2020

There’s some discussion about environment variables in this thread - https://github.com/dirs-dev/directories-jvm/issues/26#issuecomment-570288952

In my opinion, the majority of the sbt users would be happy as long as directories-jvm reliably returned any value, and it doesn’t really have to be Apple/Microsoft/freedesktop compliant. I mean it would be nice if the value was correct in some sense, but for the sake of downloading JARs in some CI box on Windows etc, erroring out because Powershell or whatever is many times worse outcome than directories-jvm falling back to some stable made-up value.

For that I think we should consider the 3-step strategy:

  1. On Windows, try environment variable first. If it’s there, no need to deal with powershell. Great.
  2. If the environment variable is missing, only then try powershell, catch any errors, and print them out in the stderr.
  3. If powershell bailed, use some hard-coded reasonable fallback.
Read more comments on GitHub >

github_iconTop Results From Across the Web

PowerShell Constrained Language Mode
PowerShell Constrained Language is a language mode of PowerShell designed to support day-to-day administrative tasks, yet restrict access to ...
Read more >
Powershell Constrained Language Mode Bypass - Ired.team
If you have the ability to downgrade to Powershell 2.0, this can allow you to bypass the ConstrainedLanguage mode. Note how $ExecutionContext.SessionState.
Read more >
A Deep Dive into PowerShell's Constrained Language Mode
Safer PowerShell Environment. First off, let's take a look at what Microsoft Docs says about the ConstrainedLanguage mode: All cmdlets in ...
Read more >
PowerShell Constrained Language Mode Enforcement and ...
https://www.twitch.tv/mattifestation.
Read more >
Mitigating PowerShell risks with Constrained Language mode
PowerShell is a robust tool that can control almost all components of Windows and applications such as Exchange.
Read more >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found