1use crate::{
2 ExtensionLibraryKind, ExtensionManifest, GrammarManifestEntry, build_debug_adapter_schema_path,
3 parse_wasm_extension_version,
4};
5use ::fs::Fs;
6use anyhow::{Context as _, Result, bail};
7use futures::{StreamExt, io};
8use heck::ToSnakeCase;
9use http_client::{self, AsyncBody, HttpClient};
10use serde::Deserialize;
11use std::{
12 env, fs, mem,
13 path::{Path, PathBuf},
14 str::FromStr,
15 sync::Arc,
16};
17use util::command::Stdio;
18use wasm_encoder::{ComponentSectionId, Encode as _, RawSection, Section as _};
19use wasmparser::Parser;
20
21/// Currently, we compile with Rust's `wasm32-wasip2` target, which works with WASI `preview2` and the component model.
22const RUST_TARGET: &str = "wasm32-wasip2";
23
24/// Compiling Tree-sitter parsers from C to WASM requires Clang 17, and a WASM build of libc
25/// and clang's runtime library. The `wasi-sdk` provides these binaries.
26///
27/// Once Clang 17 and its wasm target are available via system package managers, we won't need
28/// to download this.
29const WASI_SDK_URL: &str = "https://github.com/WebAssembly/wasi-sdk/releases/download/wasi-sdk-25/";
30const WASI_SDK_ASSET_NAME: Option<&str> = if cfg!(all(target_os = "macos", target_arch = "x86_64"))
31{
32 Some("wasi-sdk-25.0-x86_64-macos.tar.gz")
33} else if cfg!(all(target_os = "macos", target_arch = "aarch64")) {
34 Some("wasi-sdk-25.0-arm64-macos.tar.gz")
35} else if cfg!(all(target_os = "linux", target_arch = "x86_64")) {
36 Some("wasi-sdk-25.0-x86_64-linux.tar.gz")
37} else if cfg!(all(target_os = "linux", target_arch = "aarch64")) {
38 Some("wasi-sdk-25.0-arm64-linux.tar.gz")
39} else if cfg!(all(target_os = "freebsd", target_arch = "x86_64")) {
40 Some("wasi-sdk-25.0-x86_64-linux.tar.gz")
41} else if cfg!(all(target_os = "freebsd", target_arch = "aarch64")) {
42 Some("wasi-sdk-25.0-arm64-linux.tar.gz")
43} else if cfg!(all(target_os = "windows", target_arch = "x86_64")) {
44 Some("wasi-sdk-25.0-x86_64-windows.tar.gz")
45} else {
46 None
47};
48
49pub struct ExtensionBuilder {
50 cache_dir: PathBuf,
51 pub http: Arc<dyn HttpClient>,
52}
53
54pub struct CompileExtensionOptions {
55 pub release: bool,
56}
57
58impl CompileExtensionOptions {
59 pub const fn dev() -> Self {
60 Self { release: false }
61 }
62}
63
64#[derive(Deserialize)]
65struct CargoToml {
66 package: CargoTomlPackage,
67}
68
69#[derive(Deserialize)]
70struct CargoTomlPackage {
71 name: String,
72}
73
74impl ExtensionBuilder {
75 pub fn new(http_client: Arc<dyn HttpClient>, cache_dir: PathBuf) -> Self {
76 Self {
77 cache_dir,
78 http: http_client,
79 }
80 }
81
82 pub async fn compile_extension(
83 &self,
84 extension_dir: &Path,
85 extension_manifest: &mut ExtensionManifest,
86 options: CompileExtensionOptions,
87 fs: Arc<dyn Fs>,
88 ) -> Result<()> {
89 populate_defaults(extension_manifest, extension_dir, fs).await?;
90
91 if extension_dir.is_relative() {
92 bail!(
93 "extension dir {} is not an absolute path",
94 extension_dir.display()
95 );
96 }
97
98 fs::create_dir_all(&self.cache_dir).context("failed to create cache dir")?;
99
100 if extension_manifest.lib.kind == Some(ExtensionLibraryKind::Rust) {
101 log::info!("compiling Rust extension {}", extension_dir.display());
102 self.compile_rust_extension(extension_dir, extension_manifest, options)
103 .await
104 .context("failed to compile Rust extension")?;
105 log::info!("compiled Rust extension {}", extension_dir.display());
106 }
107
108 for (debug_adapter_name, meta) in &mut extension_manifest.debug_adapters {
109 let debug_adapter_schema_path =
110 extension_dir.join(build_debug_adapter_schema_path(debug_adapter_name, meta));
111
112 let debug_adapter_schema = fs::read_to_string(&debug_adapter_schema_path)
113 .with_context(|| {
114 format!("failed to read debug adapter schema for `{debug_adapter_name}` from `{debug_adapter_schema_path:?}`")
115 })?;
116 _ = serde_json::Value::from_str(&debug_adapter_schema).with_context(|| {
117 format!("Debug adapter schema for `{debug_adapter_name}` (path: `{debug_adapter_schema_path:?}`) is not a valid JSON")
118 })?;
119 }
120 for (grammar_name, grammar_metadata) in &extension_manifest.grammars {
121 let snake_cased_grammar_name = grammar_name.to_snake_case();
122 if grammar_name.as_ref() != snake_cased_grammar_name.as_str() {
123 bail!(
124 "grammar name '{grammar_name}' must be written in snake_case: {snake_cased_grammar_name}"
125 );
126 }
127
128 log::info!(
129 "compiling grammar {grammar_name} for extension {}",
130 extension_dir.display()
131 );
132 self.compile_grammar(extension_dir, grammar_name.as_ref(), grammar_metadata)
133 .await
134 .with_context(|| format!("failed to compile grammar '{grammar_name}'"))?;
135 log::info!(
136 "compiled grammar {grammar_name} for extension {}",
137 extension_dir.display()
138 );
139 }
140
141 log::info!("finished compiling extension {}", extension_dir.display());
142 Ok(())
143 }
144
145 async fn compile_rust_extension(
146 &self,
147 extension_dir: &Path,
148 manifest: &mut ExtensionManifest,
149 options: CompileExtensionOptions,
150 ) -> anyhow::Result<()> {
151 self.install_rust_wasm_target_if_needed().await?;
152
153 let cargo_toml_content = fs::read_to_string(extension_dir.join("Cargo.toml"))?;
154 let cargo_toml: CargoToml = toml::from_str(&cargo_toml_content)?;
155
156 log::info!(
157 "compiling Rust crate for extension {}",
158 extension_dir.display()
159 );
160 let output = util::command::new_command("cargo")
161 .args(["build", "--target", RUST_TARGET])
162 .args(options.release.then_some("--release"))
163 .arg("--target-dir")
164 .arg(extension_dir.join("target"))
165 // WASI builds do not work with sccache and just stuck, so disable it.
166 .env("RUSTC_WRAPPER", "")
167 .current_dir(extension_dir)
168 .output()
169 .await
170 .context("failed to run `cargo`")?;
171 if !output.status.success() {
172 bail!(
173 "failed to build extension {}",
174 String::from_utf8_lossy(&output.stderr)
175 );
176 }
177
178 log::info!(
179 "compiled Rust crate for extension {}",
180 extension_dir.display()
181 );
182
183 let mut wasm_path = PathBuf::from(extension_dir);
184 wasm_path.extend([
185 "target",
186 RUST_TARGET,
187 if options.release { "release" } else { "debug" },
188 &cargo_toml
189 .package
190 .name
191 // The wasm32-wasip2 target normalizes `-` in package names to `_` in the resulting `.wasm` file.
192 .replace('-', "_"),
193 ]);
194 wasm_path.set_extension("wasm");
195
196 log::info!(
197 "encoding wasm component for extension {}",
198 extension_dir.display()
199 );
200
201 let component_bytes = fs::read(&wasm_path)
202 .with_context(|| format!("failed to read output module `{}`", wasm_path.display()))?;
203
204 let component_bytes = self
205 .strip_custom_sections(&component_bytes)
206 .context("failed to strip debug sections from wasm component")?;
207
208 let wasm_extension_api_version =
209 parse_wasm_extension_version(&manifest.id, &component_bytes)
210 .context("compiled wasm did not contain a valid zed extension api version")?;
211 manifest.lib.version = Some(wasm_extension_api_version);
212
213 let extension_file = extension_dir.join("extension.wasm");
214 fs::write(extension_file.clone(), &component_bytes)
215 .context("failed to write extension.wasm")?;
216
217 log::info!(
218 "extension {} written to {}",
219 extension_dir.display(),
220 extension_file.display()
221 );
222
223 Ok(())
224 }
225
226 async fn compile_grammar(
227 &self,
228 extension_dir: &Path,
229 grammar_name: &str,
230 grammar_metadata: &GrammarManifestEntry,
231 ) -> Result<()> {
232 let clang_path = self.install_wasi_sdk_if_needed().await?;
233
234 let mut grammar_repo_dir = extension_dir.to_path_buf();
235 grammar_repo_dir.extend(["grammars", grammar_name]);
236
237 let mut grammar_wasm_path = grammar_repo_dir.clone();
238 grammar_wasm_path.set_extension("wasm");
239
240 log::info!("checking out {grammar_name} parser");
241 self.checkout_repo(
242 &grammar_repo_dir,
243 &grammar_metadata.repository,
244 &grammar_metadata.rev,
245 )
246 .await?;
247
248 let base_grammar_path = grammar_metadata
249 .path
250 .as_ref()
251 .map(|path| grammar_repo_dir.join(path))
252 .unwrap_or(grammar_repo_dir);
253
254 let src_path = base_grammar_path.join("src");
255 let parser_path = src_path.join("parser.c");
256 let scanner_path = src_path.join("scanner.c");
257
258 // Skip recompiling if the WASM object is already newer than the source files
259 if file_newer_than_deps(&grammar_wasm_path, &[&parser_path, &scanner_path]).unwrap_or(false)
260 {
261 log::info!(
262 "skipping compilation of {grammar_name} parser because the existing compiled grammar is up to date"
263 );
264 } else {
265 log::info!("compiling {grammar_name} parser");
266 let clang_output = util::command::new_command(&clang_path)
267 .args(["-fPIC", "-shared", "-Os"])
268 .arg(format!("-Wl,--export=tree_sitter_{grammar_name}"))
269 .arg("-o")
270 .arg(&grammar_wasm_path)
271 .arg("-I")
272 .arg(&src_path)
273 .arg(&parser_path)
274 .args(scanner_path.exists().then_some(scanner_path))
275 .output()
276 .await
277 .context("failed to run clang")?;
278
279 if !clang_output.status.success() {
280 bail!(
281 "failed to compile {} parser with clang: {}",
282 grammar_name,
283 String::from_utf8_lossy(&clang_output.stderr),
284 );
285 }
286 }
287
288 Ok(())
289 }
290
291 async fn checkout_repo(&self, directory: &Path, url: &str, rev: &str) -> Result<()> {
292 let git_dir = directory.join(".git");
293
294 if directory.exists() {
295 let remotes_output = util::command::new_command("git")
296 .arg("--git-dir")
297 .arg(&git_dir)
298 .args(["remote", "-v"])
299 .output()
300 .await?;
301 let has_remote = remotes_output.status.success()
302 && String::from_utf8_lossy(&remotes_output.stdout)
303 .lines()
304 .any(|line| {
305 let mut parts = line.split(|c: char| c.is_whitespace());
306 parts.next() == Some("origin") && parts.any(|part| part == url)
307 });
308 if !has_remote {
309 bail!(
310 "grammar directory '{}' already exists, but is not a git clone of '{}'",
311 directory.display(),
312 url
313 );
314 }
315 } else {
316 fs::create_dir_all(directory).with_context(|| {
317 format!("failed to create grammar directory {}", directory.display(),)
318 })?;
319 let init_output = util::command::new_command("git")
320 .arg("init")
321 .current_dir(directory)
322 .output()
323 .await?;
324 if !init_output.status.success() {
325 bail!(
326 "failed to run `git init` in directory '{}'",
327 directory.display()
328 );
329 }
330
331 let remote_add_output = util::command::new_command("git")
332 .arg("--git-dir")
333 .arg(&git_dir)
334 .args(["remote", "add", "origin", url])
335 .output()
336 .await
337 .context("failed to execute `git remote add`")?;
338 if !remote_add_output.status.success() {
339 bail!(
340 "failed to add remote {url} for git repository {}",
341 git_dir.display()
342 );
343 }
344 }
345
346 let fetch_output = util::command::new_command("git")
347 .arg("--git-dir")
348 .arg(&git_dir)
349 .args(["fetch", "--depth", "1", "origin", rev])
350 .output()
351 .await
352 .context("failed to execute `git fetch`")?;
353
354 let checkout_output = util::command::new_command("git")
355 .arg("--git-dir")
356 .arg(&git_dir)
357 .args(["checkout", rev])
358 .current_dir(directory)
359 .output()
360 .await
361 .context("failed to execute `git checkout`")?;
362 if !checkout_output.status.success() {
363 if !fetch_output.status.success() {
364 bail!(
365 "failed to fetch revision {} in directory '{}'",
366 rev,
367 directory.display()
368 );
369 }
370 bail!(
371 "failed to checkout revision {} in directory '{}': {}",
372 rev,
373 directory.display(),
374 String::from_utf8_lossy(&checkout_output.stderr)
375 );
376 }
377
378 Ok(())
379 }
380
381 async fn install_rust_wasm_target_if_needed(&self) -> Result<()> {
382 let rustc_output = util::command::new_command("rustc")
383 .arg("--print")
384 .arg("sysroot")
385 .output()
386 .await
387 .context("failed to run rustc")?;
388 if !rustc_output.status.success() {
389 bail!(
390 "failed to retrieve rust sysroot: {}",
391 String::from_utf8_lossy(&rustc_output.stderr)
392 );
393 }
394
395 let sysroot = PathBuf::from(String::from_utf8(rustc_output.stdout)?.trim());
396 if sysroot.join("lib/rustlib").join(RUST_TARGET).exists() {
397 return Ok(());
398 }
399
400 let output = util::command::new_command("rustup")
401 .args(["target", "add", RUST_TARGET])
402 .stderr(Stdio::piped())
403 .stdout(Stdio::inherit())
404 .output()
405 .await
406 .context("failed to run `rustup target add`")?;
407 if !output.status.success() {
408 bail!(
409 "failed to install the `{RUST_TARGET}` target: {}",
410 String::from_utf8_lossy(&rustc_output.stderr)
411 );
412 }
413
414 Ok(())
415 }
416
417 async fn install_wasi_sdk_if_needed(&self) -> Result<PathBuf> {
418 let url = if let Some(asset_name) = WASI_SDK_ASSET_NAME {
419 format!("{WASI_SDK_URL}{asset_name}")
420 } else {
421 bail!("wasi-sdk is not available for platform {}", env::consts::OS);
422 };
423
424 let wasi_sdk_dir = self.cache_dir.join("wasi-sdk");
425 let mut clang_path = wasi_sdk_dir.clone();
426 clang_path.extend(["bin", &format!("clang{}", env::consts::EXE_SUFFIX)]);
427
428 log::info!("downloading wasi-sdk to {}", wasi_sdk_dir.display());
429
430 if fs::metadata(&clang_path).is_ok_and(|metadata| metadata.is_file()) {
431 return Ok(clang_path);
432 }
433
434 let tar_out_dir = self.cache_dir.join("wasi-sdk-temp");
435
436 fs::remove_dir_all(&wasi_sdk_dir).ok();
437 fs::remove_dir_all(&tar_out_dir).ok();
438 fs::create_dir_all(&tar_out_dir).context("failed to create extraction directory")?;
439
440 let mut response = self.http.get(&url, AsyncBody::default(), true).await?;
441
442 // Write the response to a temporary file
443 let tar_gz_path = self.cache_dir.join("wasi-sdk.tar.gz");
444 let tar_gz_file =
445 fs::File::create(&tar_gz_path).context("failed to create temporary tar.gz file")?;
446 let response_body = response.body_mut();
447
448 let mut async_file = io::AllowStdIo::new(tar_gz_file);
449 io::copy(response_body, &mut async_file)
450 .await
451 .context("failed to stream response to file")?;
452 drop(async_file);
453
454 log::info!("un-tarring wasi-sdk to {}", tar_out_dir.display());
455
456 // Shell out to tar to extract the archive
457 let tar_output = util::command::new_command("tar")
458 .arg("-xzf")
459 .arg(&tar_gz_path)
460 .arg("-C")
461 .arg(&tar_out_dir)
462 .output()
463 .await
464 .context("failed to run tar")?;
465
466 if !tar_output.status.success() {
467 bail!(
468 "failed to extract wasi-sdk archive: {}",
469 String::from_utf8_lossy(&tar_output.stderr)
470 );
471 }
472
473 log::info!("finished downloading wasi-sdk");
474
475 // Clean up the temporary tar.gz file
476 fs::remove_file(&tar_gz_path).ok();
477
478 let inner_dir = fs::read_dir(&tar_out_dir)?
479 .next()
480 .context("no content")?
481 .context("failed to read contents of extracted wasi archive directory")?
482 .path();
483 fs::rename(&inner_dir, &wasi_sdk_dir).context("failed to move extracted wasi dir")?;
484 fs::remove_dir_all(&tar_out_dir).ok();
485
486 Ok(clang_path)
487 }
488
489 // This was adapted from:
490 // https://github.com/bytecodealliance/wasm-tools/blob/e8809bb17fcf69aa8c85cd5e6db7cff5cf36b1de/src/bin/wasm-tools/strip.rs
491 fn strip_custom_sections(&self, input: &Vec<u8>) -> Result<Vec<u8>> {
492 use wasmparser::Payload::*;
493
494 let strip_custom_section = |name: &str| {
495 // Default strip everything but:
496 // * the `name` section
497 // * any `component-type` sections
498 // * the `dylink.0` section
499 // * our custom version section
500 name != "name"
501 && !name.starts_with("component-type:")
502 && name != "dylink.0"
503 && name != "zed:api-version"
504 };
505
506 let mut output = Vec::new();
507 let mut stack = Vec::new();
508
509 for payload in Parser::new(0).parse_all(input) {
510 let payload = payload?;
511
512 // Track nesting depth, so that we don't mess with inner producer sections:
513 match payload {
514 Version { encoding, .. } => {
515 output.extend_from_slice(match encoding {
516 wasmparser::Encoding::Component => &wasm_encoder::Component::HEADER,
517 wasmparser::Encoding::Module => &wasm_encoder::Module::HEADER,
518 });
519 }
520 ModuleSection { .. } | ComponentSection { .. } => {
521 stack.push(mem::take(&mut output));
522 continue;
523 }
524 End { .. } => {
525 let mut parent = match stack.pop() {
526 Some(c) => c,
527 None => break,
528 };
529 if output.starts_with(&wasm_encoder::Component::HEADER) {
530 parent.push(ComponentSectionId::Component as u8);
531 output.encode(&mut parent);
532 } else {
533 parent.push(ComponentSectionId::CoreModule as u8);
534 output.encode(&mut parent);
535 }
536 output = parent;
537 }
538 _ => {}
539 }
540
541 if let CustomSection(c) = &payload
542 && strip_custom_section(c.name())
543 {
544 continue;
545 }
546 if let Some((id, range)) = payload.as_section() {
547 RawSection {
548 id,
549 data: &input[range],
550 }
551 .append_to(&mut output);
552 }
553 }
554
555 Ok(output)
556 }
557}
558
559async fn populate_defaults(
560 manifest: &mut ExtensionManifest,
561 extension_path: &Path,
562 fs: Arc<dyn Fs>,
563) -> Result<()> {
564 // For legacy extensions on the v0 schema (aka, using `extension.json`), clear out any existing
565 // contents of the computed fields, since we don't care what the existing values are.
566 if manifest.schema_version.is_v0() {
567 manifest.languages.clear();
568 manifest.grammars.clear();
569 manifest.themes.clear();
570 }
571
572 let cargo_toml_path = extension_path.join("Cargo.toml");
573 if cargo_toml_path.exists() {
574 manifest.lib.kind = Some(ExtensionLibraryKind::Rust);
575 }
576
577 let languages_dir = extension_path.join("languages");
578 if fs.is_dir(&languages_dir).await {
579 let mut language_dir_entries = fs
580 .read_dir(&languages_dir)
581 .await
582 .context("failed to list languages dir")?;
583
584 while let Some(language_dir) = language_dir_entries.next().await {
585 let language_dir = language_dir?;
586 let config_path = language_dir.join("config.toml");
587 if fs.is_file(config_path.as_path()).await {
588 let relative_language_dir =
589 language_dir.strip_prefix(extension_path)?.to_path_buf();
590 if !manifest.languages.contains(&relative_language_dir) {
591 manifest.languages.push(relative_language_dir);
592 }
593 }
594 }
595 }
596
597 let themes_dir = extension_path.join("themes");
598 if fs.is_dir(&themes_dir).await {
599 let mut theme_dir_entries = fs
600 .read_dir(&themes_dir)
601 .await
602 .context("failed to list themes dir")?;
603
604 while let Some(theme_path) = theme_dir_entries.next().await {
605 let theme_path = theme_path?;
606 if theme_path.extension() == Some("json".as_ref()) {
607 let relative_theme_path = theme_path.strip_prefix(extension_path)?.to_path_buf();
608 if !manifest.themes.contains(&relative_theme_path) {
609 manifest.themes.push(relative_theme_path);
610 }
611 }
612 }
613 }
614
615 let icon_themes_dir = extension_path.join("icon_themes");
616 if fs.is_dir(&icon_themes_dir).await {
617 let mut icon_theme_dir_entries = fs
618 .read_dir(&icon_themes_dir)
619 .await
620 .context("failed to list icon themes dir")?;
621
622 while let Some(icon_theme_path) = icon_theme_dir_entries.next().await {
623 let icon_theme_path = icon_theme_path?;
624 if icon_theme_path.extension() == Some("json".as_ref()) {
625 let relative_icon_theme_path =
626 icon_theme_path.strip_prefix(extension_path)?.to_path_buf();
627 if !manifest.icon_themes.contains(&relative_icon_theme_path) {
628 manifest.icon_themes.push(relative_icon_theme_path);
629 }
630 }
631 }
632 };
633 if manifest.snippets.is_none()
634 && let snippets_json_path = extension_path.join("snippets.json")
635 && fs.is_file(&snippets_json_path).await
636 {
637 manifest.snippets = Some("snippets.json".into());
638 }
639
640 // For legacy extensions on the v0 schema (aka, using `extension.json`), we want to populate the grammars in
641 // the manifest using the contents of the `grammars` directory.
642 if manifest.schema_version.is_v0() {
643 let grammars_dir = extension_path.join("grammars");
644 if fs.is_dir(&grammars_dir).await {
645 let mut grammar_dir_entries = fs
646 .read_dir(&grammars_dir)
647 .await
648 .context("failed to list grammars dir")?;
649
650 while let Some(grammar_path) = grammar_dir_entries.next().await {
651 let grammar_path = grammar_path?;
652 if grammar_path.extension() == Some("toml".as_ref()) {
653 #[derive(Deserialize)]
654 struct GrammarConfigToml {
655 pub repository: String,
656 pub commit: String,
657 #[serde(default)]
658 pub path: Option<String>,
659 }
660
661 let grammar_config = fs.load(&grammar_path).await?;
662 let grammar_config: GrammarConfigToml = toml::from_str(&grammar_config)?;
663
664 let grammar_name = grammar_path
665 .file_stem()
666 .and_then(|stem| stem.to_str())
667 .context("no grammar name")?;
668 if !manifest.grammars.contains_key(grammar_name) {
669 manifest.grammars.insert(
670 grammar_name.into(),
671 GrammarManifestEntry {
672 repository: grammar_config.repository,
673 rev: grammar_config.commit,
674 path: grammar_config.path,
675 },
676 );
677 }
678 }
679 }
680 }
681 }
682
683 Ok(())
684}
685
686/// Returns `true` if the target exists and its last modified time is greater than that
687/// of each dependency which exists (i.e., dependency paths which do not exist are ignored).
688///
689/// # Errors
690///
691/// Returns `Err` if any of the underlying file I/O operations fail.
692fn file_newer_than_deps(target: &Path, dependencies: &[&Path]) -> Result<bool, std::io::Error> {
693 if !target.try_exists()? {
694 return Ok(false);
695 }
696 let target_modified = target.metadata()?.modified()?;
697 for dependency in dependencies {
698 if !dependency.try_exists()? {
699 continue;
700 }
701 let dep_modified = dependency.metadata()?.modified()?;
702 if target_modified < dep_modified {
703 return Ok(false);
704 }
705 }
706 Ok(true)
707}
708
709#[cfg(test)]
710mod tests {
711 use std::{
712 path::{Path, PathBuf},
713 str::FromStr,
714 thread::sleep,
715 time::Duration,
716 };
717
718 use gpui::TestAppContext;
719 use indoc::indoc;
720
721 use crate::{
722 ExtensionManifest, ExtensionSnippets,
723 extension_builder::{file_newer_than_deps, populate_defaults},
724 };
725
726 #[test]
727 fn test_file_newer_than_deps() {
728 // Don't use TempTree because we need to guarantee the order
729 let tmpdir = tempfile::tempdir().unwrap();
730 let target = tmpdir.path().join("target.wasm");
731 let dep1 = tmpdir.path().join("parser.c");
732 let dep2 = tmpdir.path().join("scanner.c");
733
734 assert!(
735 !file_newer_than_deps(&target, &[&dep1, &dep2]).unwrap(),
736 "target doesn't exist"
737 );
738 std::fs::write(&target, "foo").unwrap(); // Create target
739 assert!(
740 file_newer_than_deps(&target, &[&dep1, &dep2]).unwrap(),
741 "dependencies don't exist; target is newer"
742 );
743 sleep(Duration::from_secs(1));
744 std::fs::write(&dep1, "foo").unwrap(); // Create dep1 (newer than target)
745 // Dependency is newer
746 assert!(
747 !file_newer_than_deps(&target, &[&dep1, &dep2]).unwrap(),
748 "a dependency is newer (target {:?}, dep1 {:?})",
749 target.metadata().unwrap().modified().unwrap(),
750 dep1.metadata().unwrap().modified().unwrap(),
751 );
752 sleep(Duration::from_secs(1));
753 std::fs::write(&dep2, "foo").unwrap(); // Create dep2
754 sleep(Duration::from_secs(1));
755 std::fs::write(&target, "foobar").unwrap(); // Update target
756 assert!(
757 file_newer_than_deps(&target, &[&dep1, &dep2]).unwrap(),
758 "target is newer than dependencies (target {:?}, dep2 {:?})",
759 target.metadata().unwrap().modified().unwrap(),
760 dep2.metadata().unwrap().modified().unwrap(),
761 );
762 }
763
764 #[gpui::test]
765 async fn test_snippet_location_is_kept(cx: &mut TestAppContext) {
766 let fs = fs::FakeFs::new(cx.executor());
767 let extension_path = Path::new("/extension");
768
769 fs.insert_tree(
770 extension_path,
771 serde_json::json!({
772 "extension.toml": indoc! {r#"
773 id = "test-manifest"
774 name = "Test Manifest"
775 version = "0.0.1"
776 schema_version = 1
777
778 snippets = "./snippets/snippets.json"
779 "#
780 },
781 "snippets.json": "",
782 }),
783 )
784 .await;
785
786 let mut manifest = ExtensionManifest::load(fs.clone(), extension_path)
787 .await
788 .unwrap();
789
790 populate_defaults(&mut manifest, extension_path, fs.clone())
791 .await
792 .unwrap();
793
794 assert_eq!(
795 manifest.snippets,
796 Some(ExtensionSnippets::Single(
797 PathBuf::from_str("./snippets/snippets.json").unwrap()
798 ))
799 )
800 }
801
802 #[gpui::test]
803 async fn test_automatic_snippet_location_is_relative(cx: &mut TestAppContext) {
804 let fs = fs::FakeFs::new(cx.executor());
805 let extension_path = Path::new("/extension");
806
807 fs.insert_tree(
808 extension_path,
809 serde_json::json!({
810 "extension.toml": indoc! {r#"
811 id = "test-manifest"
812 name = "Test Manifest"
813 version = "0.0.1"
814 schema_version = 1
815
816 "#
817 },
818 "snippets.json": "",
819 }),
820 )
821 .await;
822
823 let mut manifest = ExtensionManifest::load(fs.clone(), extension_path)
824 .await
825 .unwrap();
826
827 populate_defaults(&mut manifest, extension_path, fs.clone())
828 .await
829 .unwrap();
830
831 assert_eq!(
832 manifest.snippets,
833 Some(ExtensionSnippets::Single(
834 PathBuf::from_str("snippets.json").unwrap()
835 ))
836 )
837 }
838}