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

Adding Get-MSBuildPathModified with Telemetry under Feature flags #424

Merged
merged 10 commits into from
Jan 23, 2025

Conversation

praval-microsoft
Copy link
Contributor

@praval-microsoft praval-microsoft commented Jan 16, 2025

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
}

Testing

  • Tested the Feature Flag logic by deploying the MSBuildTask with Updated MSBuildHelpers package in a personal test organisation.
  • Verified for the Visual Studio 15,16 and 17 for both Architectures x64 and x86.

@praval-microsoft praval-microsoft marked this pull request as ready for review January 16, 2025 11:21
@praval-microsoft praval-microsoft merged commit 0488a16 into main Jan 23, 2025
6 checks passed
@praval-microsoft praval-microsoft deleted the users/prsinghal/msbuildhelpers/pathfunctions branch January 23, 2025 09:44
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.

3 participants