Skip to content

@TempDir cleanup does not handle Windows junctions correctly #4299

@quintesse

Description

@quintesse

Using junit 5.11.4

It's a very specific situation but the cleanup of a directory created with @TempDir will fail on Windows if it contains junctions that point to a directory that no longer exists. The following code reproduces the issue:

@Test
void testTempDirWithJunction(@TempDir Path dir) throws IOException, InterruptedException {
    Path test = dir.resolve("test");
    Files.createDirectory(test);
    Path link = dir.resolve("link");
    // This creates a Windows "junction" which you can't do with Java code
    Runtime.getRuntime().exec(String.format("cmd.exe /c mklink /j %s %s", link.toString(), test.toString())).waitFor();
    // The error might also occur without the source folder being deleted
    // but it depends on the order that the TempDir cleanup does its work,
    // so the following line forces the error to occur always
    Files.delete(test);
}

Why are junctions used instead of plain symbolic (or hard) links? Well on Windows you need special privileges to create links which not everybody has, while junctions are allowed by default. (Btw, junctions can only be used for directories, not files!)

Perhaps the easiest way to solve this would be to add an ignoreCleanupErrors options to @TempDir, which would continue trying to delete files and not throw any errors, because not being able to fully delete a temp folder might not be seen as a test failure in many cases. (But of course junit should also be able to handle this situation properly and delete broken junctions).

Metadata

Metadata

Assignees

Type

Projects

No projects

Milestone

Relationships

None yet

Development

No branches or pull requests

Issue actions