Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

updating the Get-MsBuildPath function #422

Open
wants to merge 5 commits into
base: main
Choose a base branch
from

Conversation

SimaTian
Copy link
Member

to avoid reflection for the new vs studio versions since it causes assembly conflict.

Related issue 20734

This portion of the Get-MsBuildPath function script first locates Microsoft.Build.Utilities.Core.dll and then uses it to locate the msbuild.exe. Under the current preview version of VS this fails due to an assembly conflict.
Fortunately for us, the reflection usage here is redundant, since the msbuild exe lives either in the same folder as this .dll file or in its direct subfolder.
These paths are fixed due to the way VS installation works - e.g. if we have location of the .dll file, we have the location of both of these versions of msbuild.

$Architecture = 'x64'

$Path = "C:\Program Files\Microsoft Visual Studio\2022\Preview\MSBuild\Current\Bin\Microsoft.Build.Utilities.Core.dll"
$msUtilities = [System.Reflection.Assembly]::LoadFrom($Path)

[type]$t = $msUtilities.GetType('Microsoft.Build.Utilities.ToolLocationHelper')
if ($t -ne $null) 
{
    [System.Reflection.MethodInfo] $mi = $t.GetMethod("GetPathToBuildToolsFile",[type[]]@( [string], [string], $msUtilities.GetType("Microsoft.Build.Utilities.DotNetFrameworkArchitecture") ))

$param3 = $mi.GetParameters()[2]
    $archValues = [System.Enum]::GetValues($param3. ParameterType)

[object] $archValue = $null
        if ($Architecture -eq 'x86') {
            $archValue = $archValues.GetValue(1) # DotNetFrameworkArchitecture.Bitness32
        } elseif ($Architecture -eq 'x64') {
            $archValue = $archValues.GetValue(2) # DotNetFrameworkArchitecture.Bitness64
        } else {
            $archValue = $archValues.GetValue(1) # DotNetFrameworkArchitecture.Bitness32
        }
    Write-Host "archValue = $archValue"

$msBuildPath = $mi.Invoke($null, @( 'msbuild.exe', '17.0', $archValue ))
    $msBuildPath
}

… vs studio versions since it causes assembly conflict.
@SimaTian SimaTian requested review from tarunramsinghani and a team as code owners January 10, 2025 10:56
@SimaTian
Copy link
Member Author

@microsoft-github-policy-service agree company="Microsoft"

Copy link
Member

@rainersigwald rainersigwald left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I strongly agree with the direction here.

common-npm-packages/msbuildhelpers/PathFunctions.ps1 Outdated Show resolved Hide resolved
common-npm-packages/msbuildhelpers/PathFunctions.ps1 Outdated Show resolved Hide resolved
$msUtilities = [System.Reflection.Assembly]::LoadFrom($msbuildUtilitiesPath)
[object]$archValue = $null
if ($Architecture -eq 'x86') {
$msBuildPath = Join-Path $specifiedStudio.installationPath MSBuild Current Bin MSBuild.exe
Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This works with powershell 7.4+
On my machine it breaks with Windows Powershell since I probably have some older version.
Is that something we should be concerned about?

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Join-Path $specifiedStudio.installationPath MSBuild Current Bin MSBuild.exe

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Ok that's a very convincing reason!

(As a rule Windows PowerShell 5.1 is the only "real" one; the new cross-platform ones are much better but since they don't come with Windows can't be relied upon in the same way.)

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

2 participants