windowspowershellvisual-studio-codeaws-sam-clipath-variables

VS code terminal can't find AWS SAM even though windows terminal can


I installed AWS SAM from the msi installer from AWS for windows. after running the installer, I ran sam --version in cmd and powershell.

PS C:\Users\dgupta> sam --version
SAM CLI, version 1.26.0

It returns the version I just installed. However in VS code, I opened a terminal and ran sam --version it errors out.

Windows PowerShell
Copyright (C) Microsoft Corporation. All rights reserved.
Try the new cross-platform PowerShell https://aka.ms/pscore6

sam : The term 'sam' is not recognized as the name of a cmdlet, function, script file, or operable program. Check the spelling of the name,                                                                                                               g of the name,    
or if a path was included, verify that the path is correct and try again.
At line:1 char:1
+ sam --version
+ ~~~
    + CategoryInfo          : ObjectNotFound: (sam:String) [], CommandNotFoundException
    + FullyQualifiedErrorId : CommandNotFoundException

why is this happening ? doesn't VS Code terminal and normal terminal have access to the same environment variables ?


Solution

  • It's impossible to diagnose your problem without further information, but perhaps these troubleshooting tips help:

    If invoking an external executable (sam) by name only fails, the problem must be that the directory in which the executable resides isn't listed in the $env:PATH environment variable defined for the current process.

    However, it is possible that the external sam executable's directory isn't in $env:PATH and that sam is an auxiliary PowerShell command of the same name that knows the true sam's location and invokes it behind the scenes. For instance an alias - such as New-Alias sam 'C:\path\to\sam.exe' - or a function - such as function sam { & C:\path\to\sam.exe $args } - may be defined.

    From the PowerShell session where sam is found:

    Get-Command sam
    

    Diagnose why a directory is missing from $env:PATH on Windows:[1]

    Possible reasons:

    $env:PATH = 
      [Environment]::GetEnvironmentVariable('Path', 'Machine') + ';' +
      [Environment]::GetEnvironmentVariable('Path', 'User')
    

    If these solutions don't help (persistently), the problem must be :

    See the next sections.


    Add a directory entry to the persistent definition of the Path environment variable yourself, on Windows:[1]


    Diagnose what profile files were loaded into a session, on all platforms:

    PowerShell's profile files are (by default) loaded (dot-sourced) on session startup and allow sessions to be customized, which can include things such as custom alias definitions, functions, and even in-process $env:PATH additions.

    There are multiple profile files, all of which are loaded (by default), if present, along two independent dimensions: all-users vs. current-user, and all-hosts vs. current-host (a host being the PowerShell host environment, such as a regular console window or the terminal in Visual Studio Code).

    The automatic $PROFILE variable reports the current-user, current-host profile-file path, but actually has normally invisible properties listing all paths (you can make them visible with $PROFILE | select * - see this answer).

    What profiles are loaded into a session for a given user is determined by the following factors:

    To see what command line was used to invoke the current session, run the following:

    [Environment]::CommandLine
    

    Note:

    It follows from the above that different host environments load different sets of profile files, and in the PIC (PowerShell Integrated Console) that comes with the PowerShell extension and which runs in Visual Studio Code's integrated terminal, different profile files are indeed loaded, IF enabled via the PowerShell: Enable Profile Loading setting - compared to regular console windows[2]

    If you want your PIC sessions to load the same current-user profile as regular console windows:

    . ($PROFILE -replace '\.VSCode', '.PowerShell')
    

    [1] Note that Unix-like platforms have no standardized mechanism for defining persistent environment variables, which is why neither PowerShell nor .NET provide APIs for it.

    [2] Note that you can use PowerShell in the Visual Studio Code terminal even without the PowerShell extension, as a general-purpose shell, and such session do use the same profiles as regular console windows - see this answer.