Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

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

Open
BuddhaBuddy1 opened this issue Feb 25, 2020 · 3 comments

Comments

@BuddhaBuddy1
Copy link

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.
@BuddhaBuddy1
Copy link
Author

This could be a problem with Visual Studio, as it only happens if Visual Studio is open. I think it is trying to track directories as they are created and deleted, which is somehow interfering with deletion of the files and folders.

@ilchenkob
Copy link

I had similar issue. It was reproducible only when I had my solution opened in Visual Studio.
My workaround is to add a call to DeleteFiles before removing a directory (because the exception message states: "The directory is not empty.").

DeleteFiles(Combine(pathToDirectory, @".\**\*.*"));
DeleteDirectory(pathToDirectory, new DeleteDirectorySettings
{
    Force = true,
    Recursive = true
});

@ilchenkob
Copy link

The workaround provided in my previous message doesn't work, after few days I've got the same error.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

No branches or pull requests

3 participants