config: show filepath and line numbers for config errors

Fixes #1063
This commit is contained in:
Mitchell Hashimoto
2024-10-17 07:32:54 -07:00
parent a4e14631ef
commit f24098cbd8
3 changed files with 28 additions and 13 deletions

View File

@ -961,7 +961,7 @@ pub fn LineIterator(comptime ReaderType: type) type {
}
// Constructs a LineIterator (see docs for that).
pub fn lineIterator(reader: anytype) LineIterator(@TypeOf(reader)) {
fn lineIterator(reader: anytype) LineIterator(@TypeOf(reader)) {
return .{ .r = reader };
}

View File

@ -2260,7 +2260,9 @@ pub fn loadFile(self: *Config, alloc: Allocator, path: []const u8) !void {
std.log.info("reading configuration file path={s}", .{path});
var buf_reader = std.io.bufferedReader(file.reader());
var iter = cli.args.lineIterator(buf_reader.reader());
const reader = buf_reader.reader();
const Iter = cli.args.LineIterator(@TypeOf(reader));
var iter: Iter = .{ .r = reader, .filepath = path };
try self.loadIter(alloc, &iter);
try self.expandPaths(std.fs.path.dirname(path).?);
}
@ -2471,7 +2473,9 @@ pub fn loadRecursiveFiles(self: *Config, alloc_gpa: Allocator) !void {
log.info("loading config-file path={s}", .{path});
var buf_reader = std.io.bufferedReader(file.reader());
var iter = cli.args.lineIterator(buf_reader.reader());
const reader = buf_reader.reader();
const Iter = cli.args.LineIterator(@TypeOf(reader));
var iter: Iter = .{ .r = reader, .filepath = path };
try self.loadIter(alloc_gpa, &iter);
try self.expandPaths(std.fs.path.dirname(path).?);
}
@ -2502,11 +2506,13 @@ fn expandPaths(self: *Config, base: []const u8) !void {
fn loadTheme(self: *Config, theme: []const u8) !void {
// Find our theme file and open it. See the open function for details.
const file: std.fs.File = (try themepkg.open(
const themefile = (try themepkg.open(
self._arena.?.allocator(),
theme,
&self._diagnostics,
)) orelse return;
const path = themefile.path;
const file = themefile.file;
defer file.close();
// From this point onwards, we load the theme and do a bit of a dance
@ -2532,7 +2538,9 @@ fn loadTheme(self: *Config, theme: []const u8) !void {
// Load our theme
var buf_reader = std.io.bufferedReader(file.reader());
var iter = cli.args.lineIterator(buf_reader.reader());
const reader = buf_reader.reader();
const Iter = cli.args.LineIterator(@TypeOf(reader));
var iter: Iter = .{ .r = reader, .filepath = path };
try new_config.loadIter(alloc_gpa, &iter);
// Replay our previous inputs so that we can override values

View File

@ -108,14 +108,20 @@ pub fn open(
arena_alloc: Allocator,
theme: []const u8,
diags: *cli.DiagnosticList,
) error{OutOfMemory}!?std.fs.File {
) error{OutOfMemory}!?struct {
path: []const u8,
file: std.fs.File,
} {
// Absolute themes are loaded a different path.
if (std.fs.path.isAbsolute(theme)) return try openAbsolute(
arena_alloc,
theme,
diags,
);
if (std.fs.path.isAbsolute(theme)) {
const file: std.fs.File = try openAbsolute(
arena_alloc,
theme,
diags,
) orelse return null;
return .{ .path = theme, .file = file };
}
const basename = std.fs.path.basename(theme);
if (!std.mem.eql(u8, theme, basename)) {
@ -135,8 +141,9 @@ pub fn open(
const cwd = std.fs.cwd();
while (try it.next()) |loc| {
const path = try std.fs.path.join(arena_alloc, &.{ loc.dir, theme });
if (cwd.openFile(path, .{})) |file| {
return file;
if (cwd.openFile(path, .{})) |file| return .{
.path = path,
.file = file,
} else |err| switch (err) {
// Not an error, just continue to the next location.
error.FileNotFound => {},