summaryrefslogtreecommitdiff
path: root/src/lsp
diff options
context:
space:
mode:
Diffstat (limited to 'src/lsp')
-rw-r--r--src/lsp/lsp_client.zig66
1 files changed, 53 insertions, 13 deletions
diff --git a/src/lsp/lsp_client.zig b/src/lsp/lsp_client.zig
index cad75c57..097bddf2 100644
--- a/src/lsp/lsp_client.zig
+++ b/src/lsp/lsp_client.zig
@@ -774,16 +774,21 @@ fn answer(io: std.Io, arena: std.mem.Allocator, si: usize, req: lsp.Req, out: *s
const g = try ensure(c, io, si, arena, req, tr);
const before = out.end;
try attempt(c, io, si, arena, req, out, tr, g);
- // A server still loading its workspace answers at once, with nothing
- // (rust-analyzer before its index): that is no answer. Wait until it says
- // it is loaded, the message row saying so, and ask again.
+ // An empty answer can beat the startup progress notification onto the wire.
const answered = out.buffered()[before..];
- if (c.loading and c.gen == g and (answered.len == 0 or std.mem.startsWith(u8, answered, "@none "))) {
- tr.note("{s} is still loading: waiting for it, then asking again", .{specs[si].name});
- post(si, .always, "{s} is still loading the workspace; the answer comes when it is done\u{2026}", .{specs[si].name});
- while (c.loading and c.gen == g and c.alive()) _ = try timedWait(c, io, nowMs() + 500);
- out.end = before;
- try attempt(c, io, si, arena, req, out, tr, g);
+ if (c.gen == g and (answered.len == 0 or std.mem.startsWith(u8, answered, "@none "))) {
+ const startup_deadline = c.ready_at_ms + 250;
+ const starting = nowMs() < startup_deadline;
+ while (starting and !c.loading and c.progress == 0 and c.gen == g and c.alive()) {
+ if (!try timedWait(c, io, startup_deadline)) break;
+ }
+ if (starting or c.loading or c.progress > 0) {
+ tr.note("{s} is still loading: waiting for it, then asking again", .{specs[si].name});
+ post(si, .always, "{s} is still loading the workspace; the answer comes when it is done\u{2026}", .{specs[si].name});
+ while ((c.loading or c.progress > 0) and c.gen == g and c.alive()) _ = try timedWait(c, io, nowMs() + 500);
+ out.end = before;
+ try attempt(c, io, si, arena, req, out, tr, g);
+ }
}
}
@@ -1866,19 +1871,29 @@ fn rootOf(dir: []const u8, markers: []const []const u8) []const u8 {
}
/// The spec's binary: the env override when set, else `bin`, PATH-searched
-/// unless it names a path.
+/// unless it names a path, then the standard per-user bin directory.
fn binOf(spec: *const Spec, buf: *[4096:0]u8) ?[:0]const u8 {
const name = if (libc.getenv(spec.env)) |v| std.mem.span(v) else spec.bin;
+ const path = if (libc.getenv("PATH")) |p| std.mem.span(p) else "/usr/local/bin:/usr/bin:/bin";
+ const home = if (libc.getenv("HOME")) |p| std.mem.span(p) else "";
+ return findBin(name, path, home, buf);
+}
+
+fn findBin(name: []const u8, path: []const u8, home: []const u8, buf: *[4096:0]u8) ?[:0]const u8 {
if (name.len == 0) return null;
if (std.mem.indexOfScalar(u8, name, '/') != null) {
const z = std.fmt.bufPrintSentinel(buf, "{s}", .{name}, 0) catch return null;
- return if (libc.access(z.ptr, libc.X_OK) == 0) z else null;
+ return if (libc.access(z.ptr, libc.X_OK) == 0 and !@import("../host_io.zig").Shell.isDirectory(z.ptr)) z else null;
}
- const path = if (libc.getenv("PATH")) |p| std.mem.span(p) else "/usr/local/bin:/usr/bin:/bin";
var it = std.mem.tokenizeScalar(u8, path, ':');
while (it.next()) |dir| {
const cand = std.fmt.bufPrintSentinel(buf, "{s}/{s}", .{ dir, name }, 0) catch continue;
- if (libc.access(cand.ptr, libc.X_OK) == 0) return cand;
+ if (libc.access(cand.ptr, libc.X_OK) == 0 and !@import("../host_io.zig").Shell.isDirectory(cand.ptr)) return cand;
+ }
+ // Desktop launchers often omit ~/.local/bin even when shells include it.
+ if (std.fs.path.isAbsolute(home)) {
+ const cand = std.fmt.bufPrintSentinel(buf, "{s}/.local/bin/{s}", .{ home, name }, 0) catch return null;
+ if (libc.access(cand.ptr, libc.X_OK) == 0 and !@import("../host_io.zig").Shell.isDirectory(cand.ptr)) return cand;
}
return null;
}
@@ -2495,6 +2510,7 @@ fn notification(c: *Conn, io: std.Io, si: usize, g: u32, raw: []const u8, method
}
if (std.mem.eql(u8, pkind, "begin")) {
c.progress += 1;
+ c.cond.broadcast(io);
const title = str(get(value, "title")) orelse "";
c.title_len = @intCast(@min(title.len, c.title.len));
@memcpy(c.title[0..c.title_len], title[0..c.title_len]);
@@ -2521,6 +2537,7 @@ fn notification(c: *Conn, io: std.Io, si: usize, g: u32, raw: []const u8, method
c.progress -= 1;
const idle = c.progress <= 0;
if (idle) c.progress = 0;
+ c.cond.broadcast(io);
c.mu.unlock(io);
// the setpoint every burst of progress ends on; `.always` + the
// dedupe above means it lands exactly once however the reports raced
@@ -2997,6 +3014,29 @@ test "rootOf takes the top-most marker and falls back to git then dir" {
try std.testing.expect(std.mem.startsWith(u8, deep, nothing));
}
+test "language server discovery honours PATH and overrides before the user bin directory" {
+ var tmp = std.testing.tmpDir(.{});
+ defer tmp.cleanup();
+ const io = std.testing.io;
+ try tmp.dir.createDirPath(io, ".local/bin");
+ try tmp.dir.createDirPath(io, "path-bin");
+ for ([_][]const u8{ ".local/bin/zls", "path-bin/zls" }) |path| {
+ const file = try tmp.dir.createFile(io, path, .{ .permissions = .fromMode(0o700) });
+ file.close(io);
+ }
+ var home_buf: [4096]u8 = undefined;
+ const home = home_buf[0..try tmp.dir.realPath(io, &home_buf)];
+ var path_buf: [4096]u8 = undefined;
+ const path = try std.fmt.bufPrint(&path_buf, "{s}/path-bin", .{home});
+ var out: [4096:0]u8 = undefined;
+ try std.testing.expect(std.mem.endsWith(u8, findBin("zls", path, home, &out).?, "/path-bin/zls"));
+ try std.testing.expect(std.mem.endsWith(u8, findBin("zls", "/no-such-bin", home, &out).?, "/.local/bin/zls"));
+ try std.testing.expect(findBin("", path, home, &out) == null);
+ try std.testing.expect(findBin("/no-such-bin/zls", path, home, &out) == null);
+ try std.testing.expect(findBin("zls", "", "", &out) == null);
+ try std.testing.expect(findBin(path, "", home, &out) == null); // a directory is not executable code
+}
+
test "specFor routes extensions and honours the disable env" {
try std.testing.expect(specFor("/x/main.rs") != null);
try std.testing.expect(specFor("/x/README.md") == null);