From 97c03d3d48696f17630822f6c508fe19b853ac36 Mon Sep 17 00:00:00 2001 From: Mateo Torres Ruiz Date: Wed, 19 Aug 2020 00:40:16 -0700 Subject: [PATCH 1/4] Return host's path when using GetCommandLineArgs from within a single-file app --- src/coreclr/src/vm/corhost.cpp | 3 +- .../EnvironmentGetCommandLineArgs.csproj | 10 ++++ .../EnvironmentGetCommandLineArgs/Program.cs | 15 +++++ .../BundleEnvironmentGetCommandLineArgs.cs | 55 +++++++++++++++++++ 4 files changed, 82 insertions(+), 1 deletion(-) create mode 100644 src/installer/tests/Assets/TestProjects/EnvironmentGetCommandLineArgs/EnvironmentGetCommandLineArgs.csproj create mode 100644 src/installer/tests/Assets/TestProjects/EnvironmentGetCommandLineArgs/Program.cs create mode 100644 src/installer/tests/Microsoft.NET.HostModel.Tests/AppHost.Bundle.Tests/BundleEnvironmentGetCommandLineArgs.cs diff --git a/src/coreclr/src/vm/corhost.cpp b/src/coreclr/src/vm/corhost.cpp index a50e1515604657..f82aeeed0181bd 100644 --- a/src/coreclr/src/vm/corhost.cpp +++ b/src/coreclr/src/vm/corhost.cpp @@ -27,6 +27,7 @@ #include "comdelegate.h" #include "dllimportcallback.h" #include "eventtrace.h" +#include "bundle.h" #include "win32threadpool.h" #include "eventtrace.h" @@ -272,7 +273,7 @@ void SetCommandLineArgs(LPCWSTR pwzAssemblyPath, int argc, LPCWSTR* argv) GCPROTECT_BEGIN(gc); gc.cmdLineArgs = (PTRARRAYREF)AllocateObjectArray(argc + 1 /* arg[0] should be the exe name*/, g_pStringClass); - OBJECTREF orAssemblyPath = StringObject::NewString(pwzAssemblyPath); + OBJECTREF orAssemblyPath = StringObject::NewString(Bundle::AppIsBundle() ? Bundle::AppBundle->Path() : pwzAssemblyPath); gc.cmdLineArgs->SetAt(0, orAssemblyPath); for (int i = 0; i < argc; ++i) diff --git a/src/installer/tests/Assets/TestProjects/EnvironmentGetCommandLineArgs/EnvironmentGetCommandLineArgs.csproj b/src/installer/tests/Assets/TestProjects/EnvironmentGetCommandLineArgs/EnvironmentGetCommandLineArgs.csproj new file mode 100644 index 00000000000000..9052b4e7ce36f7 --- /dev/null +++ b/src/installer/tests/Assets/TestProjects/EnvironmentGetCommandLineArgs/EnvironmentGetCommandLineArgs.csproj @@ -0,0 +1,10 @@ + + + + $(NetCoreAppCurrent) + Exe + $(TestTargetRid) + $(MNAVersion) + + + \ No newline at end of file diff --git a/src/installer/tests/Assets/TestProjects/EnvironmentGetCommandLineArgs/Program.cs b/src/installer/tests/Assets/TestProjects/EnvironmentGetCommandLineArgs/Program.cs new file mode 100644 index 00000000000000..c10cb9c974c9de --- /dev/null +++ b/src/installer/tests/Assets/TestProjects/EnvironmentGetCommandLineArgs/Program.cs @@ -0,0 +1,15 @@ +using System; + +namespace EnvironmentGetCommandLineArgs +{ + public class Program + { + public static void Main(string[] args) + { + foreach (var arg in Environment.GetCommandLineArgs()) + { + Console.WriteLine(arg); + } + } + } +} diff --git a/src/installer/tests/Microsoft.NET.HostModel.Tests/AppHost.Bundle.Tests/BundleEnvironmentGetCommandLineArgs.cs b/src/installer/tests/Microsoft.NET.HostModel.Tests/AppHost.Bundle.Tests/BundleEnvironmentGetCommandLineArgs.cs new file mode 100644 index 00000000000000..4f131967ecb91a --- /dev/null +++ b/src/installer/tests/Microsoft.NET.HostModel.Tests/AppHost.Bundle.Tests/BundleEnvironmentGetCommandLineArgs.cs @@ -0,0 +1,55 @@ +using System; +using BundleTests.Helpers; +using Microsoft.DotNet.Cli.Build.Framework; +using Microsoft.DotNet.CoreSetup.Test; +using Xunit; + +namespace AppHost.Bundle.Tests +{ + public class BundleEnvironmentGetCommandLineArgs : IClassFixture + { + private SharedTestState sharedTestState; + + public BundleEnvironmentGetCommandLineArgs(SharedTestState fixture) + { + sharedTestState = fixture; + } + + [Fact] + public void GetEnvironmentArgs_0_Returns_Bundled_Executable_Path() + { + var fixture = sharedTestState.TestFixture.Copy(); + var singleFile = BundleHelper.BundleApp(fixture); + var executablePath = BundleHelper.GetHostPath(fixture); + + Command.Create(singleFile) + .CaptureStdErr() + .CaptureStdOut() + .Execute() + .Should() + .Pass() + .And + .HaveStdOutContaining(executablePath); + } + + public class SharedTestState : IDisposable + { + public TestProjectFixture TestFixture { get; set; } + public RepoDirectoriesProvider RepoDirectories { get; set; } + + public SharedTestState() + { + RepoDirectories = new RepoDirectoriesProvider(); + TestFixture = new TestProjectFixture("EnvironmentGetCommandLineArgs", RepoDirectories); + TestFixture + .EnsureRestoredForRid(TestFixture.CurrentRid, RepoDirectories.CorehostPackages) + .PublishProject(runtime: TestFixture.CurrentRid, outputDirectory: BundleHelper.GetPublishPath(TestFixture)); + } + + public void Dispose() + { + TestFixture.Dispose(); + } + } + } +} From 12dedb70d61a2b282535e475b30dfa0567092478 Mon Sep 17 00:00:00 2001 From: Mateo Torres Ruiz Date: Wed, 19 Aug 2020 01:02:59 -0700 Subject: [PATCH 2/4] Look for the called exe --- .../BundleEnvironmentGetCommandLineArgs.cs | 3 +-- 1 file changed, 1 insertion(+), 2 deletions(-) diff --git a/src/installer/tests/Microsoft.NET.HostModel.Tests/AppHost.Bundle.Tests/BundleEnvironmentGetCommandLineArgs.cs b/src/installer/tests/Microsoft.NET.HostModel.Tests/AppHost.Bundle.Tests/BundleEnvironmentGetCommandLineArgs.cs index 4f131967ecb91a..67784dfc3a0a1f 100644 --- a/src/installer/tests/Microsoft.NET.HostModel.Tests/AppHost.Bundle.Tests/BundleEnvironmentGetCommandLineArgs.cs +++ b/src/installer/tests/Microsoft.NET.HostModel.Tests/AppHost.Bundle.Tests/BundleEnvironmentGetCommandLineArgs.cs @@ -20,7 +20,6 @@ public void GetEnvironmentArgs_0_Returns_Bundled_Executable_Path() { var fixture = sharedTestState.TestFixture.Copy(); var singleFile = BundleHelper.BundleApp(fixture); - var executablePath = BundleHelper.GetHostPath(fixture); Command.Create(singleFile) .CaptureStdErr() @@ -29,7 +28,7 @@ public void GetEnvironmentArgs_0_Returns_Bundled_Executable_Path() .Should() .Pass() .And - .HaveStdOutContaining(executablePath); + .HaveStdOutContaining(singleFile); } public class SharedTestState : IDisposable From 8a7ac751a4f4364995742033baa38b1a2bf8db8d Mon Sep 17 00:00:00 2001 From: mateoatr Date: Wed, 19 Aug 2020 19:41:08 +0000 Subject: [PATCH 3/4] Remove bundle header --- src/coreclr/src/vm/corhost.cpp | 3 +-- .../BundleEnvironmentGetCommandLineArgs.cs | 16 ++++++++++++++++ 2 files changed, 17 insertions(+), 2 deletions(-) diff --git a/src/coreclr/src/vm/corhost.cpp b/src/coreclr/src/vm/corhost.cpp index f82aeeed0181bd..33aa48cf5c104b 100644 --- a/src/coreclr/src/vm/corhost.cpp +++ b/src/coreclr/src/vm/corhost.cpp @@ -27,7 +27,6 @@ #include "comdelegate.h" #include "dllimportcallback.h" #include "eventtrace.h" -#include "bundle.h" #include "win32threadpool.h" #include "eventtrace.h" @@ -273,7 +272,7 @@ void SetCommandLineArgs(LPCWSTR pwzAssemblyPath, int argc, LPCWSTR* argv) GCPROTECT_BEGIN(gc); gc.cmdLineArgs = (PTRARRAYREF)AllocateObjectArray(argc + 1 /* arg[0] should be the exe name*/, g_pStringClass); - OBJECTREF orAssemblyPath = StringObject::NewString(Bundle::AppIsBundle() ? Bundle::AppBundle->Path() : pwzAssemblyPath); + OBJECTREF orAssemblyPath = StringObject::NewString(Bundle::AppIsBundle() ? static_cast(Bundle::AppBundle->Path()) : pwzAssemblyPath); gc.cmdLineArgs->SetAt(0, orAssemblyPath); for (int i = 0; i < argc; ++i) diff --git a/src/installer/tests/Microsoft.NET.HostModel.Tests/AppHost.Bundle.Tests/BundleEnvironmentGetCommandLineArgs.cs b/src/installer/tests/Microsoft.NET.HostModel.Tests/AppHost.Bundle.Tests/BundleEnvironmentGetCommandLineArgs.cs index 67784dfc3a0a1f..e3e16e17aeb600 100644 --- a/src/installer/tests/Microsoft.NET.HostModel.Tests/AppHost.Bundle.Tests/BundleEnvironmentGetCommandLineArgs.cs +++ b/src/installer/tests/Microsoft.NET.HostModel.Tests/AppHost.Bundle.Tests/BundleEnvironmentGetCommandLineArgs.cs @@ -21,6 +21,8 @@ public void GetEnvironmentArgs_0_Returns_Bundled_Executable_Path() var fixture = sharedTestState.TestFixture.Copy(); var singleFile = BundleHelper.BundleApp(fixture); + // For single-file, Environment.GetCommandLineArgs[0] + // should return the file path of the host. Command.Create(singleFile) .CaptureStdErr() .CaptureStdOut() @@ -29,6 +31,20 @@ public void GetEnvironmentArgs_0_Returns_Bundled_Executable_Path() .Pass() .And .HaveStdOutContaining(singleFile); + + // For non single-file apps, Environment.GetCommandLineArgs[0] + // should return the file path of the managed entrypoint. + var dotnet = fixture.BuiltDotnet; + var appPath = BundleHelper.GetAppPath(fixture); + + dotnet.Exec(appPath) + .CaptureStdErr() + .CaptureStdOut() + .Execute() + .Should() + .Pass() + .And + .HaveStdOutContaining(appPath); } public class SharedTestState : IDisposable From 95f97542f59fa181b7cc1ea56755f53a2621d36f Mon Sep 17 00:00:00 2001 From: mateoatr Date: Wed, 19 Aug 2020 21:36:53 +0000 Subject: [PATCH 4/4] Split test --- .../BundleEnvironmentGetCommandLineArgs.cs | 9 +++++++-- 1 file changed, 7 insertions(+), 2 deletions(-) diff --git a/src/installer/tests/Microsoft.NET.HostModel.Tests/AppHost.Bundle.Tests/BundleEnvironmentGetCommandLineArgs.cs b/src/installer/tests/Microsoft.NET.HostModel.Tests/AppHost.Bundle.Tests/BundleEnvironmentGetCommandLineArgs.cs index e3e16e17aeb600..1126272fc8a73b 100644 --- a/src/installer/tests/Microsoft.NET.HostModel.Tests/AppHost.Bundle.Tests/BundleEnvironmentGetCommandLineArgs.cs +++ b/src/installer/tests/Microsoft.NET.HostModel.Tests/AppHost.Bundle.Tests/BundleEnvironmentGetCommandLineArgs.cs @@ -31,12 +31,17 @@ public void GetEnvironmentArgs_0_Returns_Bundled_Executable_Path() .Pass() .And .HaveStdOutContaining(singleFile); + } - // For non single-file apps, Environment.GetCommandLineArgs[0] - // should return the file path of the managed entrypoint. + [Fact] + public void GetEnvironmentArgs_0_Non_Bundled_App() + { + var fixture = sharedTestState.TestFixture.Copy(); var dotnet = fixture.BuiltDotnet; var appPath = BundleHelper.GetAppPath(fixture); + // For non single-file apps, Environment.GetCommandLineArgs[0] + // should return the file path of the managed entrypoint. dotnet.Exec(appPath) .CaptureStdErr() .CaptureStdOut()