Skip to content

Commit dca9ab8

Browse files
authored
Fix timer breaking changes with the latest zig master (#140)
1 parent 720460c commit dca9ab8

File tree

6 files changed

+45
-44
lines changed

6 files changed

+45
-44
lines changed

.github/workflows/coverage.yml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -42,7 +42,7 @@ jobs:
4242
run: sed -n 's/.*url *= *"\(.*\)".*/\1/p' build.zig.zon | xargs -t -n1 zig fetch
4343

4444
- name: "Prefetch ZLS dependencies (workaround)"
45-
run: curl -L https://raw.githubusercontent.com/zigtools/zls/7b9d079c65e2400612bd126ee64ffda5dbb7e1f6/build.zig.zon | sed -n 's/.*url *= *"\(.*\)".*/\1/p' | xargs -t -n1 zig fetch
45+
run: curl -L https://raw.githubusercontent.com/zigtools/zls/0ced338490bf50ef3e51736d7357767fa9988c9d/build.zig.zon | sed -n 's/.*url *= *"\(.*\)".*/\1/p' | xargs -t -n1 zig fetch
4646

4747
- name: "Run unit tests with coverage"
4848
run: |

.gitignore

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@
22
#####
33
.zig-cache/
44
zig-out/
5+
zig-pkg/
56

67
# MacOS
78
#######

build.zig

Lines changed: 7 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -995,22 +995,21 @@ const ZlinterRun = struct {
995995
std.debug.print("zlinter command:\n\t{s}\n", .{
996996
std.Build.Step.allocPrintCmd(
997997
arena,
998-
null,
998+
.inherit,
999999
null,
10001000
argv_list.items,
10011001
) catch @panic("OOM"),
10021002
});
10031003
}
10041004

1005-
var timer = try std.time.Timer.start();
1005+
const start_time = std.Io.Clock.awake.now(io);
10061006

10071007
_ = std.debug.lockStderr(&.{});
10081008
defer std.debug.unlockStderr();
10091009

10101010
var child = std.process.spawn(io, .{
10111011
.argv = argv_list.items,
1012-
.cwd = b.build_root.path,
1013-
.cwd_dir = b.build_root.handle,
1012+
.cwd = .{ .dir = b.build_root.handle },
10141013
.environ_map = &environ_map,
10151014
// As we're using stdout and stderr inherit we don't want to update
10161015
// parent of childs progress (i.e commented out as deliberately not set)
@@ -1040,7 +1039,7 @@ const ZlinterRun = struct {
10401039

10411040
const term = try child.wait(io);
10421041

1043-
step.result_duration_ns = timer.read();
1042+
step.result_duration_ns = @intCast(start_time.untilNow(io, .awake).toNanoseconds());
10441043
step.result_peak_rss = child.resource_usage_statistics.getMaxRss() orelse 0;
10451044
step.test_results = .{};
10461045

@@ -1058,7 +1057,7 @@ const ZlinterRun = struct {
10581057
return step.fail("zlinter command crashed:\n\t{s}", .{
10591058
std.Build.Step.allocPrintCmd(
10601059
arena,
1061-
null,
1060+
.inherit,
10621061
null,
10631062
argv_list.items,
10641063
) catch @panic("OOM"),
@@ -1069,7 +1068,7 @@ const ZlinterRun = struct {
10691068
return step.fail("zlinter was terminated unexpectedly:\n\t{s}", .{
10701069
std.Build.Step.allocPrintCmd(
10711070
arena,
1072-
null,
1071+
.inherit,
10731072
null,
10741073
argv_list.items,
10751074
) catch @panic("OOM"),
@@ -1102,7 +1101,7 @@ fn readHtmlTemplate(b: *std.Build, path: std.Build.LazyPath) ![]const u8 {
11021101
// Ignore.
11031102
}
11041103

1105-
const timestamp = try std.Io.Clock.real.now(b.graph.io);
1104+
const timestamp = std.Io.Clock.real.now(b.graph.io);
11061105
const build_timestamp = b.fmt("{d}", .{@divTrunc(timestamp.nanoseconds, std.time.ns_per_ms)});
11071106
const zig_version = zig_version_string;
11081107

build.zig.zon

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -7,8 +7,8 @@
77
.zls = .{
88
// Update with `zig fetch --save git+https://github.com/zigtools/zls#master`
99
// IF changed THEN update .github/coverage.yml
10-
.url = "git+https://github.com/zigtools/zls?ref=master#be4ddd69067e1fd62644d9a999b17adffd8766c2",
11-
.hash = "zls-0.16.0-dev-rmm5flNfJQA26cb4HX9zW4379iE2lPGPDZD0qdh_VXMA",
10+
.url = "git+https://github.com/zigtools/zls?ref=master#0ced338490bf50ef3e51736d7357767fa9988c9d",
11+
.hash = "zls-0.16.0-dev-rmm5fklfJQD1jd1zHEqgN3dhCV2VWnaJYbw-0uxyXaMP",
1212
},
1313
},
1414
.paths = .{

src/exe/run_linter.zig

Lines changed: 33 additions & 32 deletions
Original file line numberDiff line numberDiff line change
@@ -131,8 +131,8 @@ fn run(
131131
args: zlinter.Args,
132132
printer: *zlinter.rendering.Printer,
133133
) !RunResult {
134-
var timer = Timer.createStarted();
135-
var total_timer = Timer.createStarted();
134+
var timer = Timer.createStarted(io);
135+
var total_timer = Timer.createStarted(io);
136136

137137
// Key is index to `lint_files` and value are errors for the file.
138138
var file_lint_problems = std.AutoArrayHashMap(
@@ -184,7 +184,7 @@ fn run(
184184
file.excluded = !index.contains(file.pathname);
185185
}
186186

187-
if (timer.lapMilliseconds()) |ms| printer.println(.verbose, "Resolving {d} files took: {d}ms", .{ lint_files.len, ms });
187+
printer.println(.verbose, "Resolving {d} files took: {d}ms", .{ lint_files.len, timer.lapMilliseconds() });
188188

189189
try runLinterRules(
190190
io,
@@ -199,7 +199,7 @@ fn run(
199199

200200
printer.printBanner(.verbose);
201201
printer.println(.verbose, "Linted {d} files", .{lint_files.len});
202-
if (total_timer.lapMilliseconds()) |ms| printer.println(.verbose, "Took {d}ms", .{ms});
202+
printer.println(.verbose, "Took {d}ms", .{total_timer.lapMilliseconds()});
203203
printer.printBanner(.verbose);
204204

205205
// ------------------------------------------------------------------------
@@ -335,16 +335,15 @@ fn runLinterRules(
335335
}
336336
printer.println(.verbose, "[{d}/{d}] Linting: {s}", .{ i + 1, lint_files.len, lint_file.pathname });
337337

338-
var rule_timer = Timer.createStarted();
338+
var rule_timer = Timer.createStarted(io);
339339
defer {
340-
if (rule_timer.lapNanoseconds()) |ns| {
341-
printer.println(.verbose, " - Total elapsed {d}ms", .{ns / std.time.ns_per_ms});
342-
if (maybe_slowest_files) |*slowest_files| {
343-
slowest_files.add(.{
344-
.name = lint_file.pathname,
345-
.elapsed_ns = ns,
346-
});
347-
}
340+
const ns = rule_timer.lapNanoseconds();
341+
printer.println(.verbose, " - Total elapsed {d}ms", .{ns / std.time.ns_per_ms});
342+
if (maybe_slowest_files) |*slowest_files| {
343+
slowest_files.add(.{
344+
.name = lint_file.pathname,
345+
.elapsed_ns = ns,
346+
});
348347
}
349348
}
350349

@@ -359,10 +358,7 @@ fn runLinterRules(
359358
};
360359
defer doc.deinit(context.gpa);
361360

362-
if (timer.lapMilliseconds()) |ms|
363-
printer.println(.verbose, " - Load document: {d}ms", .{ms})
364-
else
365-
printer.println(.verbose, " - Load document", .{});
361+
printer.println(.verbose, " - Load document: {d}ms", .{timer.lapMilliseconds()});
366362
printer.println(.verbose, " - {d} bytes", .{doc.handle.tree.source.len});
367363
printer.println(.verbose, " - {d} nodes", .{doc.handle.tree.nodes.len});
368364
printer.println(.verbose, " - {d} tokens", .{doc.handle.tree.tokens.len});
@@ -395,7 +391,7 @@ fn runLinterRules(
395391
},
396392
);
397393
}
398-
if (timer.lapMilliseconds()) |ms| printer.println(.verbose, " - Process syntax errors: {d}ms", .{ms});
394+
printer.println(.verbose, " - Process syntax errors: {d}ms", .{timer.lapMilliseconds()});
399395

400396
printer.println(.verbose, " - Rules", .{});
401397

@@ -415,12 +411,11 @@ fn runLinterRules(
415411
try results.append(gpa, result);
416412
}
417413

418-
if (timer.lapNanoseconds()) |ns| {
419-
if (maybe_rule_elapsed_times) |*rule_elapsed_time| {
420-
rule_elapsed_time[rule_index] += ns;
421-
}
422-
printer.println(.verbose, " - {s}: {d}ms", .{ rule.rule_id, ns / std.time.ns_per_ms });
423-
} else printer.println(.verbose, " - {s}", .{rule.rule_id});
414+
const ns = timer.lapNanoseconds();
415+
if (maybe_rule_elapsed_times) |*rule_elapsed_time| {
416+
rule_elapsed_time[rule_index] += ns;
417+
}
418+
printer.println(.verbose, " - {s}: {d}ms", .{ rule.rule_id, ns / std.time.ns_per_ms });
424419
}
425420

426421
if (results.items.len > 0) {
@@ -753,20 +748,26 @@ const RunResult = struct {
753748
const usage_error: RunResult = .{ .exit_code = .usage_error };
754749
};
755750

756-
/// Simple more forgiving timer for optionally timing laps in verbose mode.
757751
const Timer = struct {
758-
backing: ?std.time.Timer = null,
752+
last_timestamp: std.Io.Timestamp,
753+
io: std.Io,
759754

760-
pub fn createStarted() Timer {
761-
return .{ .backing = std.time.Timer.start() catch null };
755+
pub fn createStarted(io: std.Io) Timer {
756+
return .{
757+
.last_timestamp = std.Io.Clock.now(.awake, io),
758+
.io = io,
759+
};
762760
}
763761

764-
pub fn lapNanoseconds(self: *Timer) ?usize {
765-
return (self.backing orelse return null).lap();
762+
pub fn lapNanoseconds(self: *Timer) usize {
763+
const current = std.Io.Clock.now(.awake, self.io);
764+
const elapsed = self.last_timestamp.durationTo(current).toNanoseconds();
765+
self.last_timestamp = current;
766+
return @intCast(elapsed);
766767
}
767768

768-
pub fn lapMilliseconds(self: *Timer) ?usize {
769-
return (self.lapNanoseconds() orelse return null) / std.time.ns_per_ms;
769+
pub fn lapMilliseconds(self: *Timer) usize {
770+
return self.lapNanoseconds() / std.time.ns_per_ms;
770771
}
771772
};
772773

src/lib/session.zig

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -166,7 +166,7 @@ pub const LintContext = struct {
166166
.allocator = gpa,
167167
.io = io,
168168
},
169-
.intern_pool = try .init(gpa),
169+
.intern_pool = try .init(io, gpa),
170170
.document_store = undefined, // zlinter-disable-current-line no_undefined - set below
171171
.analyser = undefined, // zlinter-disable-current-line no_undefined - set below
172172
.io = io,

0 commit comments

Comments
 (0)