Skip to content

Commit 98cc46f

Browse files
authored
Fix potential long path issues with pipes for dotnet test (#50687)
1 parent 5832911 commit 98cc46f

File tree

3 files changed

+12
-57
lines changed

3 files changed

+12
-57
lines changed

src/Cli/dotnet/Commands/Test/MTP/IPC/NamedPipeServer.cs

Lines changed: 9 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,8 @@ namespace Microsoft.DotNet.Cli.Commands.Test.IPC;
1212

1313
internal sealed class NamedPipeServer : NamedPipeBase
1414
{
15+
private static bool IsUnix => Path.DirectorySeparatorChar == '/';
16+
1517
private readonly Func<NamedPipeServer, IRequest, Task<IResponse>> _callback;
1618
private readonly NamedPipeServerStream _namedPipeServerStream;
1719
private readonly CancellationToken _cancellationToken;
@@ -24,20 +26,18 @@ internal sealed class NamedPipeServer : NamedPipeBase
2426
private bool _disposed;
2527

2628
public NamedPipeServer(
27-
PipeNameDescription pipeNameDescription,
29+
string pipeName,
2830
Func<NamedPipeServer, IRequest, Task<IResponse>> callback,
2931
int maxNumberOfServerInstances,
3032
CancellationToken cancellationToken,
3133
bool skipUnknownMessages)
3234
{
33-
_namedPipeServerStream = new((PipeName = pipeNameDescription).Name, PipeDirection.InOut, maxNumberOfServerInstances, PipeTransmissionMode.Byte, PipeOptions.Asynchronous | PipeOptions.CurrentUserOnly);
35+
_namedPipeServerStream = new(pipeName, PipeDirection.InOut, maxNumberOfServerInstances, PipeTransmissionMode.Byte, PipeOptions.Asynchronous | PipeOptions.CurrentUserOnly);
3436
_callback = callback;
3537
_cancellationToken = cancellationToken;
3638
_skipUnknownMessages = skipUnknownMessages;
3739
}
3840

39-
public PipeNameDescription PipeName { get; private set; }
40-
4141
public bool WasConnected { get; private set; }
4242

4343
public async Task WaitConnectionAsync(CancellationToken cancellationToken)
@@ -183,22 +183,15 @@ private async Task InternalLoopAsync(CancellationToken cancellationToken)
183183
}
184184
}
185185

186-
public static PipeNameDescription GetPipeName(string name)
186+
public static string GetPipeName(string name)
187187
{
188-
if (RuntimeInformation.IsOSPlatform(OSPlatform.Windows))
188+
if (!IsUnix)
189189
{
190-
return new PipeNameDescription($"testingplatform.pipe.{name.Replace('\\', '.')}", false);
190+
return $"testingplatform.pipe.{name.Replace('\\', '.')}";
191191
}
192192

193-
string directoryId = Path.Combine(Environment.GetFolderPath(Environment.SpecialFolder.LocalApplicationData), name);
194-
Directory.CreateDirectory(directoryId);
195-
return new PipeNameDescription(
196-
!Directory.Exists(directoryId)
197-
? throw new DirectoryNotFoundException(string.Format(
198-
CultureInfo.InvariantCulture,
199-
$"Directory: {directoryId} doesn't exist.",
200-
directoryId))
201-
: Path.Combine(directoryId, ".p"), true);
193+
// Similar to https://github.com/dotnet/roslyn/blob/99bf83c7bc52fa1ff27cf792db38755d5767c004/src/Compilers/Shared/NamedPipeUtil.cs#L26-L42
194+
return Path.Combine("/tmp", name);
202195
}
203196

204197
public void Dispose()
@@ -227,8 +220,6 @@ public void Dispose()
227220
// Ensure we are still disposing the resouces correctly, even if _loopTask completes with
228221
// an exception, or if the task doesn't complete within the 90 seconds limit.
229222
_namedPipeServerStream.Dispose();
230-
PipeName.Dispose();
231-
232223
_disposed = true;
233224
}
234225
}

src/Cli/dotnet/Commands/Test/MTP/IPC/PipeNameDescription.cs

Lines changed: 0 additions & 36 deletions
This file was deleted.

src/Cli/dotnet/Commands/Test/MTP/TestApplication.cs

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -25,7 +25,7 @@ internal sealed class TestApplication(
2525

2626
private readonly List<string> _outputData = [];
2727
private readonly List<string> _errorData = [];
28-
private readonly PipeNameDescription _pipeNameDescription = NamedPipeServer.GetPipeName(Guid.NewGuid().ToString("N"));
28+
private readonly string _pipeName = NamedPipeServer.GetPipeName(Guid.NewGuid().ToString("N"));
2929
private readonly CancellationTokenSource _cancellationToken = new();
3030

3131
private Task _testAppPipeConnectionLoop;
@@ -140,7 +140,7 @@ private string GetArguments()
140140
builder.Append($" {ArgumentEscaper.EscapeSingleArg(arg)}");
141141
}
142142

143-
builder.Append($" {CliConstants.ServerOptionKey} {CliConstants.ServerOptionValue} {CliConstants.DotNetTestPipeOptionKey} {ArgumentEscaper.EscapeSingleArg(_pipeNameDescription.Name)}");
143+
builder.Append($" {CliConstants.ServerOptionKey} {CliConstants.ServerOptionValue} {CliConstants.DotNetTestPipeOptionKey} {ArgumentEscaper.EscapeSingleArg(_pipeName)}");
144144

145145
return builder.ToString();
146146
}
@@ -157,7 +157,7 @@ private async Task WaitConnectionAsync(CancellationToken token)
157157
{
158158
while (!token.IsCancellationRequested)
159159
{
160-
var pipeConnection = new NamedPipeServer(_pipeNameDescription, OnRequest, NamedPipeServerStream.MaxAllowedServerInstances, token, skipUnknownMessages: true);
160+
var pipeConnection = new NamedPipeServer(_pipeName, OnRequest, NamedPipeServerStream.MaxAllowedServerInstances, token, skipUnknownMessages: true);
161161
pipeConnection.RegisterAllSerializers();
162162

163163
await pipeConnection.WaitConnectionAsync(token);

0 commit comments

Comments
 (0)