Skip to content
Closed
Show file tree
Hide file tree
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
4 changes: 4 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -78,3 +78,7 @@ src/extensions/bundled/*/grammars/*.wasm

# local codex settings
src-tauri/.claude/settings.local.json

# local scratch dirs
.pi/
.tmp/
1 change: 1 addition & 0 deletions Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

70 changes: 65 additions & 5 deletions crates/lsp/src/client.rs
Original file line number Diff line number Diff line change
Expand Up @@ -6,8 +6,9 @@ use serde_json::{Value, json};
use std::{
collections::HashMap,
ffi::OsStr,
fs,
io::{BufRead, BufReader, Read, Write},
path::PathBuf,
path::{Path, PathBuf},
process::{Child, Command, Stdio},
sync::{
Arc, Mutex,
Expand All @@ -20,6 +21,14 @@ use tokio::sync::oneshot;

type PendingRequests = Arc<Mutex<HashMap<u64, oneshot::Sender<Result<Value>>>>>;

fn is_js_server_path(server_path: &Path) -> bool {
fs::canonicalize(server_path)
.unwrap_or_else(|_| server_path.to_path_buf())
.extension()
.map(|ext| ext == OsStr::new("js") || ext == OsStr::new("mjs") || ext == OsStr::new("cjs"))
.unwrap_or(false)
}

#[derive(Clone)]
pub struct LspClient {
request_counter: Arc<AtomicU64>,
Expand All @@ -37,10 +46,7 @@ impl LspClient {
app_handle: Option<AppHandle>,
) -> Result<(Self, Child)> {
// Check if this is a JavaScript-based language server
let is_js_server = server_path
.extension()
.map(|ext| ext == OsStr::new("js") || ext == OsStr::new("mjs") || ext == OsStr::new("cjs"))
.unwrap_or(false);
let is_js_server = is_js_server_path(&server_path);

let (command_path, final_args) = if is_js_server {
// JS-based server requires Node.js runtime
Expand Down Expand Up @@ -687,3 +693,57 @@ impl LspClient {
self.notify::<notification::DidCloseTextDocument>(params)
}
}

#[cfg(test)]
mod tests {
use super::is_js_server_path;
use std::{
fs,
path::PathBuf,
time::{SystemTime, UNIX_EPOCH},
};

fn temp_test_dir(name: &str) -> PathBuf {
let unique = SystemTime::now()
.duration_since(UNIX_EPOCH)
.expect("system time should be after unix epoch")
.as_nanos();
let dir = std::env::temp_dir().join(format!("athas-lsp-{name}-{unique}"));
fs::create_dir_all(&dir).expect("temp test dir should be created");
dir
}

#[test]
fn detects_direct_js_entrypoints() {
let dir = temp_test_dir("direct-js");
let entrypoint = dir.join("server.mjs");
fs::write(&entrypoint, "console.log('ok');").expect("entrypoint should be written");

assert!(is_js_server_path(&entrypoint));

let _ = fs::remove_dir_all(dir);
}

#[cfg(unix)]
#[test]
fn detects_js_entrypoints_through_bin_symlinks() {
use std::os::unix::fs::symlink;

let dir = temp_test_dir("symlink-js");
let package_dir = dir.join("typescript-language-server");
let bin_dir = dir.join(".bin");
fs::create_dir_all(package_dir.join("lib")).expect("package lib dir should be created");
fs::create_dir_all(&bin_dir).expect("bin dir should be created");

let entrypoint = package_dir.join("lib/cli.mjs");
fs::write(&entrypoint, "console.log('ok');").expect("entrypoint should be written");

let bin_link = bin_dir.join("typescript-language-server");
symlink("../typescript-language-server/lib/cli.mjs", &bin_link)
.expect("bin symlink should be created");

assert!(is_js_server_path(&bin_link));

let _ = fs::remove_dir_all(dir);
}
}
3 changes: 3 additions & 0 deletions src-tauri/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -101,3 +101,6 @@ tauri-plugin-window-state = "2"
[target.'cfg(target_os = "macos")'.dependencies]
rand = "0.8.5"
objc = "0.2"

[target.'cfg(target_os = "linux")'.dependencies]
wry = "0.53.5"
Loading
Loading