Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions documentation/wiki/ChangeWaves.md
Original file line number Diff line number Diff line change
Expand Up @@ -40,6 +40,7 @@ Change wave checks around features will be removed in the release that accompani
- [Out-of-proc communication uses larger read-ahead buffers and pre-buffers TaskHost packet bodies to reduce pipe reads.](https://github.com/dotnet/msbuild/pull/14505)
- [Restore no longer discards a ProjectRootElementCache that reloads changed files from disk, so the build that follows an implicit restore does not re-parse the import closure.](https://github.com/dotnet/msbuild/pull/14558)
- [Use optimized MSBuild file specification matching and enumeration](https://github.com/dotnet/msbuild/pull/14663). Regex-backed file globs use culture-invariant case folding; set `MSBUILDUSELEGACYCULTURESENSITIVEFILEGLOBS=1` to preserve legacy current-culture folding without disabling other Wave 18.11 features.
- [Isolated (`-graph -isolate`) builds fail deterministically with MSB4252 on a cross-project reference to a target that is not declared via `ProjectReferenceTargets`, instead of passing if the referenced project happened to build on that node previously.](https://github.com/dotnet/msbuild/pull/14280)

### 18.10
- [Resolve relative project paths against the Unix logical current directory from `PWD`, so builds under symlinked directories produce stable project full paths and related output paths.](https://github.com/dotnet/msbuild/pull/13752)
Expand Down
40 changes: 40 additions & 0 deletions src/Build.UnitTests/BackEnd/BuildRequestConfiguration_Tests.cs
Original file line number Diff line number Diff line change
Expand Up @@ -776,5 +776,45 @@ public void TestProjectEvaluationIdPreservedAcrossTranslateForFutureUse()

deserialized.ProjectEvaluationId.ShouldBe(expectedEvalId);
}

[Fact]
public void TestRequestedTargetsPreservedAcrossTranslateForFutureUse()
{
string projectBody = """
<Project>
<Target Name='EntryTarget' />
<Target Name='OtherTarget' />
</Project>
""".Cleanup();

using var collection = new ProjectCollection();
using ProjectFromString projectFromString = new(
projectBody,
new Dictionary<string, string>(),
ObjectModelHelpers.MSBuildDefaultToolsVersion,
collection);
Project project = projectFromString.Project;
project.FullPath = "foo";
ProjectInstance instance = project.CreateProjectInstance();

// The requested (graph-declared) top-level targets must survive the persisted (input/output cache)
// serialization path, otherwise the strict-isolation allow-list is bypassed when the config is consumed
// from an input cache. Regression test for the MSB4252 determinism fix.
BuildRequestConfiguration configuration = new(new BuildRequestData(instance, ["EntryTarget"], null), "2.0")
{
ConfigurationId = 1,
};

configuration.RequestedTargets.ShouldBe(new[] { "EntryTarget" });

// TranslateForFutureUse uses a different serialization path.
configuration.TranslateForFutureUse(TranslationHelpers.GetWriteTranslator());
ITranslator reader = TranslationHelpers.GetReadTranslator();

BuildRequestConfiguration deserialized = new();
deserialized.TranslateForFutureUse(reader);

deserialized.RequestedTargets.ShouldBe(new[] { "EntryTarget" });
}
}
}
17 changes: 11 additions & 6 deletions src/Build.UnitTests/BackEnd/ResultsCache_Tests.cs
Original file line number Diff line number Diff line change
Expand Up @@ -180,7 +180,7 @@ public void TestRetrieveSubsetTargetsFromResult()
result.AddResultsForTarget("testTarget2", BuildResultUtilities.GetEmptySucceedingTargetResult());
cache.AddResult(result);

ResultsCacheResponse response = cache.SatisfyRequest(request, new List<string>(), new List<string>(new string[] { "testTarget2" }), skippedResultsDoNotCauseCacheMiss: false);
ResultsCacheResponse response = cache.SatisfyRequest(request, new List<string>(), new List<string>(new string[] { "testTarget2" }), skippedResultsDoNotCauseCacheMiss: false, allowedTopLevelTargets: null);

Assert.Equal(ResultsCacheResponseType.Satisfied, response.Type);

Expand Down Expand Up @@ -236,19 +236,22 @@ public void TestCacheOnDifferentBuildFlagsPerRequest_ProvideProjectStateAfterBui
requestWithNoBuildDataFlags,
new List<string>(),
new List<string>(new string[] { targetName }),
skippedResultsDoNotCauseCacheMiss: false);
skippedResultsDoNotCauseCacheMiss: false,
allowedTopLevelTargets: null);

ResultsCacheResponse cachedResponseForProjectState = cache.SatisfyRequest(
requestWithProjectStateFlag,
new List<string>(),
new List<string>(new string[] { targetName }),
skippedResultsDoNotCauseCacheMiss: false);
skippedResultsDoNotCauseCacheMiss: false,
allowedTopLevelTargets: null);

ResultsCacheResponse cacheResponseForNoBuildDataFlags2 = cache.SatisfyRequest(
requestWithNoBuildDataFlags2,
new List<string>(),
new List<string>(new string[] { targetName }),
skippedResultsDoNotCauseCacheMiss: false);
skippedResultsDoNotCauseCacheMiss: false,
allowedTopLevelTargets: null);

Assert.Equal(ResultsCacheResponseType.Satisfied, cacheResponseForRequestWithNoBuildDataFlags.Type);

Expand Down Expand Up @@ -317,13 +320,15 @@ public void TestCacheOnDifferentBuildFlagsPerRequest_ProvideSubsetOfStateAfterBu
requestWithSubsetFlag1,
new List<string>(),
new List<string>(new string[] { targetName }),
skippedResultsDoNotCauseCacheMiss: false);
skippedResultsDoNotCauseCacheMiss: false,
allowedTopLevelTargets: null);

ResultsCacheResponse cachedResponseWithSubsetFlag2 = cache.SatisfyRequest(
requestWithSubsetFlag2,
new List<string>(),
new List<string>(new string[] { targetName }),
skippedResultsDoNotCauseCacheMiss: false);
skippedResultsDoNotCauseCacheMiss: false,
allowedTopLevelTargets: null);

// We used the same filter that was used for the ProjectInstance in the cache -> cache hit.
Assert.Equal(ResultsCacheResponseType.Satisfied, cachedResponseWithSubsetFlag1.Type);
Expand Down
90 changes: 90 additions & 0 deletions src/Build.UnitTests/Graph/IsolateProjects_Tests.cs
Original file line number Diff line number Diff line change
Expand Up @@ -219,6 +219,96 @@ private void CacheEnforcementImpl(bool addContinueOnError)
addContinueOnError: addContinueOnError);
}

/// <summary>
/// Regression test for https://github.com/dotnet/msbuild/issues/14225. A cross-project reference to a target that
/// only has a cached result because it ran as a DependsOnTargets dependency (and was never declared via
/// ProjectReferenceTargets) must be rejected deterministically with MSB4252, rather than being satisfied from the
/// cache depending on whether the referenced project happened to build on the same node.
/// </summary>
[Fact]
public void UndeclaredTargetSatisfiedFromDependencyResultFailsDeterministically()
{
ChangeWaves.ResetStateForTests();

AssertDependencyResultLeakBuild(
(result, logger) =>
{
result.OverallResult.ShouldBe(BuildResultCode.Failure);
logger.ErrorCount.ShouldBe(1);
logger.Errors.First().Message.ShouldStartWith("MSB4252:");
});
}

/// <summary>
/// Companion to <see cref="UndeclaredTargetSatisfiedFromDependencyResultFailsDeterministically"/>: when the change wave
/// is opted out, the previous (scheduling-dependent) behavior is preserved and the dependency-produced result satisfies
/// the undeclared reference on a single node.
/// </summary>
[Fact]
public void UndeclaredTargetSatisfiedFromDependencyResultIsAllowedWhenChangeWaveDisabled()
{
using (TestEnvironment env = TestEnvironment.Create())
{
ChangeWaves.ResetStateForTests();
env.SetEnvironmentVariable("MSBUILDDISABLEFEATURESFROMVERSION", ChangeWaves.Wave18_11.ToString());

AssertDependencyResultLeakBuild(
(result, logger) =>
{
result.OverallResult.ShouldBe(BuildResultCode.Success);
logger.Errors.ShouldBeEmpty();
});

ChangeWaves.ResetStateForTests();
}
}

private void AssertDependencyResultLeakBuild(Action<BuildResult, MockLogger> assert)
{
// The referenced project declares EntryTarget (which the static graph would request) and SpecialTarget, which
// only runs because EntryTarget depends on it. SpecialTarget is never a declared entry target of this project.
string referenceContents = @"
<Project>
<Target Name='EntryTarget' DependsOnTargets='SpecialTarget'>
<Message Text='Entry' Importance='High' />
</Target>
<Target Name='SpecialTarget'>
<Message Text='Special' Importance='High' />
</Target>
</Project>";

string referenceFile = _env.CreateFile("reference.proj", referenceContents).Path;

// The root declares only Build -> EntryTarget in the reference protocol, but its Build target calls the reference
// with the undeclared SpecialTarget.
string rootContents = $@"
<Project DefaultTargets='Build'>
<ItemGroup>
<ProjectReference Include='{referenceFile}' />
<ProjectReferenceTargets Include='Build' Targets='EntryTarget' />
</ItemGroup>
<Target Name='Build'>
<MSBuild Projects='{referenceFile}' Targets='SpecialTarget' />
</Target>
</Project>";

string rootFile = _env.CreateFile("root.proj", rootContents).Path;

BuildParameters buildParameters = _buildParametersPrototype.Clone();

using (var buildManagerSession = new Helpers.BuildManagerSession(_env, buildParameters))
{
// Seed the results cache the way the static graph would: build the reference with its declared entry target.
// This runs SpecialTarget as a dependency, leaving its result in the (single-node) local results cache.
buildManagerSession.BuildProjectFile(referenceFile, new[] { "EntryTarget" })
.OverallResult.ShouldBe(BuildResultCode.Success);

BuildResult result = buildManagerSession.BuildProjectFile(rootFile, new[] { "Build" });

assert(result, buildManagerSession.Logger);
}
}

[Fact]
public void IsolationRelatedMessagesShouldNotBePresentInNonIsolatedBuilds()
{
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -604,7 +604,12 @@ public void ReportConfigurationResponse(BuildRequestConfigurationResponse respon
request: request,
configInitialTargets: config.ProjectInitialTargets,
configDefaultTargets: config.ProjectDefaultTargets,
skippedResultsDoNotCauseCacheMiss: _componentHost.BuildParameters.SkippedResultsDoNotCauseCacheMiss());
skippedResultsDoNotCauseCacheMiss: _componentHost.BuildParameters.SkippedResultsDoNotCauseCacheMiss(),
allowedTopLevelTargets: BuildRequestConfiguration.GetIsolationAllowedTopLevelTargets(
_componentHost.BuildParameters.ProjectIsolationMode,
currentEntry.RequestConfiguration,
config,
request));

if (cacheResponse.Type == ResultsCacheResponseType.Satisfied)
{
Expand Down Expand Up @@ -1271,7 +1276,8 @@ private void IssueBuildRequests(BuildRequestEntry issuingEntry, FullyQualifiedBu
request: newRequest,
configInitialTargets: matchingConfig.ProjectInitialTargets,
configDefaultTargets: matchingConfig.ProjectDefaultTargets,
skippedResultsDoNotCauseCacheMiss: _componentHost.BuildParameters.SkippedResultsDoNotCauseCacheMiss());
skippedResultsDoNotCauseCacheMiss: _componentHost.BuildParameters.SkippedResultsDoNotCauseCacheMiss(),
allowedTopLevelTargets: BuildRequestConfiguration.GetIsolationAllowedTopLevelTargets(_componentHost.BuildParameters.ProjectIsolationMode, issuingEntry.RequestConfiguration, matchingConfig, newRequest));

if (response.Type == ResultsCacheResponseType.Satisfied)
{
Expand Down
7 changes: 6 additions & 1 deletion src/Build/BackEnd/Components/Caching/IResultsCache.cs
Original file line number Diff line number Diff line change
Expand Up @@ -53,8 +53,13 @@ internal interface IResultsCache : IBuildComponent, ITranslatable, IEnumerable<B
/// If true, then as long as there is a result in the cache (regardless of whether it was skipped or not), this method
/// will return "Satisfied". In most cases this should be false, but it may be set to true in a situation where there is no
/// chance of re-execution (which is the usual response to missing / skipped targets), and the caller just needs the data.</param>
/// <param name="allowedTopLevelTargets">When non-null, the request is only considered satisfiable if all of its explicitly
/// requested targets (or the configuration's default targets when no targets are specified) are contained in this set.
/// Used to enforce isolation constraints deterministically: a cross-project reference in an isolated build may only be
/// satisfied from the cache for targets that were explicitly requested (declared via ProjectReferenceTargets and built by
/// the static graph), not for targets that merely happen to have results because they ran as a dependency of another target.</param>
/// <returns>A response indicating the results, if any, and the targets needing to be built, if any.</returns>
ResultsCacheResponse SatisfyRequest(BuildRequest request, List<string> configInitialTargets, List<string> configDefaultTargets, bool skippedResultsDoNotCauseCacheMiss);
ResultsCacheResponse SatisfyRequest(BuildRequest request, List<string> configInitialTargets, List<string> configDefaultTargets, bool skippedResultsDoNotCauseCacheMiss, IReadOnlyCollection<string> allowedTopLevelTargets);

/// <summary>
/// Clears the results for a specific configuration.
Expand Down
43 changes: 42 additions & 1 deletion src/Build/BackEnd/Components/Caching/ResultsCache.cs
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@
using System.Collections;
using System.Collections.Concurrent;
using System.Collections.Generic;
using Microsoft.Build.Collections;
using Microsoft.Build.Execution;

#nullable disable
Expand Down Expand Up @@ -157,12 +158,36 @@ public BuildResult GetResultsForConfiguration(int configurationId)
/// If true, then as long as there is a result in the cache (regardless of whether it was skipped or not), this method
/// will return "Satisfied". In most cases this should be false, but it may be set to true in a situation where there is no
/// chance of re-execution (which is the usual response to missing / skipped targets), and the caller just needs the data.</param>
/// <param name="allowedTopLevelTargets">When non-null, the request is only considered satisfiable if all of its explicitly
/// requested targets (or the configuration's default targets when no targets are specified) are contained in this set.
/// Used to enforce isolation constraints deterministically for cross-project references in isolated builds.</param>
/// <returns>A response indicating the results, if any, and the targets needing to be built, if any.</returns>
public ResultsCacheResponse SatisfyRequest(BuildRequest request, List<string> configInitialTargets, List<string> configDefaultTargets, bool skippedResultsDoNotCauseCacheMiss)
public ResultsCacheResponse SatisfyRequest(BuildRequest request, List<string> configInitialTargets, List<string> configDefaultTargets, bool skippedResultsDoNotCauseCacheMiss, IReadOnlyCollection<string> allowedTopLevelTargets)
{
Assumed.True(request.IsConfigurationResolved, "UnresolvedConfigurationInRequest");
ResultsCacheResponse response = new(ResultsCacheResponseType.NotSatisfied);

// In isolated builds a cross-project reference may only be satisfied from the cache for targets that were
// explicitly requested for the referenced configuration (i.e. declared via ProjectReferenceTargets and built
// by the static graph). A target that merely has a cached result because it ran as a dependency of another
// target must not satisfy the reference, otherwise the build result would depend on node scheduling (whether
// the dependency happened to run on the same node). Treat such requests as a cache miss so the isolation
// constraint check runs and deterministically reports the incomplete graph.
if (allowedTopLevelTargets != null)
{
List<string> targetsToCheck = request.Targets.Count > 0 ? request.Targets : configDefaultTargets;
if (targetsToCheck != null)
{
foreach (string target in targetsToCheck)
{
if (!ContainsIgnoreCase(allowedTopLevelTargets, target))
{
return response;
}
}
}
}

lock (_resultsByConfiguration)
{
if (_resultsByConfiguration.TryGetValue(request.ConfigurationId, out BuildResult allResults))
Expand Down Expand Up @@ -295,6 +320,22 @@ internal static IBuildComponent CreateComponent(BuildComponentType componentType
return new ResultsCache();
}

/// <summary>
/// Determines whether the given collection contains the specified target name, ignoring case.
/// </summary>
private static bool ContainsIgnoreCase(IReadOnlyCollection<string> targets, string target)
{
foreach (string candidate in targets)
{
if (MSBuildNameIgnoreCaseComparer.Default.Equals(candidate, target))
{
return true;
}
}

return false;
}

/// <summary>
/// Looks for results for the specified targets.
/// </summary>
Expand Down
Loading