Provide wasm extensions with APIs needed for using pre-installed LSP binaries (#9085)

In this PR, we've added two new methods that LSP extensions can call:
* `shell_env()`, for retrieving the environment variables set in the
user's default shell in the worktree
* `which(command)`, for looking up paths to an executable (accounting
for the user's shell env in the worktree)

To test this out, we moved the `uiua` language support into an
extension. We went ahead and removed the built-in support, since this
language is extremely obscure. Sorry @mikayla-maki. To continue coding
in Uiua in Zed, for now you can `Add Dev Extension` from the extensions
pane, and select the `extensions/uiua` directory in the Zed repo. Very
soon, we'll support publishing these extensions so that you'll be able
to just install it normally.

Release Notes:

- N/A

---------

Co-authored-by: Marshall <marshall@zed.dev>
This commit is contained in:
Max Brunsfeld
2024-03-08 17:18:06 -05:00
committed by GitHub
co-authored by Marshall
parent 5abcc1c3c5
commit 8a6264d933
23 changed files with 235 additions and 256 deletions
+2 -1
View File
@@ -37,7 +37,8 @@ settings.workspace = true
theme.workspace = true
toml.workspace = true
util.workspace = true
wasmtime = { workspace = true, features = ["async"] }
wasm-encoder.workspace = true
wasmtime.workspace = true
wasmtime-wasi.workspace = true
wasmparser.workspace = true
wit-component.workspace = true
+79 -5
View File
@@ -6,13 +6,16 @@ use async_tar::Archive;
use futures::io::BufReader;
use futures::AsyncReadExt;
use serde::Deserialize;
use std::mem;
use std::{
env, fs,
path::{Path, PathBuf},
process::{Command, Stdio},
sync::Arc,
};
use util::http::{AsyncBody, HttpClient};
use util::http::{self, AsyncBody, HttpClient};
use wasm_encoder::{ComponentSectionId, Encode as _, RawSection, Section as _};
use wasmparser::Parser;
use wit_component::ComponentEncoder;
/// Currently, we compile with Rust's `wasm32-wasi` target, which works with WASI `preview1`.
@@ -59,8 +62,11 @@ struct CargoTomlPackage {
}
impl ExtensionBuilder {
pub fn new(cache_dir: PathBuf, http: Arc<dyn HttpClient>) -> Self {
Self { cache_dir, http }
pub fn new(cache_dir: PathBuf) -> Self {
Self {
cache_dir,
http: http::client(),
}
}
pub async fn compile_extension(
@@ -138,6 +144,10 @@ impl ExtensionBuilder {
.encode()
.context("failed to encode wasm component")?;
let component_bytes = self
.strip_custom_sections(&component_bytes)
.context("failed to strip debug sections from wasm component")?;
fs::write(extension_dir.join("extension.wasm"), &component_bytes)
.context("failed to write extension.wasm")?;
@@ -310,7 +320,7 @@ impl ExtensionBuilder {
async fn install_wasi_preview1_adapter_if_needed(&self) -> Result<Vec<u8>> {
let cache_path = self.cache_dir.join("wasi_snapshot_preview1.reactor.wasm");
if let Ok(content) = fs::read(&cache_path) {
if wasmparser::Parser::is_core_wasm(&content) {
if Parser::is_core_wasm(&content) {
return Ok(content);
}
}
@@ -333,7 +343,7 @@ impl ExtensionBuilder {
fs::write(&cache_path, &content)
.with_context(|| format!("failed to save file {}", cache_path.display()))?;
if !wasmparser::Parser::is_core_wasm(&content) {
if !Parser::is_core_wasm(&content) {
bail!("downloaded wasi adapter is invalid");
}
Ok(content)
@@ -379,4 +389,68 @@ impl ExtensionBuilder {
Ok(clang_path)
}
// This was adapted from:
// https://github.com/bytecodealliance/wasm-tools/1791a8f139722e9f8679a2bd3d8e423e55132b22/src/bin/wasm-tools/strip.rs
fn strip_custom_sections(&self, input: &Vec<u8>) -> Result<Vec<u8>> {
use wasmparser::Payload::*;
let strip_custom_section = |name: &str| name.starts_with(".debug");
let mut output = Vec::new();
let mut stack = Vec::new();
for payload in Parser::new(0).parse_all(input) {
let payload = payload?;
// Track nesting depth, so that we don't mess with inner producer sections:
match payload {
Version { encoding, .. } => {
output.extend_from_slice(match encoding {
wasmparser::Encoding::Component => &wasm_encoder::Component::HEADER,
wasmparser::Encoding::Module => &wasm_encoder::Module::HEADER,
});
}
ModuleSection { .. } | ComponentSection { .. } => {
stack.push(mem::take(&mut output));
continue;
}
End { .. } => {
let mut parent = match stack.pop() {
Some(c) => c,
None => break,
};
if output.starts_with(&wasm_encoder::Component::HEADER) {
parent.push(ComponentSectionId::Component as u8);
output.encode(&mut parent);
} else {
parent.push(ComponentSectionId::CoreModule as u8);
output.encode(&mut parent);
}
output = parent;
}
_ => {}
}
match &payload {
CustomSection(c) => {
if strip_custom_section(c.name()) {
continue;
}
}
_ => {}
}
if let Some((id, range)) = payload.as_section() {
RawSection {
id,
data: &input[range],
}
.append_to(&mut output);
}
}
Ok(output)
}
}
+2 -2
View File
@@ -193,7 +193,7 @@ impl ExtensionStore {
extension_index: Default::default(),
installed_dir,
index_path,
builder: Arc::new(ExtensionBuilder::new(build_dir, http_client.clone())),
builder: Arc::new(ExtensionBuilder::new(build_dir)),
outstanding_operations: Default::default(),
modified_extensions: Default::default(),
reload_complete_senders: Vec::new(),
@@ -545,7 +545,7 @@ impl ExtensionStore {
builder
.compile_extension(
&extension_source_path,
CompileExtensionOptions { release: true },
CompileExtensionOptions { release: false },
)
.await
}
+1 -17
View File
@@ -7,10 +7,7 @@ use collections::BTreeMap;
use fs::{FakeFs, Fs, RealFs};
use futures::{io::BufReader, AsyncReadExt, StreamExt};
use gpui::{Context, TestAppContext};
use language::{
Language, LanguageConfig, LanguageMatcher, LanguageRegistry, LanguageServerBinaryStatus,
LanguageServerName,
};
use language::{LanguageMatcher, LanguageRegistry, LanguageServerBinaryStatus, LanguageServerName};
use node_runtime::FakeNodeRuntime;
use parking_lot::Mutex;
use project::Project;
@@ -573,19 +570,6 @@ async fn test_extension_store_with_gleam_extension(cx: &mut TestAppContext) {
})
.await
.unwrap();
project.update(cx, |project, cx| {
project.set_language_for_buffer(
&buffer,
Arc::new(Language::new(
LanguageConfig {
name: "Gleam".into(),
..Default::default()
},
None,
)),
cx,
)
});
let fake_server = fake_servers.next().await.unwrap();
let expected_server_path = extensions_dir.join("work/gleam/gleam-v1.2.3/gleam");
+36 -26
View File
@@ -3,7 +3,7 @@ use anyhow::{anyhow, bail, Context as _, Result};
use async_compression::futures::bufread::GzipDecoder;
use async_tar::Archive;
use async_trait::async_trait;
use fs::Fs;
use fs::{normalize_path, Fs};
use futures::{
channel::{
mpsc::{self, UnboundedSender},
@@ -72,8 +72,6 @@ type ExtensionCall = Box<
static WASM_ENGINE: OnceLock<wasmtime::Engine> = OnceLock::new();
const EXTENSION_WORK_DIR_PATH: &str = "/zed/work";
impl WasmHost {
pub fn new(
fs: Arc<dyn Fs>,
@@ -181,11 +179,12 @@ impl WasmHost {
.await
.context("failed to create extension work dir")?;
let work_dir_preopen = Dir::open_ambient_dir(extension_work_dir, ambient_authority())
let work_dir_preopen = Dir::open_ambient_dir(&extension_work_dir, ambient_authority())
.context("failed to preopen extension work directory")?;
let current_dir_preopen = work_dir_preopen
.try_clone()
.context("failed to preopen extension current directory")?;
let extension_work_dir = extension_work_dir.to_string_lossy();
let perms = wasi::FilePerms::all();
let dir_perms = wasi::DirPerms::all();
@@ -193,26 +192,24 @@ impl WasmHost {
Ok(wasi::WasiCtxBuilder::new()
.inherit_stdio()
.preopened_dir(current_dir_preopen, dir_perms, perms, ".")
.preopened_dir(work_dir_preopen, dir_perms, perms, EXTENSION_WORK_DIR_PATH)
.env("PWD", EXTENSION_WORK_DIR_PATH)
.env("RUST_BACKTRACE", "1")
.preopened_dir(work_dir_preopen, dir_perms, perms, &extension_work_dir)
.env("PWD", &extension_work_dir)
.env("RUST_BACKTRACE", "full")
.build())
}
pub fn path_from_extension(&self, id: &Arc<str>, path: &Path) -> PathBuf {
self.writeable_path_from_extension(id, path)
.unwrap_or_else(|| path.to_path_buf())
let extension_work_dir = self.work_dir.join(id.as_ref());
normalize_path(&extension_work_dir.join(path))
}
pub fn writeable_path_from_extension(&self, id: &Arc<str>, path: &Path) -> Option<PathBuf> {
let path = path.strip_prefix(EXTENSION_WORK_DIR_PATH).unwrap_or(path);
if path.is_relative() {
let mut result = self.work_dir.clone();
result.push(id.as_ref());
result.extend(path);
Some(result)
pub fn writeable_path_from_extension(&self, id: &Arc<str>, path: &Path) -> Result<PathBuf> {
let extension_work_dir = self.work_dir.join(id.as_ref());
let path = normalize_path(&extension_work_dir.join(path));
if path.starts_with(&extension_work_dir) {
Ok(path)
} else {
None
Err(anyhow!("cannot write to path {}", path.display()))
}
}
}
@@ -252,13 +249,6 @@ impl WasmExtension {
}
}
impl WasmState {
pub fn writeable_path_from_extension(&self, path: &Path) -> Option<PathBuf> {
self.host
.writeable_path_from_extension(&self.manifest.id, path)
}
}
#[async_trait]
impl wit::HostWorktree for WasmState {
async fn read_text_file(
@@ -273,6 +263,26 @@ impl wit::HostWorktree for WasmState {
.map_err(|error| error.to_string()))
}
async fn shell_env(
&mut self,
delegate: Resource<Arc<dyn LspAdapterDelegate>>,
) -> wasmtime::Result<wit::EnvVars> {
let delegate = self.table.get(&delegate)?;
Ok(delegate.shell_env().await.into_iter().collect())
}
async fn which(
&mut self,
delegate: Resource<Arc<dyn LspAdapterDelegate>>,
binary_name: String,
) -> wasmtime::Result<Option<String>> {
let delegate = self.table.get(&delegate)?;
Ok(delegate
.which(binary_name.as_ref())
.await
.map(|path| path.to_string_lossy().to_string()))
}
fn drop(&mut self, _worktree: Resource<wit::Worktree>) -> Result<()> {
// we only ever hand out borrows of worktrees
Ok(())
@@ -395,8 +405,8 @@ impl wit::ExtensionImports for WasmState {
this.host.fs.create_dir(&extension_work_dir).await?;
let destination_path = this
.writeable_path_from_extension(&path)
.ok_or_else(|| anyhow!("cannot write to path {:?}", path))?;
.host
.writeable_path_from_extension(&this.manifest.id, &path)?;
let mut response = this
.host
+5 -1
View File
@@ -61,14 +61,18 @@ world extension {
/// Updates the installation status for the given language server.
import set-language-server-installation-status: func(language-server-name: string, status: language-server-installation-status);
type env-vars = list<tuple<string, string>>;
record command {
command: string,
args: list<string>,
env: list<tuple<string, string>>,
env: env-vars,
}
resource worktree {
read-text-file: func(path: string) -> result<string, string>;
which: func(binary-name: string) -> option<string>;
shell-env: func() -> env-vars;
}
record language-server-config {
+3 -2
View File
@@ -40,7 +40,7 @@ use smol::future::FutureExt as _;
use std::{
any::Any,
cell::RefCell,
ffi::OsString,
ffi::OsStr,
fmt::Debug,
hash::Hash,
mem,
@@ -277,7 +277,8 @@ pub trait LspAdapterDelegate: Send + Sync {
fn http_client(&self) -> Arc<dyn HttpClient>;
fn update_status(&self, language: LanguageServerName, status: LanguageServerBinaryStatus);
async fn which_command(&self, command: OsString) -> Option<(PathBuf, HashMap<String, String>)>;
async fn which(&self, command: &OsStr) -> Option<PathBuf>;
async fn shell_env(&self) -> HashMap<String, String>;
async fn read_text_file(&self, path: PathBuf) -> Result<String>;
}
-1
View File
@@ -76,7 +76,6 @@ tree-sitter-scheme.workspace = true
tree-sitter-svelte.workspace = true
tree-sitter-toml.workspace = true
tree-sitter-typescript.workspace = true
tree-sitter-uiua.workspace = true
tree-sitter-vue.workspace = true
tree-sitter-yaml.workspace = true
tree-sitter-zig.workspace = true
+2 -1
View File
@@ -58,7 +58,8 @@ impl super::LspAdapter for GoLspAdapter {
&self,
delegate: &dyn LspAdapterDelegate,
) -> Option<LanguageServerBinary> {
let (path, env) = delegate.which_command(OsString::from("gopls")).await?;
let env = delegate.shell_env().await;
let path = delegate.which("gopls".as_ref()).await?;
Some(LanguageServerBinary {
path,
arguments: server_binary_arguments(),
-3
View File
@@ -39,7 +39,6 @@ mod tailwind;
mod terraform;
mod toml;
mod typescript;
mod uiua;
mod vue;
mod yaml;
mod zig;
@@ -114,7 +113,6 @@ pub fn init(
("toml", tree_sitter_toml::language()),
("tsx", tree_sitter_typescript::language_tsx()),
("typescript", tree_sitter_typescript::language_typescript()),
("uiua", tree_sitter_uiua::language()),
("vue", tree_sitter_vue::language()),
("yaml", tree_sitter_yaml::language()),
("zig", tree_sitter_zig::language()),
@@ -344,7 +342,6 @@ pub fn init(
"vue",
vec![Arc::new(vue::VueLspAdapter::new(node_runtime.clone()))]
);
language!("uiua", vec![Arc::new(uiua::UiuaLanguageServer {})]);
language!("proto");
language!("terraform", vec![Arc::new(terraform::TerraformLspAdapter)]);
language!(
-11
View File
@@ -1,11 +0,0 @@
name = "Uiua"
grammar = "uiua"
path_suffixes = ["ua"]
line_comments = ["# "]
autoclose_before = ")]}\""
brackets = [
{ start = "{", end = "}", close = true, newline = false},
{ start = "[", end = "]", close = true, newline = false },
{ start = "(", end = ")", close = true, newline = false },
{ start = "\"", end = "\"", close = true, newline = false, not_in = ["string"] },
]
-50
View File
@@ -1,50 +0,0 @@
[
(openParen)
(closeParen)
(openCurly)
(closeCurly)
(openBracket)
(closeBracket)
] @punctuation.bracket
[
(branchSeparator)
(underscore)
] @constructor
; ] @punctuation.delimiter
[ (character) ] @constant.character
[ (comment) ] @comment
[ (constant) ] @constant.numeric
[ (identifier) ] @variable
[ (leftArrow) ] @keyword
[ (function) ] @function
[ (modifier1) ] @operator
[ (modifier2) ] @operator
[ (number) ] @constant.numeric
[ (placeHolder) ] @special
[ (otherConstant) ] @string.special
[ (signature) ] @type
[ (system) ] @function.builtin
[ (tripleMinus) ] @module
; planet
[
"id"
"identity"
"∘"
"dip"
"⊙"
"gap"
"⋅"
] @tag
[
(string)
(multiLineString)
] @string
; [
; (deprecated)
; (identifierDeprecated)
; ] @warning
-3
View File
@@ -1,3 +0,0 @@
[
(array)
] @indent
+2 -2
View File
@@ -7,7 +7,6 @@ use language::{LanguageServerName, LspAdapter, LspAdapterDelegate};
use lsp::LanguageServerBinary;
use smol::fs;
use std::env::consts::{ARCH, OS};
use std::ffi::OsString;
use std::{any::Any, path::PathBuf};
use util::async_maybe;
use util::github::latest_github_release;
@@ -45,7 +44,8 @@ impl LspAdapter for ZlsAdapter {
&self,
delegate: &dyn LspAdapterDelegate,
) -> Option<LanguageServerBinary> {
let (path, env) = delegate.which_command(OsString::from("zls")).await?;
let env = delegate.shell_env().await;
let path = delegate.which("zls".as_ref()).await?;
Some(LanguageServerBinary {
path,
arguments: vec![],
+28 -26
View File
@@ -72,7 +72,7 @@ use std::{
cmp::{self, Ordering},
convert::TryInto,
env,
ffi::OsString,
ffi::OsStr,
hash::Hash,
mem,
num::NonZeroU32,
@@ -9390,6 +9390,7 @@ struct ProjectLspAdapterDelegate {
fs: Arc<dyn Fs>,
http_client: Arc<dyn HttpClient>,
language_registry: Arc<LanguageRegistry>,
shell_env: Mutex<Option<HashMap<String, String>>>,
}
impl ProjectLspAdapterDelegate {
@@ -9400,8 +9401,21 @@ impl ProjectLspAdapterDelegate {
fs: project.fs.clone(),
http_client: project.client.http_client(),
language_registry: project.languages.clone(),
shell_env: Default::default(),
})
}
async fn load_shell_env(&self) {
let worktree_abs_path = self.worktree.abs_path();
let shell_env = load_shell_environment(&worktree_abs_path)
.await
.with_context(|| {
format!("failed to determine load login shell environment in {worktree_abs_path:?}")
})
.log_err()
.unwrap_or_default();
*self.shell_env.lock() = Some(shell_env);
}
}
#[async_trait]
@@ -9416,32 +9430,20 @@ impl LspAdapterDelegate for ProjectLspAdapterDelegate {
self.http_client.clone()
}
async fn which_command(&self, command: OsString) -> Option<(PathBuf, HashMap<String, String>)> {
async fn shell_env(&self) -> HashMap<String, String> {
self.load_shell_env().await;
self.shell_env.lock().as_ref().cloned().unwrap_or_default()
}
async fn which(&self, command: &OsStr) -> Option<PathBuf> {
let worktree_abs_path = self.worktree.abs_path();
let shell_env = load_shell_environment(&worktree_abs_path)
.await
.with_context(|| {
format!("failed to determine load login shell environment in {worktree_abs_path:?}")
})
.log_err();
if let Some(shell_env) = shell_env {
let shell_path = shell_env.get("PATH");
match which::which_in(&command, shell_path, &worktree_abs_path) {
Ok(command_path) => Some((command_path, shell_env)),
Err(error) => {
log::warn!(
"failed to determine path for command {:?} in shell PATH {:?}: {error}",
command.to_string_lossy(),
shell_path.map(String::as_str).unwrap_or("")
);
None
}
}
} else {
None
}
self.load_shell_env().await;
let shell_path = self
.shell_env
.lock()
.as_ref()
.and_then(|shell_env| shell_env.get("PATH").cloned());
which::which_in(command, shell_path.as_ref(), &worktree_abs_path).ok()
}
fn update_status(