Skip to content

Commit

Permalink
Merged PR 32376: 7.0.1xx Process start path
Browse files Browse the repository at this point in the history
  • Loading branch information
Forgind committed Jul 14, 2023
2 parents 268d482 + a7ce5de commit 3ac4443
Show file tree
Hide file tree
Showing 10 changed files with 52 additions and 22 deletions.
Original file line number Diff line number Diff line change
@@ -1,6 +1,8 @@
// Copyright (c) .NET Foundation and contributors. All rights reserved.
// Licensed under the MIT license. See LICENSE file in the project root for full license information.

#if NET

using System.Collections.Generic;
using System.Diagnostics;
using Microsoft.DotNet.Cli.Utils;
Expand All @@ -13,8 +15,6 @@ namespace Microsoft.DotNet.Cli.Utils
/// </summary>
internal class ForwardingAppImplementation
{
private const string HostExe = "dotnet";

private readonly string _forwardApplicationPath;
private readonly IEnumerable<string> _argsToForward;
private readonly string _depsFile;
Expand Down Expand Up @@ -97,7 +97,10 @@ public ForwardingAppImplementation WithEnvironmentVariable(string name, string v

private string GetHostExeName()
{
return $"{HostExe}{FileNameSuffixes.CurrentPlatform.Exe}";
// Should instead make this a full path to dotnet
return System.Environment.ProcessPath;
}
}
}

#endif
Original file line number Diff line number Diff line change
Expand Up @@ -50,7 +50,7 @@ private CommandSpec CreateCommandSpecWrappedWithCmd(
string command,
IEnumerable<string> args)
{
var comSpec = Environment.GetEnvironmentVariable("ComSpec") ?? "cmd.exe";
var comSpec = Environment.GetEnvironmentVariable("ComSpec") ?? Path.Combine(Environment.GetFolderPath(Environment.SpecialFolder.System), "cmd.exe");

// Handle the case where ComSpec is already the command
if (command.Equals(comSpec, StringComparison.OrdinalIgnoreCase))
Expand Down
17 changes: 14 additions & 3 deletions src/Cli/dotnet/Telemetry/MacAddressGetter.cs
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@
using System.Net.NetworkInformation;
using System.ComponentModel;
using Microsoft.DotNet.Cli.Utils;
using System.IO;

namespace Microsoft.DotNet.Cli.Telemetry
{
Expand Down Expand Up @@ -64,9 +65,14 @@ private static string ParseMACAddress(string shelloutput)

private static string GetIpCommandOutput()
{
var fileName = File.Exists(@"/usr/bin/ip") ? @"/usr/bin/ip" :
File.Exists(@"/usr/sbin/ip") ? @"/usr/sbin/ip" :
File.Exists(@"/sbin/ip") ? @"/sbin/ip" :
"ip";

var ipResult = new ProcessStartInfo
{
FileName = "ip",
FileName = fileName,
Arguments = "link",
UseShellExecute = false
}.ExecuteAndCaptureOutput(out string ipStdOut, out string ipStdErr);
Expand All @@ -87,7 +93,7 @@ private static string GetShellOutMacAddressOutput()
{
var result = new ProcessStartInfo
{
FileName = "getmac.exe",
FileName = Path.Combine(Environment.GetFolderPath(Environment.SpecialFolder.System), "getmac.exe"),
UseShellExecute = false
}.ExecuteAndCaptureOutput(out string stdOut, out string stdErr);

Expand All @@ -104,9 +110,14 @@ private static string GetShellOutMacAddressOutput()
{
try
{
var fileName = File.Exists("/sbin/ifconfig") ? "/sbin/ifconfig" :
File.Exists("/usr/sbin/ifconfig") ? "/usr/sbin/ifconfig" :
File.Exists("/usr/bin/ifconfig") ? "/usr/bin/ifconfig" :
"ifconfig";

var ifconfigResult = new ProcessStartInfo
{
FileName = "ifconfig",
FileName = fileName,
Arguments = "-a",
UseShellExecute = false
}.ExecuteAndCaptureOutput(out string ifconfigStdOut, out string ifconfigStdErr);
Expand Down
12 changes: 8 additions & 4 deletions src/Cli/dotnet/commands/dotnet-help/HelpCommand.cs
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@
using System.CommandLine;
using System.CommandLine.Parsing;
using System.Diagnostics;
using System.Linq;
using System.IO;
using Microsoft.DotNet.Cli;
using Microsoft.DotNet.Cli.Utils;

Expand Down Expand Up @@ -54,23 +54,27 @@ public static Process ConfigureProcess(string docUrl)
{
psInfo = new ProcessStartInfo
{
FileName = "cmd",
FileName = Path.Combine(Environment.GetFolderPath(Environment.SpecialFolder.System), "cmd.exe"),
Arguments = $"/c start {docUrl}"
};
}
else if (OperatingSystem.IsMacOS())
{
psInfo = new ProcessStartInfo
{
FileName = "open",
FileName = @"/usr/bin/open",
Arguments = docUrl
};
}
else
{
var fileName = File.Exists(@"/usr/bin/xdg-open") ? @"/usr/bin/xdg-open" :
File.Exists(@"/usr/sbin/xdg-open") ? @"/usr/sbin/xdg-open" :
File.Exists(@"/sbin/xdg-open") ? @"/sbin/xdg-open" :
"xdg-open";
psInfo = new ProcessStartInfo
{
FileName = "xdg-open",
FileName = fileName,
Arguments = docUrl
};
}
Expand Down
12 changes: 11 additions & 1 deletion src/RazorSdk/Tool/ServerProtocol/ServerConnection.cs
Original file line number Diff line number Diff line change
Expand Up @@ -288,7 +288,17 @@ internal static bool TryCreateServerCore(string clientDir, string pipeName, out

// The server should be in the same directory as the client
var expectedCompilerPath = Path.Combine(clientDir, ServerName);
var expectedPath = Environment.GetEnvironmentVariable("DOTNET_HOST_PATH") ?? "dotnet";

var expectedPath = Environment.GetEnvironmentVariable("DOTNET_HOST_PATH");
if (string.IsNullOrEmpty(expectedPath))
{
#if NET
expectedPath = System.Environment.ProcessPath;
#else
expectedPath = Process.GetCurrentProcess().MainModule.FileName;
#endif
}

var argumentList = new string[]
{
expectedCompilerPath,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@
using FluentAssertions;
using Microsoft.DotNet.Cli.Utils;
using Microsoft.DotNet.CommandFactory;
using Microsoft.NET.TestFramework;
using Xunit;

namespace Microsoft.DotNet.Tests
Expand Down Expand Up @@ -208,7 +209,7 @@ public void It_prefers_EXE_over_CMD_when_two_command_candidates_exist_and_using_
commandFile.Should().Be("projectpathtestcommand1.exe");
}

[Fact]
[WindowsOnlyFact]
public void It_wraps_command_with_CMD_EXE_when_command_has_CMD_Extension_and_using_WindowsExePreferredCommandSpecFactory()
{
var environment = new EnvironmentProvider(new[] { ".cmd" });
Expand All @@ -231,7 +232,7 @@ public void It_wraps_command_with_CMD_EXE_when_command_has_CMD_Extension_and_usi
result.Should().NotBeNull();

var commandFile = Path.GetFileName(result.Path);
commandFile.Should().Be("cmd.exe");
commandFile.Should().EndWith("cmd.exe");

result.Args.Should().Contain(testCommandPath);
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@
using FluentAssertions;
using Microsoft.DotNet.Cli.Utils;
using Microsoft.DotNet.CommandFactory;
using Microsoft.NET.TestFramework;
using Xunit;

namespace Microsoft.DotNet.Tests
Expand Down Expand Up @@ -153,7 +154,7 @@ public void It_prefers_EXE_over_CMD_when_two_command_candidates_exist_and_using_
commandFile.Should().Be("appbasetestcommand1.exe");
}

[Fact]
[WindowsOnlyFact]
public void It_wraps_command_with_CMD_EXE_when_command_has_CMD_Extension_and_using_WindowsExePreferredCommandSpecFactory()
{
var environment = new EnvironmentProvider(new[] { ".cmd" });
Expand All @@ -175,7 +176,7 @@ public void It_wraps_command_with_CMD_EXE_when_command_has_CMD_Extension_and_usi
result.Should().NotBeNull();

var commandFile = Path.GetFileName(result.Path);
commandFile.Should().Be("cmd.exe");
commandFile.Should().EndWith("cmd.exe");

result.Args.Should().Contain(testCommandPath);
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -128,7 +128,7 @@ public void WhenCommandWithoutDocLinkIsPassedToDotnetHelpItPrintsError(string co
public void WhenRunOnWindowsDotnetHelpCommandShouldContainProperProcessInformation()
{
var proc = HelpCommand.ConfigureProcess("https://aka.ms/dotnet-build");
Assert.Equal("cmd", proc.StartInfo.FileName);
Assert.EndsWith("cmd.exe", proc.StartInfo.FileName);
Assert.Equal("/c start https://aka.ms/dotnet-build", proc.StartInfo.Arguments);
}

Expand All @@ -144,7 +144,7 @@ public void WhenRunOnLinuxDotnetHelpCommandShouldContainProperProcessInformation
public void WhenRunOnMacOsDotnetHelpCommandShouldContainProperProcessInformation()
{
var proc = HelpCommand.ConfigureProcess("https://aka.ms/dotnet-build");
Assert.Equal("open", proc.StartInfo.FileName);
Assert.EndsWith("open", proc.StartInfo.FileName);
Assert.Equal("https://aka.ms/dotnet-build", proc.StartInfo.Arguments);
}
}
Expand Down
4 changes: 2 additions & 2 deletions src/Tests/dotnet.Tests/dotnet-msbuild/GivenForwardingApp.cs
Original file line number Diff line number Diff line change
Expand Up @@ -17,14 +17,14 @@ public class GivenForwardingApp
public void DotnetExeIsExecuted()
{
new ForwardingApp("<apppath>", new string[0])
.GetProcessStartInfo().FileName.Should().Be("dotnet.exe");
.GetProcessStartInfo().FileName.Should().EndWith("dotnet.exe");
}

[UnixOnlyFact]
public void DotnetIsExecuted()
{
new ForwardingApp("<apppath>", new string[0])
.GetProcessStartInfo().FileName.Should().Be("dotnet");
.GetProcessStartInfo().FileName.Should().EndWith("dotnet");
}

[Fact]
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -23,15 +23,15 @@ public void DotnetExeIsExecuted()
{
var msbuildPath = "<msbuildpath>";
new MSBuildForwardingApp(new string[0], msbuildPath)
.GetProcessStartInfo().FileName.Should().Be("dotnet.exe");
.GetProcessStartInfo().FileName.Should().EndWith("dotnet.exe");
}

[UnixOnlyFact]
public void DotnetIsExecuted()
{
var msbuildPath = "<msbuildpath>";
new MSBuildForwardingApp(new string[0], msbuildPath)
.GetProcessStartInfo().FileName.Should().Be("dotnet");
.GetProcessStartInfo().FileName.Should().EndWith("dotnet");
}

[Theory]
Expand Down

0 comments on commit 3ac4443

Please sign in to comment.