Skip to content

Commit 1dc6d2d

Browse files
Avoid long path issues for pipes by @Youssef1313 in #6536 (backport to rel/3.10) (#6538)
Co-authored-by: Youssef1313 <youssefvictor00@gmail.com>
1 parent a9aac82 commit 1dc6d2d

File tree

2 files changed

+17
-42
lines changed

2 files changed

+17
-42
lines changed

src/Platform/Microsoft.Testing.Platform/IPC/NamedPipeServer.cs

Lines changed: 14 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -22,6 +22,8 @@ internal sealed class NamedPipeServer : NamedPipeBase, IServer
2222
;
2323
#pragma warning restore CA1416 // Validate platform compatibility
2424

25+
private static bool IsUnix => Path.DirectorySeparatorChar == '/';
26+
2527
private readonly Func<IRequest, Task<IResponse>> _callback;
2628
private readonly IEnvironment _environment;
2729
private readonly NamedPipeServerStream _namedPipeServerStream;
@@ -277,32 +279,24 @@ private async Task InternalLoopAsync(CancellationToken cancellationToken)
277279
// If core MTP is updated, but old version of TRX is still used, it will try to call this overload at runtime.
278280
// Without it, MissingMethodException will be thrown at runtime.
279281
public static PipeNameDescription GetPipeName(string name)
280-
=> GetPipeName(name, new SystemEnvironment());
281-
282-
public static PipeNameDescription GetPipeName(string name, IEnvironment environment)
283282
{
284-
if (RuntimeInformation.IsOSPlatform(OSPlatform.Windows))
283+
if (!IsUnix)
285284
{
286-
return new PipeNameDescription($"testingplatform.pipe.{name.Replace('\\', '.')}", false);
285+
return new PipeNameDescription($"testingplatform.pipe.{name.Replace('\\', '.')}");
287286
}
288287

289-
#pragma warning disable RS0030 // Do not use banned APIs - We are using IEnvironment, but we still need the enum from the Environment class in BCL. This is safe.
290-
string directoryId = Path.Combine(environment.GetFolderPath(Environment.SpecialFolder.LocalApplicationData, Environment.SpecialFolderOption.None), name);
291-
#pragma warning disable RS0030 // Do not use banned APIs
292-
Directory.CreateDirectory(directoryId);
293-
return new PipeNameDescription(
294-
!Directory.Exists(directoryId)
295-
? throw new DirectoryNotFoundException(string.Format(
296-
CultureInfo.InvariantCulture,
297-
#if PLATFORM_MSBUILD
298-
$"Directory: {directoryId} doesn't exist.",
299-
#else
300-
PlatformResources.CouldNotFindDirectoryErrorMessage,
301-
#endif
302-
directoryId))
303-
: Path.Combine(directoryId, ".p"), true);
288+
// Similar to https://github.com/dotnet/roslyn/blob/99bf83c7bc52fa1ff27cf792db38755d5767c004/src/Compilers/Shared/NamedPipeUtil.cs#L26-L42
289+
return new PipeNameDescription(Path.Combine("/tmp", name));
304290
}
305291

292+
// For compatibility only.
293+
// Old versions of MTP used to have this overload without IEnvironment.
294+
// Extensions (e.g, TRX) calls into this overload.
295+
// If core MTP is updated, but old version of TRX is still used, it will try to call this overload at runtime.
296+
// Without it, MissingMethodException will be thrown at runtime.
297+
public static PipeNameDescription GetPipeName(string name, IEnvironment _)
298+
=> GetPipeName(name);
299+
306300
public void Dispose()
307301
{
308302
if (_disposed)

src/Platform/Microsoft.Testing.Platform/IPC/PipeNameDescription.cs

Lines changed: 3 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -3,32 +3,13 @@
33

44
namespace Microsoft.Testing.Platform.IPC;
55

6-
internal sealed class PipeNameDescription(string name, bool isDirectory) : IDisposable
6+
internal sealed class PipeNameDescription(string name) : IDisposable
77
{
8-
private readonly bool _isDirectory = isDirectory;
9-
private bool _disposed;
10-
118
public string Name { get; } = name;
129

10+
// This is available via IVT.
11+
// Avoid removing it as it can be seen as a binary breaking change when users use newer version of core MTP but older version of one of the extensions.
1312
public void Dispose()
1413
{
15-
if (_disposed)
16-
{
17-
return;
18-
}
19-
20-
if (_isDirectory)
21-
{
22-
try
23-
{
24-
Directory.Delete(Path.GetDirectoryName(Name)!, true);
25-
}
26-
catch (IOException)
27-
{
28-
// This folder is created inside the temp directory and will be cleaned up eventually by the OS
29-
}
30-
}
31-
32-
_disposed = true;
3314
}
3415
}

0 commit comments

Comments
 (0)