| ... | @@ -318,11 +318,18 @@ const FmtError = error{ | ... | @@ -318,11 +318,18 @@ const FmtError = error{ |
| 318 | } || fs.File.OpenError; | 318 | } || fs.File.OpenError; |
| 319 | | 319 | |
| 320 | fn fmtPath(fmt: *Fmt, file_path: []const u8, check_mode: bool) FmtError!void { | 320 | fn fmtPath(fmt: *Fmt, file_path: []const u8, check_mode: bool) FmtError!void { |
| 321 | if (fmt.seen.exists(file_path)) return; | 321 | // get the real path here to avoid Windows failing on relative file paths with . or .. in them |
| 322 | try fmt.seen.put(file_path); | 322 | var real_path = fs.realpathAlloc(fmt.allocator, file_path) catch |err| { |
| | 323 | try stderr.print("unable to open '{}': {}\n", .{ file_path, err }); |
| | 324 | fmt.any_error = true; |
| | 325 | return; |
| | 326 | }; |
| | 327 | defer fmt.allocator.free(real_path); |
| | 328 | |
| | 329 | if (fmt.seen.exists(real_path)) return; |
| | 330 | try fmt.seen.put(real_path); |
| 323 | | 331 | |
| 324 | const max = std.math.maxInt(usize); | 332 | const source_code = fs.cwd().readFileAlloc(fmt.allocator, real_path, self_hosted_main.max_src_size) catch |err| switch (err) { |
| 325 | const source_code = fs.cwd().readFileAlloc(fmt.allocator, file_path, max) catch |err| switch (err) { | | |
| 326 | error.IsDir, error.AccessDenied => { | 333 | error.IsDir, error.AccessDenied => { |
| 327 | // TODO make event based (and dir.next()) | 334 | // TODO make event based (and dir.next()) |
| 328 | var dir = try fs.cwd().openDir(file_path, .{ .iterate = true }); | 335 | 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 { | ... | @@ -332,7 +339,7 @@ fn fmtPath(fmt: *Fmt, file_path: []const u8, check_mode: bool) FmtError!void { |
| 332 | | 339 | |
| 333 | while (try dir_it.next()) |entry| { | 340 | while (try dir_it.next()) |entry| { |
| 334 | if (entry.kind == .Directory or mem.endsWith(u8, entry.name, ".zig")) { | 341 | if (entry.kind == .Directory or mem.endsWith(u8, entry.name, ".zig")) { |
| 335 | const full_path = try fs.path.join(fmt.allocator, &[_][]const u8{ file_path, entry.name }); | 342 | const full_path = try fs.path.join(fmt.allocator, &[_][]const u8{ real_path, entry.name }); |
| 336 | try fmtPath(fmt, full_path, check_mode); | 343 | try fmtPath(fmt, full_path, check_mode); |
| 337 | } | 344 | } |
| 338 | } | 345 | } |
| ... | @@ -370,7 +377,7 @@ fn fmtPath(fmt: *Fmt, file_path: []const u8, check_mode: bool) FmtError!void { | ... | @@ -370,7 +377,7 @@ fn fmtPath(fmt: *Fmt, file_path: []const u8, check_mode: bool) FmtError!void { |
| 370 | fmt.any_error = true; | 377 | fmt.any_error = true; |
| 371 | } | 378 | } |
| 372 | } else { | 379 | } else { |
| 373 | const baf = try io.BufferedAtomicFile.create(fmt.allocator, file_path); | 380 | const baf = try io.BufferedAtomicFile.create(fmt.allocator, real_path); |
| 374 | defer baf.destroy(); | 381 | defer baf.destroy(); |
| 375 | | 382 | |
| 376 | const anything_changed = try std.zig.render(fmt.allocator, baf.stream(), tree); | 383 | const anything_changed = try std.zig.render(fmt.allocator, baf.stream(), tree); |