Search code examples
windowsfilesystemszig

Zig's filesystem example doesn't work on windows


I am learning zig from [this site](https://ziglearn.org/chapter-2/). I'm on windows with wsl2 installed. This is zig version:

$ zig version
0.11.0-dev.2985+3f3b1a680

I use git bash for everything mentioned here.

I encountered a problem while learning about filesystem. This example doesn't work on my machine (CTRL+F "make dir" [here](https://ziglearn.org/chapter-2/) to find it):

test "make dir" {
    try std.fs.cwd().makeDir("test-tmp");
    const iter_dir = try std.fs.cwd().openIterableDir(
        "test-tmp",
        .{},
    );
    defer {
        std.fs.cwd().deleteTree("test-tmp") catch unreachable;
    }

    _ = try iter_dir.dir.createFile("x", .{});
    _ = try iter_dir.dir.createFile("y", .{});
    _ = try iter_dir.dir.createFile("z", .{});

    var file_count: usize = 0;
    var iter = iter_dir.iterate();
    while (try iter.next()) |entry| {
        if (entry.kind == .File) file_count += 1;
    }

    try expect(file_count == 3);
}

The cleanup line executes the catch unreachable, meaning that deleteTree(...) call fails in this line:

std.fs.cwd().deleteTree("test-tmp") catch unreachable;

Here is the error I see. Somewhere EBUSY error comes up.

C:\zig\zig-windows-x86_64-0.11.0-dev.2985+3f3b1a680\lib\std\os\windows.zig:935:31: 0x7ff656a5edc2 in DeleteFile (test.exe.obj)
        .SHARING_VIOLATION => return error.FileBusy,
                              ^
C:\zig\zig-windows-x86_64-0.11.0-dev.2985+3f3b1a680\lib\std\os.zig:2468:5: 0x7ff656a4c221 in unlinkatW (test.exe.obj)
    return windows.DeleteFile(sub_path_w, .{ .dir = dirfd, .remove_dir = remove_dir });
    ^
C:\zig\zig-windows-x86_64-0.11.0-dev.2985+3f3b1a680\lib\std\fs.zig:1914:25: 0x7ff656a18e0a in deleteDirW (test.exe.obj)
            else => |e| return e,
                        ^
C:\zig\zig-windows-x86_64-0.11.0-dev.2985+3f3b1a680\lib\std\fs.zig:1889:13: 0x7ff656a0c49e in deleteDir (test.exe.obj)
            return self.deleteDirW(sub_path_w.span());
            ^
C:\zig\zig-windows-x86_64-0.11.0-dev.2985+3f3b1a680\lib\std\fs.zig:2216:29: 0x7ff656a0377f in deleteTree (test.exe.obj)
                else => |e| return e,
                            ^
C:\Users\user\Documents\zigg\third-steps\src\stuff2.zig:74:51: 0x7ff656a02b77 in test.make dir (test.exe.obj)
        std.fs.cwd().deleteTree("test-tmp") catch unreachable;
                                                  ^
C:\zig\zig-windows-x86_64-0.11.0-dev.2985+3f3b1a680\lib\test_runner.zig:177:28: 0x7ff656a13b2e in mainTerminal (test.exe.obj)
        } else test_fn.func();
                           ^
C:\zig\zig-windows-x86_64-0.11.0-dev.2985+3f3b1a680\lib\test_runner.zig:37:28: 0x7ff656a04905 in main (test.exe.obj)
        return mainTerminal();
                           ^
C:\zig\zig-windows-x86_64-0.11.0-dev.2985+3f3b1a680\lib\std\start.zig:377:41: 0x7ff656a046a7 in WinStartup (test.exe.obj)
    std.debug.maybeEnableSegfaultHandler();
                                        ^
???:?:?: 0x7ffb3875269c in ??? (KERNEL32.DLL)
???:?:?: 0x7ffb39d8a9f7 in ??? (ntdll.dll)
error: the following test command failed with exit code 3:
C:\Users\user\Documents\zigg\third-steps\zig-cache\o\3d7c85ec680dad6ca22ab16356c3a336\test.exe

What could be the problem? The example is copied 1 to 1 from the learning resource. Is it unique to me, or does it not work for many people is also what I wonder (maybe the example is outdated).

What I tried: I tried running it, not much to say. I also wasn't able to find anything about "zig deleteTree not working" or "zig deleteTree EBUSY" online.
Maybe I should mention, that I remembered to remove the directory manually after test fails to do so.

Actually, only now I realized, that the directory is empty after test runs. This probably means that deleteTree removes the contents successfully. However, I expected the directory to be removed too, and no runtime errors.

Note: other tests from the learning resource I use up to this point worked fine.


Solution

  • It seems to be a bug in the example code. It's missing the close() call for iter_dir. Update the code like this:

        var iter_dir = try std.fs.cwd().openIterableDir(
            "test-tmp",
            .{},
        );
        defer {
            iter_dir.close();
            std.fs.cwd().deleteTree("test-tmp") catch unreachable;
        }