extension_builder.rs

  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 language::LanguageConfig;
 11use serde::Deserialize;
 12use std::{
 13    env, fs, mem,
 14    path::{Path, PathBuf},
 15    str::FromStr,
 16    sync::Arc,
 17};
 18use util::{command::Stdio, rel_path::PathExt};
 19use wasm_encoder::{ComponentSectionId, Encode as _, RawSection, Section as _};
 20use wasmparser::Parser;
 21
 22/// Currently, we compile with Rust's `wasm32-wasip2` target, which works with WASI `preview2` and the component model.
 23const RUST_TARGET: &str = "wasm32-wasip2";
 24
 25/// Compiling Tree-sitter parsers from C to WASM requires Clang 17, and a WASM build of libc
 26/// and clang's runtime library. The `wasi-sdk` provides these binaries.
 27///
 28/// Once Clang 17 and its wasm target are available via system package managers, we won't need
 29/// to download this.
 30const WASI_SDK_URL: &str = "https://github.com/WebAssembly/wasi-sdk/releases/download/wasi-sdk-25/";
 31const WASI_SDK_ASSET_NAME: Option<&str> = if cfg!(all(target_os = "macos", target_arch = "x86_64"))
 32{
 33    Some("wasi-sdk-25.0-x86_64-macos.tar.gz")
 34} else if cfg!(all(target_os = "macos", target_arch = "aarch64")) {
 35    Some("wasi-sdk-25.0-arm64-macos.tar.gz")
 36} else if cfg!(all(target_os = "linux", target_arch = "x86_64")) {
 37    Some("wasi-sdk-25.0-x86_64-linux.tar.gz")
 38} else if cfg!(all(target_os = "linux", target_arch = "aarch64")) {
 39    Some("wasi-sdk-25.0-arm64-linux.tar.gz")
 40} else if cfg!(all(target_os = "freebsd", target_arch = "x86_64")) {
 41    Some("wasi-sdk-25.0-x86_64-linux.tar.gz")
 42} else if cfg!(all(target_os = "freebsd", target_arch = "aarch64")) {
 43    Some("wasi-sdk-25.0-arm64-linux.tar.gz")
 44} else if cfg!(all(target_os = "windows", target_arch = "x86_64")) {
 45    Some("wasi-sdk-25.0-x86_64-windows.tar.gz")
 46} else {
 47    None
 48};
 49
 50pub struct ExtensionBuilder {
 51    cache_dir: PathBuf,
 52    pub http: Arc<dyn HttpClient>,
 53}
 54
 55pub struct CompileExtensionOptions {
 56    pub release: bool,
 57}
 58
 59impl CompileExtensionOptions {
 60    pub const fn dev() -> Self {
 61        Self { release: false }
 62    }
 63}
 64
 65#[derive(Deserialize)]
 66struct CargoToml {
 67    package: CargoTomlPackage,
 68}
 69
 70#[derive(Deserialize)]
 71struct CargoTomlPackage {
 72    name: String,
 73}
 74
 75impl ExtensionBuilder {
 76    pub fn new(http_client: Arc<dyn HttpClient>, cache_dir: PathBuf) -> Self {
 77        Self {
 78            cache_dir,
 79            http: http_client,
 80        }
 81    }
 82
 83    pub async fn compile_extension(
 84        &self,
 85        extension_dir: &Path,
 86        extension_manifest: &mut ExtensionManifest,
 87        options: CompileExtensionOptions,
 88        fs: Arc<dyn Fs>,
 89    ) -> Result<()> {
 90        populate_defaults(extension_manifest, extension_dir, fs).await?;
 91
 92        if extension_dir.is_relative() {
 93            bail!(
 94                "extension dir {} is not an absolute path",
 95                extension_dir.display()
 96            );
 97        }
 98
 99        fs::create_dir_all(&self.cache_dir).context("failed to create cache dir")?;
100
101        if extension_manifest.lib.kind == Some(ExtensionLibraryKind::Rust) {
102            log::info!("compiling Rust extension {}", extension_dir.display());
103            self.compile_rust_extension(extension_dir, extension_manifest, options)
104                .await
105                .context("failed to compile Rust extension")?;
106            log::info!("compiled Rust extension {}", extension_dir.display());
107        }
108
109        for (debug_adapter_name, meta) in &mut extension_manifest.debug_adapters {
110            let debug_adapter_schema_path =
111                extension_dir.join(build_debug_adapter_schema_path(debug_adapter_name, meta)?);
112
113            let debug_adapter_schema = fs::read_to_string(&debug_adapter_schema_path)
114                .with_context(|| {
115                    format!("failed to read debug adapter schema for `{debug_adapter_name}` from `{debug_adapter_schema_path:?}`")
116                })?;
117            _ = serde_json::Value::from_str(&debug_adapter_schema).with_context(|| {
118                format!("Debug adapter schema for `{debug_adapter_name}` (path: `{debug_adapter_schema_path:?}`) is not a valid JSON")
119            })?;
120        }
121        for (grammar_name, grammar_metadata) in &extension_manifest.grammars {
122            let snake_cased_grammar_name = grammar_name.to_snake_case();
123            if grammar_name.as_ref() != snake_cased_grammar_name.as_str() {
124                bail!(
125                    "grammar name '{grammar_name}' must be written in snake_case: {snake_cased_grammar_name}"
126                );
127            }
128
129            log::info!(
130                "compiling grammar {grammar_name} for extension {}",
131                extension_dir.display()
132            );
133            self.compile_grammar(extension_dir, grammar_name.as_ref(), grammar_metadata)
134                .await
135                .with_context(|| format!("failed to compile grammar '{grammar_name}'"))?;
136            log::info!(
137                "compiled grammar {grammar_name} for extension {}",
138                extension_dir.display()
139            );
140        }
141
142        log::info!("finished compiling extension {}", extension_dir.display());
143        Ok(())
144    }
145
146    async fn compile_rust_extension(
147        &self,
148        extension_dir: &Path,
149        manifest: &mut ExtensionManifest,
150        options: CompileExtensionOptions,
151    ) -> anyhow::Result<()> {
152        self.install_rust_wasm_target_if_needed().await?;
153
154        let cargo_toml_content = fs::read_to_string(extension_dir.join("Cargo.toml"))?;
155        let cargo_toml: CargoToml = toml::from_str(&cargo_toml_content)?;
156
157        log::info!(
158            "compiling Rust crate for extension {}",
159            extension_dir.display()
160        );
161        let output = util::command::new_command("cargo")
162            .args(["build", "--target", RUST_TARGET])
163            .args(options.release.then_some("--release"))
164            .arg("--target-dir")
165            .arg(extension_dir.join("target"))
166            // WASI builds do not work with sccache and just stuck, so disable it.
167            .env("RUSTC_WRAPPER", "")
168            .current_dir(extension_dir)
169            .output()
170            .await
171            .context("failed to run `cargo`")?;
172        if !output.status.success() {
173            bail!(
174                "failed to build extension {}",
175                String::from_utf8_lossy(&output.stderr)
176            );
177        }
178
179        log::info!(
180            "compiled Rust crate for extension {}",
181            extension_dir.display()
182        );
183
184        let mut wasm_path = PathBuf::from(extension_dir);
185        wasm_path.extend([
186            "target",
187            RUST_TARGET,
188            if options.release { "release" } else { "debug" },
189            &cargo_toml
190                .package
191                .name
192                // The wasm32-wasip2 target normalizes `-` in package names to `_` in the resulting `.wasm` file.
193                .replace('-', "_"),
194        ]);
195        wasm_path.set_extension("wasm");
196
197        log::info!(
198            "encoding wasm component for extension {}",
199            extension_dir.display()
200        );
201
202        let component_bytes = fs::read(&wasm_path)
203            .with_context(|| format!("failed to read output module `{}`", wasm_path.display()))?;
204
205        let component_bytes = self
206            .strip_custom_sections(&component_bytes)
207            .context("failed to strip debug sections from wasm component")?;
208
209        let wasm_extension_api_version =
210            parse_wasm_extension_version(&manifest.id, &component_bytes)
211                .context("compiled wasm did not contain a valid zed extension api version")?;
212        manifest.lib.version = Some(wasm_extension_api_version);
213
214        let extension_file = extension_dir.join("extension.wasm");
215        fs::write(extension_file.clone(), &component_bytes)
216            .context("failed to write extension.wasm")?;
217
218        log::info!(
219            "extension {} written to {}",
220            extension_dir.display(),
221            extension_file.display()
222        );
223
224        Ok(())
225    }
226
227    async fn compile_grammar(
228        &self,
229        extension_dir: &Path,
230        grammar_name: &str,
231        grammar_metadata: &GrammarManifestEntry,
232    ) -> Result<()> {
233        let clang_path = self.install_wasi_sdk_if_needed().await?;
234
235        let mut grammar_repo_dir = extension_dir.to_path_buf();
236        grammar_repo_dir.extend(["grammars", grammar_name]);
237
238        let mut grammar_wasm_path = grammar_repo_dir.clone();
239        grammar_wasm_path.set_extension("wasm");
240
241        log::info!("checking out {grammar_name} parser");
242        self.checkout_repo(
243            &grammar_repo_dir,
244            &grammar_metadata.repository,
245            &grammar_metadata.rev,
246        )
247        .await?;
248
249        let base_grammar_path = grammar_metadata
250            .path
251            .as_ref()
252            .map(|path| grammar_repo_dir.join(path))
253            .unwrap_or(grammar_repo_dir);
254
255        let src_path = base_grammar_path.join("src");
256        let parser_path = src_path.join("parser.c");
257        let scanner_path = src_path.join("scanner.c");
258
259        // Skip recompiling if the WASM object is already newer than the source files
260        if file_newer_than_deps(&grammar_wasm_path, &[&parser_path, &scanner_path]).unwrap_or(false)
261        {
262            log::info!(
263                "skipping compilation of {grammar_name} parser because the existing compiled grammar is up to date"
264            );
265        } else {
266            log::info!("compiling {grammar_name} parser");
267            let clang_output = util::command::new_command(&clang_path)
268                .args(["-fPIC", "-shared", "-Os"])
269                .arg(format!("-Wl,--export=tree_sitter_{grammar_name}"))
270                .arg("-o")
271                .arg(&grammar_wasm_path)
272                .arg("-I")
273                .arg(&src_path)
274                .arg(&parser_path)
275                .args(scanner_path.exists().then_some(scanner_path))
276                .output()
277                .await
278                .context("failed to run clang")?;
279
280            if !clang_output.status.success() {
281                bail!(
282                    "failed to compile {} parser with clang: {}",
283                    grammar_name,
284                    String::from_utf8_lossy(&clang_output.stderr),
285                );
286            }
287        }
288
289        Ok(())
290    }
291
292    async fn checkout_repo(&self, directory: &Path, url: &str, rev: &str) -> Result<()> {
293        let git_dir = directory.join(".git");
294
295        if directory.exists() {
296            let remotes_output = util::command::new_command("git")
297                .arg("--git-dir")
298                .arg(&git_dir)
299                .args(["remote", "get-url", "origin"])
300                .env("GIT_CONFIG_GLOBAL", "/dev/null")
301                .output()
302                .await?;
303            let has_remote = remotes_output.status.success()
304                && String::from_utf8_lossy(&remotes_output.stdout).trim() == url;
305            if !has_remote {
306                bail!(
307                    "grammar directory '{}' already exists, but is not a git clone of '{}'",
308                    directory.display(),
309                    url
310                );
311            }
312        } else {
313            fs::create_dir_all(directory).with_context(|| {
314                format!("failed to create grammar directory {}", directory.display(),)
315            })?;
316            let init_output = util::command::new_command("git")
317                .arg("init")
318                .current_dir(directory)
319                .output()
320                .await?;
321            if !init_output.status.success() {
322                bail!(
323                    "failed to run `git init` in directory '{}'",
324                    directory.display()
325                );
326            }
327
328            let remote_add_output = util::command::new_command("git")
329                .arg("--git-dir")
330                .arg(&git_dir)
331                .args(["remote", "add", "origin", url])
332                .output()
333                .await
334                .context("failed to execute `git remote add`")?;
335            if !remote_add_output.status.success() {
336                bail!(
337                    "failed to add remote {url} for git repository {}",
338                    git_dir.display()
339                );
340            }
341        }
342
343        let fetch_output = util::command::new_command("git")
344            .arg("--git-dir")
345            .arg(&git_dir)
346            .args(["fetch", "--depth", "1", "origin", rev])
347            .output()
348            .await
349            .context("failed to execute `git fetch`")?;
350
351        let checkout_output = util::command::new_command("git")
352            .arg("--git-dir")
353            .arg(&git_dir)
354            .args(["checkout", rev])
355            .current_dir(directory)
356            .output()
357            .await
358            .context("failed to execute `git checkout`")?;
359        if !checkout_output.status.success() {
360            if !fetch_output.status.success() {
361                bail!(
362                    "failed to fetch revision {} in directory '{}'",
363                    rev,
364                    directory.display()
365                );
366            }
367            bail!(
368                "failed to checkout revision {} in directory '{}': {}",
369                rev,
370                directory.display(),
371                String::from_utf8_lossy(&checkout_output.stderr)
372            );
373        }
374
375        Ok(())
376    }
377
378    async fn install_rust_wasm_target_if_needed(&self) -> Result<()> {
379        let rustc_output = util::command::new_command("rustc")
380            .arg("--print")
381            .arg("sysroot")
382            .output()
383            .await
384            .context("failed to run rustc")?;
385        if !rustc_output.status.success() {
386            bail!(
387                "failed to retrieve rust sysroot: {}",
388                String::from_utf8_lossy(&rustc_output.stderr)
389            );
390        }
391
392        let sysroot = PathBuf::from(String::from_utf8(rustc_output.stdout)?.trim());
393        if sysroot.join("lib/rustlib").join(RUST_TARGET).exists() {
394            return Ok(());
395        }
396
397        let output = util::command::new_command("rustup")
398            .args(["target", "add", RUST_TARGET])
399            .stderr(Stdio::piped())
400            .stdout(Stdio::inherit())
401            .output()
402            .await
403            .context("failed to run `rustup target add`")?;
404        if !output.status.success() {
405            bail!(
406                "failed to install the `{RUST_TARGET}` target: {}",
407                String::from_utf8_lossy(&rustc_output.stderr)
408            );
409        }
410
411        Ok(())
412    }
413
414    async fn install_wasi_sdk_if_needed(&self) -> Result<PathBuf> {
415        let url = if let Some(asset_name) = WASI_SDK_ASSET_NAME {
416            format!("{WASI_SDK_URL}{asset_name}")
417        } else {
418            bail!("wasi-sdk is not available for platform {}", env::consts::OS);
419        };
420
421        let wasi_sdk_dir = self.cache_dir.join("wasi-sdk");
422        let mut clang_path = wasi_sdk_dir.clone();
423        clang_path.extend(["bin", &format!("clang{}", env::consts::EXE_SUFFIX)]);
424
425        log::info!("downloading wasi-sdk to {}", wasi_sdk_dir.display());
426
427        if fs::metadata(&clang_path).is_ok_and(|metadata| metadata.is_file()) {
428            return Ok(clang_path);
429        }
430
431        let tar_out_dir = self.cache_dir.join("wasi-sdk-temp");
432
433        fs::remove_dir_all(&wasi_sdk_dir).ok();
434        fs::remove_dir_all(&tar_out_dir).ok();
435        fs::create_dir_all(&tar_out_dir).context("failed to create extraction directory")?;
436
437        let mut response = self.http.get(&url, AsyncBody::default(), true).await?;
438
439        // Write the response to a temporary file
440        let tar_gz_path = self.cache_dir.join("wasi-sdk.tar.gz");
441        let tar_gz_file =
442            fs::File::create(&tar_gz_path).context("failed to create temporary tar.gz file")?;
443        let response_body = response.body_mut();
444
445        let mut async_file = io::AllowStdIo::new(tar_gz_file);
446        io::copy(response_body, &mut async_file)
447            .await
448            .context("failed to stream response to file")?;
449        drop(async_file);
450
451        log::info!("un-tarring wasi-sdk to {}", tar_out_dir.display());
452
453        // Shell out to tar to extract the archive
454        let tar_output = util::command::new_command("tar")
455            .arg("-xzf")
456            .arg(&tar_gz_path)
457            .arg("-C")
458            .arg(&tar_out_dir)
459            .output()
460            .await
461            .context("failed to run tar")?;
462
463        if !tar_output.status.success() {
464            bail!(
465                "failed to extract wasi-sdk archive: {}",
466                String::from_utf8_lossy(&tar_output.stderr)
467            );
468        }
469
470        log::info!("finished downloading wasi-sdk");
471
472        // Clean up the temporary tar.gz file
473        fs::remove_file(&tar_gz_path).ok();
474
475        let inner_dir = fs::read_dir(&tar_out_dir)?
476            .next()
477            .context("no content")?
478            .context("failed to read contents of extracted wasi archive directory")?
479            .path();
480        fs::rename(&inner_dir, &wasi_sdk_dir).context("failed to move extracted wasi dir")?;
481        fs::remove_dir_all(&tar_out_dir).ok();
482
483        Ok(clang_path)
484    }
485
486    // This was adapted from:
487    // https://github.com/bytecodealliance/wasm-tools/blob/e8809bb17fcf69aa8c85cd5e6db7cff5cf36b1de/src/bin/wasm-tools/strip.rs
488    fn strip_custom_sections(&self, input: &Vec<u8>) -> Result<Vec<u8>> {
489        use wasmparser::Payload::*;
490
491        let strip_custom_section = |name: &str| {
492            // Default strip everything but:
493            // * the `name` section
494            // * any `component-type` sections
495            // * the `dylink.0` section
496            // * our custom version section
497            name != "name"
498                && !name.starts_with("component-type:")
499                && name != "dylink.0"
500                && name != "zed:api-version"
501        };
502
503        let mut output = Vec::new();
504        let mut stack = Vec::new();
505
506        for payload in Parser::new(0).parse_all(input) {
507            let payload = payload?;
508
509            // Track nesting depth, so that we don't mess with inner producer sections:
510            match payload {
511                Version { encoding, .. } => {
512                    output.extend_from_slice(match encoding {
513                        wasmparser::Encoding::Component => &wasm_encoder::Component::HEADER,
514                        wasmparser::Encoding::Module => &wasm_encoder::Module::HEADER,
515                    });
516                }
517                ModuleSection { .. } | ComponentSection { .. } => {
518                    stack.push(mem::take(&mut output));
519                    continue;
520                }
521                End { .. } => {
522                    let mut parent = match stack.pop() {
523                        Some(c) => c,
524                        None => break,
525                    };
526                    if output.starts_with(&wasm_encoder::Component::HEADER) {
527                        parent.push(ComponentSectionId::Component as u8);
528                        output.encode(&mut parent);
529                    } else {
530                        parent.push(ComponentSectionId::CoreModule as u8);
531                        output.encode(&mut parent);
532                    }
533                    output = parent;
534                }
535                _ => {}
536            }
537
538            if let CustomSection(c) = &payload
539                && strip_custom_section(c.name())
540            {
541                continue;
542            }
543            if let Some((id, range)) = payload.as_section() {
544                RawSection {
545                    id,
546                    data: &input[range],
547                }
548                .append_to(&mut output);
549            }
550        }
551
552        Ok(output)
553    }
554}
555
556async fn populate_defaults(
557    manifest: &mut ExtensionManifest,
558    extension_path: &Path,
559    fs: Arc<dyn Fs>,
560) -> Result<()> {
561    // For legacy extensions on the v0 schema (aka, using `extension.json`), clear out any existing
562    // contents of the computed fields, since we don't care what the existing values are.
563    if manifest.schema_version.is_v0() {
564        manifest.languages.clear();
565        manifest.grammars.clear();
566        manifest.themes.clear();
567    }
568
569    let cargo_toml_path = extension_path.join("Cargo.toml");
570    if cargo_toml_path.exists() {
571        manifest.lib.kind = Some(ExtensionLibraryKind::Rust);
572    }
573
574    let languages_dir = extension_path.join("languages");
575    if fs.is_dir(&languages_dir).await {
576        let mut language_dir_entries = fs
577            .read_dir(&languages_dir)
578            .await
579            .context("failed to list languages dir")?;
580
581        while let Some(language_dir) = language_dir_entries.next().await {
582            let language_dir = language_dir?;
583            let config_path = language_dir.join(LanguageConfig::FILE_NAME);
584            if fs.is_file(config_path.as_path()).await {
585                let relative_language_dir = language_dir
586                    .strip_prefix(extension_path)?
587                    .to_rel_path_buf()?;
588                if !manifest.languages.contains(&relative_language_dir) {
589                    manifest.languages.push(relative_language_dir);
590                }
591            }
592        }
593    }
594
595    let themes_dir = extension_path.join("themes");
596    if fs.is_dir(&themes_dir).await {
597        let mut theme_dir_entries = fs
598            .read_dir(&themes_dir)
599            .await
600            .context("failed to list themes dir")?;
601
602        while let Some(theme_path) = theme_dir_entries.next().await {
603            let theme_path = theme_path?;
604            if theme_path.extension() == Some("json".as_ref()) {
605                let relative_theme_path =
606                    theme_path.strip_prefix(extension_path)?.to_rel_path_buf()?;
607                if !manifest.themes.contains(&relative_theme_path) {
608                    manifest.themes.push(relative_theme_path);
609                }
610            }
611        }
612    }
613
614    let icon_themes_dir = extension_path.join("icon_themes");
615    if fs.is_dir(&icon_themes_dir).await {
616        let mut icon_theme_dir_entries = fs
617            .read_dir(&icon_themes_dir)
618            .await
619            .context("failed to list icon themes dir")?;
620
621        while let Some(icon_theme_path) = icon_theme_dir_entries.next().await {
622            let icon_theme_path = icon_theme_path?;
623            if icon_theme_path.extension() == Some("json".as_ref()) {
624                let relative_icon_theme_path = icon_theme_path
625                    .strip_prefix(extension_path)?
626                    .to_rel_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}