diff --git a/src/Microsoft.DotNet.Cli.Utils/CommandResolver.cs b/src/Microsoft.DotNet.Cli.Utils/CommandResolver.cs index 7c5114fe8..4930e1f07 100644 --- a/src/Microsoft.DotNet.Cli.Utils/CommandResolver.cs +++ b/src/Microsoft.DotNet.Cli.Utils/CommandResolver.cs @@ -17,6 +17,7 @@ namespace Microsoft.DotNet.Cli.Utils return ResolveFromRootedCommand(commandName, args) ?? ResolveFromProjectDependencies(commandName, args, framework) ?? ResolveFromProjectTools(commandName, args) ?? + ResolveFromAppBase(commandName, args) ?? ResolveFromPath(commandName, args); } @@ -24,22 +25,18 @@ namespace Microsoft.DotNet.Cli.Utils { var commandPath = Env.GetCommandPath(commandName); - if (commandPath == null) return null; + return commandPath == null + ? null + : CreateCommandSpecPreferringExe(commandName, args, commandPath, CommandResolutionStrategy.Path); + } - if (RuntimeInformation.IsOSPlatform(OSPlatform.Windows) && - Path.GetExtension(commandPath).Equals(".cmd", StringComparison.OrdinalIgnoreCase)) - { - var preferredCommandPath = Env.GetCommandPath(commandName, ".exe"); + private static CommandSpec ResolveFromAppBase(string commandName, string args) + { + var commandPath = Env.GetCommandPathFromAppBase(AppContext.BaseDirectory, commandName); - if (preferredCommandPath != null) - { - commandPath = Environment.GetEnvironmentVariable("ComSpec"); - - args = $"/S /C \"\"{preferredCommandPath}\" {args}\""; - } - } - - return new CommandSpec(commandPath, args, CommandResolutionStrategy.Path); + return commandPath == null + ? null + : CreateCommandSpecPreferringExe(commandName, args, commandPath, CommandResolutionStrategy.BaseDirectory); } private static CommandSpec ResolveFromRootedCommand(string commandName, string args) @@ -194,5 +191,24 @@ namespace Microsoft.DotNet.Cli.Utils return Path.Combine(context.GetOutputDirectoryPath(buildConfiguration), context.ProjectFile.Name + FileNameSuffixes.Deps); } + + private static CommandSpec CreateCommandSpecPreferringExe(string commandName, string args, string commandPath, + CommandResolutionStrategy resolutionStrategy) + { + if (RuntimeInformation.IsOSPlatform(OSPlatform.Windows) && + Path.GetExtension(commandPath).Equals(".cmd", StringComparison.OrdinalIgnoreCase)) + { + var preferredCommandPath = Env.GetCommandPath(commandName, ".exe"); + + if (preferredCommandPath != null) + { + commandPath = Environment.GetEnvironmentVariable("ComSpec"); + + args = $"/S /C \"\"{preferredCommandPath}\" {args}\""; + } + } + + return new CommandSpec(commandPath, args, resolutionStrategy); + } } } diff --git a/src/Microsoft.DotNet.Cli.Utils/Env.cs b/src/Microsoft.DotNet.Cli.Utils/Env.cs index 5f2486ed3..dc14b422d 100644 --- a/src/Microsoft.DotNet.Cli.Utils/Env.cs +++ b/src/Microsoft.DotNet.Cli.Utils/Env.cs @@ -59,5 +59,18 @@ namespace Microsoft.DotNet.Cli.Utils return commandPath; } + + public static string GetCommandPathFromAppBase(string appBase, string commandName, params string[] extensions) + { + if (!extensions.Any()) + { + extensions = Env.ExecutableExtensions.ToArray(); + } + + var commandPath = extensions.Select(e => Path.Combine(appBase, commandName + e)) + .FirstOrDefault(File.Exists); + + return commandPath; + } } }