Repository navigation
Stop forwarding SDK location defaults to MSBuild #57174
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
Open
baronfel
wants to merge
5
commits into
dotnet:release/11.0.2xx
Choose a base branch
from
baronfel:baronfel-remove-msbuild-sdk-path-vars
base: release/11.0.2xx
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
+365
−50
Open
Changes from all commits
Commits
Show all changes
5 commits
Select commit
Hold shift + click to select a range
45ce8a6
Stop forwarding SDK location defaults to MSBuild
baronfel 57c5b74
Address MSBuild SDK discovery PR feedback
baronfel 32c35e8
Cover project telemetry SDK discovery
baronfel 6139072
Fix cross-platform SDK discovery test
baronfel 0e2121a
Normalize SDK paths in discovery evaluation test
baronfel File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
8 changes: 8 additions & 0 deletions
8
test/TestAssets/TestProjects/MSBuildSdkDiscovery/Directory.Build.targets
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,8 @@ | ||
| <Project> | ||
| <Target Name="CheckDiscoveryEnvironment" BeforeTargets="Build;ComputeRunArguments"> | ||
| <Error Condition="$([System.Environment]::GetEnvironmentVariable('MSBuildSDKsPath')) != ''" | ||
| Text="MSBuildSDKsPath leaked into the build environment." /> | ||
| <Error Condition="$([System.Environment]::GetEnvironmentVariable('MSBuildExtensionsPath')) != ''" | ||
| Text="MSBuildExtensionsPath leaked into the build environment." /> | ||
| </Target> | ||
| </Project> |
5 changes: 5 additions & 0 deletions
5
test/TestAssets/TestProjects/MSBuildSdkDiscovery/Extensions/Discovery.props
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,5 @@ | ||
| <Project> | ||
| <PropertyGroup> | ||
| <DiscoveryExtensionsPath>$(MSBuildThisFileDirectory)</DiscoveryExtensionsPath> | ||
| </PropertyGroup> | ||
| </Project> |
3 changes: 3 additions & 0 deletions
3
test/TestAssets/TestProjects/MSBuildSdkDiscovery/Override.proj
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,3 @@ | ||
| <Project Sdk="Discovery.Sdk"> | ||
| <Import Project="$(MSBuildExtensionsPath)\Discovery.props" /> | ||
| </Project> |
23 changes: 23 additions & 0 deletions
23
test/TestAssets/TestProjects/MSBuildSdkDiscovery/Probe.proj
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,23 @@ | ||
| <Project Sdk="Microsoft.NET.Sdk"> | ||
| <PropertyGroup> | ||
| <TargetFramework>$(CurrentTargetFramework)</TargetFramework> | ||
| </PropertyGroup> | ||
| <Target Name="Report"> | ||
| <Error Condition="$([System.Environment]::GetEnvironmentVariable('MSBuildSDKsPath')) != ''" | ||
| Text="MSBuildSDKsPath leaked into the build environment." /> | ||
| <Error Condition="$([System.Environment]::GetEnvironmentVariable('MSBuildExtensionsPath')) != ''" | ||
| Text="MSBuildExtensionsPath leaked into the build environment." /> | ||
| <WriteLinesToFile File="$(MSBuildProjectDirectory)\paths.txt" | ||
| Lines="$(MSBuildToolsPath);$(MSBuildExtensionsPath);$(MSBuildSDKsPath);$([System.IO.Path]::GetFullPath('$(MicrosoftNETBuildTasksAssembly)'))" | ||
| Overwrite="true" /> | ||
| </Target> | ||
| <Target Name="Probe" DependsOnTargets="Report"> | ||
| <MSBuild Projects="$(MSBuildProjectFullPath)" | ||
| Targets="Report" | ||
| Properties="DiscoveryWorker=true" | ||
| BuildInParallel="true" /> | ||
| <Exec Condition="'$(Nested)' != 'true'" | ||
| WorkingDirectory="$(MSBuildProjectDirectory)\nested" | ||
| Command=""$(DOTNET_HOST_PATH)" msbuild Probe.proj -t:Probe -p:Nested=true -p:DiscoveryNodeReuse=$(DiscoveryNodeReuse) -nr:$(DiscoveryNodeReuse) -bl:2-{}.binlog" /> | ||
| </Target> | ||
| </Project> |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,3 @@ | ||
| #:property PublishAot=false | ||
|
|
||
| System.Console.WriteLine("SDK discovery succeeded"); |
5 changes: 5 additions & 0 deletions
5
test/TestAssets/TestProjects/MSBuildSdkDiscovery/Sdks/Discovery.Sdk/Sdk/Sdk.props
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,5 @@ | ||
| <Project> | ||
| <PropertyGroup> | ||
| <DiscoverySdkPath>$(MSBuildThisFileDirectory)</DiscoverySdkPath> | ||
| </PropertyGroup> | ||
| </Project> |
1 change: 1 addition & 0 deletions
1
test/TestAssets/TestProjects/MSBuildSdkDiscovery/Sdks/Discovery.Sdk/Sdk/Sdk.targets
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1 @@ | ||
| <Project /> |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
148 changes: 148 additions & 0 deletions
148
test/dotnet.Tests/CommandTests/MSBuild/GivenMSBuildSdkDiscovery.cs
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,148 @@ | ||
| // Licensed to the .NET Foundation under one or more agreements. | ||
| // The .NET Foundation licenses this file to you under the MIT license. | ||
|
|
||
| using System.Text.Json; | ||
| using Microsoft.Build.Framework; | ||
| using Microsoft.Build.Logging.StructuredLogger; | ||
|
|
||
| namespace Microsoft.DotNet.Cli.MSBuild.Tests; | ||
|
|
||
| [TestClass] | ||
| public sealed class GivenMSBuildSdkDiscovery : SdkTest | ||
| { | ||
| [TestMethod] | ||
| public void NestedInvocationsDiscoverTheirSelectedToolset() | ||
| { | ||
| var toolset = SdkTestContext.Current.ToolsetUnderTest; | ||
| var asset = TestAssetsManager.CopyTestAsset("MSBuildSdkDiscovery", identifier: Guid.NewGuid().ToString("N")).WithSource(); | ||
| string nestedDirectory = Path.Combine(asset.Path, "nested"); | ||
| string alternateRoot = Path.Combine(asset.Path, "alternate"); | ||
| string alternateSdk = Path.Combine(alternateRoot, "sdk", "99.0.100"); | ||
|
|
||
| Directory.CreateDirectory(nestedDirectory); | ||
| File.Copy(Path.Combine(asset.Path, "Probe.proj"), Path.Combine(nestedDirectory, "Probe.proj"), overwrite: true); | ||
| Directory.CreateDirectory(alternateSdk); | ||
| foreach (string file in Directory.EnumerateFiles(toolset.SdkFolderUnderTest)) | ||
| { | ||
| File.Copy(file, Path.Combine(alternateSdk, Path.GetFileName(file))); | ||
| } | ||
| foreach (string directory in Directory.EnumerateDirectories(toolset.SdkFolderUnderTest)) | ||
| { | ||
| Directory.CreateSymbolicLink(Path.Combine(alternateSdk, Path.GetFileName(directory)), directory); | ||
| } | ||
| Directory.CreateSymbolicLink(Path.Combine(alternateRoot, "shared"), Path.Combine(toolset.DotNetRoot, "shared")); | ||
| Directory.CreateSymbolicLink(Path.Combine(alternateRoot, "host"), Path.Combine(toolset.DotNetRoot, "host")); | ||
|
|
||
| WriteGlobalJson(asset.Path, toolset.DotNetRoot, toolset.SdkVersion); | ||
| WriteGlobalJson(nestedDirectory, alternateRoot, "99.0.100"); | ||
|
|
||
| int invocation = 0; | ||
| foreach (var (outOfProc, useServer, nodeReuse) in new[] | ||
| { | ||
| (false, false, false), | ||
| (true, false, false), | ||
| (false, true, false), | ||
| (false, true, true) | ||
| }) | ||
| { | ||
| string binlog = Path.Combine(asset.Path, $"{++invocation}.binlog"); | ||
| var command = CreateDiscoveryCommand(asset.Path, "msbuild"); | ||
| command.WithEnvironmentVariable("DOTNET_CLI_RUN_MSBUILD_OUTOFPROC", outOfProc.ToString()); | ||
| command.WithEnvironmentVariable("DOTNET_CLI_USE_MSBUILD_SERVER", useServer.ToString()); | ||
| command.WithEnvironmentVariable("MSBUILDUSESERVER", useServer ? "1" : "0"); | ||
| command.WithEnvironmentVariable("MSBUILDNOINPROCNODE", "1"); | ||
| var result = command.Execute("Probe.proj", "-t:Probe", $"-nr:{nodeReuse}", | ||
| $"-p:DiscoveryUseServer={useServer}", $"-p:DiscoveryNodeReuse={nodeReuse}", $"-bl:{binlog}"); | ||
| if (File.Exists(binlog)) | ||
| { | ||
| TestContext.AddResultFile(binlog); | ||
| } | ||
| result.Should().Pass(); | ||
|
|
||
| AssertPaths(asset.Path, toolset.SdkFolderUnderTest); | ||
| AssertPaths(nestedDirectory, alternateSdk); | ||
|
|
||
| var events = BinaryLog.ReadRecords(binlog).Select(record => record.Args).ToArray(); | ||
| events.OfType<TargetStartedEventArgs>().Where(args => args.TargetName == "Report") | ||
| .Should().Contain(args => args.BuildEventContext!.NodeId > 1); | ||
| } | ||
| } | ||
|
|
||
| [TestMethod] | ||
| public void EvaluationWithoutBuildDiscoversSdkImports() | ||
| { | ||
| var asset = TestAssetsManager.CopyTestAsset("MSBuildSdkDiscovery").WithSource(); | ||
| var toolset = SdkTestContext.Current.ToolsetUnderTest; | ||
| WriteGlobalJson(asset.Path, toolset.DotNetRoot, toolset.SdkVersion); | ||
|
|
||
| var command = CreateDiscoveryCommand(asset.Path, "msbuild"); | ||
| command.WithEnvironmentVariable("DOTNET_CLI_USE_MSBUILD_SERVER", "false"); | ||
| command.WithEnvironmentVariable("MSBUILDUSESERVER", "0"); | ||
| var result = command.Execute("Probe.proj", "-getProperty:MSBuildToolsPath,MSBuildExtensionsPath,MSBuildSDKsPath,MicrosoftNETBuildTasksAssembly", BinLogArgument([])); | ||
| result.Should().Pass(); | ||
| using var output = JsonDocument.Parse(result.StdOut!); | ||
| var properties = output.RootElement.GetProperty("Properties"); | ||
| string sdkDirectory = TestPathUtility.ResolveTempPrefixLink(toolset.SdkFolderUnderTest); | ||
| string sdksDirectory = TestPathUtility.ResolveTempPrefixLink(toolset.SdksPath); | ||
| Path.TrimEndingDirectorySeparator(properties.GetProperty("MSBuildToolsPath").GetString()!) | ||
| .Should().Be(sdkDirectory); | ||
| Path.TrimEndingDirectorySeparator(properties.GetProperty("MSBuildExtensionsPath").GetString()!) | ||
| .Should().Be(sdkDirectory); | ||
| properties.GetProperty("MSBuildSDKsPath").GetString().Should().Be(sdksDirectory); | ||
| Path.GetFullPath(properties.GetProperty("MicrosoftNETBuildTasksAssembly").GetString()!) | ||
| .Should().StartWith(sdksDirectory + Path.DirectorySeparatorChar); | ||
| } | ||
|
|
||
| [TestMethod] | ||
| public void FileBasedRunDiscoversSdkWithoutEnvironmentDefaults() | ||
| { | ||
| var asset = TestAssetsManager.CopyTestAsset("MSBuildSdkDiscovery").WithSource(); | ||
| var toolset = SdkTestContext.Current.ToolsetUnderTest; | ||
| WriteGlobalJson(asset.Path, toolset.DotNetRoot, toolset.SdkVersion); | ||
|
|
||
| CreateDiscoveryCommand(asset.Path, "run", "--file", "Program.cs") | ||
| .Execute(BinLogArgument([])) | ||
| .Should().Pass() | ||
| .And.HaveStdOutContaining("SDK discovery succeeded"); | ||
| } | ||
|
|
||
| [TestMethod] | ||
| public void ExplicitEnvironmentOverridesSelectSdkImports() | ||
| { | ||
| var asset = TestAssetsManager.CopyTestAsset("MSBuildSdkDiscovery").WithSource(); | ||
| string extensionsPath = Path.Combine(asset.Path, "Extensions"); | ||
| string sdksPath = Path.Combine(asset.Path, "Sdks"); | ||
| var command = new DotnetCommand(Log, "msbuild") { WorkingDirectory = asset.Path }; | ||
| command.EnvironmentToRemove.Add("MSBUILD_EXE_PATH"); | ||
| command.WithEnvironmentVariable("MSBuildExtensionsPath", extensionsPath); | ||
| command.WithEnvironmentVariable("MSBuildSDKsPath", sdksPath); | ||
| command.Execute("Override.proj", "-getProperty:DiscoveryExtensionsPath,DiscoverySdkPath", BinLogArgument([])) | ||
| .Should().Pass() | ||
| .And.HaveStdOutContaining(JsonSerializer.Serialize(extensionsPath + Path.DirectorySeparatorChar)) | ||
| .And.HaveStdOutContaining(JsonSerializer.Serialize(Path.Combine(sdksPath, "Discovery.Sdk", "Sdk") + Path.DirectorySeparatorChar)); | ||
| } | ||
|
|
||
| private DotnetCommand CreateDiscoveryCommand(string workingDirectory, params string[] args) | ||
| { | ||
| var command = new DotnetCommand(Log, args) { WorkingDirectory = workingDirectory }; | ||
| command.EnvironmentToRemove.AddRange(["MSBUILD_EXE_PATH", "MSBuildSDKsPath", "MSBuildExtensionsPath"]); | ||
| return command; | ||
| } | ||
|
|
||
| private static void WriteGlobalJson(string directory, string root, string version) | ||
| => File.WriteAllText(Path.Combine(directory, "global.json"), JsonSerializer.Serialize(new | ||
| { | ||
| sdk = new { version, paths = new[] { root }, rollForward = "disable", allowPrerelease = true } | ||
| })); | ||
|
|
||
| private static void AssertPaths(string projectDirectory, string sdkDirectory) | ||
| { | ||
| sdkDirectory = TestPathUtility.ResolveTempPrefixLink(sdkDirectory); | ||
| string[] paths = File.ReadAllLines(Path.Combine(projectDirectory, "paths.txt")); | ||
| paths.Should().HaveCount(4); | ||
| Path.TrimEndingDirectorySeparator(paths[0]).Should().Be(sdkDirectory); | ||
| Path.TrimEndingDirectorySeparator(paths[1]).Should().Be(sdkDirectory); | ||
| Path.TrimEndingDirectorySeparator(paths[2]).Should().Be(Path.Combine(sdkDirectory, "Sdks")); | ||
| paths[3].Should().StartWith(Path.Combine(sdkDirectory, "Sdks") + Path.DirectorySeparatorChar); | ||
| } | ||
| } |
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.