From ea28eb30eb94f4c835e77e2533cd8e304aaf9987 Mon Sep 17 00:00:00 2001 From: "copilot-swe-agent[bot]" <198982749+Copilot@users.noreply.github.com> Date: Tue, 28 Jul 2026 14:25:18 +0000 Subject: [PATCH 1/7] Initial plan From 15ee31d323910aabb8a84db30baae1a708d287d7 Mon Sep 17 00:00:00 2001 From: "copilot-swe-agent[bot]" <198982749+Copilot@users.noreply.github.com> Date: Tue, 28 Jul 2026 14:37:38 +0000 Subject: [PATCH 2/7] Include the resolved change wave in the node handshake salt Co-authored-by: JanProvaznik <25267098+JanProvaznik@users.noreply.github.com> --- src/Framework/BackEnd/Handshake.cs | 41 +++++++++++++++++++++++++----- 1 file changed, 34 insertions(+), 7 deletions(-) diff --git a/src/Framework/BackEnd/Handshake.cs b/src/Framework/BackEnd/Handshake.cs index a8dbf87984f..ca1529f6f12 100644 --- a/src/Framework/BackEnd/Handshake.cs +++ b/src/Framework/BackEnd/Handshake.cs @@ -92,7 +92,13 @@ protected Handshake(HandshakeOptions nodeType, bool includeSessionId, string? to // Calculate salt from environment and tools directory string handshakeSalt = Environment.GetEnvironmentVariable("MSBUILDNODEHANDSHAKESALT") ?? ""; - int salt = CommunicationsUtilities.GetHashCode($"{handshakeSalt}{toolsDirectory}"); + // A node resolves its change wave once and keeps it for the rest of its lifetime, so a node reused + // by a later build would silently keep applying the change wave of the build that started it. Fold + // the change wave into the salt so that a node which resolved a different change wave rejects the + // connection and the parent starts a fresh node instead. + string changeWaveSalt = GetChangeWaveSalt(); + + int salt = CommunicationsUtilities.GetHashCode($"{handshakeSalt}{toolsDirectory}{changeWaveSalt}"); // The .NET task host parent (.NET Framework MSBuild, e.g. Visual Studio) and child // (.NET SDK MSBuild) compute this salt independently from different sources, and on Windows @@ -102,10 +108,11 @@ protected Handshake(HandshakeOptions nodeType, bool includeSessionId, string? to // the parent's spelling (see IsNetTaskHostSaltMatch). Only the child consults this value. if (IsNetTaskHost && TryGetDriveLetterCaseVariant(toolsDirectory, out string? alternateToolsDirectory)) { - _netTaskHostSaltDriveCaseVariant = CommunicationsUtilities.GetHashCode($"{handshakeSalt}{alternateToolsDirectory}"); + _netTaskHostSaltDriveCaseVariant = CommunicationsUtilities.GetHashCode($"{handshakeSalt}{alternateToolsDirectory}{changeWaveSalt}"); } CommunicationsUtilities.Trace($"Handshake salt is {handshakeSalt}"); + CommunicationsUtilities.Trace($"Change wave salt is {changeWaveSalt}"); CommunicationsUtilities.Trace($"Tools directory root is {toolsDirectory}"); // Get session ID if needed (expensive call) @@ -128,6 +135,31 @@ protected Handshake(HandshakeOptions nodeType, bool includeSessionId, string? to private bool IsNetTaskHost => IsHandshakeOptionEnabled(HandshakeOptions, HandshakeOptions.NET | HandshakeOptions.TaskHost); + private bool IsClr2TaskHost + => IsHandshakeOptionEnabled(HandshakeOptions, HandshakeOptions.CLR2 | HandshakeOptions.TaskHost); + + /// + /// The part of the handshake salt that represents the change wave this process resolved, so that nodes + /// which disagree about MSBUILDDISABLEFEATURESFROMVERSION never talk to each other. The resolved + /// wave is used rather than the raw environment variable so that values which mean the same thing (for + /// example an unset variable and one that is out of rotation) still allow node reuse. + /// Returns an empty string when no change wave is disabled, which is both the default and the case where + /// there is nothing to segregate, so the salt stays identical to what previous versions computed. + /// Also returns an empty string for the CLR2 task host, whose separate legacy handshake implementation in + /// MSBuildTaskHost.exe does not know about change waves; including it here would break that connection. + /// + private string GetChangeWaveSalt() + { + if (IsClr2TaskHost) + { + return string.Empty; + } + + Version disabledWave = ChangeWaves.DisabledWave; + + return disabledWave == ChangeWaves.EnableAllFeatures ? string.Empty : disabledWave.ToString(); + } + /// /// Determines whether matches this node's salt computed for the /// alternate drive-letter casing of its tools directory. Used only by the .NET task host child to @@ -169,11 +201,6 @@ private static bool TryGetDriveLetterCaseVariant(string? path, out string? varia return true; } -#if NETFRAMEWORK - private bool IsClr2TaskHost - => IsHandshakeOptionEnabled(HandshakeOptions, HandshakeOptions.CLR2 | HandshakeOptions.TaskHost); -#endif - private static HandshakeComponents CreateNetTaskHostComponents(int options, int salt, int sessionId) => new( options, salt, From 032539d7e97be89fc0259784f5472c763a50e32e Mon Sep 17 00:00:00 2001 From: "copilot-swe-agent[bot]" <198982749+Copilot@users.noreply.github.com> Date: Tue, 28 Jul 2026 14:47:05 +0000 Subject: [PATCH 3/7] Add handshake regression tests and document change wave node segregation Co-authored-by: JanProvaznik <25267098+JanProvaznik@users.noreply.github.com> --- documentation/wiki/ChangeWaves.md | 4 + .../BackEnd/Handshake_Tests.cs | 134 ++++++++++++++++++ 2 files changed, 138 insertions(+) create mode 100644 src/Build.UnitTests/BackEnd/Handshake_Tests.cs diff --git a/documentation/wiki/ChangeWaves.md b/documentation/wiki/ChangeWaves.md index 13f4a8b5666..b291e6ab5c9 100644 --- a/documentation/wiki/ChangeWaves.md +++ b/documentation/wiki/ChangeWaves.md @@ -10,6 +10,10 @@ The opt-out comes in the form of setting the environment variable `MSBUILDDISABL The opt-out should be just a *temporary* workaround for a problem - as the feature will anyways become permanent eventually. For this reason - **please make sure to create or upvote a bug describing the issue making you opt-out**. +### Change waves and reused processes + +`MSBUILDDISABLEFEATURESFROMVERSION` is read once per process, so a process that outlives a single build - a reused worker node, a task host, or an MSBuild Server node - keeps the change wave it started with. To make sure every process taking part in a build agrees on the change wave, the resolved change wave is part of the node handshake: a node that resolved a different change wave refuses the connection and MSBuild starts a fresh one instead. Changing `MSBUILDDISABLEFEATURESFROMVERSION` between builds therefore starts new nodes rather than reusing the existing ones. Values that resolve to the same change wave (for example an unset variable and a value in an invalid format) still allow node reuse. + ## When do they become permanent? A wave of features is eligible to "rotate out" (i.e. become standard functionality) six months after its release. For example, wave 16.8 stayed opt-out through wave 16.10, becoming standard functionality when wave 17.0 is introduced. diff --git a/src/Build.UnitTests/BackEnd/Handshake_Tests.cs b/src/Build.UnitTests/BackEnd/Handshake_Tests.cs new file mode 100644 index 00000000000..b4c62161099 --- /dev/null +++ b/src/Build.UnitTests/BackEnd/Handshake_Tests.cs @@ -0,0 +1,134 @@ +// Licensed to the .NET Foundation under one or more agreements. +// The .NET Foundation licenses this file to you under the MIT license. + +using System; +using Microsoft.Build.Framework; +using Microsoft.Build.Internal; +using Microsoft.Build.Shared; +using Microsoft.Build.UnitTests; +using Shouldly; +using Xunit; + +namespace Microsoft.Build.Engine.UnitTests.BackEnd +{ + /// + /// Tests that the change wave a process resolved participates in the handshake. A node caches its + /// change wave for its whole lifetime, so a node reused by a later build would otherwise keep + /// applying the change wave of the build that started it. + /// + public sealed class Handshake_Tests : IDisposable + { + private const string DisableFeaturesFromVersion = "MSBUILDDISABLEFEATURESFROMVERSION"; + + private static readonly HandshakeOptions s_workerNodeOptions = CommunicationsUtilities.GetHandshakeOptions( + taskHost: false, + taskHostParameters: TaskHostParameters.Empty); + + private readonly ITestOutputHelper _output; + + public Handshake_Tests(ITestOutputHelper output) => _output = output; + + /// + /// The change wave is cached statically, so drop what these tests cached once the environment + /// variable has been restored. + /// + public void Dispose() => ChangeWaves.ResetStateForTests(); + + /// + /// Change waves are cached statically, so the cache has to be dropped whenever the environment + /// variable driving it changes. + /// + private static void SetChangeWave(string? wave, TestEnvironment env) + { + ChangeWaves.ResetStateForTests(); + env.SetEnvironmentVariable(DisableFeaturesFromVersion, wave); + } + + private static string GetKeyForChangeWave(string? wave, TestEnvironment env, HandshakeOptions options) + { + SetChangeWave(wave, env); + + return new Handshake(options).GetKey(); + } + + [Fact] + public void DifferentChangeWavesProduceDifferentHandshakes() + { + using TestEnvironment env = TestEnvironment.Create(_output); + + string firstKey = GetKeyForChangeWave(ChangeWaves.Wave17_12.ToString(), env, s_workerNodeOptions); + string secondKey = GetKeyForChangeWave(ChangeWaves.HighestWave.ToString(), env, s_workerNodeOptions); + + secondKey.ShouldNotBe(firstKey); + } + + [Fact] + public void SameChangeWaveProducesTheSameHandshake() + { + using TestEnvironment env = TestEnvironment.Create(_output); + + string firstKey = GetKeyForChangeWave(ChangeWaves.Wave17_12.ToString(), env, s_workerNodeOptions); + string secondKey = GetKeyForChangeWave(ChangeWaves.Wave17_12.ToString(), env, s_workerNodeOptions); + + secondKey.ShouldBe(firstKey); + } + + /// + /// Values that resolve to the same change wave have to keep producing the same handshake, otherwise + /// nodes that behave identically would needlessly refuse to talk to each other. + /// + [Theory] + [InlineData("")] + [InlineData("999.999")] + [InlineData("not a version")] + public void EquivalentChangeWaveValuesProduceTheSameHandshake(string wave) + { + using TestEnvironment env = TestEnvironment.Create(_output); + + string unsetKey = GetKeyForChangeWave(null, env, s_workerNodeOptions); + string equivalentKey = GetKeyForChangeWave(wave, env, s_workerNodeOptions); + + equivalentKey.ShouldBe(unsetKey); + } + + /// + /// MSBuild Server clients find their server by a pipe name derived from the handshake, so a server + /// that resolved a different change wave must not be reused. + /// + [Fact] + public void DifferentChangeWavesProduceDifferentServerNodeHandshakes() + { + using TestEnvironment env = TestEnvironment.Create(_output); + + HandshakeOptions options = CommunicationsUtilities.GetHandshakeOptions( + taskHost: false, + taskHostParameters: TaskHostParameters.Empty, + architectureFlagToSet: XMakeAttributes.GetCurrentMSBuildArchitecture()); + + SetChangeWave(ChangeWaves.Wave17_12.ToString(), env); + string firstHash = new ServerNodeHandshake(options).ComputeHash(); + + SetChangeWave(ChangeWaves.HighestWave.ToString(), env); + string secondHash = new ServerNodeHandshake(options).ComputeHash(); + + secondHash.ShouldNotBe(firstHash); + } + + /// + /// The CLR2 task host computes its handshake with its own copy of this code in MSBuildTaskHost.exe, + /// which knows nothing about change waves, so it must be left out. + /// + [Fact] + public void Clr2TaskHostHandshakeIgnoresChangeWave() + { + using TestEnvironment env = TestEnvironment.Create(_output); + + HandshakeOptions options = HandshakeOptions.CLR2 | HandshakeOptions.TaskHost; + + string firstKey = GetKeyForChangeWave(ChangeWaves.Wave17_12.ToString(), env, options); + string secondKey = GetKeyForChangeWave(ChangeWaves.HighestWave.ToString(), env, options); + + secondKey.ShouldBe(firstKey); + } + } +} From 770a6069bded86186419fd9f4d4e87ba95466791 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Jan=20Provazn=C3=ADk?= Date: Tue, 4 Aug 2026 14:59:53 +0200 Subject: [PATCH 4/7] Potential fix for pull request finding Co-authored-by: Copilot Autofix powered by AI <175728472+Copilot@users.noreply.github.com> --- src/Build.UnitTests/BackEnd/Handshake_Tests.cs | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/Build.UnitTests/BackEnd/Handshake_Tests.cs b/src/Build.UnitTests/BackEnd/Handshake_Tests.cs index b4c62161099..37579116631 100644 --- a/src/Build.UnitTests/BackEnd/Handshake_Tests.cs +++ b/src/Build.UnitTests/BackEnd/Handshake_Tests.cs @@ -56,7 +56,7 @@ public void DifferentChangeWavesProduceDifferentHandshakes() { using TestEnvironment env = TestEnvironment.Create(_output); - string firstKey = GetKeyForChangeWave(ChangeWaves.Wave17_12.ToString(), env, s_workerNodeOptions); + string firstKey = GetKeyForChangeWave(ChangeWaves.LowestWave.ToString(), env, s_workerNodeOptions); string secondKey = GetKeyForChangeWave(ChangeWaves.HighestWave.ToString(), env, s_workerNodeOptions); secondKey.ShouldNotBe(firstKey); From ae6ca1040489da9e19bc8e9f7d46831e0b775f0c Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Jan=20Provazn=C3=ADk?= Date: Tue, 4 Aug 2026 15:00:03 +0200 Subject: [PATCH 5/7] Potential fix for pull request finding Co-authored-by: Copilot Autofix powered by AI <175728472+Copilot@users.noreply.github.com> --- src/Framework/BackEnd/Handshake.cs | 5 ++--- 1 file changed, 2 insertions(+), 3 deletions(-) diff --git a/src/Framework/BackEnd/Handshake.cs b/src/Framework/BackEnd/Handshake.cs index ca1529f6f12..71fd03c3d26 100644 --- a/src/Framework/BackEnd/Handshake.cs +++ b/src/Framework/BackEnd/Handshake.cs @@ -141,9 +141,8 @@ private bool IsClr2TaskHost /// /// The part of the handshake salt that represents the change wave this process resolved, so that nodes /// which disagree about MSBUILDDISABLEFEATURESFROMVERSION never talk to each other. The resolved - /// wave is used rather than the raw environment variable so that values which mean the same thing (for - /// example an unset variable and one that is out of rotation) still allow node reuse. - /// Returns an empty string when no change wave is disabled, which is both the default and the case where + /// wave is used rather than the raw environment variable so that values which resolve to the same wave + /// (for example an unset variable and one in an invalid format) still allow node reuse. /// there is nothing to segregate, so the salt stays identical to what previous versions computed. /// Also returns an empty string for the CLR2 task host, whose separate legacy handshake implementation in /// MSBuildTaskHost.exe does not know about change waves; including it here would break that connection. From 4a2436965904cbe922290aa10ab2fa717a0c9cd3 Mon Sep 17 00:00:00 2001 From: "copilot-swe-agent[bot]" <198982749+Copilot@users.noreply.github.com> Date: Tue, 4 Aug 2026 13:18:54 +0000 Subject: [PATCH 6/7] Fix handshake test wave references Co-authored-by: JanProvaznik <25267098+JanProvaznik@users.noreply.github.com> --- src/Build.UnitTests/BackEnd/Handshake_Tests.cs | 8 ++++---- src/Framework/BackEnd/Handshake.cs | 1 - 2 files changed, 4 insertions(+), 5 deletions(-) diff --git a/src/Build.UnitTests/BackEnd/Handshake_Tests.cs b/src/Build.UnitTests/BackEnd/Handshake_Tests.cs index 37579116631..d6e6d5f8109 100644 --- a/src/Build.UnitTests/BackEnd/Handshake_Tests.cs +++ b/src/Build.UnitTests/BackEnd/Handshake_Tests.cs @@ -67,8 +67,8 @@ public void SameChangeWaveProducesTheSameHandshake() { using TestEnvironment env = TestEnvironment.Create(_output); - string firstKey = GetKeyForChangeWave(ChangeWaves.Wave17_12.ToString(), env, s_workerNodeOptions); - string secondKey = GetKeyForChangeWave(ChangeWaves.Wave17_12.ToString(), env, s_workerNodeOptions); + string firstKey = GetKeyForChangeWave(ChangeWaves.LowestWave.ToString(), env, s_workerNodeOptions); + string secondKey = GetKeyForChangeWave(ChangeWaves.LowestWave.ToString(), env, s_workerNodeOptions); secondKey.ShouldBe(firstKey); } @@ -105,7 +105,7 @@ public void DifferentChangeWavesProduceDifferentServerNodeHandshakes() taskHostParameters: TaskHostParameters.Empty, architectureFlagToSet: XMakeAttributes.GetCurrentMSBuildArchitecture()); - SetChangeWave(ChangeWaves.Wave17_12.ToString(), env); + SetChangeWave(ChangeWaves.LowestWave.ToString(), env); string firstHash = new ServerNodeHandshake(options).ComputeHash(); SetChangeWave(ChangeWaves.HighestWave.ToString(), env); @@ -125,7 +125,7 @@ public void Clr2TaskHostHandshakeIgnoresChangeWave() HandshakeOptions options = HandshakeOptions.CLR2 | HandshakeOptions.TaskHost; - string firstKey = GetKeyForChangeWave(ChangeWaves.Wave17_12.ToString(), env, options); + string firstKey = GetKeyForChangeWave(ChangeWaves.LowestWave.ToString(), env, options); string secondKey = GetKeyForChangeWave(ChangeWaves.HighestWave.ToString(), env, options); secondKey.ShouldBe(firstKey); diff --git a/src/Framework/BackEnd/Handshake.cs b/src/Framework/BackEnd/Handshake.cs index 71fd03c3d26..9df9ac93f2f 100644 --- a/src/Framework/BackEnd/Handshake.cs +++ b/src/Framework/BackEnd/Handshake.cs @@ -143,7 +143,6 @@ private bool IsClr2TaskHost /// which disagree about MSBUILDDISABLEFEATURESFROMVERSION never talk to each other. The resolved /// wave is used rather than the raw environment variable so that values which resolve to the same wave /// (for example an unset variable and one in an invalid format) still allow node reuse. - /// there is nothing to segregate, so the salt stays identical to what previous versions computed. /// Also returns an empty string for the CLR2 task host, whose separate legacy handshake implementation in /// MSBuildTaskHost.exe does not know about change waves; including it here would break that connection. /// From 1152edd319804e31886619abb904c67275553d75 Mon Sep 17 00:00:00 2001 From: Jan Provaznik Date: Tue, 4 Aug 2026 17:58:47 +0200 Subject: [PATCH 7/7] Exclude all task hosts from the change wave handshake salt A task host connection is the one handshake that can legitimately span two MSBuild versions, and ChangeWaves.DisabledWave is a version-relative value: it clamps and rounds MSBUILDDISABLEFEATURESFROMVERSION into the binary's own AllWaves list, which differs between versions. A .NET Framework parent (e.g. Visual Studio) talking to a .NET task host taken from the installed SDK can therefore resolve the same variable to different waves on either side. Unlike a worker node or an MSBuild Server node, a task host mismatch is not self-healing: the parent can only relaunch the same SDK binary, so the build fails with MSB4216 rather than starting a compatible host. Widen the existing CLR2 carve-out to every task host and restore IsClr2TaskHost to its original NETFRAMEWORK-only form. Co-authored-by: Copilot <223556219+Copilot@users.noreply.github.com> Copilot-Session: dc08a50a-edbe-43e1-9057-1f78cc75922a --- documentation/wiki/ChangeWaves.md | 4 +- .../BackEnd/Handshake_Tests.cs | 39 ++++++++++++++++--- src/Framework/BackEnd/Handshake.cs | 26 ++++++++++--- 3 files changed, 57 insertions(+), 12 deletions(-) diff --git a/documentation/wiki/ChangeWaves.md b/documentation/wiki/ChangeWaves.md index a48005b3c07..3115536c667 100644 --- a/documentation/wiki/ChangeWaves.md +++ b/documentation/wiki/ChangeWaves.md @@ -12,7 +12,9 @@ The opt-out should be just a *temporary* workaround for a problem - as the featu ### Change waves and reused processes -`MSBUILDDISABLEFEATURESFROMVERSION` is read once per process, so a process that outlives a single build - a reused worker node, a task host, or an MSBuild Server node - keeps the change wave it started with. To make sure every process taking part in a build agrees on the change wave, the resolved change wave is part of the node handshake: a node that resolved a different change wave refuses the connection and MSBuild starts a fresh one instead. Changing `MSBUILDDISABLEFEATURESFROMVERSION` between builds therefore starts new nodes rather than reusing the existing ones. Values that resolve to the same change wave (for example an unset variable and a value in an invalid format) still allow node reuse. +`MSBUILDDISABLEFEATURESFROMVERSION` is read once per process, so a process that outlives a single build - a reused worker node or an MSBuild Server node - keeps the change wave it started with. To make sure every process taking part in a build agrees on the change wave, the resolved change wave is part of the node handshake: a node that resolved a different change wave refuses the connection and MSBuild starts a fresh one instead. Changing `MSBUILDDISABLEFEATURESFROMVERSION` between builds therefore starts new nodes rather than reusing the existing ones. Values that resolve to the same change wave (for example an unset variable and a value in an invalid format) still allow node reuse. + +Task hosts are deliberately left out of this. A task host connection can span two different MSBuild versions - most notably the .NET task host, where a .NET Framework parent such as Visual Studio talks to a child taken from the installed .NET SDK - and the resolved change wave is version-relative, because the environment variable is clamped and rounded into the wave list of whichever binary reads it. Two versions can therefore resolve the same variable to different waves. Unlike a worker node, a task host that fails the handshake cannot be replaced by a compatible one, since the parent can only relaunch the very same executable, so the build would fail with MSB4216. A task host consequently keeps the change wave it started with even across builds. ## When do they become permanent? diff --git a/src/Build.UnitTests/BackEnd/Handshake_Tests.cs b/src/Build.UnitTests/BackEnd/Handshake_Tests.cs index d6e6d5f8109..f1c9457df8d 100644 --- a/src/Build.UnitTests/BackEnd/Handshake_Tests.cs +++ b/src/Build.UnitTests/BackEnd/Handshake_Tests.cs @@ -115,20 +115,49 @@ public void DifferentChangeWavesProduceDifferentServerNodeHandshakes() } /// - /// The CLR2 task host computes its handshake with its own copy of this code in MSBuildTaskHost.exe, - /// which knows nothing about change waves, so it must be left out. + /// A task host connection is the one place where the two ends can be different MSBuild versions, and + /// the resolved wave is version-relative: clamps and rounds the + /// environment variable into that binary's own wave list. The CLR2 task host additionally computes its + /// handshake from a separate legacy copy of this code that knows nothing about change waves. Unlike a + /// worker node, a task host mismatch cannot be recovered from by starting a different host, so no task + /// host may take the change wave into account. /// - [Fact] - public void Clr2TaskHostHandshakeIgnoresChangeWave() + [Theory] + [InlineData((int)(HandshakeOptions.TaskHost | HandshakeOptions.CLR2))] + [InlineData((int)(HandshakeOptions.TaskHost | HandshakeOptions.NET))] + [InlineData((int)(HandshakeOptions.TaskHost | HandshakeOptions.NET | HandshakeOptions.X64))] + [InlineData((int)(HandshakeOptions.TaskHost | HandshakeOptions.NET | HandshakeOptions.SidecarTaskHost))] + [InlineData((int)HandshakeOptions.TaskHost)] + public void TaskHostHandshakeIgnoresChangeWave(int taskHostOptions) { using TestEnvironment env = TestEnvironment.Create(_output); - HandshakeOptions options = HandshakeOptions.CLR2 | HandshakeOptions.TaskHost; + var options = (HandshakeOptions)taskHostOptions; string firstKey = GetKeyForChangeWave(ChangeWaves.LowestWave.ToString(), env, options); string secondKey = GetKeyForChangeWave(ChangeWaves.HighestWave.ToString(), env, options); secondKey.ShouldBe(firstKey); } + + /// + /// The .NET task host is the connection at risk: a .NET Framework parent (e.g. Visual Studio) talks to + /// a child taken from the installed .NET SDK, which resolves the same environment variable against a + /// different wave list. Its handshake has to stay identical to what a version without change wave + /// salting computes, otherwise the build fails with MSB4216 and the parent can only relaunch the same + /// SDK binary rather than start a compatible host. + /// + [Fact] + public void NetTaskHostHandshakeIsUnaffectedByChangeWave() + { + using TestEnvironment env = TestEnvironment.Create(_output); + + HandshakeOptions options = HandshakeOptions.TaskHost | HandshakeOptions.NET; + + string unsetKey = GetKeyForChangeWave(null, env, options); + string waveSetKey = GetKeyForChangeWave(ChangeWaves.LowestWave.ToString(), env, options); + + waveSetKey.ShouldBe(unsetKey); + } } } diff --git a/src/Framework/BackEnd/Handshake.cs b/src/Framework/BackEnd/Handshake.cs index 9df9ac93f2f..22b5c167d80 100644 --- a/src/Framework/BackEnd/Handshake.cs +++ b/src/Framework/BackEnd/Handshake.cs @@ -95,7 +95,7 @@ protected Handshake(HandshakeOptions nodeType, bool includeSessionId, string? to // A node resolves its change wave once and keeps it for the rest of its lifetime, so a node reused // by a later build would silently keep applying the change wave of the build that started it. Fold // the change wave into the salt so that a node which resolved a different change wave rejects the - // connection and the parent starts a fresh node instead. + // connection and the parent starts a fresh node instead. Task hosts are excluded, see GetChangeWaveSalt. string changeWaveSalt = GetChangeWaveSalt(); int salt = CommunicationsUtilities.GetHashCode($"{handshakeSalt}{toolsDirectory}{changeWaveSalt}"); @@ -135,20 +135,29 @@ protected Handshake(HandshakeOptions nodeType, bool includeSessionId, string? to private bool IsNetTaskHost => IsHandshakeOptionEnabled(HandshakeOptions, HandshakeOptions.NET | HandshakeOptions.TaskHost); - private bool IsClr2TaskHost - => IsHandshakeOptionEnabled(HandshakeOptions, HandshakeOptions.CLR2 | HandshakeOptions.TaskHost); + private bool IsTaskHost + => IsHandshakeOptionEnabled(HandshakeOptions, HandshakeOptions.TaskHost); /// /// The part of the handshake salt that represents the change wave this process resolved, so that nodes /// which disagree about MSBUILDDISABLEFEATURESFROMVERSION never talk to each other. The resolved /// wave is used rather than the raw environment variable so that values which resolve to the same wave /// (for example an unset variable and one in an invalid format) still allow node reuse. - /// Also returns an empty string for the CLR2 task host, whose separate legacy handshake implementation in - /// MSBuildTaskHost.exe does not know about change waves; including it here would break that connection. + /// + /// Returns an empty string for every task host, because a task host connection is the one place where the + /// two sides can be different MSBuild versions and the resolved wave is a version-relative value: + /// clamps and rounds the environment variable into the binary's own + /// AllWaves list, which differs between versions. The CLR2 task host computes its handshake from a + /// separate legacy copy of this code in MSBuildTaskHost.exe that knows nothing about change waves, and the + /// .NET task host pairs a .NET Framework parent (e.g. Visual Studio) with a child from the installed .NET + /// SDK, so the same variable can legitimately resolve to different waves on either side. Unlike a worker + /// node or an MSBuild Server node, a task host mismatch is not self-healing: the parent can only relaunch + /// the very same SDK binary, so the build fails with MSB4216 instead of starting a compatible host. + /// /// private string GetChangeWaveSalt() { - if (IsClr2TaskHost) + if (IsTaskHost) { return string.Empty; } @@ -199,6 +208,11 @@ private static bool TryGetDriveLetterCaseVariant(string? path, out string? varia return true; } +#if NETFRAMEWORK + private bool IsClr2TaskHost + => IsHandshakeOptionEnabled(HandshakeOptions, HandshakeOptions.CLR2 | HandshakeOptions.TaskHost); +#endif + private static HandshakeComponents CreateNetTaskHostComponents(int options, int salt, int sessionId) => new( options, salt,