Skip to content

Commit 3ca52dd

Browse files
Copilotnkolev92
andauthored
Use VS MSBuild.exe instead of dotnet msbuild in no-op tests
Agent-Logs-Url: https://github.com/NuGet/NuGet.Client/sessions/c6cec734-7d92-4cd2-a819-9e05ed4bbacb Co-authored-by: nkolev92 <[email protected]>
1 parent 281447d commit 3ca52dd

2 files changed

Lines changed: 33 additions & 6 deletions

File tree

test/NuGet.Tests.Apex/NuGet.Tests.Apex/NuGetEndToEndTests/NetCoreProjectTestCase.cs

Lines changed: 8 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -252,10 +252,11 @@ public void NetCoreVSAndMSBuildRestoreIsNoOp()
252252

253253
var vsRestoreTimestamp = File.GetLastWriteTime(cacheFilePath.FullName).Ticks;
254254

255-
// Act - run MSBuild restore externally
255+
// Act - run MSBuild restore externally using VS MSBuild
256+
var msbuildPath = CommonUtility.GetMSBuildExePath();
256257
using var process = new Process();
257-
process.StartInfo.FileName = "dotnet";
258-
process.StartInfo.Arguments = $"msbuild /t:restore \"{testContext.Project.FullPath}\"";
258+
process.StartInfo.FileName = msbuildPath;
259+
process.StartInfo.Arguments = $"/t:restore \"{testContext.Project.FullPath}\"";
259260
process.StartInfo.UseShellExecute = false;
260261
process.StartInfo.RedirectStandardError = true;
261262
process.Start();
@@ -305,10 +306,11 @@ public void NetCoreMultipleTargetFrameworksVSAndMSBuildRestoreIsNoOp()
305306

306307
var vsRestoreTimestamp = File.GetLastWriteTime(cacheFilePath.FullName).Ticks;
307308

308-
// Act - run MSBuild restore externally
309+
// Act - run MSBuild restore externally using VS MSBuild
310+
var msbuildPath = CommonUtility.GetMSBuildExePath();
309311
using var process = new Process();
310-
process.StartInfo.FileName = "dotnet";
311-
process.StartInfo.Arguments = $"msbuild /t:restore \"{testContext.Project.FullPath}\"";
312+
process.StartInfo.FileName = msbuildPath;
313+
process.StartInfo.Arguments = $"/t:restore \"{testContext.Project.FullPath}\"";
312314
process.StartInfo.UseShellExecute = false;
313315
process.StartInfo.RedirectStandardError = true;
314316
process.Start();

test/NuGet.Tests.Apex/NuGet.Tests.Apex/Utility/CommonUtility.cs

Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -556,5 +556,30 @@ public static void AssertUninstalledPackageByProjectType(VisualStudioHost visual
556556
CommonUtility.AssertPackageReferenceDoesNotExist(project, packageName, logger);
557557
}
558558
}
559+
560+
/// <summary>
561+
/// Gets the path to MSBuild.exe from the Visual Studio installation.
562+
/// Uses the VSAPPIDDIR environment variable to find the VS root folder,
563+
/// same approach as Get-MSBuildExe in the PS E2E test infrastructure.
564+
/// </summary>
565+
public static string GetMSBuildExePath()
566+
{
567+
string vsAppIdDir = Environment.GetEnvironmentVariable("VSAPPIDDIR") ?? Environment.GetEnvironmentVariable("DevEnvDir");
568+
if (string.IsNullOrEmpty(vsAppIdDir))
569+
{
570+
throw new InvalidOperationException("Could not determine Visual Studio installation path. Neither VSAPPIDDIR nor DevEnvDir environment variable is set.");
571+
}
572+
573+
// VSAPPIDDIR is <VSRoot>\Common7\IDE\, go up 2 levels to get VS root
574+
string vsRoot = Path.GetFullPath(Path.Combine(vsAppIdDir, "..", ".."));
575+
string msbuildPath = Path.Combine(vsRoot, "MSBuild", "Current", "bin", "MSBuild.exe");
576+
577+
if (!File.Exists(msbuildPath))
578+
{
579+
throw new FileNotFoundException($"MSBuild.exe not found at expected path: {msbuildPath}");
580+
}
581+
582+
return msbuildPath;
583+
}
559584
}
560585
}

0 commit comments

Comments
 (0)