From 8ebe9cc4a720989956cb08a4be8927eabf6af446 Mon Sep 17 00:00:00 2001 From: Nate McMaster Date: Tue, 23 May 2017 09:50:26 -0700 Subject: [PATCH 1/2] Use Process.MainModule to find the dotnet muxer --- .../Utilities/DotNetMuxer.cs | 10 ++++++++++ .../DotNetMuxerTests.cs | 1 + 2 files changed, 11 insertions(+) diff --git a/shared/Microsoft.Extensions.CommandLineUtils.Sources/Utilities/DotNetMuxer.cs b/shared/Microsoft.Extensions.CommandLineUtils.Sources/Utilities/DotNetMuxer.cs index ffa6ae26961..0fe33a62cd7 100644 --- a/shared/Microsoft.Extensions.CommandLineUtils.Sources/Utilities/DotNetMuxer.cs +++ b/shared/Microsoft.Extensions.CommandLineUtils.Sources/Utilities/DotNetMuxer.cs @@ -5,6 +5,7 @@ #if !NET451 && !NET452 && !NET46 && !NET461 using System; +using System.Diagnostics; using System.IO; using System.Runtime.InteropServices; @@ -37,6 +38,15 @@ public static string MuxerPathOrDefault() private static string TryFindMuxerPath() { + var mainModule = Process.GetCurrentProcess().MainModule; + if (!string.IsNullOrEmpty(mainModule?.FileName) && File.Exists(mainModule.FileName)) + { + return mainModule.FileName; + } + + // if Process.MainModule is not available, fallback to trying to navigate to the muxer + // by using the location of the shared framework + var fileName = MuxerName; if (RuntimeInformation.IsOSPlatform(OSPlatform.Windows)) { diff --git a/test/Microsoft.Extensions.CommandLineUtils.Tests/DotNetMuxerTests.cs b/test/Microsoft.Extensions.CommandLineUtils.Tests/DotNetMuxerTests.cs index 2de35a5f364..ef9741f9f37 100644 --- a/test/Microsoft.Extensions.CommandLineUtils.Tests/DotNetMuxerTests.cs +++ b/test/Microsoft.Extensions.CommandLineUtils.Tests/DotNetMuxerTests.cs @@ -15,6 +15,7 @@ public void FindsTheMuxer() var muxerPath = DotNetMuxer.MuxerPath; Assert.NotNull(muxerPath); Assert.True(File.Exists(muxerPath), "The file did not exist"); + Assert.True(Path.IsPathRooted(muxerPath), "The path should be rooted"); } } } From f22ae4630598d3b2cf02733ed7131ad72c49ee19 Mon Sep 17 00:00:00 2001 From: Nate McMaster Date: Wed, 24 May 2017 10:22:22 -0700 Subject: [PATCH 2/2] fixup! Use Process.MainModule to find the dotnet muxer --- .../Utilities/DotNetMuxer.cs | 17 +++++++++-------- .../DotNetMuxerTests.cs | 2 ++ 2 files changed, 11 insertions(+), 8 deletions(-) diff --git a/shared/Microsoft.Extensions.CommandLineUtils.Sources/Utilities/DotNetMuxer.cs b/shared/Microsoft.Extensions.CommandLineUtils.Sources/Utilities/DotNetMuxer.cs index 0fe33a62cd7..ecfa0549152 100644 --- a/shared/Microsoft.Extensions.CommandLineUtils.Sources/Utilities/DotNetMuxer.cs +++ b/shared/Microsoft.Extensions.CommandLineUtils.Sources/Utilities/DotNetMuxer.cs @@ -38,21 +38,22 @@ public static string MuxerPathOrDefault() private static string TryFindMuxerPath() { + var fileName = MuxerName; + if (RuntimeInformation.IsOSPlatform(OSPlatform.Windows)) + { + fileName += ".exe"; + } + var mainModule = Process.GetCurrentProcess().MainModule; - if (!string.IsNullOrEmpty(mainModule?.FileName) && File.Exists(mainModule.FileName)) + if (!string.IsNullOrEmpty(mainModule?.FileName) + && Path.GetFileName(mainModule.FileName).Equals(fileName, StringComparison.OrdinalIgnoreCase)) { return mainModule.FileName; } - // if Process.MainModule is not available, fallback to trying to navigate to the muxer + // if Process.MainModule is not available or it does not equal "dotnet(.exe)?", fallback to navigating to the muxer // by using the location of the shared framework - var fileName = MuxerName; - if (RuntimeInformation.IsOSPlatform(OSPlatform.Windows)) - { - fileName += ".exe"; - } - var fxDepsFile = AppContext.GetData("FX_DEPS_FILE") as string; if (string.IsNullOrEmpty(fxDepsFile)) diff --git a/test/Microsoft.Extensions.CommandLineUtils.Tests/DotNetMuxerTests.cs b/test/Microsoft.Extensions.CommandLineUtils.Tests/DotNetMuxerTests.cs index ef9741f9f37..b8612fa1ac6 100644 --- a/test/Microsoft.Extensions.CommandLineUtils.Tests/DotNetMuxerTests.cs +++ b/test/Microsoft.Extensions.CommandLineUtils.Tests/DotNetMuxerTests.cs @@ -3,6 +3,7 @@ #if NETCOREAPP2_0 using System.IO; +using System.Runtime.InteropServices; using Xunit; namespace Microsoft.Extensions.CommandLineUtils @@ -16,6 +17,7 @@ public void FindsTheMuxer() Assert.NotNull(muxerPath); Assert.True(File.Exists(muxerPath), "The file did not exist"); Assert.True(Path.IsPathRooted(muxerPath), "The path should be rooted"); + Assert.Equal("dotnet", Path.GetFileNameWithoutExtension(muxerPath), ignoreCase: true); } } }