Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
Show all changes
19 commits
Select commit Hold shift + click to select a range
68d2621
Adding Strategies for node 24 to node 6 | Adding Interfaces created f…
rishabhmalikMS Dec 8, 2025
0854386
Merge branch 'master' into users/rishabhmalikMS/NodehandlerStrategies
rishabhmalikMS Dec 8, 2025
e3b655a
adding EnableEOLNodeVersionPolicy knob
rishabhmalikMS Dec 8, 2025
32e38b9
Merge branch 'users/rishabhmalikMS/NodehandlerStrategies' of https://…
rishabhmalikMS Dec 8, 2025
11d1b64
adding agent knob AGENT_RESTRICT_EOL_NODE_VERSIONS
rishabhmalikMS Dec 8, 2025
b5a899b
Minor fix
rishabhmalikMS Dec 9, 2025
a52fbdf
Adding localized strings in string.json.
rishabhmalikMS Dec 9, 2025
3e73fe2
Added NodeVersionNotAvailable string
rishabhmalikMS Dec 9, 2025
baec4a9
Added IUnifiedNodeVersionStrategy in ServiceInterfaceL0 test
rishabhmalikMS Dec 9, 2025
79110c8
Removed NodeVersionNotAvailable string temporarily.
rishabhmalikMS Dec 9, 2025
7ae51f0
Updating nomenclature
rishabhmalikMS Dec 10, 2025
e785a37
- Add NodeVersionOrchestrator to centralize Node.js version selection…
rishabhmalikMS Dec 11, 2025
90b4c7a
_ Added integration for node strategy orchestrator in nodehandler to …
rishabhmalikMS Dec 15, 2025
61fb138
Merge branch 'master' into users/rishabhmalikMS/NodehandlerStrategies
rishabhmalikMS Dec 15, 2025
04ee161
- Consolidate glibc functionality into single provider class
rishabhmalikMS Dec 15, 2025
99ab6bd
Added comment in orchestrator for priorities of strategies which is u…
rishabhmalikMS Dec 15, 2025
92584a2
- Added check for only performing glibc compatibility checks on Linux…
rishabhmalikMS Dec 16, 2025
0cc45e3
- Replace hardcoded string literals with NodeVersion enum across all …
rishabhmalikMS Dec 17, 2025
8f937b1
Minor update to log stack trace
rishabhmalikMS Dec 17, 2025
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
66 changes: 65 additions & 1 deletion src/Agent.Worker/Handlers/NodeHandler.cs
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@
using Agent.Sdk;
using Agent.Sdk.Knob;
using Microsoft.VisualStudio.Services.Agent.Util;
using Microsoft.VisualStudio.Services.Agent.Worker.NodeVersionStrategies;
using System.IO;
using System.Text;
using System.Threading.Tasks;
Expand Down Expand Up @@ -55,6 +56,7 @@ public string[] GetFilteredPossibleNodeFolders(string nodeFolderName, string[] p
public sealed class NodeHandler : Handler, INodeHandler
{
private readonly INodeHandlerHelper nodeHandlerHelper;
private readonly Lazy<NodeVersionOrchestrator> nodeVersionOrchestrator;
private const string Node10Folder = "node10";
internal const string NodeFolder = "node";
internal static readonly string Node16Folder = "node16";
Expand Down Expand Up @@ -89,11 +91,15 @@ public sealed class NodeHandler : Handler, INodeHandler
public NodeHandler()
{
this.nodeHandlerHelper = new NodeHandlerHelper();
this.nodeVersionOrchestrator = new Lazy<NodeVersionOrchestrator>(() =>
new NodeVersionOrchestrator(ExecutionContext, HostContext));
}

public NodeHandler(INodeHandlerHelper nodeHandlerHelper)
{
this.nodeHandlerHelper = nodeHandlerHelper;
this.nodeVersionOrchestrator = new Lazy<NodeVersionOrchestrator>(() =>
new NodeVersionOrchestrator(ExecutionContext, HostContext));
}

public BaseNodeHandlerData Data { get; set; }
Expand Down Expand Up @@ -361,8 +367,44 @@ private string GetNodeFolderWithFallback(string preferredNodeFolder, bool node20
}
}


public string GetNodeLocation(bool node20ResultsInGlibCError, bool node24ResultsInGlibCError, bool inContainer)
{
bool useStrategyPattern = AgentKnobs.UseNodeVersionStrategy.GetValue(ExecutionContext).AsBoolean();

// Publish telemetry to track strategy pattern vs legacy code usage
PublishNodeSelectionMethodTelemetry(useStrategyPattern, inContainer);

if (useStrategyPattern)
{
return GetNodeLocationUsingStrategy(inContainer).GetAwaiter().GetResult();
}

return GetNodeLocationLegacy(node20ResultsInGlibCError, node24ResultsInGlibCError, inContainer);
}

private async Task<string> GetNodeLocationUsingStrategy(bool inContainer)
{
try
{
var taskContext = new TaskContext
{
HandlerData = Data,
Container = inContainer ? (ExecutionContext.StepTarget() as ContainerInfo) : null,
StepTarget = inContainer ? null : ExecutionContext.StepTarget()
};

NodeRunnerInfo result = await nodeVersionOrchestrator.Value.SelectNodeVersionAsync(taskContext);
return result.NodePath;
}
catch (Exception ex)
{
ExecutionContext.Error($"Strategy-based node selection failed: {ex.Message}");
ExecutionContext.Debug($"Stack trace: {ex}");
throw;
}
}

private string GetNodeLocationLegacy(bool node20ResultsInGlibCError, bool node24ResultsInGlibCError, bool inContainer)
{
bool useNode10 = AgentKnobs.UseNode10.GetValue(ExecutionContext).AsBoolean();
bool useNode20_1 = AgentKnobs.UseNode20_1.GetValue(ExecutionContext).AsBoolean();
Expand Down Expand Up @@ -649,5 +691,27 @@ private void PublishHandlerTelemetry(string realHandler)
};
ExecutionContext.PublishTaskRunnerTelemetry(telemetryData);
}

private void PublishNodeSelectionMethodTelemetry(bool useStrategyPattern, bool inContainer)
{
try
{
var telemetryData = new Dictionary<string, string>
{
{ "UseStrategyPattern", useStrategyPattern.ToString() },
{ "SelectionMethod", useStrategyPattern ? "Strategy" : "Legacy" },
{ "IsContainer", inContainer.ToString() },
{ "JobId", ExecutionContext.Variables.System_JobId.ToString() },
{ "PlanId", ExecutionContext.Variables.Get(Constants.Variables.System.PlanId) ?? "" },
{ "AgentName", ExecutionContext.Variables.Get(Constants.Variables.Agent.Name) ?? "" }
};

ExecutionContext.PublishTaskRunnerTelemetry(telemetryData);
}
catch (Exception ex)
{
ExecutionContext.Debug($"Failed to publish node selection method telemetry: {ex.Message}");
}
}
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,40 @@
// Copyright (c) Microsoft Corporation.
// Licensed under the MIT License.

namespace Microsoft.VisualStudio.Services.Agent.Worker.NodeVersionStrategies
{
/// <summary>
/// Contains glibc compatibility information for different Node.js versions.
/// </summary>
public class GlibcCompatibilityInfo
{
/// <summary>
/// True if Node24 has glibc compatibility errors (requires glibc 2.28+).
/// </summary>
public bool Node24HasGlibcError { get; set; }

/// <summary>
/// True if Node20 has glibc compatibility errors (requires glibc 2.17+).
/// </summary>
public bool Node20HasGlibcError { get; set; }

/// <summary>
/// Creates a new instance with no glibc errors (compatible system).
/// </summary>
public static GlibcCompatibilityInfo Compatible => new GlibcCompatibilityInfo
{
Node24HasGlibcError = false,
Node20HasGlibcError = false
};

/// <summary>
/// Creates a new instance with specific compatibility information.
/// </summary>
public static GlibcCompatibilityInfo Create(bool node24HasGlibcError, bool node20HasGlibcError) =>
new GlibcCompatibilityInfo
{
Node24HasGlibcError = node24HasGlibcError,
Node20HasGlibcError = node20HasGlibcError
};
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,198 @@
// Copyright (c) Microsoft Corporation.
// Licensed under the MIT License.

using System;
using System.Collections.Generic;
using System.IO;
using System.Threading.Tasks;
using System.Threading;
using Agent.Sdk;
using Agent.Sdk.Knob;
using Microsoft.VisualStudio.Services.Agent.Util;

namespace Microsoft.VisualStudio.Services.Agent.Worker.NodeVersionStrategies
{
/// <summary>
/// Utility class for checking glibc compatibility with Node.js versions on Linux systems.
/// </summary>
public class GlibcCompatibilityInfoProvider : AgentService, IGlibcCompatibilityInfoProvider
{
private readonly IExecutionContext _executionContext;
private readonly IHostContext _hostContext;
private static bool? _supportsNode20;
private static bool? _supportsNode24;

public GlibcCompatibilityInfoProvider(IExecutionContext executionContext, IHostContext hostContext)
{
ArgUtil.NotNull(executionContext, nameof(executionContext));
ArgUtil.NotNull(hostContext, nameof(hostContext));
_executionContext = executionContext;
_hostContext = hostContext;
}

/// <summary>
/// Checks glibc compatibility for both Node20 and Node24.
/// This method combines the behavior from NodeHandler for both Node versions.
/// </summary>
/// <returns>GlibcCompatibilityInfo containing compatibility results for both Node versions</returns>
public virtual async Task<GlibcCompatibilityInfo> CheckGlibcCompatibilityAsync()
{
bool useNode20InUnsupportedSystem = AgentKnobs.UseNode20InUnsupportedSystem.GetValue(_executionContext).AsBoolean();
bool useNode24InUnsupportedSystem = AgentKnobs.UseNode24InUnsupportedSystem.GetValue(_executionContext).AsBoolean();

bool node20HasGlibcError = false;
bool node24HasGlibcError = false;

// Only perform glibc compatibility checks on Linux systems
if (!IsLinuxPlatform())
{
// Non-Linux systems (Windows, macOS) don't have glibc compatibility issues
return GlibcCompatibilityInfo.Create(node24HasGlibcError: false, node20HasGlibcError: false);
}

if (!useNode20InUnsupportedSystem)
{
if (_supportsNode20.HasValue)
{
node20HasGlibcError = !_supportsNode20.Value;
}
else
{
node20HasGlibcError = await CheckIfNodeResultsInGlibCErrorAsync("node20_1");
_executionContext.EmitHostNode20FallbackTelemetry(node20HasGlibcError);
_supportsNode20 = !node20HasGlibcError;
}
}

if (!useNode24InUnsupportedSystem)
{
if (_supportsNode24.HasValue)
{
node24HasGlibcError = !_supportsNode24.Value;
}
else
{
node24HasGlibcError = await CheckIfNodeResultsInGlibCErrorAsync("node24");
_executionContext.EmitHostNode24FallbackTelemetry(node24HasGlibcError);
_supportsNode24 = !node24HasGlibcError;
}
}

return GlibcCompatibilityInfo.Create(node24HasGlibcError, node20HasGlibcError);
}

/// <summary>
/// Gets glibc compatibility information based on the execution context (host vs container).
/// </summary>
/// <param name="context">The task context containing container and handler information</param>
/// <returns>Glibc compatibility information for the current execution environment</returns>
public virtual async Task<GlibcCompatibilityInfo> GetGlibcCompatibilityAsync(TaskContext context)
{
ArgUtil.NotNull(context, nameof(context));

string environmentType = context.Container != null ? "Container" : "Host";

if (context.Container == null)
{
// Host execution - check actual glibc compatibility
var glibcInfo = await CheckGlibcCompatibilityAsync();

_executionContext.Debug($"[{environmentType}] Host glibc compatibility - Node24: {!glibcInfo.Node24HasGlibcError}, Node20: {!glibcInfo.Node20HasGlibcError}");

return glibcInfo;
}
else
{
// Container execution - use container-specific redirect information
var glibcInfo = GlibcCompatibilityInfo.Create(
node24HasGlibcError: context.Container.NeedsNode20Redirect,
node20HasGlibcError: context.Container.NeedsNode16Redirect);

_executionContext.Debug($"[{environmentType}] Container glibc compatibility - Node24: {!glibcInfo.Node24HasGlibcError}, Node20: {!glibcInfo.Node20HasGlibcError}");

return glibcInfo;
}
}

/// <summary>
/// Checks if the specified Node.js version results in glibc compatibility errors.
/// </summary>
/// <param name="nodeFolder">The node folder name (e.g., "node20_1", "node24")</param>
/// <returns>True if glibc error is detected, false otherwise</returns>
public virtual async Task<bool> CheckIfNodeResultsInGlibCErrorAsync(string nodeFolder)
{
var nodePath = Path.Combine(_hostContext.GetDirectory(WellKnownDirectory.Externals), nodeFolder, "bin", $"node{IOUtil.ExeExtension}");
List<string> nodeVersionOutput = await ExecuteCommandAsync(_executionContext, nodePath, "-v", requireZeroExitCode: false, showOutputOnFailureOnly: true);
var nodeResultsInGlibCError = WorkerUtilities.IsCommandResultGlibcError(_executionContext, nodeVersionOutput, out string nodeInfoLine);

return nodeResultsInGlibCError;
}

/// <summary>
/// Determines if the current platform is Linux. Virtual for testing override.
/// </summary>
/// <returns>True if running on Linux, false otherwise</returns>
protected virtual bool IsLinuxPlatform()
{
return PlatformUtil.HostOS == PlatformUtil.OS.Linux;
}

private async Task<List<string>> ExecuteCommandAsync(IExecutionContext context, string command, string arg, bool requireZeroExitCode, bool showOutputOnFailureOnly)
{
string commandLog = $"{command} {arg}";
if (!showOutputOnFailureOnly)
{
context.Command(commandLog);
}

List<string> outputs = new List<string>();
object outputLock = new object();
var processInvoker = _hostContext.CreateService<IProcessInvoker>();
processInvoker.OutputDataReceived += delegate (object sender, ProcessDataReceivedEventArgs message)
{
if (!string.IsNullOrEmpty(message.Data))
{
lock (outputLock)
{
outputs.Add(message.Data);
}
}
};

processInvoker.ErrorDataReceived += delegate (object sender, ProcessDataReceivedEventArgs message)
{
if (!string.IsNullOrEmpty(message.Data))
{
lock (outputLock)
{
outputs.Add(message.Data);
}
}
};

var exitCode = await processInvoker.ExecuteAsync(
workingDirectory: _hostContext.GetDirectory(WellKnownDirectory.Work),
fileName: command,
arguments: arg,
environment: null,
requireExitCodeZero: requireZeroExitCode,
outputEncoding: null,
cancellationToken: System.Threading.CancellationToken.None);

if (!showOutputOnFailureOnly || exitCode != 0)
{
if (showOutputOnFailureOnly)
{
context.Command(commandLog);
}

foreach (var outputLine in outputs)
{
context.Debug(outputLine);
}
}

return outputs;
}
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,36 @@
// Copyright (c) Microsoft Corporation.
// Licensed under the MIT License.

using System.Threading.Tasks;
using Microsoft.VisualStudio.Services.Agent;
using Microsoft.VisualStudio.Services.Agent.Worker;

namespace Microsoft.VisualStudio.Services.Agent.Worker.NodeVersionStrategies
{
/// <summary>
/// Interface for checking glibc compatibility with Node.js versions on Linux systems.
/// </summary>
[ServiceLocator(Default = typeof(GlibcCompatibilityInfoProvider))]
public interface IGlibcCompatibilityInfoProvider : IAgentService
{
/// <summary>
/// Checks glibc compatibility for both Node20 and Node24.
/// </summary>
/// <returns>GlibcCompatibilityInfo containing compatibility results for both Node versions</returns>
Task<GlibcCompatibilityInfo> CheckGlibcCompatibilityAsync();

/// <summary>
/// Gets glibc compatibility information, adapting to execution context (host vs container).
/// </summary>
/// <param name="context">Task execution context for determining environment</param>
/// <returns>GlibcCompatibilityInfo containing compatibility results for both Node versions</returns>
Task<GlibcCompatibilityInfo> GetGlibcCompatibilityAsync(TaskContext context);

/// <summary>
/// Checks if the specified Node.js version results in glibc compatibility errors.
/// </summary>
/// <param name="nodeFolder">The node folder name (e.g., "node20_1", "node24")</param>
/// <returns>True if glibc error is detected, false otherwise</returns>
Task<bool> CheckIfNodeResultsInGlibCErrorAsync(string nodeFolder);
}
}
Loading
Loading