Skip to content

Commit 1f54746

Browse files
committed
update to zig 0.16.0
1 parent 6199cf8 commit 1f54746

10 files changed

Lines changed: 243 additions & 161 deletions

File tree

.gitignore

Lines changed: 1 addition & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,6 @@
11
zig-out
2-
zig-cache
2+
zig-pkg
33
.zig-cache
44
saved_logs
55
repos
6-
node_modules
76
.env
8-
d_*.log

build.zig.zon

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,11 @@
11
.{
22
.name = .sus,
33
.version = "0.1.0",
4-
.minimum_zig_version = "0.15.1",
4+
.minimum_zig_version = "0.16.0",
55
.dependencies = .{
66
.lsp_kit = .{
7-
.url = "git+https://github.com/zigtools/lsp-kit.git#6274eebace9a6a82ce182e24468fef88e0b95f37",
8-
.hash = "lsp_kit-0.1.0-bi_PLzAyCgClDh8_M0U9Q50ysdsQBuRuBTZfwg6rZPd6",
7+
.url = "git+https://github.com/zigtools/lsp-kit.git#b886a2b0d5cee85ecbcc3089b863f7517cc9ff7f",
8+
.hash = "lsp_kit-0.1.0-bi_PL3IyDACfp1xdTnkiOHEok2YpPCCCJHuuOcNzjl1D",
99
},
1010
},
1111
.paths = .{

src/Client.zig

Lines changed: 27 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@ const lsp = @import("lsp");
66

77
const Client = @This();
88

9+
io: std.Io,
910
allocator: std.mem.Allocator,
1011
child_process: std.process.Child,
1112
transport_read_buffer: [256]u8 = undefined,
@@ -14,12 +15,14 @@ text_document_uri: []const u8,
1415
zig_path: []const u8,
1516

1617
pub fn init(
18+
io: std.Io,
1719
allocator: std.mem.Allocator,
20+
environ_map: *const std.process.Environ.Map,
1821
zls_path: []const u8,
1922
zig_path: []const u8,
20-
stderr_behavior: std.process.Child.StdIo,
21-
) (std.process.GetEnvMapError || std.process.Child.SpawnError)!Client {
22-
var env_map = try std.process.getEnvMap(allocator);
23+
stderr_behavior: std.process.SpawnOptions.StdIo,
24+
) (std.process.SpawnError)!Client {
25+
var env_map = try environ_map.clone(allocator);
2326
defer env_map.deinit();
2427

2528
try env_map.put("NO_COLOR", "1");
@@ -31,14 +34,14 @@ pub fn init(
3134
"--disable-lsp-logs",
3235
};
3336

34-
var child_process: std.process.Child = .init(argv, allocator);
35-
child_process.env_map = &env_map;
36-
child_process.stdin_behavior = .Pipe;
37-
child_process.stderr_behavior = stderr_behavior;
38-
child_process.stdout_behavior = .Pipe;
39-
40-
try child_process.spawn();
41-
errdefer _ = child_process.kill() catch @panic("failed to kill ZLS process");
37+
var child_process = try std.process.spawn(io, .{
38+
.environ_map = &env_map,
39+
.argv = argv,
40+
.stdin = .pipe,
41+
.stderr = stderr_behavior,
42+
.stdout = .pipe,
43+
});
44+
errdefer child_process.kill(io);
4245

4346
const text_document_uri = try std.fmt.allocPrint(allocator, "{f}", .{std.Uri{
4447
.scheme = "untitled",
@@ -47,6 +50,7 @@ pub fn init(
4750
errdefer allocator.free(text_document_uri);
4851

4952
return .{
53+
.io = io,
5054
.allocator = allocator,
5155
.child_process = child_process,
5256
.stdio_transport = undefined, // See `initialize`
@@ -56,11 +60,11 @@ pub fn init(
5660
}
5761

5862
pub fn wait(client: *Client) std.process.Child.WaitError!void {
59-
_ = try client.child_process.wait();
63+
_ = try client.child_process.wait(client.io);
6064
}
6165

6266
pub fn kill(client: *Client) void {
63-
_ = client.child_process.wait() catch @panic("failed to kill ZLS process");
67+
client.child_process.kill(client.io);
6468
}
6569

6670
pub fn transport(client: *Client) *lsp.Transport {
@@ -75,18 +79,18 @@ pub fn initialize(client: *Client) !void {
7579
});
7680
try client.sendNotification("initialized", .{});
7781

78-
var settings: std.json.ObjectMap = .init(client.allocator);
79-
defer settings.deinit();
80-
try settings.putNoClobber("skip_std_references", .{ .bool = true }); // references collection into std is very slow
81-
try settings.putNoClobber("zig_exe_path", .{ .string = client.zig_path });
82+
var settings: std.json.ObjectMap = .empty;
83+
defer settings.deinit(client.allocator);
84+
try settings.putNoClobber(client.allocator, "skip_std_references", .{ .bool = true }); // references collection into std is very slow
85+
try settings.putNoClobber(client.allocator, "zig_exe_path", .{ .string = client.zig_path });
8286

83-
try client.sendNotification("workspace/didChangeConfiguration", lsp.types.DidChangeConfigurationParams{
87+
try client.sendNotification("workspace/didChangeConfiguration", lsp.types.workspace.configuration.did_change.Params{
8488
.settings = .{ .object = settings },
8589
});
8690

87-
try client.sendNotification("textDocument/didOpen", lsp.types.DidOpenTextDocumentParams{ .textDocument = .{
91+
try client.sendNotification("textDocument/didOpen", lsp.types.TextDocument.DidOpenParams{ .textDocument = .{
8892
.uri = client.text_document_uri,
89-
.languageId = "zig",
93+
.languageId = .{ .custom_value = "zig" },
9094
.version = 0,
9195
.text = "",
9296
} });
@@ -108,6 +112,7 @@ fn sendRequest(
108112
params: lsp.ParamsType(method),
109113
) !void {
110114
try client.transport().writeRequest(
115+
client.io,
111116
client.allocator,
112117
id,
113118
method,
@@ -116,6 +121,7 @@ fn sendRequest(
116121
.{ .emit_null_optional_fields = false },
117122
);
118123
try utils.waitForResponseToRequest(
124+
client.io,
119125
client.allocator,
120126
client.transport(),
121127
id,
@@ -128,6 +134,7 @@ fn sendNotification(
128134
params: lsp.ParamsType(method),
129135
) !void {
130136
try client.transport().writeNotification(
137+
client.io,
131138
client.allocator,
132139
method,
133140
lsp.ParamsType(method),

src/Fuzzer.zig

Lines changed: 21 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -44,6 +44,7 @@ pub const Config = struct {
4444
}
4545
};
4646

47+
io: std.Io,
4748
allocator: std.mem.Allocator,
4849
mode: *Mode,
4950
config: *const Config,
@@ -53,28 +54,36 @@ cycle: usize = 0,
5354
client: Client,
5455
rand: std.Random.DefaultPrng,
5556
pending_request_ids: std.AutoArrayHashMapUnmanaged(i64, void) = .empty,
56-
text_document: lsp.types.TextDocumentItem,
57+
text_document: lsp.types.TextDocument,
5758

5859
pub fn init(
60+
io: std.Io,
5961
allocator: std.mem.Allocator,
62+
environ_map: *const std.process.Environ.Map,
6063
mode: *Mode,
6164
config: *const Config,
6265
) !Fuzzer {
6366
var client: Client = try .init(
67+
io,
6468
allocator,
69+
environ_map,
6570
config.zls_path,
6671
config.zig_env.zig_exe,
67-
if (config.seed != null) .Inherit else .Ignore,
72+
if (config.seed != null) .inherit else .ignore,
6873
);
69-
errdefer _ = client.child_process.kill() catch @panic("failed to kill ZLS process");
74+
errdefer client.child_process.kill(io);
7075

71-
const seed = config.seed orelse std.crypto.random.int(u64);
76+
const rng_impl: std.Random.IoSource = .{ .io = io };
77+
const rng = rng_impl.interface();
78+
79+
const seed = config.seed orelse rng.int(u64);
7280

7381
var pending_request_ids: std.AutoArrayHashMapUnmanaged(i64, void) = .empty;
7482
try pending_request_ids.ensureTotalCapacity(allocator, config.cycles_per_gen);
7583
errdefer pending_request_ids.deinit(allocator);
7684

7785
return .{
86+
.io = io,
7887
.allocator = allocator,
7988
.mode = mode,
8089
.config = config,
@@ -85,7 +94,7 @@ pub fn init(
8594
.pending_request_ids = pending_request_ids,
8695
.text_document = .{
8796
.uri = client.text_document_uri,
88-
.languageId = "zig",
97+
.languageId = .{ .custom_value = "zig" },
8998
.version = 0,
9099
.text = "",
91100
},
@@ -104,6 +113,7 @@ pub fn fuzz(fuzzer: *Fuzzer) (lsp.Transport.WriteError || lsp.Transport.ReadErro
104113

105114
if (fuzzer.cycle % fuzzer.config.cycles_per_gen == 0) {
106115
try utils.waitForResponseToRequests(
116+
fuzzer.io,
107117
fuzzer.allocator,
108118
&fuzzer.client.stdio_transport.transport,
109119
&fuzzer.pending_request_ids,
@@ -120,22 +130,24 @@ pub fn fuzz(fuzzer: *Fuzzer) (lsp.Transport.WriteError || lsp.Transport.ReadErro
120130

121131
fuzzer.text_document.version += 1;
122132
try fuzzer.client.transport().writeNotification(
133+
fuzzer.io,
123134
fuzzer.allocator,
124135
"textDocument/didChange",
125-
lsp.types.DidChangeTextDocumentParams,
136+
lsp.types.TextDocument.DidChangeParams,
126137
.{
127138
.textDocument = .{ .uri = fuzzer.text_document.uri, .version = fuzzer.text_document.version },
128-
.contentChanges = &[1]lsp.types.TextDocumentContentChangeEvent{
129-
.{ .literal_1 = .{ .text = fuzzer.text_document.text } },
139+
.contentChanges = &[1]lsp.types.TextDocument.ContentChangeEvent{
140+
.{ .text_document_content_change_whole_document = .{ .text = fuzzer.text_document.text } },
130141
},
131142
},
132143
.{ .emit_null_optional_fields = false },
133144
);
134145
}
135146

136147
const id, const json_message = try utils.fuzzFeatureRandom(
137-
fuzzer.client.transport(),
148+
fuzzer.io,
138149
fuzzer.allocator,
150+
fuzzer.client.transport(),
139151
fuzzer.text_document,
140152
fuzzer.rand.random(),
141153
);

src/Reducer.zig

Lines changed: 32 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -8,19 +8,23 @@ const Client = @import("Client.zig");
88
const lsp = @import("lsp");
99

1010
pub fn reduce(
11+
io: std.Io,
1112
allocator: std.mem.Allocator,
13+
environ_map: *const std.process.Environ.Map,
1214
mode: *Mode,
1315
config: *const Fuzzer.Config,
1416
seed: u64,
1517
progress_node: std.Progress.Node,
1618
) !void {
1719
var client: Client = try .init(
20+
io,
1821
allocator,
22+
environ_map,
1923
config.zls_path,
2024
config.zig_env.zig_exe,
21-
.Pipe,
25+
.pipe,
2226
);
23-
errdefer _ = client.child_process.kill() catch @panic("failed to kill ZLS process");
27+
errdefer client.child_process.kill(io);
2428

2529
try client.initialize();
2630

@@ -29,6 +33,7 @@ pub fn reduce(
2933

3034
var keep_running_stderr: std.atomic.Value(bool) = .init(true);
3135
const stderr_thread: std.Thread = try .spawn(.{}, readStderr, .{
36+
io,
3237
allocator,
3338
client.child_process.stderr.?,
3439
&stderr,
@@ -40,9 +45,9 @@ pub fn reduce(
4045
}
4146

4247
var rand: std.Random.DefaultPrng = .init(seed);
43-
var text_document: lsp.types.TextDocumentItem = .{
48+
var text_document: lsp.types.TextDocument = .{
4449
.uri = client.text_document_uri,
45-
.languageId = "zig",
50+
.languageId = .{ .custom_value = "zig" },
4651
.version = 0,
4752
.text = "",
4853
};
@@ -62,28 +67,30 @@ pub fn reduce(
6267

6368
text_document.version += 1;
6469
try client.transport().writeNotification(
70+
io,
6571
allocator,
6672
"textDocument/didChange",
67-
lsp.types.DidChangeTextDocumentParams,
73+
lsp.types.TextDocument.DidChangeParams,
6874
.{
6975
.textDocument = .{ .uri = text_document.uri, .version = text_document.version },
70-
.contentChanges = &[1]lsp.types.TextDocumentContentChangeEvent{
71-
.{ .literal_1 = .{ .text = text_document.text } },
76+
.contentChanges = &[1]lsp.types.TextDocument.ContentChangeEvent{
77+
.{ .text_document_content_change_whole_document = .{ .text = text_document.text } },
7278
},
7379
},
7480
.{ .emit_null_optional_fields = false },
7581
);
7682
}
7783

7884
const id, const json_message = try utils.fuzzFeatureRandom(
79-
client.transport(),
85+
io,
8086
allocator,
87+
client.transport(),
8188
text_document,
8289
rand.random(),
8390
);
8491
errdefer allocator.free(json_message);
8592

86-
utils.waitForResponseToRequest(client.allocator, client.transport(), id) catch |err| switch (err) {
93+
utils.waitForResponseToRequest(io, allocator, client.transport(), id) catch |err| switch (err) {
8794
error.EndOfStream => break json_message,
8895
else => |e| return e,
8996
};
@@ -98,6 +105,8 @@ pub fn reduce(
98105
else
99106
stderr.items;
100107

108+
const now: std.Io.Timestamp = .now(io, .awake);
109+
101110
if (config.rpc) {
102111
var data = [_][]const u8{
103112
std.mem.asBytes(&@as(u32, @intCast(
@@ -109,7 +118,7 @@ pub fn reduce(
109118
2 + processed_stderr.len,
110119
))),
111120

112-
std.mem.asBytes(&std.time.milliTimestamp()),
121+
std.mem.asBytes(&now.toMilliseconds()),
113122

114123
std.mem.asBytes(&@as(u8, @intCast(config.zig_env.version.len))),
115124
config.zig_env.version,
@@ -127,22 +136,22 @@ pub fn reduce(
127136
processed_stderr,
128137
};
129138

130-
var file_writer = std.fs.File.stdout().writer(&.{});
139+
var file_writer = std.Io.File.stdout().writer(io, &.{});
131140
try file_writer.interface.writeVecAll(&data);
132141
} else {
133-
var logs_dir = try std.fs.cwd().makeOpenPath("saved_logs", .{});
134-
defer logs_dir.close();
142+
var logs_dir = try std.Io.Dir.cwd().createDirPathOpen(io, "saved_logs", .{});
143+
defer logs_dir.close(io);
135144

136145
const file_name = std.fmt.hex(seed) ++ ".md";
137-
const entry_file = try logs_dir.createFile(file_name, .{});
138-
defer entry_file.close();
146+
const entry_file = try logs_dir.createFile(io, file_name, .{});
147+
defer entry_file.close(io);
139148

140-
var file_writer = entry_file.writer(&.{});
149+
var file_writer = entry_file.writer(io, &.{});
141150

142151
var timestamp_buf: [32]u8 = undefined;
143152
var data = [_][]const u8{
144153
"timestamp: ",
145-
try std.fmt.bufPrint(&timestamp_buf, "{d}", .{std.time.milliTimestamp()}),
154+
try std.fmt.bufPrint(&timestamp_buf, "{d}", .{now}),
146155
"\n",
147156
"zig version: ",
148157
config.zig_env.version,
@@ -164,14 +173,17 @@ pub fn reduce(
164173
}
165174

166175
fn readStderr(
176+
io: std.Io,
167177
allocator: std.mem.Allocator,
168-
stderr: std.fs.File,
178+
stderr: std.Io.File,
169179
out: *std.ArrayList(u8),
170180
keep_running: *std.atomic.Value(bool),
171181
) void {
172182
var buffer: [4096]u8 = undefined;
183+
var reader = stderr.reader(io, &buffer);
184+
var aw: std.Io.Writer.Allocating = .fromArrayList(allocator, out);
185+
defer aw.deinit();
173186
while (keep_running.load(.acquire)) {
174-
const amt = stderr.read(&buffer) catch break;
175-
out.appendSlice(allocator, buffer[0..amt]) catch break;
187+
_ = reader.interface.streamRemaining(&aw.writer) catch break;
176188
}
177189
}

0 commit comments

Comments
 (0)