cake-build / cake-build/cake

DeleteDirectory frequently throws "The directory is not empty" exception on medium sized directory trees

Open
#2,720 3 comments 0 reactions 0 assignees View on GitHub
Improvement
Dominant language
C#
Stars
4.2k
Forks
778
Avg merge
3h 37m
Merged PRs (30d)
21

Description

### What You Are Seeing?
Frequent exception trying to delete a directory with 72 subdirectories with a couple subdirectories each (NuGet cache)
```
DeleteDirectory(nuGetCacheFolder, new DeleteDirectorySettings {Recursive = true, Force = true});
```
There are originally 72 folders, each with four layers deep, with the last layer having up to three folders (eg. \destructurama.jsonnet\1.2.1\lib\[net452|netcoreapp1.1|netstandard2.0). When I look in the folder after a failure, most of the subdirectories have been deleted, but one or more are left, and they can be different ones. If I just run the script again, it works.

In order to make my builds more dependable, I ended up putting it in a try/catch and doing it again in the catch:
```
try
{
DeleteDirectory(nuGetCacheFolder, new DeleteDirectorySettings {Recursive = true, Force = true});
}
catch (Exception)
{
// It sometimes fails to delete everything, give it one more try
DeleteDirectory(nuGetCacheFolder, new DeleteDirectorySettings {Recursive = true, Force = true});
}
```

### What is Expected?
Successful deletion of directory tree

### What version of Cake are you using?
0.32.1

### Are you running on a 32 or 64 bit system?
64

### What environment are you running on? Windows? Linux? Mac?
Windows 10 Pro

### Are you running on a CI Server? If so, which one?
Yes, but no problem there since they are to a clean workspace every build

### How Did You Get This To Happen? (Steps to Reproduce)
Clean task attempts to delete local NuGet cache

### Output Log
```
Error:
One or more errors occurred.
The directory is not empty.
```

Contributor guide

Open the contributing guide

Research direction

Start from the DeleteDirectory call shown in the report and reproduce the failure on Windows 10 with a medium-sized nested directory tree, including the NuGet cache layout described. The work is complete when a recursive, forced deletion reliably removes the entire directory tree without requiring a second attempt.

Written by the indexing model from the issue text.

Assessment

Tech stack
csharp
Domain
build-system, tooling
Issue type
Bug
Difficulty
3/5
Estimated time
1-2 days
Activity status
Stale
Clarity
Mostly clear
Newbie friendliness
45/100

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.