Zig: Zig fmt fails to open local-path files

Created on 2 Mar 2020  路  5Comments  路  Source: ziglang/zig

Zig 0.5.0+6a0927d8c on Windows

The file window.zig exists, but these fail. Just using zig fmt window.zig works fine.

PS C:\Projects\ZigVulkan> zig fmt .\window.zig
unable to open '.\window.zig': error.FileNotFound
PS C:\Projects\ZigVulkan> zig fmt ./window.zig
unable to open './window.zig': error.BadPathName

For the second one I guess it's the same as #2668

bug contributor friendly os-windows standard library

Most helpful comment

This can be closed now (fixed by #5187)

All 5 comments

As far as I can tell, NtCreateFile treats ObjectName literally and doesn't do any resolving, so it's actually searching for the impossible path C:\Projects\ZigVulkan\.\window.zig. To fix this particular issue, it seems like either paths need to always be resolved before calls to std.fs.openFileWindows or resolved within std.fs.openFileWindows (and other callers of NtCreateFile?).

Also worth noting that .. as a directory in ObjectName is invalid and gives an STATUS_OBJECT_NAME_INVALID error (0xC0000033):

  • zig fmt ..\test.zig -> error.Unexpected NTSTATUS=0xc0000033
  • zig fmt test\..\test.zig -> error.Unexpected NTSTATUS=0xc0000033
  • zig fmt test..\test.zig -> error.FileNotFound (accurate, that file didn't exist)

Pre-resolving has huge implications for following symlinks (called/using "reparse points" in NT-speak). I don't think its required, but will need to do some research.

Tiny reproduction:

const std = @import("std");

test "windows open file" {
    var first = try std.fs.cwd().openFile("test.zig", .{});
    first.close();

    var second = try std.fs.cwd().openFile(".\\test.zig", .{});
    second.close();
}

If test.zig exists, the first open succeeds, but the second fails, even though they would resolve to the same path.

So I did some research, and processing '..' on windows requires a dance where you use NtQueryInformationFile + NtOpenFile to traverse up a directory for each time you encounter '..'.
There is an example at: https://git.midipix.org/ntapi/tree/src/fs/ntapi_tt_open_physical_parent_directory.c

This can be closed now (fixed by #5187)

Was this page helpful?
0 / 5 - 0 ratings

Related issues

jorangreef picture jorangreef  路  3Comments

jorangreef picture jorangreef  路  3Comments

daurnimator picture daurnimator  路  3Comments

dobkeratops picture dobkeratops  路  3Comments

S0urc3C0de picture S0urc3C0de  路  3Comments