Skip to content
Open
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@
using System.Collections.Generic;
using System.Diagnostics;
using System.IO.Enumeration;
using System.Runtime.CompilerServices;
using System.Text;
using Microsoft.Win32.SafeHandles;

Expand Down Expand Up @@ -496,6 +497,8 @@ private static void RemoveDirectoryRecursive(string fullPath)
{
if (isDirectory)
{
// Since this is a recursive call, we have to ensure that we have sufficient stack space.
RuntimeHelpers.EnsureSufficientExecutionStack();
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I'm a little concerned this is going to start introducing failures that weren't there before. Previously if we recurred too deeply the process would crash with a stack overflow, but EnsureSufficientExecutionStack is fairly conservative about what "sufficient execution stack" means, and it could end up throwing an exception well before the process would otherwise crash. I expect there are some cases today of very nested directories that are working today because they're not deep enough to cause a crash but would start throwing an exception with this because they are deep enough to hit the stack execution depth limit.

Alternatives might include:

  • Switching the whole implementation to be iterative with a manually-managed stack rather than recursive.
  • Only doing so once TryEnsureSufficientExecutionStack returns false.
  • When TryEnsureSufficientExecutionStack returns false, fork off an additional thread to continue the recursive processing.

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Thank yoju for the feedback, @stephentoub ! 👍
Let me refactor it.

RemoveDirectoryRecursive(childPath);
}
else
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@
using System.Buffers;
using System.ComponentModel;
using System.Diagnostics;
using System.Runtime.CompilerServices;
using System.Runtime.InteropServices;
using Microsoft.Win32.SafeHandles;

Expand Down Expand Up @@ -344,6 +345,8 @@ private static void RemoveDirectoryRecursive(string fullPath, ref Interop.Kernel
// Not a reparse point, or the reparse point isn't a name surrogate, recurse.
try
{
// Since this is a recursive call, we have to ensure that we have sufficient stack space.
RuntimeHelpers.EnsureSufficientExecutionStack();
RemoveDirectoryRecursive(
Path.Combine(fullPath, fileName),
findData: ref findData,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -288,6 +288,24 @@
Delete(rootDirectory, recursive: true);
}

[Fact]
[PlatformSpecific(TestPlatforms.Windows)]
Copy link

Copilot AI Feb 17, 2026

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This test creates 9000 nested directories which makes it very slow (similar to RecursiveDelete_DeepNesting which creates 2000 nested directories and is marked as [OuterLoop]). Consider adding the [OuterLoop] attribute to prevent this test from running in the default test suite, similar to the pattern used in RecursiveDelete_DeepNesting at line 266.

Suggested change
[PlatformSpecific(TestPlatforms.Windows)]
[PlatformSpecific(TestPlatforms.Windows)]
[OuterLoop("This test is very slow.", ~TestPlatforms.Browser)]

Copilot uses AI. Check for mistakes.
public void RecursiveDelete_Issue84344()
{
// Test the scenario described in GitHub issue #84344 where the nesting depth of subdirectories is huge (14000).
// Using 14000 as the depth makes the test very slow, so we reduce the depth to 9000 for quicker execution while still
// exercising a very deep directory tree. See https://github.com/dotnet/runtime/issues/84344.
// The original scenario and this test are limited to Windows; on Unix-like systems the much shorter maximum path length
// (for example, 4096 characters) would cause failures due to path length limits rather than testing Directory.Delete recursion.
DirectoryInfo subDir = testDir.CreateSubdirectory("test_issue84344");

Check failure on line 300 in src/libraries/System.Runtime/tests/System.IO.FileSystem.Tests/Directory/Delete.cs

View check run for this annotation

Azure Pipelines / runtime (Build linux-arm64 Release NativeAOT_Libraries)

src/libraries/System.Runtime/tests/System.IO.FileSystem.Tests/Directory/Delete.cs#L300

src/libraries/System.Runtime/tests/System.IO.FileSystem.Tests/Directory/Delete.cs(300,36): error CS0103: (NETCORE_ENGINEERING_TELEMETRY=Build) The name 'testDir' does not exist in the current context

Check failure on line 300 in src/libraries/System.Runtime/tests/System.IO.FileSystem.Tests/Directory/Delete.cs

View check run for this annotation

Azure Pipelines / runtime (Build linux-x64 Debug Mono_MiniJIT_LibrariesTests)

src/libraries/System.Runtime/tests/System.IO.FileSystem.Tests/Directory/Delete.cs#L300

src/libraries/System.Runtime/tests/System.IO.FileSystem.Tests/Directory/Delete.cs(300,36): error CS0103: (NETCORE_ENGINEERING_TELEMETRY=Build) The name 'testDir' does not exist in the current context

Check failure on line 300 in src/libraries/System.Runtime/tests/System.IO.FileSystem.Tests/Directory/Delete.cs

View check run for this annotation

Azure Pipelines / runtime (Build linux-x64 Debug Mono_Interpreter_LibrariesTests)

src/libraries/System.Runtime/tests/System.IO.FileSystem.Tests/Directory/Delete.cs#L300

src/libraries/System.Runtime/tests/System.IO.FileSystem.Tests/Directory/Delete.cs(300,36): error CS0103: (NETCORE_ENGINEERING_TELEMETRY=Build) The name 'testDir' does not exist in the current context

Check failure on line 300 in src/libraries/System.Runtime/tests/System.IO.FileSystem.Tests/Directory/Delete.cs

View check run for this annotation

Azure Pipelines / runtime (Build linux-arm64 Debug Mono_MiniJIT_LibrariesTests)

src/libraries/System.Runtime/tests/System.IO.FileSystem.Tests/Directory/Delete.cs#L300

src/libraries/System.Runtime/tests/System.IO.FileSystem.Tests/Directory/Delete.cs(300,36): error CS0103: (NETCORE_ENGINEERING_TELEMETRY=Build) The name 'testDir' does not exist in the current context

Check failure on line 300 in src/libraries/System.Runtime/tests/System.IO.FileSystem.Tests/Directory/Delete.cs

View check run for this annotation

Azure Pipelines / runtime (Build browser-wasm linux Release LibraryTests_EAT)

src/libraries/System.Runtime/tests/System.IO.FileSystem.Tests/Directory/Delete.cs#L300

src/libraries/System.Runtime/tests/System.IO.FileSystem.Tests/Directory/Delete.cs(300,36): error CS0103: (NETCORE_ENGINEERING_TELEMETRY=Build) The name 'testDir' does not exist in the current context

Check failure on line 300 in src/libraries/System.Runtime/tests/System.IO.FileSystem.Tests/Directory/Delete.cs

View check run for this annotation

Azure Pipelines / runtime (Build linux_musl-arm checked CoreCLR_ReleaseLibraries)

src/libraries/System.Runtime/tests/System.IO.FileSystem.Tests/Directory/Delete.cs#L300

src/libraries/System.Runtime/tests/System.IO.FileSystem.Tests/Directory/Delete.cs(300,36): error CS0103: (NETCORE_ENGINEERING_TELEMETRY=Build) The name 'testDir' does not exist in the current context

Check failure on line 300 in src/libraries/System.Runtime/tests/System.IO.FileSystem.Tests/Directory/Delete.cs

View check run for this annotation

Azure Pipelines / runtime (Build browser-wasm linux Release LibraryTests)

src/libraries/System.Runtime/tests/System.IO.FileSystem.Tests/Directory/Delete.cs#L300

src/libraries/System.Runtime/tests/System.IO.FileSystem.Tests/Directory/Delete.cs(300,36): error CS0103: (NETCORE_ENGINEERING_TELEMETRY=Build) The name 'testDir' does not exist in the current context

Check failure on line 300 in src/libraries/System.Runtime/tests/System.IO.FileSystem.Tests/Directory/Delete.cs

View check run for this annotation

Azure Pipelines / runtime (Build linux-x64 Debug CoreCLR_Libraries)

src/libraries/System.Runtime/tests/System.IO.FileSystem.Tests/Directory/Delete.cs#L300

src/libraries/System.Runtime/tests/System.IO.FileSystem.Tests/Directory/Delete.cs(300,36): error CS0103: (NETCORE_ENGINEERING_TELEMETRY=Build) The name 'testDir' does not exist in the current context

Check failure on line 300 in src/libraries/System.Runtime/tests/System.IO.FileSystem.Tests/Directory/Delete.cs

View check run for this annotation

Azure Pipelines / runtime (Build linux_musl-x64 Debug CoreCLR_Libraries)

src/libraries/System.Runtime/tests/System.IO.FileSystem.Tests/Directory/Delete.cs#L300

src/libraries/System.Runtime/tests/System.IO.FileSystem.Tests/Directory/Delete.cs(300,36): error CS0103: (NETCORE_ENGINEERING_TELEMETRY=Build) The name 'testDir' does not exist in the current context

Check failure on line 300 in src/libraries/System.Runtime/tests/System.IO.FileSystem.Tests/Directory/Delete.cs

View check run for this annotation

Azure Pipelines / runtime (Build linux_musl-arm64 checked CoreCLR_ReleaseLibraries)

src/libraries/System.Runtime/tests/System.IO.FileSystem.Tests/Directory/Delete.cs#L300

src/libraries/System.Runtime/tests/System.IO.FileSystem.Tests/Directory/Delete.cs(300,36): error CS0103: (NETCORE_ENGINEERING_TELEMETRY=Build) The name 'testDir' does not exist in the current context

Check failure on line 300 in src/libraries/System.Runtime/tests/System.IO.FileSystem.Tests/Directory/Delete.cs

View check run for this annotation

Azure Pipelines / runtime (Build linux_musl-x64 Debug Libraries_CheckedCoreCLR)

src/libraries/System.Runtime/tests/System.IO.FileSystem.Tests/Directory/Delete.cs#L300

src/libraries/System.Runtime/tests/System.IO.FileSystem.Tests/Directory/Delete.cs(300,36): error CS0103: (NETCORE_ENGINEERING_TELEMETRY=Build) The name 'testDir' does not exist in the current context

Check failure on line 300 in src/libraries/System.Runtime/tests/System.IO.FileSystem.Tests/Directory/Delete.cs

View check run for this annotation

Azure Pipelines / runtime (Build linux-arm64 Debug Libraries_CheckedCoreCLR)

src/libraries/System.Runtime/tests/System.IO.FileSystem.Tests/Directory/Delete.cs#L300

src/libraries/System.Runtime/tests/System.IO.FileSystem.Tests/Directory/Delete.cs(300,36): error CS0103: (NETCORE_ENGINEERING_TELEMETRY=Build) The name 'testDir' does not exist in the current context

Check failure on line 300 in src/libraries/System.Runtime/tests/System.IO.FileSystem.Tests/Directory/Delete.cs

View check run for this annotation

Azure Pipelines / runtime (Build linux-x64 checked CoreCLR_ReleaseLibraries)

src/libraries/System.Runtime/tests/System.IO.FileSystem.Tests/Directory/Delete.cs#L300

src/libraries/System.Runtime/tests/System.IO.FileSystem.Tests/Directory/Delete.cs(300,36): error CS0103: (NETCORE_ENGINEERING_TELEMETRY=Build) The name 'testDir' does not exist in the current context

Check failure on line 300 in src/libraries/System.Runtime/tests/System.IO.FileSystem.Tests/Directory/Delete.cs

View check run for this annotation

Azure Pipelines / runtime (Build osx-arm64 Release NativeAOT_Libraries)

src/libraries/System.Runtime/tests/System.IO.FileSystem.Tests/Directory/Delete.cs#L300

src/libraries/System.Runtime/tests/System.IO.FileSystem.Tests/Directory/Delete.cs(300,36): error CS0103: (NETCORE_ENGINEERING_TELEMETRY=Build) The name 'testDir' does not exist in the current context

Check failure on line 300 in src/libraries/System.Runtime/tests/System.IO.FileSystem.Tests/Directory/Delete.cs

View check run for this annotation

Azure Pipelines / runtime (Build osx-arm64 Debug CoreCLR_Libraries)

src/libraries/System.Runtime/tests/System.IO.FileSystem.Tests/Directory/Delete.cs#L300

src/libraries/System.Runtime/tests/System.IO.FileSystem.Tests/Directory/Delete.cs(300,36): error CS0103: (NETCORE_ENGINEERING_TELEMETRY=Build) The name 'testDir' does not exist in the current context

Check failure on line 300 in src/libraries/System.Runtime/tests/System.IO.FileSystem.Tests/Directory/Delete.cs

View check run for this annotation

Azure Pipelines / runtime (Build osx-x64 Debug Mono_MiniJIT_LibrariesTests)

src/libraries/System.Runtime/tests/System.IO.FileSystem.Tests/Directory/Delete.cs#L300

src/libraries/System.Runtime/tests/System.IO.FileSystem.Tests/Directory/Delete.cs(300,36): error CS0103: (NETCORE_ENGINEERING_TELEMETRY=Build) The name 'testDir' does not exist in the current context

Check failure on line 300 in src/libraries/System.Runtime/tests/System.IO.FileSystem.Tests/Directory/Delete.cs

View check run for this annotation

Azure Pipelines / runtime (Build osx-arm64 Debug Libraries_CheckedCoreCLR)

src/libraries/System.Runtime/tests/System.IO.FileSystem.Tests/Directory/Delete.cs#L300

src/libraries/System.Runtime/tests/System.IO.FileSystem.Tests/Directory/Delete.cs(300,36): error CS0103: (NETCORE_ENGINEERING_TELEMETRY=Build) The name 'testDir' does not exist in the current context
for (int i = 0; i < depth; i++)

Check failure on line 301 in src/libraries/System.Runtime/tests/System.IO.FileSystem.Tests/Directory/Delete.cs

View check run for this annotation

Azure Pipelines / runtime (Build linux-arm64 Release NativeAOT_Libraries)

src/libraries/System.Runtime/tests/System.IO.FileSystem.Tests/Directory/Delete.cs#L301

src/libraries/System.Runtime/tests/System.IO.FileSystem.Tests/Directory/Delete.cs(301,33): error CS0103: (NETCORE_ENGINEERING_TELEMETRY=Build) The name 'depth' does not exist in the current context

Check failure on line 301 in src/libraries/System.Runtime/tests/System.IO.FileSystem.Tests/Directory/Delete.cs

View check run for this annotation

Azure Pipelines / runtime (Build linux-x64 Debug Mono_MiniJIT_LibrariesTests)

src/libraries/System.Runtime/tests/System.IO.FileSystem.Tests/Directory/Delete.cs#L301

src/libraries/System.Runtime/tests/System.IO.FileSystem.Tests/Directory/Delete.cs(301,33): error CS0103: (NETCORE_ENGINEERING_TELEMETRY=Build) The name 'depth' does not exist in the current context

Check failure on line 301 in src/libraries/System.Runtime/tests/System.IO.FileSystem.Tests/Directory/Delete.cs

View check run for this annotation

Azure Pipelines / runtime (Build linux-x64 Debug Mono_Interpreter_LibrariesTests)

src/libraries/System.Runtime/tests/System.IO.FileSystem.Tests/Directory/Delete.cs#L301

src/libraries/System.Runtime/tests/System.IO.FileSystem.Tests/Directory/Delete.cs(301,33): error CS0103: (NETCORE_ENGINEERING_TELEMETRY=Build) The name 'depth' does not exist in the current context

Check failure on line 301 in src/libraries/System.Runtime/tests/System.IO.FileSystem.Tests/Directory/Delete.cs

View check run for this annotation

Azure Pipelines / runtime (Build linux-arm64 Debug Mono_MiniJIT_LibrariesTests)

src/libraries/System.Runtime/tests/System.IO.FileSystem.Tests/Directory/Delete.cs#L301

src/libraries/System.Runtime/tests/System.IO.FileSystem.Tests/Directory/Delete.cs(301,33): error CS0103: (NETCORE_ENGINEERING_TELEMETRY=Build) The name 'depth' does not exist in the current context

Check failure on line 301 in src/libraries/System.Runtime/tests/System.IO.FileSystem.Tests/Directory/Delete.cs

View check run for this annotation

Azure Pipelines / runtime (Build browser-wasm linux Release LibraryTests_EAT)

src/libraries/System.Runtime/tests/System.IO.FileSystem.Tests/Directory/Delete.cs#L301

src/libraries/System.Runtime/tests/System.IO.FileSystem.Tests/Directory/Delete.cs(301,33): error CS0103: (NETCORE_ENGINEERING_TELEMETRY=Build) The name 'depth' does not exist in the current context

Check failure on line 301 in src/libraries/System.Runtime/tests/System.IO.FileSystem.Tests/Directory/Delete.cs

View check run for this annotation

Azure Pipelines / runtime (Build linux_musl-arm checked CoreCLR_ReleaseLibraries)

src/libraries/System.Runtime/tests/System.IO.FileSystem.Tests/Directory/Delete.cs#L301

src/libraries/System.Runtime/tests/System.IO.FileSystem.Tests/Directory/Delete.cs(301,33): error CS0103: (NETCORE_ENGINEERING_TELEMETRY=Build) The name 'depth' does not exist in the current context

Check failure on line 301 in src/libraries/System.Runtime/tests/System.IO.FileSystem.Tests/Directory/Delete.cs

View check run for this annotation

Azure Pipelines / runtime (Build browser-wasm linux Release LibraryTests)

src/libraries/System.Runtime/tests/System.IO.FileSystem.Tests/Directory/Delete.cs#L301

src/libraries/System.Runtime/tests/System.IO.FileSystem.Tests/Directory/Delete.cs(301,33): error CS0103: (NETCORE_ENGINEERING_TELEMETRY=Build) The name 'depth' does not exist in the current context

Check failure on line 301 in src/libraries/System.Runtime/tests/System.IO.FileSystem.Tests/Directory/Delete.cs

View check run for this annotation

Azure Pipelines / runtime (Build linux-x64 Debug CoreCLR_Libraries)

src/libraries/System.Runtime/tests/System.IO.FileSystem.Tests/Directory/Delete.cs#L301

src/libraries/System.Runtime/tests/System.IO.FileSystem.Tests/Directory/Delete.cs(301,33): error CS0103: (NETCORE_ENGINEERING_TELEMETRY=Build) The name 'depth' does not exist in the current context

Check failure on line 301 in src/libraries/System.Runtime/tests/System.IO.FileSystem.Tests/Directory/Delete.cs

View check run for this annotation

Azure Pipelines / runtime (Build linux_musl-x64 Debug CoreCLR_Libraries)

src/libraries/System.Runtime/tests/System.IO.FileSystem.Tests/Directory/Delete.cs#L301

src/libraries/System.Runtime/tests/System.IO.FileSystem.Tests/Directory/Delete.cs(301,33): error CS0103: (NETCORE_ENGINEERING_TELEMETRY=Build) The name 'depth' does not exist in the current context

Check failure on line 301 in src/libraries/System.Runtime/tests/System.IO.FileSystem.Tests/Directory/Delete.cs

View check run for this annotation

Azure Pipelines / runtime (Build linux_musl-arm64 checked CoreCLR_ReleaseLibraries)

src/libraries/System.Runtime/tests/System.IO.FileSystem.Tests/Directory/Delete.cs#L301

src/libraries/System.Runtime/tests/System.IO.FileSystem.Tests/Directory/Delete.cs(301,33): error CS0103: (NETCORE_ENGINEERING_TELEMETRY=Build) The name 'depth' does not exist in the current context

Check failure on line 301 in src/libraries/System.Runtime/tests/System.IO.FileSystem.Tests/Directory/Delete.cs

View check run for this annotation

Azure Pipelines / runtime (Build linux_musl-x64 Debug Libraries_CheckedCoreCLR)

src/libraries/System.Runtime/tests/System.IO.FileSystem.Tests/Directory/Delete.cs#L301

src/libraries/System.Runtime/tests/System.IO.FileSystem.Tests/Directory/Delete.cs(301,33): error CS0103: (NETCORE_ENGINEERING_TELEMETRY=Build) The name 'depth' does not exist in the current context

Check failure on line 301 in src/libraries/System.Runtime/tests/System.IO.FileSystem.Tests/Directory/Delete.cs

View check run for this annotation

Azure Pipelines / runtime (Build linux-arm64 Debug Libraries_CheckedCoreCLR)

src/libraries/System.Runtime/tests/System.IO.FileSystem.Tests/Directory/Delete.cs#L301

src/libraries/System.Runtime/tests/System.IO.FileSystem.Tests/Directory/Delete.cs(301,33): error CS0103: (NETCORE_ENGINEERING_TELEMETRY=Build) The name 'depth' does not exist in the current context

Check failure on line 301 in src/libraries/System.Runtime/tests/System.IO.FileSystem.Tests/Directory/Delete.cs

View check run for this annotation

Azure Pipelines / runtime (Build linux-x64 checked CoreCLR_ReleaseLibraries)

src/libraries/System.Runtime/tests/System.IO.FileSystem.Tests/Directory/Delete.cs#L301

src/libraries/System.Runtime/tests/System.IO.FileSystem.Tests/Directory/Delete.cs(301,33): error CS0103: (NETCORE_ENGINEERING_TELEMETRY=Build) The name 'depth' does not exist in the current context

Check failure on line 301 in src/libraries/System.Runtime/tests/System.IO.FileSystem.Tests/Directory/Delete.cs

View check run for this annotation

Azure Pipelines / runtime (Build osx-arm64 Release NativeAOT_Libraries)

src/libraries/System.Runtime/tests/System.IO.FileSystem.Tests/Directory/Delete.cs#L301

src/libraries/System.Runtime/tests/System.IO.FileSystem.Tests/Directory/Delete.cs(301,33): error CS0103: (NETCORE_ENGINEERING_TELEMETRY=Build) The name 'depth' does not exist in the current context

Check failure on line 301 in src/libraries/System.Runtime/tests/System.IO.FileSystem.Tests/Directory/Delete.cs

View check run for this annotation

Azure Pipelines / runtime (Build osx-arm64 Debug CoreCLR_Libraries)

src/libraries/System.Runtime/tests/System.IO.FileSystem.Tests/Directory/Delete.cs#L301

src/libraries/System.Runtime/tests/System.IO.FileSystem.Tests/Directory/Delete.cs(301,33): error CS0103: (NETCORE_ENGINEERING_TELEMETRY=Build) The name 'depth' does not exist in the current context

Check failure on line 301 in src/libraries/System.Runtime/tests/System.IO.FileSystem.Tests/Directory/Delete.cs

View check run for this annotation

Azure Pipelines / runtime (Build osx-x64 Debug Mono_MiniJIT_LibrariesTests)

src/libraries/System.Runtime/tests/System.IO.FileSystem.Tests/Directory/Delete.cs#L301

src/libraries/System.Runtime/tests/System.IO.FileSystem.Tests/Directory/Delete.cs(301,33): error CS0103: (NETCORE_ENGINEERING_TELEMETRY=Build) The name 'depth' does not exist in the current context

Check failure on line 301 in src/libraries/System.Runtime/tests/System.IO.FileSystem.Tests/Directory/Delete.cs

View check run for this annotation

Azure Pipelines / runtime (Build osx-arm64 Debug Libraries_CheckedCoreCLR)

src/libraries/System.Runtime/tests/System.IO.FileSystem.Tests/Directory/Delete.cs#L301

src/libraries/System.Runtime/tests/System.IO.FileSystem.Tests/Directory/Delete.cs(301,33): error CS0103: (NETCORE_ENGINEERING_TELEMETRY=Build) The name 'depth' does not exist in the current context
Comment on lines +291 to +301
Copy link

Copilot AI Feb 9, 2026

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This adds a very deep nesting (9000) using CreateSubdirectory in a tight loop, but the test isn't marked [OuterLoop]. This is likely to be very slow/flaky in CI and duplicates coverage with the existing RecursiveDelete_DeepNesting test just above. Consider reusing/adjusting the existing deep-nesting test (including its [OuterLoop] annotation) rather than adding a second long-running variant.

Copilot uses AI. Check for mistakes.
{
subDir = subDir.CreateSubdirectory("a");
}
// Now delete the subdir recursively.
Directory.Delete(subDir.FullName, recursive: true);
Comment on lines +300 to +306
Copy link

Copilot AI Feb 9, 2026

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This test currently deletes only the leaf directory: subDir is reassigned in the loop to the deepest "a" directory, so Directory.Delete(subDir.FullName, recursive: true) won't exercise recursive deletion and will leave thousands of parent directories behind. Keep a reference to the top of the created tree (e.g., the initial test_issue84344 directory or testDir) and delete that instead; also ensure cleanup on failure (e.g., via try/finally) to avoid polluting the test temp root.

Suggested change
DirectoryInfo subDir = testDir.CreateSubdirectory("test_issue84344");
for (int i = 0; i < depth; i++)
{
subDir = subDir.CreateSubdirectory("a");
}
// Now delete the subdir recursively.
Directory.Delete(subDir.FullName, recursive: true);
DirectoryInfo rootSubDir = testDir.CreateSubdirectory("test_issue84344");
DirectoryInfo currentSubDir = rootSubDir;
for (int i = 0; i < depth; i++)
{
currentSubDir = currentSubDir.CreateSubdirectory("a");
}
// Now delete the subdir recursively, starting from the top of the created tree.
try
{
Directory.Delete(rootSubDir.FullName, recursive: true);
}
finally
{
try
{
if (rootSubDir.Exists)
{
Directory.Delete(rootSubDir.FullName, recursive: true);
}
}
catch (IOException)
{
}
catch (UnauthorizedAccessException)
{
}
}

Copilot uses AI. Check for mistakes.
}

[Fact]
[PlatformSpecific(TestPlatforms.Windows)] // Recursive delete throws IOException if directory contains in-use file
public void RecursiveDelete_ShouldThrowIOExceptionIfContainedFileInUse()
Expand Down
Loading