Skip to content
Merged
Show file tree
Hide file tree
Changes from 8 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
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.

1 change: 1 addition & 0 deletions Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -582,6 +582,7 @@ fork = "0.4.0"
futures = "0.3.32"
futures-concurrency = "7.7.1"
futures-lite = "1.13"
futures-util = "0.3.32"
gh-workflow = { git = "https://github.com/zed-industries/gh-workflow", rev = "37f3c0575d379c218a9c455ee67585184e40d43f" }
git2 = { version = "0.20.1", default-features = false, features = ["vendored-libgit2"] }
globset = "0.4"
Expand Down
176 changes: 127 additions & 49 deletions crates/extension/src/extension_builder.rs
Original file line number Diff line number Diff line change
@@ -1,21 +1,26 @@
use crate::{
ExtensionLibraryKind, ExtensionManifest, GrammarManifestEntry, build_debug_adapter_schema_path,
parse_wasm_extension_version,
ExtensionLibraryKind, ExtensionManifest, GrammarManifestEntry, parse_wasm_extension_version,
};
use ::fs::Fs;
use anyhow::{Context as _, Result, bail};
use futures::{StreamExt, io};
use futures::{
FutureExt, StreamExt,
channel::oneshot::{self, Sender},
io,
};
use heck::ToSnakeCase;
use http_client::{self, AsyncBody, HttpClient};
use language::LanguageConfig;
use semver::Version;
use serde::Deserialize;
use std::{
env, fs, mem,
num::NonZeroUsize,
ops::Not,
path::{Path, PathBuf},
str::FromStr,
sync::Arc,
};
use util::{command::Stdio, rel_path::PathExt};
use util::{ResultExt, command::Stdio, rel_path::PathExt};
use wasm_encoder::{ComponentSectionId, Encode as _, RawSection, Section as _};
use wasmparser::Parser;

Expand Down Expand Up @@ -52,13 +57,24 @@ pub struct ExtensionBuilder {
pub http: Arc<dyn HttpClient>,
}

pub enum CompilationConcurrency {
Unbounded,
Bounded(NonZeroUsize),
}

const DEFAULT_COMPILATION_CONCURRENCY: NonZeroUsize = NonZeroUsize::new(3).unwrap();

pub struct CompileExtensionOptions {
pub release: bool,
pub max_concurrency: CompilationConcurrency,
}

impl CompileExtensionOptions {
pub const fn dev() -> Self {
Self { release: false }
Self {
release: false,
max_concurrency: CompilationConcurrency::Bounded(DEFAULT_COMPILATION_CONCURRENCY),
}
}
}

Expand Down Expand Up @@ -87,7 +103,9 @@ impl ExtensionBuilder {
options: CompileExtensionOptions,
fs: Arc<dyn Fs>,
) -> Result<()> {
populate_defaults(extension_manifest, extension_dir, fs).await?;
let start = std::time::Instant::now();

populate_defaults(extension_manifest, extension_dir, fs.clone()).await?;

if extension_dir.is_relative() {
bail!(
Expand All @@ -96,58 +114,117 @@ impl ExtensionBuilder {
);
}

fs::create_dir_all(&self.cache_dir).context("failed to create cache dir")?;
fs.create_dir(&self.cache_dir)
.await
.context("failed to create cache dir")?;

if extension_manifest.lib.kind == Some(ExtensionLibraryKind::Rust) {
log::info!("compiling Rust extension {}", extension_dir.display());
self.compile_rust_extension(extension_dir, extension_manifest, options)
.await
.context("failed to compile Rust extension")?;
log::info!("compiled Rust extension {}", extension_dir.display());
}
let (tx, mut rx) = oneshot::channel();

for (debug_adapter_name, meta) in &mut extension_manifest.debug_adapters {
let debug_adapter_schema_path =
extension_dir.join(build_debug_adapter_schema_path(debug_adapter_name, meta)?);
let clang_path = extension_manifest.grammars.is_empty().not().then(|| {
std::iter::repeat_n(
Comment thread
MrSubidubi marked this conversation as resolved.
Resolved
async {
self.install_wasi_sdk_if_needed()
.await
.log_err()
.map(Arc::new)
}
.shared(),
extension_manifest.grammars.len(),
)
});

let rust_compilation_task =
(extension_manifest.lib.kind == Some(ExtensionLibraryKind::Rust)).then(|| {
async {
log::info!("compiling Rust extension {}", extension_dir.display());
self.compile_rust_extension(extension_dir, extension_manifest, tx, &options)
.await
.context("failed to compile Rust extension")?;

log::info!("compiled Rust extension {}", extension_dir.display());
Ok(())
}
.boxed()
});

let grammar_compilation_tasks = extension_manifest
.grammars
.iter()
.zip(clang_path.into_iter().flatten())
.map(|((grammar_name, grammar_metadata), clang_path_task)| {
async move {
let snake_cased_grammar_name = grammar_name.to_snake_case();
if grammar_name.as_ref() != snake_cased_grammar_name.as_str() {
bail!(
"grammar name '{grammar_name}' must be \
written in snake_case: {snake_cased_grammar_name}"
);
}

let debug_adapter_schema = fs::read_to_string(&debug_adapter_schema_path)
.with_context(|| {
format!("failed to read debug adapter schema for `{debug_adapter_name}` from `{debug_adapter_schema_path:?}`")
})?;
_ = serde_json::Value::from_str(&debug_adapter_schema).with_context(|| {
format!("Debug adapter schema for `{debug_adapter_name}` (path: `{debug_adapter_schema_path:?}`) is not a valid JSON")
})?;
}
for (grammar_name, grammar_metadata) in &extension_manifest.grammars {
let snake_cased_grammar_name = grammar_name.to_snake_case();
if grammar_name.as_ref() != snake_cased_grammar_name.as_str() {
bail!(
"grammar name '{grammar_name}' must be written in snake_case: {snake_cased_grammar_name}"
);
log::info!(
"compiling grammar {grammar_name} for extension {}",
extension_dir.display()
);

let clang_path = clang_path_task
.await
.context("Failed to resolve clang path")?;

self.compile_grammar(
extension_dir,
grammar_name.as_ref(),
grammar_metadata,
&clang_path,
)
.await
.with_context(|| format!("failed to compile grammar '{grammar_name}'"))?;
log::info!(
"compiled grammar {grammar_name} for extension {}",
extension_dir.display()
);

Ok(())
}
.boxed()
});

let tasks = rust_compilation_task
.into_iter()
.chain(grammar_compilation_tasks)
.collect::<Vec<_>>();

match options.max_concurrency {
CompilationConcurrency::Unbounded => {
futures::future::try_join_all(tasks).await?;
}
CompilationConcurrency::Bounded(max_concurrency) => {
let mut stream =
futures::stream::iter(tasks).buffer_unordered(max_concurrency.get());
Comment thread
MrSubidubi marked this conversation as resolved.
Resolved

log::info!(
"compiling grammar {grammar_name} for extension {}",
extension_dir.display()
);
self.compile_grammar(extension_dir, grammar_name.as_ref(), grammar_metadata)
.await
.with_context(|| format!("failed to compile grammar '{grammar_name}'"))?;
log::info!(
"compiled grammar {grammar_name} for extension {}",
extension_dir.display()
);
while let Some(result) = stream.next().await {
result?;
}
}
}

log::info!("finished compiling extension {}", extension_dir.display());
if let Ok(version) = rx.try_recv() {
extension_manifest.lib.version = version;
}

log::info!(
"finished compiling extension {} in {time:.2}s",
extension_dir.display(),
time = start.elapsed().as_secs_f64(),
);
Ok(())
}

async fn compile_rust_extension(
&self,
extension_dir: &Path,
manifest: &mut ExtensionManifest,
options: CompileExtensionOptions,
manifest: &ExtensionManifest,
wasm_extension_api_version_tx: Sender<Version>,
options: &CompileExtensionOptions,
) -> anyhow::Result<()> {
self.install_rust_wasm_target_if_needed().await?;

Expand Down Expand Up @@ -209,7 +286,9 @@ impl ExtensionBuilder {
let wasm_extension_api_version =
parse_wasm_extension_version(&manifest.id, &component_bytes)
.context("compiled wasm did not contain a valid zed extension api version")?;
manifest.lib.version = Some(wasm_extension_api_version);
wasm_extension_api_version_tx
.send(wasm_extension_api_version)
.map_err(|_| anyhow::anyhow!("Failed to send API version"))?;

let extension_file = extension_dir.join("extension.wasm");
fs::write(extension_file.clone(), &component_bytes)
Expand All @@ -229,9 +308,8 @@ impl ExtensionBuilder {
extension_dir: &Path,
grammar_name: &str,
grammar_metadata: &GrammarManifestEntry,
clang_path: &Path,
) -> Result<()> {
let clang_path = self.install_wasi_sdk_if_needed().await?;

let mut grammar_repo_dir = extension_dir.to_path_buf();
grammar_repo_dir.extend(["grammars", grammar_name]);

Expand Down
1 change: 1 addition & 0 deletions crates/extension_cli/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@ clap = { workspace = true, features = ["derive"] }
cloud_api_types.workspace = true
env_logger.workspace = true
extension.workspace = true
futures.workspace = true
fs.workspace = true
gpui_platform.workspace = true
language.workspace = true
Expand Down
Loading
Loading