From 63a09c654ea97cddfd0fcbfd0fb8d5536280d301 Mon Sep 17 00:00:00 2001 From: Beyley Thomas Date: Mon, 21 Aug 2023 20:38:30 -0700 Subject: [PATCH] Close file immediately after writing to it This ensures the file fully exists when we try to realpath it --- src/config.zig | 8 ++++++-- 1 file changed, 6 insertions(+), 2 deletions(-) diff --git a/src/config.zig b/src/config.zig index 29d7cae..3ec4866 100644 --- a/src/config.zig +++ b/src/config.zig @@ -30,13 +30,17 @@ pub fn getConfig(allocator: std.mem.Allocator) !Self { if (err == std.fs.File.OpenError.FileNotFound) { const default_config = Self{}; + //Create the new config file var file = try (config_dir orelse cwd).createFile(config_filename, .{}); - defer file.close(); var buffered_writer = std.io.bufferedWriter(file.writer()); + //Serialize our config as ini into the file try zini.stringify(buffered_writer.writer(), default_config); try buffered_writer.flush(); + //Close before realpath so that windows is happy + file.close(); + //Get the full path of the config var full_path = try (config_dir orelse cwd).realpathAlloc(allocator, config_filename); defer allocator.free(full_path); @@ -47,7 +51,7 @@ pub fn getConfig(allocator: std.mem.Allocator) !Self { var msg = std.ArrayList(u8).init(allocator); defer msg.deinit(); //Write out the message - try std.fmt.format(msg.writer(), "Config created at {s}!\nWould you like to open the config in your default text editor?\x00", .{full_path}); + try std.fmt.format(msg.writer(), "Config created at {s}!\nWould you like to open the config for editing?\x00", .{full_path}); //Display the message to the user if (c.boxerShow(