66using System . CommandLine ;
77using Microsoft . DotNet . Cli . Extensions ;
88using Microsoft . DotNet . Cli . Utils ;
9+ using Microsoft . DotNet . ProjectTools ;
910
1011namespace Microsoft . DotNet . Cli . Commands . NuGet ;
1112
12- public class NuGetCommand
13+ internal class NuGetCommand
1314{
14- public static int Run ( string [ ] args )
15+ public static int Run ( string [ ] args , bool isFileBasedApp = false )
1516 {
16- return Run ( args , new NuGetCommandRunner ( ) ) ;
17+ return Run ( args , isFileBasedApp
18+ ? new InProcessNuGetCommandRunner ( NuGetVirtualProjectBuilder . Instance )
19+ : new NuGetCommandRunner ( ) ) ;
1720 }
1821
1922 public static int Run ( ParseResult parseResult )
2023 {
21- return Run ( parseResult . GetArguments ( ) , new NuGetCommandRunner ( ) ) ;
24+ ICommandRunner runner ;
25+
26+ if ( parseResult . CommandResult . Command . Name == "why"
27+ && parseResult . CommandResult . Command . Arguments . FirstOrDefault ( ) is Argument < string > pathArg
28+ && parseResult . GetValue ( pathArg ) is { } path
29+ && VirtualProjectBuilder . IsValidEntryPointPath ( path ) )
30+ {
31+ runner = new InProcessNuGetCommandRunner ( NuGetVirtualProjectBuilder . Instance ) ;
32+ }
33+ else
34+ {
35+ runner = new NuGetCommandRunner ( ) ;
36+ }
37+
38+ return Run ( parseResult . GetArguments ( ) , runner ) ;
2239 }
2340
2441 public static int Run ( string [ ] args , ICommandRunner nugetCommandRunner )
@@ -43,11 +60,28 @@ private class NuGetCommandRunner : ICommandRunner
4360 public int Run ( string [ ] args )
4461 {
4562 var nugetApp = new NuGetForwardingApp ( args ) ;
46- nugetApp . WithEnvironmentVariable ( " DOTNET_HOST_PATH" , GetDotnetPath ( ) ) ;
63+ nugetApp . WithEnvironmentVariable ( EnvironmentVariableNames . DOTNET_HOST_PATH , GetDotnetPath ( ) ) ;
4764 return nugetApp . Execute ( ) ;
4865 }
4966 }
5067
68+ private class InProcessNuGetCommandRunner ( NuGetVirtualProjectBuilder virtualProjectBuilder ) : ICommandRunner
69+ {
70+ public int Run ( string [ ] args )
71+ {
72+ var originalDotNetHostPath = Environment . GetEnvironmentVariable ( EnvironmentVariableNames . DOTNET_HOST_PATH ) ;
73+ Environment . SetEnvironmentVariable ( EnvironmentVariableNames . DOTNET_HOST_PATH , GetDotnetPath ( ) ) ;
74+ try
75+ {
76+ return global ::NuGet . CommandLine . XPlat . Program . Run ( args , virtualProjectBuilder ) ;
77+ }
78+ finally
79+ {
80+ Environment . SetEnvironmentVariable ( EnvironmentVariableNames . DOTNET_HOST_PATH , originalDotNetHostPath ) ;
81+ }
82+ }
83+ }
84+
5185 private static string GetDotnetPath ( )
5286 {
5387 return new Muxer ( ) . MuxerPath ;
0 commit comments