microsoft / vssetup.powershell

PowerShell module to interact with Visual Studio Setup
MIT License
227 stars 40 forks source link

Doesn't actually setup an environment in your shell #83

Closed genio closed 4 months ago

genio commented 4 months ago

This doesn't help load a dev environment in the current shell... at all!

Loading the default developer powershell environment loads as if I want to build 32-bit things on a 64-bit machine. The insanity that is required to actually load a proper dev environment in Windows is baffling.

Is there really no better way than to write something akin to:

[String]$vswherePath = "${env:ProgramFiles(x86)}\Microsoft Visual Studio\Installer\vswhere.exe";
[Boolean]$haveVSWhere = [System.IO.File]::Exists($vswherePath);

Write-Host("Do we have ${vswherePath} installed? ${haveVSWhere}");
if (!$haveVSWhere) {
    throw "Not found. Good luck!";
}

# Visual Studio path <https://github.com/microsoft/vswhere/wiki/Find-VC>
[String]$vsPath = &"${vswherePath}" -latest -products * -requires Microsoft.VisualStudio.Component.VC.Tools.x86.x64 -property installationpath
if (!$vsPath) {
    throw "Unable to find the installation path for Visual Studio.";
}
Write-Host "Do we have a version of Visual Studio that's usable? ${vsPath}"

[String]$dllPath = (& "${vswherePath}" -latest -prerelease -products * -requires Microsoft.Component.MSBuild -find Common7\Tools\Microsoft.VisualStudio.DevShell.dll)
if (!$dllPath) {
    throw "No path to run the DLL environment import.";
}
Write-Host("DLL Path: ${dllPath}");
Import-Module $dllPath;

Enter-VsDevShell -VsInstallPath $vsPath -SkipAutomaticLocation -DevCmdArguments '-arch=x64'
Set-Item -Path "env:CC" -Value "cl.exe"
Set-Item -Path "env:CXX" -Value "cl.exe"

Even all of that nonsense assumes lots of things and doesn't "Just work". On MacOS, it's one command. On Linux, it's one command. On Windows, it's "yeah.... good luck". WHY?!?

heaths commented 4 months ago

You can start the "Developer Command Prompt" in either cmd.exe or pwsh.exe/powershell.exe (whichever is found first) from the shortcuts installed with VS. This module is used to get metadata for every VS instance installed and is likely not what you're looking for.

Long ago I also added support to Windows Terminal directly to find instances, so you can also find profiles already in WT (just disabled by default, but you can easily enable them).

In either case, you just start it and have a shell ready.

genio commented 4 months ago

Again, those start things for 32-bit builds. sigh

heaths commented 4 months ago

See https://learn.microsoft.com/visualstudio/ide/reference/command-prompt-powershell?view=vs-2022. You can use these cmdlets (though you're using vswhere, which also works, but is a different repo: https://github.com/microsoft/vswhere) to find that and launch it:

$vs = Get-VSSetupInstance -Prerelease | Select-VSSetupInstance -Latest
& "$(Join-Path $vs.InstallationPath Common7\Tools\Launch-VsDevShell.ps1)" -Arch amd64

In Windows Terminal, you can modify the profiles to suit your needs. vswhere and these cmdlets are only for finding VS instances. If you would like to suggest changes to the shortcuts, I recommend filing feedback through Visual Studio so it gets to the right team.