Skip to content

resolve relative paths from workspace/didChangeConfiguration #2372

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Closed
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
81 changes: 44 additions & 37 deletions src/Server.zig
Original file line number Diff line number Diff line change
Expand Up @@ -754,42 +754,9 @@ fn handleConfiguration(server: *Server, json: std.json.Value) error{OutOfMemory}
}
}

const maybe_root_dir: ?[]const u8 = if (server.workspaces.items.len == 1) dir: {
const uri = std.Uri.parse(server.workspaces.items[0].uri) catch |err| {
log.err("failed to parse root uri for workspace {s}: {!}", .{
server.workspaces.items[0].uri, err,
});
break :dir null;
};
break :dir try uri.path.toRawMaybeAlloc(arena);
} else null;

check_relative: inline for (&.{
"zig_exe_path",
"builtin_path",
"build_runner_path",
"zig_lib_path",
"global_cache_path",
}) |fieldName| {
const field: *?[]const u8 = &@field(new_config, fieldName);
if (field.*) |maybe_relative| resolve: {
if (maybe_relative.len == 0) break :resolve;
if (std.fs.path.isAbsolute(maybe_relative)) break :resolve;

const root_dir = maybe_root_dir orelse {
log.err("relative path only supported for {s} with exactly one workspace", .{fieldName});
break :check_relative;
};

const absolute = try std.fs.path.join(arena, &.{
root_dir, maybe_relative,
});

field.* = absolute;
}
}

server.updateConfiguration(new_config, .{}) catch |err| {
server.updateConfiguration(new_config, .{
.allow_relative_paths = true,
}) catch |err| {
log.err("failed to update configuration: {}", .{err});
};
}
Expand Down Expand Up @@ -980,12 +947,15 @@ fn didChangeConfigurationHandler(server: *Server, arena: std.mem.Allocator, noti
return error.ParseError;
};

try server.updateConfiguration(new_config, .{});
try server.updateConfiguration(new_config, .{
.allow_relative_paths = true,
});
}

pub const UpdateConfigurationOptions = struct {
resolve: bool = true,
leaky_config_arena: bool = false,
allow_relative_paths: bool = false,
};

pub fn updateConfiguration2(
Expand Down Expand Up @@ -1016,6 +986,43 @@ pub fn updateConfiguration(

var new_config: configuration.Configuration = param_new_config;

if (options.allow_relative_paths) {
const maybe_root_dir: ?[]const u8 = if (server.workspaces.items.len == 1) dir: {
const uri = std.Uri.parse(server.workspaces.items[0].uri) catch |err| {
log.err("failed to parse root uri for workspace {s}: {!}", .{
server.workspaces.items[0].uri, err,
});
break :dir null;
};
break :dir try uri.path.toRawMaybeAlloc(config_arena);
} else null;

check_relative: inline for (&.{
"zig_exe_path",
"builtin_path",
"build_runner_path",
"zig_lib_path",
"global_cache_path",
}) |fieldName| {
const field: *?[]const u8 = &@field(new_config, fieldName);
if (field.*) |maybe_relative| resolve: {
if (maybe_relative.len == 0) break :resolve;
if (std.fs.path.isAbsolute(maybe_relative)) break :resolve;

const root_dir = maybe_root_dir orelse {
log.err("relative path only supported for {s} with exactly one workspace", .{fieldName});
break :check_relative;
};

const absolute = try std.fs.path.resolve(config_arena, &.{
root_dir, maybe_relative,
});

field.* = absolute;
}
}
}

server.validateConfiguration(&new_config);
inline for (std.meta.fields(Config)) |field| {
@field(new_config, field.name) = if (@field(new_config, field.name)) |new_value|
Expand Down