Skip to content

Commit afc85c7

Browse files
committed
Fix up copilot code for dnx shim
Use top level statements and argument escaper
1 parent 4edf4ff commit afc85c7

File tree

2 files changed

+44
-47
lines changed

2 files changed

+44
-47
lines changed

src/Layout/dnx/dnx.cs

Lines changed: 40 additions & 47 deletions
Original file line numberDiff line numberDiff line change
@@ -4,56 +4,49 @@
44
using System;
55
using System.Diagnostics;
66
using System.IO;
7+
using Microsoft.DotNet.Cli.Utils;
78

8-
class Program
9+
10+
// Get the path to the current executable
11+
string? exePath = Environment.ProcessPath;
12+
if (exePath == null)
913
{
10-
static int Main(string[] args)
11-
{
12-
// Get the path to the current executable
13-
string? exePath = Process.GetCurrentProcess().MainModule?.FileName;
14-
if (exePath == null)
15-
{
16-
Console.Error.WriteLine("Could not determine the path to the current executable.");
17-
return 1;
18-
}
19-
string? exeDir = Path.GetDirectoryName(exePath);
20-
if (exeDir == null)
21-
{
22-
Console.Error.WriteLine("Could not determine the directory of the current executable.");
23-
return 1;
24-
}
25-
// Path to dotnet in the same directory
26-
string dotnetPath = Path.Combine(exeDir, "dotnet");
14+
Console.Error.WriteLine("Could not determine the path to the current executable.");
15+
return 1;
16+
}
17+
string? exeDir = Path.GetDirectoryName(exePath);
18+
if (exeDir == null)
19+
{
20+
Console.Error.WriteLine("Could not determine the directory of the current executable.");
21+
return 1;
22+
}
23+
// Path to dotnet in the same directory
24+
string dotnetPath = Path.Combine(exeDir, "dotnet");
2725
#if WINDOWS
28-
dotnetPath += ".exe";
26+
dotnetPath += ".exe";
2927
#endif
30-
// Build argument list: "dnx" + all args
31-
string arguments = "dnx";
32-
if (args.Length > 0)
33-
{
34-
arguments += " " + string.Join(" ", args.Select(a => $"\"{a.Replace("\"", "\\\"")}"));
35-
}
36-
var psi = new ProcessStartInfo
37-
{
38-
FileName = dotnetPath,
39-
Arguments = arguments,
40-
UseShellExecute = false
41-
};
42-
try
43-
{
44-
using var process = Process.Start(psi);
45-
if (process == null)
46-
{
47-
Console.Error.WriteLine($"Failed to start process: {dotnetPath}");
48-
return 1;
49-
}
50-
process.WaitForExit();
51-
return process.ExitCode;
52-
}
53-
catch (Exception ex)
54-
{
55-
Console.Error.WriteLine($"Error launching process: {ex.Message}");
56-
return 1;
57-
}
28+
29+
string argumentsToForward = ArgumentEscaper.EscapeAndConcatenateArgArrayForProcessStart(["dnx", ..args]);
30+
31+
var psi = new ProcessStartInfo
32+
{
33+
FileName = dotnetPath,
34+
Arguments = argumentsToForward,
35+
UseShellExecute = false
36+
};
37+
try
38+
{
39+
using var process = Process.Start(psi);
40+
if (process == null)
41+
{
42+
Console.Error.WriteLine($"Failed to start process: {dotnetPath}");
43+
return 1;
5844
}
45+
process.WaitForExit();
46+
return process.ExitCode;
47+
}
48+
catch (Exception ex)
49+
{
50+
Console.Error.WriteLine($"Error launching process: {ex.ToString()}");
51+
return 1;
5952
}

src/Layout/dnx/dnx.csproj

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,10 @@
1212
<_IsPublishing>true</_IsPublishing>
1313
</PropertyGroup>
1414

15+
<ItemGroup>
16+
<Compile Include="$(RepoRoot)src\Cli\Microsoft.DotNet.Cli.Utils\ArgumentEscaper.cs" />
17+
</ItemGroup>
18+
1519
<Target Name="PublishOnBuild" AfterTargets="Build" DependsOnTargets="Publish" Condition="'$(PublishAot)' == 'true'" />
1620

1721
</Project>

0 commit comments

Comments
 (0)