Skip to content

Commit 32403df

Browse files
authored
[ty] Avoid panicking when there are multiple workspaces (astral-sh#18151)
## Summary This PR updates the language server to avoid panicking when there are multiple workspace folders passed during initialization. The server currently picks up the first workspace folder and provides a warning and a log message. ## Test Plan <img width="1724" alt="Screenshot 2025-05-17 at 11 43 09" src="https://github.com/user-attachments/assets/1a7ddbc3-198d-4191-a28f-9b69321e8f99" />
1 parent 76ab342 commit 32403df

File tree

2 files changed

+22
-4
lines changed

2 files changed

+22
-4
lines changed

crates/ty_server/src/message.rs

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -44,3 +44,11 @@ macro_rules! show_err_msg {
4444
crate::message::show_message(::core::format_args!($msg$(, $($arg)*)?).to_string(), lsp_types::MessageType::ERROR)
4545
};
4646
}
47+
48+
/// Sends a request to display a warning to the client with a formatted message. The warning is
49+
/// sent in a `window/showMessage` notification.
50+
macro_rules! show_warn_msg {
51+
($msg:expr$(, $($arg:tt)*)?) => {
52+
crate::message::show_message(::core::format_args!($msg$(, $($arg)*)?).to_string(), lsp_types::MessageType::WARNING)
53+
};
54+
}

crates/ty_server/src/server.rs

Lines changed: 14 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -96,10 +96,20 @@ impl Server {
9696
anyhow::anyhow!("Failed to get the current working directory while creating a default workspace.")
9797
})?;
9898

99-
if workspaces.len() > 1 {
100-
// TODO(dhruvmanila): Support multi-root workspaces
101-
anyhow::bail!("Multi-root workspaces are not supported yet");
102-
}
99+
let workspaces = if workspaces.len() > 1 {
100+
let first_workspace = workspaces.into_iter().next().unwrap();
101+
tracing::warn!(
102+
"Multiple workspaces are not yet supported, using the first workspace: {}",
103+
&first_workspace.0
104+
);
105+
show_warn_msg!(
106+
"Multiple workspaces are not yet supported, using the first workspace: {}",
107+
&first_workspace.0
108+
);
109+
vec![first_workspace]
110+
} else {
111+
workspaces
112+
};
103113

104114
Ok(Self {
105115
connection,

0 commit comments

Comments
 (0)