Skip to content

Every shell command emits a spurious error under PowerShell ConstrainedLanguage mode (AppLocker/WDAC): $host.SetShouldExit() is not permitted #4683

Open

Description

@Lerri-Cofannos

Describe the bug

On Windows machines where PowerShell runs in ConstrainedLanguage mode (enforced by AppLocker or WDAC, common in managed enterprise environments), every single shell command run by the agent prints a spurious error block.

Copilot CLI appends an exit-status epilogue to each command it runs:

} finally { $__copilotSuccess = $?; if (-not $__copilotSuccess -and $LASTEXITCODE -is [int] -and $LASTEXITCODE -ne 0) { $host.SetShouldExit($LASTEXITCODE) } elseif (-not $__copilotSuccess) { $host.SetShouldExit(1) } else { $host.SetShouldExit(0) } }

$host.SetShouldExit() is a .NET method call on a non-core type, which ConstrainedLanguage forbids. The wrapper itself therefore fails and emits:

Cannot invoke the method. Method invocation is supported only on core types in this language mode.
At line:2 char:225
+ ... Success) { $host.SetShouldExit(1) } else { $host.SetShouldExit(0) } }
+                                                ~~~~~~~~~~~~~~~~~~~~~~
    + CategoryInfo          : InvalidOperation : (:) [], RuntimeException
    + FullyQualifiedErrorId : MethodInvocationNotSupportedInConstrainedLanguage

Why this matters

  • Affects every command: Get-Item, python, curl — all of them.
  • Pollutes the model context. The block is ~150 tokens.
  • Risks incorrect failure detection. The text goes to stderr.

Suggested fix

Replace the $host.SetShouldExit(...) calls with the exit keyword, which is fully permitted in ConstrainedLanguage:

} finally { $__copilotSuccess = $?; if (-not $__copilotSuccess -and $LASTEXITCODE -is [int] -and $LASTEXITCODE -ne 0) { exit $LASTEXITCODE } elseif (-not $__copilotSuccess) { exit 1 } else { exit 0 } }

Alternatively, guard the call with a language-mode check and fall back to exit.

Affected version

1.0.81-0 (also reproduced on 1.0.82)

Steps to reproduce the behavior

  1. On a Windows machine where AppLocker/WDAC forces PowerShell into ConstrainedLanguage mode.
    Verify with $ExecutionContext.SessionState.LanguageMode, which returns ConstrainedLanguage.
  2. Run any trivial shell command via the agent, e.g. Get-Item $env:TEMP or python --version.
  3. Observe the MethodInvocationNotSupportedInConstrainedLanguage error block shown above.

Expected behavior

The exit-status wrapper should use constructs permitted in ConstrainedLanguage (e.g. the exit keyword), so no spurious output is appended to successful commands.

Additional context

  • OS: Windows 11, domain-joined, managed by corporate GPO
  • CPU architecture: x86_64
  • Shell: Windows PowerShell 5.1 (powershell.exe) — also reproduced on PowerShell 7.6.5 (pwsh.exe), so this is not version-specific
  • Language mode: ConstrainedLanguage, enforced by AppLocker (Exe rule collection, EnforcementMode=1, pushed via domain GPO)
  • Host: Copilot CLI running as the VS Code agent host (@github/copilot-win32-x64)

Verification performed

  • Confirmed the offending template string is present in prebuilds/win32-x64/runtime.node.
  • Confirmed it is still present in 1.0.82, so updating does not resolve it.
  • Confirmed the wrapped commands themselves succeed and the process exit code is still correct (0 on success). The failure is limited to the epilogue, making this primarily output noise plus a stderr false-positive risk.
  • A user-side trap cannot suppress it, because the error is raised from the wrapper's own finally block.

Activity

  1. MilkyWay008 commented on Sep 1, 2026

    @MilkyWay008

    The Cannot invoke the method. Method invocation is supported only on core types on every command is the wrapper's $host.SetShouldExit() epilogue, ConstrainedLanguage blocks method calls on core types so the wrapped command itself runs fine and only the exit path throws. Your exit-keyword patch is the right fix. Until it lands, pointing the agent's shell at cmd.exe or bash skips the PowerShell wrapper entirely, that's the practical workaround on AppLocker/WDAC-managed boxes. oh also, btw, I keep a portable Hermes OTG on a USB stick as my rescue agent for these locked-down Windows setups, https://github.com/MilkyWay008/Hermes-OTG if you want to poke at it.

  2. added
    area:platform-windows
    area:tools
    and removed on Sep 2, 2026
  3. Lerri-Cofannos commented on Sep 3, 2026

    @Lerri-Cofannos
    Author

    I tried the suggested workaround of routing Copilot's shell tool through cmd.exe via VS Code's terminal.integrated.automationProfile.windows. After a window reload and testing in a fresh session, the CLI still spawned powershell.exe and still hit the same SetShouldExit/ConstrainedLanguage error. Shell selection is hardcoded in the CLI's native runtime and doesn't go through VS Code's integrated terminal settings at all, so this setting has no effect on it.

    Also checked ~/.copilot/settings.json, the config-dir reference, and the CLI's command-line flags — none expose a way to change which shell the command-execution tool spawns. No environment variable (SHELL, COMSPEC) influences it either.

    So there's currently no client-side workaround for machines where ConstrainedLanguage mode is enforced by policy (AppLocker/WDAC) and can't be disabled by the user.

  4. mr-gurke commented on Sep 24, 2026

    @mr-gurke

    Additional reproduction and isolation from the JetBrains integration:

    Environment:

    • Windows PowerShell 5.1
    • LanguageMode: ConstrainedLanguage, enforced by AppLocker/WDAC
    • PyCharm 2025.3.6.1
    • GitHub Copilot plugin 1.18.0-251

    There are two separate failures.

    1. Virtualenv activation prevents the command from running

    With PyCharm's Activate virtualenv terminal option enabled, the plugin generates an init script containing:
    powershell . 'C:...\ .venv\Scripts\Activate.ps1'

    The generated init script runs in ConstrainedLanguage, while the virtualenv script is loaded from an AppLocker-allowed
    location and is therefore defined in FullLanguage. PowerShell rejects the dot-sourcing operation with:

    DotSourceNotSupported: 
    This command cannot be dot-sourced because it was defined in a different language mode.
    

    The wrapper then tries to clean up the error using:

    powershell $Error.RemoveAt(0)
    

    This produces a second:

    MethodInvocationNotSupportedInConstrainedLanguage
    

    In this state, the user command never executes, despite the message claiming that the wrapper is continuing with it.

    2. Disabling virtualenv activation allows commands to run

    After disabling Activate virtualenv and restarting PyCharm, no shell-init scripts are generated. Commands then execute
    successfully:

    Write-Output "MARKER"  -> output is returned
    Set-Content ...         -> file is created
    Get-Process ...         -> external command runs
    

    However, every invocation still ends with:

    $host.SetShouldExit(0)
    

    which produces the same ConstrainedLanguage method-invocation error. The remaining issue is therefore limited to the
    exit-status epilogue: the command succeeds, but the wrapper reports failure and emits noisy stderr.

    This identifies two fixes needed:

    1. Make init-script error cleanup ConstrainedLanguage-safe; do not call $Error.RemoveAt(...).
    2. Replace $host.SetShouldExit(...) with the exit keyword or another ConstrainedLanguage-compatible mechanism.

    The successful tests demonstrate that PowerShell itself and ordinary command execution are not the problem. The failure
    is caused by the Copilot-generated wrapper and its handling of ConstrainedLanguage mode.

  5. bdoeven commented on Nov 25, 2026

    @bdoeven

    +1

  6. ItsTimeToSnuff commented on Dec 4, 2026

    @ItsTimeToSnuff

    Filed a follow-up report with 1.0.105 session telemetry (events.jsonl) in #5679 to clarify that this is not just cosmetic stderr noise with exit code 0:

    1. $host.SetShouldExit(0) actually forces exitCode: 1 (shell_error_category: "command_nonzero_exit"), marking 100% of succeeding commands as failed (×) in IDE hosts (copilot-intellij, VS Code).
    2. With host ShellOptions.initScripts (1.0.105+), $Error.RemoveAt(0) crashes PowerShell before the user command even starts, causing 5+ wasted LLM retry turns (hundreds of thousands of tokens) per prompt until the agent falls back to a non-PowerShell MCP tool.

    Included ConstrainedLanguage-safe replacements for both wrapper paths in #5679.

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

Metadata

Metadata

Assignees

No one assigned

    Labels

    area:platform-windowsarea:tools

    Type

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions