diff --git a/src/coreclr/src/vm/corhost.cpp b/src/coreclr/src/vm/corhost.cpp index a50e1515604657..33aa48cf5c104b 100644 --- a/src/coreclr/src/vm/corhost.cpp +++ b/src/coreclr/src/vm/corhost.cpp @@ -272,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(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/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..1126272fc8a73b --- /dev/null +++ b/src/installer/tests/Microsoft.NET.HostModel.Tests/AppHost.Bundle.Tests/BundleEnvironmentGetCommandLineArgs.cs @@ -0,0 +1,75 @@ +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); + + // For single-file, Environment.GetCommandLineArgs[0] + // should return the file path of the host. + Command.Create(singleFile) + .CaptureStdErr() + .CaptureStdOut() + .Execute() + .Should() + .Pass() + .And + .HaveStdOutContaining(singleFile); + } + + [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() + .Execute() + .Should() + .Pass() + .And + .HaveStdOutContaining(appPath); + } + + 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(); + } + } + } +}