2022-07-31 22:38:27 +01:00
|
|
|
const std = @import("std");
|
|
|
|
const zig_builtin = @import("builtin");
|
|
|
|
const build_options = @import("build_options");
|
|
|
|
const tracy = @import("tracy.zig");
|
|
|
|
const known_folders = @import("known-folders");
|
|
|
|
const Config = @import("Config.zig");
|
2022-12-11 11:10:48 +00:00
|
|
|
const configuration = @import("configuration.zig");
|
2022-07-31 22:38:27 +01:00
|
|
|
const Server = @import("Server.zig");
|
2022-12-27 06:47:57 +00:00
|
|
|
const Header = @import("Header.zig");
|
2022-12-30 23:42:53 +00:00
|
|
|
const debug = @import("debug.zig");
|
2022-07-31 22:38:27 +01:00
|
|
|
|
|
|
|
const logger = std.log.scoped(.main);
|
|
|
|
|
|
|
|
// Always set this to debug to make std.log call into our handler, then control the runtime
|
|
|
|
// value in the definition below.
|
|
|
|
pub const log_level = .debug;
|
|
|
|
|
|
|
|
var actual_log_level: std.log.Level = switch (zig_builtin.mode) {
|
|
|
|
.Debug => .debug,
|
|
|
|
else => @intToEnum(std.log.Level, @enumToInt(build_options.log_level)), // temporary fix to build failing on release-safe due to a Zig bug
|
|
|
|
};
|
|
|
|
|
2022-09-11 22:48:15 +01:00
|
|
|
pub fn log(
|
|
|
|
comptime level: std.log.Level,
|
|
|
|
comptime scope: @TypeOf(.EnumLiteral),
|
|
|
|
comptime format: []const u8,
|
|
|
|
args: anytype,
|
|
|
|
) void {
|
|
|
|
if (@enumToInt(level) > @enumToInt(actual_log_level)) return;
|
|
|
|
|
|
|
|
const level_txt = comptime level.asText();
|
2022-09-13 21:12:32 +01:00
|
|
|
|
|
|
|
std.debug.print("{s:<5}: ({s:^6}): ", .{ level_txt, @tagName(scope) });
|
|
|
|
std.debug.print(format ++ "\n", args);
|
2022-09-11 22:48:15 +01:00
|
|
|
}
|
|
|
|
|
2022-12-31 06:45:45 +00:00
|
|
|
fn loop(
|
|
|
|
server: *Server,
|
|
|
|
record_file: ?std.fs.File,
|
|
|
|
replay_file: ?std.fs.File,
|
|
|
|
) !void {
|
|
|
|
const std_in = std.io.getStdIn().reader();
|
2022-12-27 06:47:57 +00:00
|
|
|
const std_out = std.io.getStdOut().writer();
|
2022-12-31 06:45:45 +00:00
|
|
|
|
|
|
|
var buffered_reader = std.io.bufferedReader(if (replay_file) |file| file.reader() else std_in);
|
|
|
|
const reader = buffered_reader.reader();
|
|
|
|
|
2022-12-27 06:47:57 +00:00
|
|
|
var buffered_writer = std.io.bufferedWriter(std_out);
|
|
|
|
const writer = buffered_writer.writer();
|
2022-07-31 22:38:27 +01:00
|
|
|
|
2022-09-30 05:04:27 +01:00
|
|
|
while (true) {
|
2022-12-27 06:47:57 +00:00
|
|
|
var arena = std.heap.ArenaAllocator.init(server.allocator);
|
|
|
|
defer arena.deinit();
|
|
|
|
|
|
|
|
// write server -> client messages
|
|
|
|
for (server.outgoing_messages.items) |outgoing_message| {
|
|
|
|
const header = Header{ .content_length = outgoing_message.len };
|
2022-12-31 06:45:45 +00:00
|
|
|
try header.write(true, writer);
|
|
|
|
try writer.writeAll(outgoing_message);
|
2022-12-27 06:47:57 +00:00
|
|
|
}
|
2022-12-31 06:45:45 +00:00
|
|
|
try buffered_writer.flush();
|
2022-12-27 06:47:57 +00:00
|
|
|
for (server.outgoing_messages.items) |outgoing_message| {
|
|
|
|
server.allocator.free(outgoing_message);
|
|
|
|
}
|
|
|
|
server.outgoing_messages.clearRetainingCapacity();
|
|
|
|
|
|
|
|
// read and handle client -> server message
|
2022-12-31 06:45:45 +00:00
|
|
|
const header = try Header.parse(arena.allocator(), replay_file == null, reader);
|
2022-08-06 00:30:43 +01:00
|
|
|
|
2022-12-27 06:47:57 +00:00
|
|
|
const json_message = try arena.allocator().alloc(u8, header.content_length);
|
|
|
|
try reader.readNoEof(json_message);
|
2022-07-31 22:38:27 +01:00
|
|
|
|
2022-12-31 06:45:45 +00:00
|
|
|
if (record_file) |file| {
|
|
|
|
try header.write(false, file.writer());
|
|
|
|
try file.writeAll(json_message);
|
|
|
|
}
|
|
|
|
|
2022-12-27 06:47:57 +00:00
|
|
|
server.processJsonRpc(&arena, json_message);
|
2022-07-31 22:38:27 +01:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2022-12-31 06:45:45 +00:00
|
|
|
fn getRecordFile(config: Config) ?std.fs.File {
|
|
|
|
if (!config.record_session) return null;
|
|
|
|
|
|
|
|
if (config.record_session_path) |record_path| {
|
|
|
|
if (std.fs.createFileAbsolute(record_path, .{})) |file| {
|
|
|
|
std.debug.print("recording to {s}\n", .{record_path});
|
|
|
|
return file;
|
|
|
|
} else |err| {
|
|
|
|
std.log.err("failed to create record file at {s}: {}", .{ record_path, err });
|
|
|
|
return null;
|
|
|
|
}
|
|
|
|
} else {
|
|
|
|
std.log.err("`record_session` is set but `record_session_path` is unspecified", .{});
|
|
|
|
return null;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
fn getReplayFile(config: Config) ?std.fs.File {
|
|
|
|
const replay_path = config.replay_session_path orelse config.record_session_path orelse return null;
|
|
|
|
|
|
|
|
if (std.fs.openFileAbsolute(replay_path, .{})) |file| {
|
|
|
|
std.debug.print("replaying from {s}\n", .{replay_path});
|
|
|
|
return file;
|
|
|
|
} else |err| {
|
|
|
|
std.log.err("failed to open replay file at {s}: {}", .{ replay_path, err });
|
|
|
|
return null;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
/// when recording we add a message that saves the current configuration in the replay
|
|
|
|
/// when replaying we read this message and replace the current config
|
|
|
|
fn updateConfig(
|
|
|
|
allocator: std.mem.Allocator,
|
|
|
|
config: *Config,
|
|
|
|
record_file: ?std.fs.File,
|
|
|
|
replay_file: ?std.fs.File,
|
|
|
|
) !void {
|
|
|
|
if (record_file) |file| {
|
|
|
|
var cfg = config.*;
|
|
|
|
cfg.record_session = false;
|
|
|
|
cfg.record_session_path = null;
|
|
|
|
cfg.replay_session_path = null;
|
|
|
|
|
|
|
|
var buffer = std.ArrayListUnmanaged(u8){};
|
|
|
|
defer buffer.deinit(allocator);
|
|
|
|
|
|
|
|
try std.json.stringify(cfg, .{}, buffer.writer(allocator));
|
|
|
|
const header = Header{ .content_length = buffer.items.len };
|
|
|
|
try header.write(false, file.writer());
|
|
|
|
try file.writeAll(buffer.items);
|
|
|
|
}
|
|
|
|
|
|
|
|
if (replay_file) |file| {
|
|
|
|
const header = try Header.parse(allocator, false, file.reader());
|
|
|
|
defer header.deinit(allocator);
|
|
|
|
const json_message = try allocator.alloc(u8, header.content_length);
|
|
|
|
defer allocator.free(json_message);
|
|
|
|
try file.reader().readNoEof(json_message);
|
|
|
|
|
|
|
|
var token_stream = std.json.TokenStream.init(json_message);
|
|
|
|
const new_config = try std.json.parse(Config, &token_stream, .{ .allocator = allocator });
|
|
|
|
std.json.parseFree(Config, config.*, .{ .allocator = allocator });
|
|
|
|
config.* = new_config;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2022-07-31 22:38:27 +01:00
|
|
|
const ConfigWithPath = struct {
|
|
|
|
config: Config,
|
|
|
|
config_path: ?[]const u8,
|
|
|
|
};
|
|
|
|
|
2022-08-18 18:28:56 +01:00
|
|
|
fn getConfig(
|
|
|
|
allocator: std.mem.Allocator,
|
|
|
|
config_path: ?[]const u8,
|
|
|
|
) !ConfigWithPath {
|
|
|
|
if (config_path) |path| {
|
2022-12-31 06:45:45 +00:00
|
|
|
if (configuration.loadFromFile(allocator, path)) |config| {
|
|
|
|
return ConfigWithPath{ .config = config, .config_path = path };
|
2022-07-31 22:38:27 +01:00
|
|
|
}
|
|
|
|
std.debug.print(
|
|
|
|
\\Could not open configuration file '{s}'
|
|
|
|
\\Falling back to a lookup in the local and global configuration folders
|
|
|
|
\\
|
|
|
|
, .{path});
|
|
|
|
}
|
|
|
|
|
|
|
|
if (try known_folders.getPath(allocator, .local_configuration)) |path| {
|
2022-12-31 06:45:45 +00:00
|
|
|
if (configuration.loadFromFolder(allocator, path)) |config| {
|
|
|
|
return ConfigWithPath{ .config = config, .config_path = path };
|
2022-07-31 22:38:27 +01:00
|
|
|
}
|
2022-08-18 18:28:56 +01:00
|
|
|
allocator.free(path);
|
2022-07-31 22:38:27 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
if (try known_folders.getPath(allocator, .global_configuration)) |path| {
|
2022-12-31 06:45:45 +00:00
|
|
|
if (configuration.loadFromFolder(allocator, path)) |config| {
|
|
|
|
return ConfigWithPath{ .config = config, .config_path = path };
|
2022-07-31 22:38:27 +01:00
|
|
|
}
|
2022-08-18 18:28:56 +01:00
|
|
|
allocator.free(path);
|
2022-07-31 22:38:27 +01:00
|
|
|
}
|
|
|
|
|
2022-08-06 00:30:43 +01:00
|
|
|
return ConfigWithPath{
|
|
|
|
.config = Config{},
|
|
|
|
.config_path = null,
|
|
|
|
};
|
2022-07-31 22:38:27 +01:00
|
|
|
}
|
|
|
|
|
2022-12-31 06:45:45 +00:00
|
|
|
const ParseArgsResult = struct {
|
|
|
|
action: enum { proceed, exit },
|
|
|
|
config_path: ?[]const u8,
|
|
|
|
replay_enabled: bool,
|
|
|
|
replay_session_path: ?[]const u8,
|
|
|
|
};
|
|
|
|
|
|
|
|
fn parseArgs(allocator: std.mem.Allocator) !ParseArgsResult {
|
|
|
|
var result = ParseArgsResult{
|
|
|
|
.action = .exit,
|
|
|
|
.config_path = null,
|
|
|
|
.replay_enabled = false,
|
|
|
|
.replay_session_path = null,
|
|
|
|
};
|
|
|
|
|
2022-08-18 18:28:56 +01:00
|
|
|
const ArgId = enum {
|
|
|
|
help,
|
|
|
|
version,
|
2022-12-31 06:45:45 +00:00
|
|
|
replay,
|
2022-08-18 18:28:56 +01:00
|
|
|
@"enable-debug-log",
|
2022-08-29 23:46:13 +01:00
|
|
|
@"show-config-path",
|
2022-08-18 18:28:56 +01:00
|
|
|
@"config-path",
|
|
|
|
};
|
|
|
|
const arg_id_map = std.ComptimeStringMap(ArgId, comptime blk: {
|
|
|
|
const fields = @typeInfo(ArgId).Enum.fields;
|
2022-12-31 06:45:45 +00:00
|
|
|
const KV = struct { []const u8, ArgId };
|
2022-08-18 18:28:56 +01:00
|
|
|
var pairs: [fields.len]KV = undefined;
|
|
|
|
for (pairs) |*pair, i| pair.* = .{ fields[i].name, @intToEnum(ArgId, fields[i].value) };
|
|
|
|
break :blk pairs[0..];
|
|
|
|
});
|
|
|
|
const help_message: []const u8 = comptime help_message: {
|
|
|
|
var help_message: []const u8 =
|
|
|
|
\\Usage: zls [command]
|
|
|
|
\\
|
|
|
|
\\Commands:
|
|
|
|
\\
|
|
|
|
\\
|
|
|
|
;
|
|
|
|
const InfoMap = std.enums.EnumArray(ArgId, []const u8);
|
|
|
|
var cmd_infos: InfoMap = InfoMap.init(.{
|
|
|
|
.help = "Prints this message.",
|
|
|
|
.version = "Prints the compiler version with which the server was compiled.",
|
2022-12-31 06:45:45 +00:00
|
|
|
.replay = "Replay a previous recorded zls session",
|
2022-08-18 18:28:56 +01:00
|
|
|
.@"enable-debug-log" = "Enables debug logs.",
|
|
|
|
.@"config-path" = "Specify the path to a configuration file specifying LSP behaviour.",
|
2022-08-29 23:46:13 +01:00
|
|
|
.@"show-config-path" = "Prints the path to the configuration file to stdout",
|
2022-08-18 18:28:56 +01:00
|
|
|
});
|
|
|
|
var info_it = cmd_infos.iterator();
|
|
|
|
while (info_it.next()) |entry| {
|
|
|
|
help_message = help_message ++ std.fmt.comptimePrint(" --{s}: {s}\n", .{ @tagName(entry.key), entry.value.* });
|
|
|
|
}
|
|
|
|
help_message = help_message ++ "\n";
|
|
|
|
break :help_message help_message;
|
|
|
|
};
|
|
|
|
|
|
|
|
var args_it = try std.process.ArgIterator.initWithAllocator(allocator);
|
|
|
|
defer args_it.deinit();
|
|
|
|
if (!args_it.skip()) @panic("Could not find self argument");
|
|
|
|
|
|
|
|
// Makes behavior of enabling debug more logging consistent regardless of argument order.
|
|
|
|
var specified = std.enums.EnumArray(ArgId, bool).initFill(false);
|
2022-08-29 23:46:13 +01:00
|
|
|
const stdout = std.io.getStdOut().writer();
|
2022-08-18 18:28:56 +01:00
|
|
|
const stderr = std.io.getStdErr().writer();
|
|
|
|
|
|
|
|
while (args_it.next()) |tok| {
|
|
|
|
if (!std.mem.startsWith(u8, tok, "--") or tok.len == 2) {
|
|
|
|
try stderr.print("{s}\n", .{help_message});
|
|
|
|
try stderr.print("Unexpected positional argument '{s}'.\n", .{tok});
|
2022-12-31 06:45:45 +00:00
|
|
|
return result;
|
2022-08-18 18:28:56 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
const argname = tok["--".len..];
|
|
|
|
const id = arg_id_map.get(argname) orelse {
|
|
|
|
try stderr.print("{s}\n", .{help_message});
|
|
|
|
try stderr.print("Unrecognized argument '{s}'.\n", .{argname});
|
2022-12-31 06:45:45 +00:00
|
|
|
return result;
|
2022-08-18 18:28:56 +01:00
|
|
|
};
|
|
|
|
|
|
|
|
if (specified.get(id)) {
|
|
|
|
try stderr.print("{s}\n", .{help_message});
|
|
|
|
try stderr.print("Duplicate argument '{s}'.\n", .{argname});
|
2022-12-31 06:45:45 +00:00
|
|
|
return result;
|
2022-08-18 18:28:56 +01:00
|
|
|
}
|
|
|
|
specified.set(id, true);
|
|
|
|
|
|
|
|
switch (id) {
|
2023-01-02 18:54:13 +00:00
|
|
|
.help,
|
|
|
|
.version,
|
|
|
|
.@"enable-debug-log",
|
|
|
|
.@"show-config-path",
|
|
|
|
=> {},
|
2022-08-18 18:28:56 +01:00
|
|
|
.@"config-path" => {
|
|
|
|
const path = args_it.next() orelse {
|
|
|
|
try stderr.print("Expected configuration file path after --config-path argument.\n", .{});
|
2022-12-31 06:45:45 +00:00
|
|
|
return result;
|
2022-08-18 18:28:56 +01:00
|
|
|
};
|
2022-12-31 06:45:45 +00:00
|
|
|
result.config_path = try allocator.dupe(u8, path);
|
|
|
|
},
|
|
|
|
.replay => {
|
|
|
|
result.replay_enabled = true;
|
|
|
|
const path = args_it.next() orelse break;
|
|
|
|
result.replay_session_path = try allocator.dupe(u8, path);
|
2022-08-18 18:28:56 +01:00
|
|
|
},
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
if (specified.get(.help)) {
|
|
|
|
try stderr.print("{s}\n", .{help_message});
|
2022-12-31 06:45:45 +00:00
|
|
|
return result;
|
2022-08-18 18:28:56 +01:00
|
|
|
}
|
|
|
|
if (specified.get(.version)) {
|
2022-12-31 06:45:45 +00:00
|
|
|
try stdout.writeAll(build_options.version ++ "\n");
|
|
|
|
return result;
|
2022-08-18 18:28:56 +01:00
|
|
|
}
|
|
|
|
if (specified.get(.@"enable-debug-log")) {
|
|
|
|
actual_log_level = .debug;
|
|
|
|
logger.info("Enabled debug logging.\n", .{});
|
|
|
|
}
|
|
|
|
if (specified.get(.@"config-path")) {
|
2022-12-31 06:45:45 +00:00
|
|
|
std.debug.assert(result.config_path != null);
|
2022-08-18 18:28:56 +01:00
|
|
|
}
|
2022-08-29 23:46:13 +01:00
|
|
|
if (specified.get(.@"show-config-path")) {
|
2022-12-31 06:45:45 +00:00
|
|
|
const new_config = try getConfig(allocator, result.config_path);
|
2022-08-29 23:46:13 +01:00
|
|
|
defer if (new_config.config_path) |path| allocator.free(path);
|
|
|
|
defer std.json.parseFree(Config, new_config.config, .{ .allocator = allocator });
|
|
|
|
|
2023-01-02 18:54:13 +00:00
|
|
|
const full_path = if (new_config.config_path) |path| blk: {
|
|
|
|
break :blk try std.fs.path.resolve(allocator, &.{ path, "zls.json" });
|
|
|
|
} else blk: {
|
|
|
|
const local_config_path = try known_folders.getPath(allocator, .local_configuration) orelse {
|
|
|
|
logger.err("failed to find local configuration folder", .{});
|
|
|
|
return result;
|
|
|
|
};
|
|
|
|
defer allocator.free(local_config_path);
|
|
|
|
break :blk try std.fs.path.resolve(allocator, &.{ local_config_path, "zls.json" });
|
|
|
|
};
|
|
|
|
defer allocator.free(full_path);
|
|
|
|
try stdout.writeAll(full_path);
|
|
|
|
try stdout.writeByte('\n');
|
2022-12-31 06:45:45 +00:00
|
|
|
return result;
|
2022-08-29 23:46:13 +01:00
|
|
|
}
|
2022-08-18 18:28:56 +01:00
|
|
|
|
2022-12-31 06:45:45 +00:00
|
|
|
result.action = .proceed;
|
|
|
|
return result;
|
2022-08-18 18:28:56 +01:00
|
|
|
}
|
|
|
|
|
2022-07-31 22:38:27 +01:00
|
|
|
const stack_frames = switch (zig_builtin.mode) {
|
|
|
|
.Debug => 10,
|
|
|
|
else => 0,
|
|
|
|
};
|
|
|
|
|
2022-08-18 23:31:41 +01:00
|
|
|
pub fn main() !void {
|
2022-07-31 22:38:27 +01:00
|
|
|
var gpa_state = std.heap.GeneralPurposeAllocator(.{ .stack_trace_frames = stack_frames }){};
|
|
|
|
defer _ = gpa_state.deinit();
|
|
|
|
|
2022-12-30 23:42:53 +00:00
|
|
|
var tracy_state = if (tracy.enable_allocation) tracy.tracyAllocator(gpa_state.allocator()) else void{};
|
|
|
|
const inner_allocator: std.mem.Allocator = if (tracy.enable_allocation) tracy_state.allocator() else gpa_state.allocator();
|
2023-01-02 18:54:13 +00:00
|
|
|
|
|
|
|
var failing_allocator_state = if (build_options.enable_failing_allocator) debug.FailingAllocator.init(inner_allocator, build_options.enable_failing_allocator_likelihood) else void{};
|
|
|
|
const allocator: std.mem.Allocator = if (build_options.enable_failing_allocator) failing_allocator_state.allocator() else inner_allocator;
|
2022-07-31 22:38:27 +01:00
|
|
|
|
2022-12-31 06:45:45 +00:00
|
|
|
const result = try parseArgs(allocator);
|
|
|
|
defer if (result.config_path) |path| allocator.free(path);
|
|
|
|
defer if (result.replay_session_path) |path| allocator.free(path);
|
|
|
|
switch (result.action) {
|
2022-08-18 18:28:56 +01:00
|
|
|
.proceed => {},
|
|
|
|
.exit => return,
|
2022-07-31 22:38:27 +01:00
|
|
|
}
|
|
|
|
|
2022-12-31 06:45:45 +00:00
|
|
|
var config = try getConfig(allocator, result.config_path);
|
2022-09-22 02:31:48 +01:00
|
|
|
defer std.json.parseFree(Config, config.config, .{ .allocator = allocator });
|
2022-12-31 06:45:45 +00:00
|
|
|
defer if (config.config_path) |path| allocator.free(path);
|
|
|
|
|
|
|
|
if (result.replay_enabled and config.config.replay_session_path == null and config.config.record_session_path == null) {
|
|
|
|
logger.err("No replay file specified", .{});
|
|
|
|
return;
|
|
|
|
}
|
2022-09-22 02:31:48 +01:00
|
|
|
|
2022-08-06 00:30:43 +01:00
|
|
|
if (config.config_path == null) {
|
2022-07-31 22:38:27 +01:00
|
|
|
logger.info("No config file zls.json found.", .{});
|
2022-08-06 00:30:43 +01:00
|
|
|
}
|
2022-07-31 22:38:27 +01:00
|
|
|
|
2022-12-31 06:45:45 +00:00
|
|
|
const record_file = if (!result.replay_enabled) getRecordFile(config.config) else null;
|
|
|
|
defer if (record_file) |file| file.close();
|
|
|
|
|
|
|
|
const replay_file = if (result.replay_enabled) getReplayFile(config.config) else null;
|
|
|
|
defer if (replay_file) |file| file.close();
|
|
|
|
|
|
|
|
std.debug.assert(record_file == null or replay_file == null);
|
|
|
|
|
|
|
|
try updateConfig(allocator, &config.config, record_file, replay_file);
|
|
|
|
|
2022-07-31 22:38:27 +01:00
|
|
|
var server = try Server.init(
|
|
|
|
allocator,
|
2022-09-22 02:31:48 +01:00
|
|
|
&config.config,
|
2022-08-06 00:30:43 +01:00
|
|
|
config.config_path,
|
2022-12-31 06:45:45 +00:00
|
|
|
record_file != null,
|
|
|
|
replay_file != null,
|
2022-07-31 22:38:27 +01:00
|
|
|
);
|
|
|
|
defer server.deinit();
|
|
|
|
|
2022-12-31 06:45:45 +00:00
|
|
|
try loop(&server, record_file, replay_file);
|
2022-07-31 22:38:27 +01:00
|
|
|
}
|