-
-
Notifications
You must be signed in to change notification settings - Fork 435
Expand file tree
/
Copy pathconfiguration.zig
More file actions
257 lines (220 loc) · 8.82 KB
/
Copy pathconfiguration.zig
File metadata and controls
257 lines (220 loc) · 8.82 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
//! read and resolve configuration options.
const std = @import("std");
const builtin = @import("builtin");
const tracy = @import("tracy");
const known_folders = @import("known-folders");
const Config = @import("Config.zig");
const logger = std.log.scoped(.config);
fn getLocalConfigPath(allocator: std.mem.Allocator) known_folders.Error!?[]const u8 {
const folder_path = try known_folders.getPath(allocator, .local_configuration) orelse return null;
defer allocator.free(folder_path);
return try std.fs.path.join(allocator, &.{ folder_path, "zls.json" });
}
fn getGlobalConfigPath(allocator: std.mem.Allocator) known_folders.Error!?[]const u8 {
const folder_path = try known_folders.getPath(allocator, .global_configuration) orelse return null;
defer allocator.free(folder_path);
return try std.fs.path.join(allocator, &.{ folder_path, "zls.json" });
}
pub fn load(allocator: std.mem.Allocator) error{OutOfMemory}!LoadConfigResult {
if (builtin.target.os.tag == .wasi) return .not_found;
const local_config_path = getLocalConfigPath(allocator) catch |err| blk: {
logger.warn("failed to resolve local configuration path: {}", .{err});
break :blk null;
};
defer if (local_config_path) |path| allocator.free(path);
const global_config_path = getGlobalConfigPath(allocator) catch |err| blk: {
logger.warn("failed to resolve global configuration path: {}", .{err});
break :blk null;
};
defer if (global_config_path) |path| allocator.free(path);
for ([_]?[]const u8{ local_config_path, global_config_path }) |config_path| {
const result = try loadFromFile(allocator, config_path orelse continue);
switch (result) {
.success, .failure => return result,
.not_found => {},
}
}
return .not_found;
}
pub const LoadConfigResult = union(enum) {
success: struct {
config: std.json.Parsed(Config),
/// file path of the config.json
path: []const u8,
},
failure: struct {
/// `null` indicates that the error has already been logged
error_bundle: ?std.zig.ErrorBundle,
pub fn toMessage(self: @This(), allocator: std.mem.Allocator) error{OutOfMemory}!?[]u8 {
const error_bundle = self.error_bundle orelse return null;
var msg: std.ArrayListUnmanaged(u8) = .empty;
errdefer msg.deinit(allocator);
error_bundle.renderToWriter(.{ .ttyconf = .no_color }, msg.writer(allocator)) catch |err| switch (err) {
error.OutOfMemory => |e| return e,
else => unreachable, // why does renderToWriter return `anyerror!void`?
};
return try msg.toOwnedSlice(allocator);
}
},
not_found,
pub fn deinit(self: *LoadConfigResult, allocator: std.mem.Allocator) void {
switch (self.*) {
.success => |*config_with_path| {
config_with_path.config.deinit();
allocator.free(config_with_path.path);
},
.failure => |*payload| {
if (payload.error_bundle) |*error_bundle| error_bundle.deinit(allocator);
},
.not_found => {},
}
}
};
pub fn loadFromFile(allocator: std.mem.Allocator, file_path: []const u8) error{OutOfMemory}!LoadConfigResult {
const tracy_zone = tracy.trace(@src());
defer tracy_zone.end();
const file_buf = std.fs.cwd().readFileAlloc(allocator, file_path, 16 * 1024 * 1024) catch |err| switch (err) {
error.FileNotFound => return .not_found,
error.OutOfMemory => |e| return e,
else => {
logger.warn("Error while reading configuration file: {}", .{err});
return .{ .failure = .{ .error_bundle = null } };
},
};
defer allocator.free(file_buf);
const parse_options: std.json.ParseOptions = .{
.ignore_unknown_fields = true,
.allocate = .alloc_always,
};
var parse_diagnostics: std.json.Diagnostics = .{};
var scanner: std.json.Scanner = .initCompleteInput(allocator, file_buf);
defer scanner.deinit();
scanner.enableDiagnostics(&parse_diagnostics);
@setEvalBranchQuota(10000);
const config = std.json.parseFromTokenSource(
Config,
allocator,
&scanner,
parse_options,
) catch |err| {
var eb: std.zig.ErrorBundle.Wip = undefined;
try eb.init(allocator);
errdefer eb.deinit();
const src_path = try eb.addString(file_path);
const msg = try eb.addString(@errorName(err));
const src_loc = try eb.addSourceLocation(.{
.src_path = src_path,
.line = @intCast(parse_diagnostics.getLine()),
.column = @intCast(parse_diagnostics.getColumn()),
.span_start = @intCast(parse_diagnostics.getByteOffset()),
.span_main = @intCast(parse_diagnostics.getByteOffset()),
.span_end = @intCast(parse_diagnostics.getByteOffset()),
});
try eb.addRootErrorMessage(.{
.msg = msg,
.src_loc = src_loc,
});
return .{ .failure = .{ .error_bundle = try eb.toOwnedBundle("") } };
};
return .{ .success = .{
.config = config,
.path = try allocator.dupe(u8, file_path),
} };
}
pub const Env = struct {
zig_exe: []const u8,
lib_dir: ?[]const u8,
std_dir: []const u8,
global_cache_dir: []const u8,
version: []const u8,
target: ?[]const u8 = null,
};
pub fn getZigEnv(allocator: std.mem.Allocator, zig_exe_path: []const u8) ?std.json.Parsed(Env) {
const zig_env_result = std.process.Child.run(.{
.allocator = allocator,
.argv = &.{ zig_exe_path, "env" },
}) catch {
logger.err("Failed to execute zig env", .{});
return null;
};
defer {
allocator.free(zig_env_result.stdout);
allocator.free(zig_env_result.stderr);
}
switch (zig_env_result.term) {
.Exited => |code| {
if (code != 0) {
logger.err("zig env failed with error_code: {}", .{code});
return null;
}
},
else => logger.err("zig env invocation failed", .{}),
}
return std.json.parseFromSlice(
Env,
allocator,
zig_env_result.stdout,
.{ .ignore_unknown_fields = true, .allocate = .alloc_always },
) catch {
logger.err("Failed to parse zig env JSON result", .{});
return null;
};
}
/// the same struct as Config but every field is optional
pub const Configuration = getConfigurationType();
// returns a Struct which is the same as `Config` except that every field is optional.
fn getConfigurationType() type {
var config_info: std.builtin.Type = @typeInfo(Config);
var fields: [config_info.@"struct".fields.len]std.builtin.Type.StructField = undefined;
for (config_info.@"struct".fields, &fields) |field, *new_field| {
new_field.* = field;
if (@typeInfo(field.type) != .optional) {
new_field.type = @Type(.{
.optional = .{ .child = field.type },
});
}
new_field.default_value_ptr = &@as(new_field.type, null);
}
config_info.@"struct".fields = fields[0..];
config_info.@"struct".decls = &.{};
return @Type(config_info);
}
pub fn findZig(allocator: std.mem.Allocator) error{OutOfMemory}!?[]const u8 {
const env_path = std.process.getEnvVarOwned(allocator, "PATH") catch |err| switch (err) {
error.EnvironmentVariableNotFound => return null,
error.OutOfMemory => |e| return e,
error.InvalidWtf8 => |e| {
logger.err("failed to load 'PATH' environment variable: {}", .{e});
return null;
},
};
defer allocator.free(env_path);
const zig_exe = "zig" ++ comptime builtin.target.exeFileExt();
var it = std.mem.tokenizeScalar(u8, env_path, std.fs.path.delimiter);
while (it.next()) |path| {
var full_path = try std.fs.path.join(allocator, &.{ path, zig_exe });
defer allocator.free(full_path);
if (!std.fs.path.isAbsolute(full_path)) {
logger.warn("ignoring entry in PATH '{s}' because it is not an absolute file path", .{full_path});
continue;
}
const file = std.fs.openFileAbsolute(full_path, .{}) catch |err| switch (err) {
error.FileNotFound => continue,
else => |e| {
logger.warn("failed to open entry in PATH '{s}': {}", .{ full_path, e });
continue;
},
};
defer file.close();
stat_failed: {
const stat = file.stat() catch break :stat_failed;
if (stat.kind == .directory) {
logger.warn("ignoring entry in PATH '{s}' because it is a directory", .{full_path});
continue;
}
}
defer full_path = "";
return full_path;
}
return null;
}