From 66b335e49601d5a5bfa7e3d76f3f0fab9ecfd689 Mon Sep 17 00:00:00 2001 From: "copilot-swe-agent[bot]" <198982749+Copilot@users.noreply.github.com> Date: Mon, 24 Nov 2025 22:39:17 +0000 Subject: [PATCH 1/2] Initial plan From 74c3a19fcdd2fe732a29ac528c895855bff64467 Mon Sep 17 00:00:00 2001 From: "copilot-swe-agent[bot]" <198982749+Copilot@users.noreply.github.com> Date: Mon, 24 Nov 2025 22:58:12 +0000 Subject: [PATCH 2/2] Remove unused command resolver and parser files Following PR #51661, this removes additional unused files: - TopLevelCommandParserResult.cs - internal class with no references - OutputPathCommandResolver.cs - unused command resolver - PathCommandResolverPolicy.cs - unused command resolver policy - DepsJsonCommandResolver.cs - unused command resolver Co-authored-by: marcpopMSFT <12663534+marcpopMSFT@users.noreply.github.com> --- .../DepsJsonCommandResolver.cs | 245 ------------------ .../OutputPathCommandResolver.cs | 69 ----- .../PathCommandResolverPolicy.cs | 47 ---- src/Cli/dotnet/TopLevelCommandParserResult.cs | 16 -- 4 files changed, 377 deletions(-) delete mode 100644 src/Cli/dotnet/CommandFactory/CommandResolution/DepsJsonCommandResolver.cs delete mode 100644 src/Cli/dotnet/CommandFactory/CommandResolution/OutputPathCommandResolver.cs delete mode 100644 src/Cli/dotnet/CommandFactory/CommandResolution/PathCommandResolverPolicy.cs delete mode 100644 src/Cli/dotnet/TopLevelCommandParserResult.cs diff --git a/src/Cli/dotnet/CommandFactory/CommandResolution/DepsJsonCommandResolver.cs b/src/Cli/dotnet/CommandFactory/CommandResolution/DepsJsonCommandResolver.cs deleted file mode 100644 index 501a51fdfa66..000000000000 --- a/src/Cli/dotnet/CommandFactory/CommandResolution/DepsJsonCommandResolver.cs +++ /dev/null @@ -1,245 +0,0 @@ -// Licensed to the .NET Foundation under one or more agreements. -// The .NET Foundation licenses this file to you under the MIT license. - -#nullable disable - -using Microsoft.DotNet.Cli.Utils; -using Microsoft.DotNet.Cli.Utils.Extensions; -using Microsoft.Extensions.DependencyModel; - -namespace Microsoft.DotNet.Cli.CommandFactory.CommandResolution; - -public class DepsJsonCommandResolver(Muxer muxer, string nugetPackageRoot) : ICommandResolver -{ - private static readonly string[] s_extensionPreferenceOrder = - [ - "", - ".exe", - ".dll" - ]; - - private readonly string _nugetPackageRoot = nugetPackageRoot; - private readonly Muxer _muxer = muxer; - - public DepsJsonCommandResolver(string nugetPackageRoot) - : this(new Muxer(), nugetPackageRoot) { } - - public CommandSpec Resolve(CommandResolverArguments commandResolverArguments) - { - if (commandResolverArguments.CommandName == null - || commandResolverArguments.DepsJsonFile == null) - { - return null; - } - - return ResolveFromDepsJsonFile( - commandResolverArguments.CommandName, - commandResolverArguments.CommandArguments.OrEmptyIfNull(), - commandResolverArguments.DepsJsonFile); - } - - private CommandSpec ResolveFromDepsJsonFile( - string commandName, - IEnumerable commandArgs, - string depsJsonFile) - { - var dependencyContext = LoadDependencyContextFromFile(depsJsonFile); - - var commandPath = GetCommandPathFromDependencyContext(commandName, dependencyContext); - if (commandPath == null) - { - return null; - } - - return CreateCommandSpecUsingMuxerIfPortable( - commandPath, - commandArgs, - depsJsonFile, - _nugetPackageRoot, - IsPortableApp(commandPath)); - } - - public DependencyContext LoadDependencyContextFromFile(string depsJsonFile) - { - DependencyContext dependencyContext = null; - DependencyContextJsonReader contextReader = new(); - - using (var contextStream = File.OpenRead(depsJsonFile)) - { - dependencyContext = contextReader.Read(contextStream); - } - - return dependencyContext; - } - - public string GetCommandPathFromDependencyContext(string commandName, DependencyContext dependencyContext) - { - var commandCandidates = new List(); - - var assemblyCommandCandidates = GetCommandCandidates( - commandName, - dependencyContext, - CommandCandidateType.RuntimeCommandCandidate); - var nativeCommandCandidates = GetCommandCandidates( - commandName, - dependencyContext, - CommandCandidateType.NativeCommandCandidate); - - commandCandidates.AddRange(assemblyCommandCandidates); - commandCandidates.AddRange(nativeCommandCandidates); - - var command = ChooseCommandCandidate(commandCandidates); - - return command?.GetAbsoluteCommandPath(_nugetPackageRoot); - } - - private IEnumerable GetCommandCandidates( - string commandName, - DependencyContext dependencyContext, - CommandCandidateType commandCandidateType) - { - var commandCandidates = new List(); - - foreach (var runtimeLibrary in dependencyContext.RuntimeLibraries) - { - IEnumerable runtimeAssetGroups = null; - - if (commandCandidateType == CommandCandidateType.NativeCommandCandidate) - { - runtimeAssetGroups = runtimeLibrary.NativeLibraryGroups; - } - else if (commandCandidateType == CommandCandidateType.RuntimeCommandCandidate) - { - runtimeAssetGroups = runtimeLibrary.RuntimeAssemblyGroups; - } - - commandCandidates.AddRange(GetCommandCandidatesFromRuntimeAssetGroups( - commandName, - runtimeAssetGroups, - runtimeLibrary.Name, - runtimeLibrary.Version)); - } - - return commandCandidates; - } - - private static IEnumerable GetCommandCandidatesFromRuntimeAssetGroups( - string commandName, - IEnumerable runtimeAssetGroups, - string PackageName, - string PackageVersion) - { - var candidateAssetGroups = runtimeAssetGroups - .Where(r => r.Runtime == string.Empty) - .Where(a => - a.AssetPaths.Any(p => - Path.GetFileNameWithoutExtension(p).Equals(commandName, StringComparison.OrdinalIgnoreCase))); - - var commandCandidates = new List(); - foreach (var candidateAssetGroup in candidateAssetGroups) - { - var candidateAssetPaths = candidateAssetGroup.AssetPaths.Where( - p => Path.GetFileNameWithoutExtension(p) - .Equals(commandName, StringComparison.OrdinalIgnoreCase)); - - foreach (var candidateAssetPath in candidateAssetPaths) - { - commandCandidates.Add(new CommandCandidate - { - PackageName = PackageName, - PackageVersion = PackageVersion, - RelativeCommandPath = candidateAssetPath - }); - } - } - - return commandCandidates; - } - - private static CommandCandidate ChooseCommandCandidate(IEnumerable commandCandidates) - { - foreach (var extension in s_extensionPreferenceOrder) - { - var candidate = commandCandidates - .FirstOrDefault(p => Path.GetExtension(p.RelativeCommandPath) - .Equals(extension, StringComparison.OrdinalIgnoreCase)); - - if (candidate != null) - { - return candidate; - } - } - - return null; - } - - private CommandSpec CreateCommandSpecUsingMuxerIfPortable( - string commandPath, - IEnumerable commandArgs, - string depsJsonFile, - string nugetPackagesRoot, - bool isPortable) - { - var depsFileArguments = GetDepsFileArguments(depsJsonFile); - var additionalProbingPathArguments = GetAdditionalProbingPathArguments(); - - List muxerArgs = ["exec"]; - muxerArgs.AddRange(depsFileArguments); - muxerArgs.AddRange(additionalProbingPathArguments); - muxerArgs.Add(commandPath); - muxerArgs.AddRange(commandArgs); - - var escapedArgString = ArgumentEscaper.EscapeAndConcatenateArgArrayForProcessStart(muxerArgs); - - return new CommandSpec(_muxer.MuxerPath, escapedArgString); - } - - private static bool IsPortableApp(string commandPath) - { - var commandDir = Path.GetDirectoryName(commandPath); - - var runtimeConfigPath = Directory.EnumerateFiles(commandDir) - .FirstOrDefault(x => x.EndsWith("runtimeconfig.json")); - - if (runtimeConfigPath == null) - { - return false; - } - - var runtimeConfig = new RuntimeConfig(runtimeConfigPath); - - return runtimeConfig.IsPortable; - } - - private static IEnumerable GetDepsFileArguments(string depsJsonFile) - { - return ["--depsfile", depsJsonFile]; - } - - private IEnumerable GetAdditionalProbingPathArguments() - { - return ["--additionalProbingPath", _nugetPackageRoot]; - } - - private class CommandCandidate - { - public string PackageName { get; set; } - public string PackageVersion { get; set; } - public string RelativeCommandPath { get; set; } - - public string GetAbsoluteCommandPath(string nugetPackageRoot) - { - return Path.Combine( - nugetPackageRoot.Replace('/', Path.DirectorySeparatorChar), - PackageName.Replace('/', Path.DirectorySeparatorChar), - PackageVersion.Replace('/', Path.DirectorySeparatorChar), - RelativeCommandPath.Replace('/', Path.DirectorySeparatorChar)); - } - } - - private enum CommandCandidateType - { - NativeCommandCandidate, - RuntimeCommandCandidate - } -} diff --git a/src/Cli/dotnet/CommandFactory/CommandResolution/OutputPathCommandResolver.cs b/src/Cli/dotnet/CommandFactory/CommandResolution/OutputPathCommandResolver.cs deleted file mode 100644 index 40cffdcb07f1..000000000000 --- a/src/Cli/dotnet/CommandFactory/CommandResolution/OutputPathCommandResolver.cs +++ /dev/null @@ -1,69 +0,0 @@ -// Licensed to the .NET Foundation under one or more agreements. -// The .NET Foundation licenses this file to you under the MIT license. - -#nullable disable - -using Microsoft.DotNet.Cli.Utils; -using Microsoft.DotNet.Cli.Utils.Extensions; -using NuGet.Frameworks; - -namespace Microsoft.DotNet.Cli.CommandFactory.CommandResolution; - -public class OutputPathCommandResolver(IEnvironmentProvider environment, - IPlatformCommandSpecFactory commandSpecFactory) : AbstractPathBasedCommandResolver(environment, commandSpecFactory) -{ - internal override string ResolveCommandPath(CommandResolverArguments commandResolverArguments) - { - if (commandResolverArguments.Framework == null - || commandResolverArguments.ProjectDirectory == null - || commandResolverArguments.Configuration == null - || commandResolverArguments.CommandName == null) - { - return null; - } - - return ResolveFromProjectOutput( - commandResolverArguments.ProjectDirectory, - commandResolverArguments.Framework, - commandResolverArguments.Configuration, - commandResolverArguments.CommandName, - commandResolverArguments.CommandArguments.OrEmptyIfNull(), - commandResolverArguments.OutputPath, - commandResolverArguments.BuildBasePath); - } - - private string ResolveFromProjectOutput( - string projectDirectory, - NuGetFramework framework, - string configuration, - string commandName, - IEnumerable commandArguments, - string outputPath, - string buildBasePath) - { - var projectFactory = new ProjectFactory(_environment); - - var project = projectFactory.GetProject( - projectDirectory, - framework, - configuration, - buildBasePath, - outputPath); - - if (project == null) - { - return null; - } - - var buildOutputPath = project.FullOutputPath; - - if (!Directory.Exists(buildOutputPath)) - { - Reporter.Verbose.WriteLine( - string.Format(CliStrings.BuildOutputPathDoesNotExist, buildOutputPath)); - return null; - } - - return _environment.GetCommandPathFromRootPath(buildOutputPath, commandName); - } -} diff --git a/src/Cli/dotnet/CommandFactory/CommandResolution/PathCommandResolverPolicy.cs b/src/Cli/dotnet/CommandFactory/CommandResolution/PathCommandResolverPolicy.cs deleted file mode 100644 index 3cd19a0b3064..000000000000 --- a/src/Cli/dotnet/CommandFactory/CommandResolution/PathCommandResolverPolicy.cs +++ /dev/null @@ -1,47 +0,0 @@ -// Licensed to the .NET Foundation under one or more agreements. -// The .NET Foundation licenses this file to you under the MIT license. - -#nullable disable - -using Microsoft.DotNet.Cli.Utils; - -namespace Microsoft.DotNet.Cli.CommandFactory.CommandResolution; - -public class PathCommandResolverPolicy : ICommandResolverPolicy -{ - public CompositeCommandResolver CreateCommandResolver(string currentWorkingDirectory = null) - { - return Create(); - } - - public static CompositeCommandResolver Create() - { - var environment = new EnvironmentProvider(); - - IPlatformCommandSpecFactory platformCommandSpecFactory; - if (OperatingSystem.IsWindows()) - { - platformCommandSpecFactory = new WindowsExePreferredCommandSpecFactory(); - } - else - { - platformCommandSpecFactory = new GenericPlatformCommandSpecFactory(); - } - - return CreatePathCommandResolverPolicy( - environment, - platformCommandSpecFactory); - } - - public static CompositeCommandResolver CreatePathCommandResolverPolicy( - IEnvironmentProvider environment, - IPlatformCommandSpecFactory platformCommandSpecFactory) - { - var compositeCommandResolver = new CompositeCommandResolver(); - - compositeCommandResolver.AddCommandResolver( - new PathCommandResolver(environment, platformCommandSpecFactory)); - - return compositeCommandResolver; - } -} diff --git a/src/Cli/dotnet/TopLevelCommandParserResult.cs b/src/Cli/dotnet/TopLevelCommandParserResult.cs deleted file mode 100644 index 10a1d8c7291d..000000000000 --- a/src/Cli/dotnet/TopLevelCommandParserResult.cs +++ /dev/null @@ -1,16 +0,0 @@ -// Licensed to the .NET Foundation under one or more agreements. -// The .NET Foundation licenses this file to you under the MIT license. - -#nullable disable - -namespace Microsoft.DotNet.Cli; - -internal class TopLevelCommandParserResult(string command) -{ - public static TopLevelCommandParserResult Empty - { - get { return new TopLevelCommandParserResult(string.Empty); } - } - - public string Command { get; } = command; -}