| ... | ... | @@ -318,11 +318,18 @@ const FmtError = error{ |
| 318 | 318 | } || fs.File.OpenError; |
| 319 | 319 | |
| 320 | 320 | fn fmtPath(fmt: *Fmt, file_path: []const u8, check_mode: bool) FmtError!void { |
| 321 | | if (fmt.seen.exists(file_path)) return; |
| 322 | | try fmt.seen.put(file_path); |
| 321 | // get the real path here to avoid Windows failing on relative file paths with . or .. in them |
| 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); |
| 325 | | const source_code = fs.cwd().readFileAlloc(fmt.allocator, file_path, max) catch |err| switch (err) { |
| 332 | const source_code = fs.cwd().readFileAlloc(fmt.allocator, real_path, self_hosted_main.max_src_size) catch |err| switch (err) { |
| 326 | 333 | error.IsDir, error.AccessDenied => { |
| 327 | 334 | // TODO make event based (and dir.next()) |
| 328 | 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 | 339 | |
| 333 | 340 | while (try dir_it.next()) |entry| { |
| 334 | 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 | 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 | 377 | fmt.any_error = true; |
| 371 | 378 | } |
| 372 | 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 | 381 | defer baf.destroy(); |
| 375 | 382 | |
| 376 | 383 | const anything_changed = try std.zig.render(fmt.allocator, baf.stream(), tree); |