Commit fdff381a56

Ryan Liptak <squeek502@hotmail.com>
2020-03-06 05:15:55
fmt: Fix relative paths with . and .. on Windows
This is a band-aid fix due to NtCreateFile failing on paths with . or .. in them.
1 parent 0df8288
Changed files (1)
src-self-hosted
src-self-hosted/stage2.zig
@@ -318,11 +318,18 @@ const FmtError = error{
 } || fs.File.OpenError;
 
 fn fmtPath(fmt: *Fmt, file_path: []const u8, check_mode: bool) FmtError!void {
-    if (fmt.seen.exists(file_path)) return;
-    try fmt.seen.put(file_path);
+    // get the real path here to avoid Windows failing on relative file paths with . or .. in them
+    var real_path = fs.realpathAlloc(fmt.allocator, file_path) catch |err| {
+        try stderr.print("unable to open '{}': {}\n", .{ file_path, err });
+        fmt.any_error = true;
+        return;
+    };
+    defer fmt.allocator.free(real_path);
+
+    if (fmt.seen.exists(real_path)) return;
+    try fmt.seen.put(real_path);
 
-    const max = std.math.maxInt(usize);
-    const source_code = fs.cwd().readFileAlloc(fmt.allocator, file_path, max) catch |err| switch (err) {
+    const source_code = fs.cwd().readFileAlloc(fmt.allocator, real_path, self_hosted_main.max_src_size) catch |err| switch (err) {
         error.IsDir, error.AccessDenied => {
             // TODO make event based (and dir.next())
             var dir = try fs.cwd().openDir(file_path, .{ .iterate = true });
@@ -332,7 +339,7 @@ fn fmtPath(fmt: *Fmt, file_path: []const u8, check_mode: bool) FmtError!void {
 
             while (try dir_it.next()) |entry| {
                 if (entry.kind == .Directory or mem.endsWith(u8, entry.name, ".zig")) {
-                    const full_path = try fs.path.join(fmt.allocator, &[_][]const u8{ file_path, entry.name });
+                    const full_path = try fs.path.join(fmt.allocator, &[_][]const u8{ real_path, entry.name });
                     try fmtPath(fmt, full_path, check_mode);
                 }
             }
@@ -370,7 +377,7 @@ fn fmtPath(fmt: *Fmt, file_path: []const u8, check_mode: bool) FmtError!void {
             fmt.any_error = true;
         }
     } else {
-        const baf = try io.BufferedAtomicFile.create(fmt.allocator, file_path);
+        const baf = try io.BufferedAtomicFile.create(fmt.allocator, real_path);
         defer baf.destroy();
 
         const anything_changed = try std.zig.render(fmt.allocator, baf.stream(), tree);