Skip to content

Commit

Permalink
Fix lsp client path comparisons on Windows
Browse files Browse the repository at this point in the history
The api method for finding an LSP client for a given document URI was
written to stop traversing when encountering the OS root. This logic
assumed POSIX paths and did not work correctly on Windows platforms.

This updates the clojure-lsp client ids to use the uri.fsPath which is
platform agnostic and updates the root path comparison in the lsp api to
compare in a platform agnostic manner.
  • Loading branch information
julienvincent committed Feb 25, 2023
1 parent 3c63e76 commit 4ac9237
Show file tree
Hide file tree
Showing 3 changed files with 14 additions and 5 deletions.
15 changes: 12 additions & 3 deletions src/lsp/api.ts
Expand Up @@ -13,6 +13,15 @@ export const clientIsAlive = (client: defs.LspClient) => {
return [defs.LspStatus.Starting, defs.LspStatus.Running].includes(client.status);
};

/**
* Checks if a given path is the root of the filesystem in a platform agnostic manner (this should
* work identically on POSIX and Windows)
*/
const isRoot = (dir: string) => {
const normalized = path.normalize(dir);
return path.dirname(normalized) === normalized;
};

/**
* Find the closest, active clojure-lsp client to a given URI. This works by traversing up the path component of the
* provided URI until a client at the same level is found. This works because clients in the LspClientStore use their
Expand All @@ -23,13 +32,13 @@ export const clientIsAlive = (client: defs.LspClient) => {
* must be explicitly referenced.
*/
export const getActiveClientForUri = (clients: defs.LspClientStore, uri: vscode.Uri) => {
let current = uri.path;
while (current !== '/') {
let current = uri.fsPath;
while (!isRoot(current)) {
const client = clients.get(current);
if (client && clientIsAlive(client)) {
return client;
}
current = path.resolve(current, '..');
current = path.join(current, '..');
}
const fallback_client = clients.get(FALLBACK_CLIENT_ID);
if (fallback_client && clientIsAlive(fallback_client)) {
Expand Down
2 changes: 1 addition & 1 deletion src/lsp/commands/vscode-commands.ts
Expand Up @@ -11,7 +11,7 @@ export const filterOutRootsWithClients = (
clients: defs.LspClientStore
) => {
return uris.filter((root) => {
const client = clients.get(root.uri.path);
const client = clients.get(root.uri.fsPath);
return !client || !api.clientIsAlive(client);
});
};
Expand Down
2 changes: 1 addition & 1 deletion src/lsp/provider.ts
Expand Up @@ -101,7 +101,7 @@ export const createClientProvider = (params: CreateClientProviderParams) => {
};

let lsp_server_path: Promise<string | void> | undefined = undefined;
const provisionClient = async (uri: vscode.Uri, id = uri.path) => {
const provisionClient = async (uri: vscode.Uri, id = uri.fsPath) => {
if (lsp_server_path === undefined) {
lsp_server_path = lsp_client.ensureLSPServer(params.context).catch((err) => {
console.error('Failed to download lsp server', err);
Expand Down

0 comments on commit 4ac9237

Please sign in to comment.