英文:
Zig's filesystem example doesn't work on windows
问题
我明白了。以下是你的中文翻译:
我正在从这个网站学习 Zig。我在装有 WSL2 的 Windows 上学习。这是我的 Zig 版本:
$ zig version
0.11.0-dev.2985+3f3b1a680
我在所有地方都使用 Git Bash。
在学习有关文件系统的过程中,我遇到了一个问题。在我的机器上,这个例子无法工作(在这里搜索“make dir”来找到它):
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);
}
清理行执行了 catch unreachable
,这意味着在这一行中 deleteTree(...)
调用失败:
std.fs.cwd().deleteTree("test-tmp") catch unreachable;
这是我看到的错误。在某处会出现 EBUSY 错误。
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
可能的问题是什么?这个例子被完全复制自学习资源。我想知道这是否只是对我来说独特的情况,还是对许多人来说也无法正常工作(也许这个例子已经过时)。
我尝试过的:我尝试运行它,没有什么特别的。我也无法在线找到关于“zig deleteTree not working”或“zig deleteTree EBUSY”的信息。也许我应该提到,我记得在测试失败后手动删除目录。
实际上,我现在才意识到,测试运行后目录是空的。这可能意味着 deleteTree
成功地删除了内容。然而,我预期目录也会被删除,并且没有运行时错误。
注意:直到目前为止,我使用的学习资源中的其他测试都正常运行。
英文:
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.
答案1
得分: 2
这似乎是示例代码中的一个错误。它缺少对 iter_dir
的 close()
调用。请将代码更新如下:
var iter_dir = try std.fs.cwd().openIterableDir(
"test-tmp",
.{},
);
defer {
iter_dir.close();
std.fs.cwd().deleteTree("test-tmp") catch unreachable;
}
英文:
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;
}
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论