-
Notifications
You must be signed in to change notification settings - Fork 1.2k
Migrate 5 MSBuild tasks to IMultiThreadableTask (Group 2: Bundle, Deps, Config & Hosts) #52936
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
Closed
Closed
Changes from all commits
Commits
Show all changes
15 commits
Select commit
Hold shift + click to select a range
b7f379e
Refactor CliFolderPathCalculatorCore to support env var injection
SimaTian 7f63866
Remove static convenience methods per review feedback
SimaTian 6626457
Migrate Group 2 tasks to MSBuildMultiThreadableTask
SimaTian f74c678
Fix NRE: add TaskEnvironment to GivenTasksUseAbsolutePaths tests
SimaTian f580864
Fix GenerateDepsFile: guard empty RuntimeGraphPath before GetAbsolute…
SimaTian 1874997
Fix NotBeOfType null-subject assertion and TaskEnvironment lazy-init …
SimaTian 3984040
Fix AbsolutePath combining constructor: normalize '..' segments
SimaTian c1fcd1f
Address PR review feedback for merge-group-2
SimaTian 0ca92d3
Rework Group 2 migration: council review + PR feedback
SimaTian a705bc1
Fix ItHandlesEmptyOutputDir: empty OutputDir falls back to ProjectDir…
SimaTian 33e33f6
Fix FrameworkReferenceResolver: route all env var reads through delegate
SimaTian 1ca5cb9
Fix CI: guard empty paths, add TaskEnvironment to test, stabilize CWD…
SimaTian e660b41
Fix CWD race: remove savedCwd pattern, use stable fallback
SimaTian 4716c03
Fix AbsolutePath usage: absolutize at I/O, preserve originals for out…
SimaTian e847ee4
Fix CWD race in ClsidMap and RuntimeConfig tests (macOS)
SimaTian 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
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -9,8 +9,20 @@ | |
|
|
||
| namespace Microsoft.NET.Build.Tasks | ||
| { | ||
| public class ResolveAppHosts : TaskBase | ||
| [MSBuildMultiThreadableTask] | ||
| public class ResolveAppHosts : TaskBase, IMultiThreadableTask | ||
| { | ||
| #if NETFRAMEWORK | ||
| private TaskEnvironment _taskEnvironment; | ||
| public TaskEnvironment TaskEnvironment | ||
| { | ||
| get => _taskEnvironment ??= TaskEnvironmentDefaults.Create(); | ||
| set => _taskEnvironment = value; | ||
| } | ||
| #else | ||
| public TaskEnvironment TaskEnvironment { get; set; } | ||
| #endif | ||
|
|
||
| public string TargetFrameworkIdentifier { get; set; } | ||
|
|
||
| public string TargetFrameworkVersion { get; set; } | ||
|
|
@@ -78,8 +90,18 @@ public class ResolveAppHosts : TaskBase | |
| [Output] | ||
| public ITaskItem[] PackAsToolShimAppHostPacks { get; set; } | ||
|
|
||
| private AbsolutePath? _absoluteRuntimeGraphPath; | ||
| private AbsolutePath? _absoluteTargetingPackRoot; | ||
|
|
||
| protected override void ExecuteCore() | ||
| { | ||
| _absoluteRuntimeGraphPath = !string.IsNullOrEmpty(RuntimeGraphPath) | ||
|
Member
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Consider backing the property with |
||
| ? TaskEnvironment.GetAbsolutePath(RuntimeGraphPath) | ||
| : null; | ||
| _absoluteTargetingPackRoot = !string.IsNullOrEmpty(TargetingPackRoot) | ||
| ? TaskEnvironment.GetAbsolutePath(TargetingPackRoot) | ||
| : null; | ||
|
|
||
| var normalizedTargetFrameworkVersion = ProcessFrameworkReferences.NormalizeVersion(new Version(TargetFrameworkVersion)); | ||
|
|
||
| var knownAppHostPacksForTargetFramework = KnownAppHostPacks | ||
|
|
@@ -245,7 +267,7 @@ private ITaskItem GetHostItem(string runtimeIdentifier, | |
| } | ||
|
|
||
| string bestAppHostRuntimeIdentifier = NuGetUtils.GetBestMatchingRidWithExclusion( | ||
| new RuntimeGraphCache(this).GetRuntimeGraph(RuntimeGraphPath), | ||
| new RuntimeGraphCache(this).GetRuntimeGraph(_absoluteRuntimeGraphPath.Value.Value), | ||
| runtimeIdentifier, | ||
| runtimeIdentifiersToExclude.Split(';'), | ||
| appHostRuntimeIdentifiers.Split(';'), | ||
|
|
@@ -288,15 +310,16 @@ private ITaskItem GetHostItem(string runtimeIdentifier, | |
|
|
||
| TaskItem appHostItem = new(itemName); | ||
| string appHostPackPath = null; | ||
| if (!string.IsNullOrEmpty(TargetingPackRoot)) | ||
| if (_absoluteTargetingPackRoot.HasValue) | ||
| { | ||
| appHostPackPath = Path.Combine(TargetingPackRoot, hostPackName, appHostPackVersion); | ||
| appHostPackPath = Path.Combine(_absoluteTargetingPackRoot.Value.Value, hostPackName, appHostPackVersion); | ||
| } | ||
| if (appHostPackPath != null && Directory.Exists(appHostPackPath)) | ||
| { | ||
| // Use AppHost from packs folder | ||
| appHostItem.SetMetadata(MetadataKeys.PackageDirectory, appHostPackPath); | ||
| appHostItem.SetMetadata(MetadataKeys.Path, Path.Combine(appHostPackPath, hostRelativePathInPackage)); | ||
| string originalAppHostPackPath = Path.Combine(TargetingPackRoot, hostPackName, appHostPackVersion); | ||
| appHostItem.SetMetadata(MetadataKeys.PackageDirectory, originalAppHostPackPath); | ||
| appHostItem.SetMetadata(MetadataKeys.Path, Path.Combine(originalAppHostPackPath, hostRelativePathInPackage)); | ||
| } | ||
| else if (EnableAppHostPackDownload) | ||
| { | ||
|
|
||
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
72 changes: 72 additions & 0 deletions
72
test/Microsoft.NET.Build.Tasks.Tests/GivenAFrameworkReferenceResolver.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,72 @@ | ||
| // Licensed to the .NET Foundation under one or more agreements. | ||
| // The .NET Foundation licenses this file to you under the MIT license. | ||
|
|
||
| using FluentAssertions; | ||
| using Xunit; | ||
|
|
||
| namespace Microsoft.NET.Build.Tasks.UnitTests | ||
| { | ||
| public class GivenAFrameworkReferenceResolver | ||
| { | ||
| [Fact] | ||
| public void ItReadsReferenceAssembliesPathThroughInjectedDelegate() | ||
| { | ||
| var envVarsRead = new List<string>(); | ||
| var resolver = new FrameworkReferenceResolver(name => | ||
| { | ||
| envVarsRead.Add(name); | ||
| return name == "DOTNET_REFERENCE_ASSEMBLIES_PATH" ? "/custom/ref/path" : null; | ||
| }); | ||
|
|
||
| var result = resolver.GetDefaultReferenceAssembliesPath(); | ||
|
|
||
| result.Should().Be("/custom/ref/path"); | ||
| envVarsRead.Should().Contain("DOTNET_REFERENCE_ASSEMBLIES_PATH", | ||
| "env var should be read through the injected delegate, not process-global Environment"); | ||
| } | ||
|
|
||
| [Fact] | ||
| public void ItFallsToProgramFilesWhenReferenceAssembliesPathNotSet() | ||
| { | ||
| if (!RuntimeInformation.IsOSPlatform(OSPlatform.Windows)) | ||
| { | ||
| // On non-Windows, it returns null when env var is not set | ||
| var resolver = new FrameworkReferenceResolver(_ => null); | ||
| resolver.GetDefaultReferenceAssembliesPath().Should().BeNull(); | ||
| return; | ||
| } | ||
|
|
||
| var resolver2 = new FrameworkReferenceResolver(name => | ||
| name == "ProgramFiles(x86)" ? @"C:\Program Files (x86)" : null); | ||
|
|
||
| var result = resolver2.GetDefaultReferenceAssembliesPath(); | ||
|
|
||
| result.Should().Be(@"C:\Program Files (x86)\Reference Assemblies\Microsoft\Framework"); | ||
| } | ||
|
|
||
| [Fact] | ||
| public void ItDoesNotCallProcessGlobalEnvironment() | ||
| { | ||
| // Set a process-level env var that should NOT be seen by the resolver | ||
| // if it correctly uses the injected delegate | ||
| var originalValue = Environment.GetEnvironmentVariable("DOTNET_REFERENCE_ASSEMBLIES_PATH"); | ||
| try | ||
| { | ||
| Environment.SetEnvironmentVariable("DOTNET_REFERENCE_ASSEMBLIES_PATH", "/process-global-path"); | ||
|
|
||
| // Inject a delegate that returns a DIFFERENT value | ||
| var resolver = new FrameworkReferenceResolver(name => | ||
| name == "DOTNET_REFERENCE_ASSEMBLIES_PATH" ? "/injected-path" : null); | ||
|
|
||
| var result = resolver.GetDefaultReferenceAssembliesPath(); | ||
|
|
||
| result.Should().Be("/injected-path", | ||
| "resolver should use injected delegate, not process-global Environment.GetEnvironmentVariable"); | ||
| } | ||
| finally | ||
| { | ||
| Environment.SetEnvironmentVariable("DOTNET_REFERENCE_ASSEMBLIES_PATH", originalValue); | ||
| } | ||
| } | ||
| } | ||
| } |
Oops, something went wrong.
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.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
If this path is relative, there is a problem. It gets transformed into the full path down the line in DependencyContextBuilder.cs like following:
string fullProjectPath = Path.GetFullPath(Path.Combine(mainProjectDirectory, projectPath));There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Took me a while, but now the agents came to an agreement and claim that this was resolved and is now properly absolutized before use.
I'm mostly confident they are correct, unless I overlook something while reviewing.