From f16644d24cce4f2a5cbab4a14f9f1801c4c76ec5 Mon Sep 17 00:00:00 2001 From: Xinzhao Xu Date: Thu, 1 Aug 2024 03:15:56 +0800 Subject: [PATCH] Stop generating the bindings file for projects without wit files and dependencies (#318) --- src/bindings.rs | 50 ++++++++++++++++++++++++++------------------- src/commands/new.rs | 5 +---- src/lib.rs | 7 ++++++- tests/run.rs | 3 --- 4 files changed, 36 insertions(+), 29 deletions(-) diff --git a/src/bindings.rs b/src/bindings.rs index b9b646cf..89fb78b5 100644 --- a/src/bindings.rs +++ b/src/bindings.rs @@ -73,26 +73,26 @@ impl<'a> BindingsGenerator<'a> { /// Returns a tuple of the bindings generator and a map of import names. pub fn new( resolution: &'a PackageDependencyResolution<'a>, - ) -> Result<(Self, HashMap)> { + ) -> Result)>> { let mut import_name_map = Default::default(); - let (resolve, world, source_files) = - Self::create_target_world(resolution, &mut import_name_map).with_context(|| { - format!( - "failed to create a target world for package `{name}` ({path})", - name = resolution.metadata.name, - path = resolution.metadata.manifest_path.display() - ) - })?; - - Ok(( - Self { - resolution, - resolve, - world, - source_files, - }, - import_name_map, - )) + match Self::create_target_world(resolution, &mut import_name_map).with_context(|| { + format!( + "failed to create a target world for package `{name}` ({path})", + name = resolution.metadata.name, + path = resolution.metadata.manifest_path.display() + ) + })? { + Some((resolve, world, source_files)) => Ok(Some(( + Self { + resolution, + resolve, + world, + source_files, + }, + import_name_map, + ))), + None => Ok(None), + } } /// Gets the cargo metadata for the package that the bindings are for. @@ -223,19 +223,23 @@ impl<'a> BindingsGenerator<'a> { fn create_target_world( resolution: &PackageDependencyResolution, import_name_map: &mut HashMap, - ) -> Result<(Resolve, WorldId, Vec)> { + ) -> Result)>> { log::debug!( "creating target world for package `{name}` ({path})", name = resolution.metadata.name, path = resolution.metadata.manifest_path.display() ); + // A flag used to determine whether the target is empty. It must meet two conditions: + // no wit files and no dependencies. + let mut empty_target = false; let (mut merged, world_id, source_files) = if let Some(name) = resolution.metadata.target_package() { Self::target_package(resolution, name, resolution.metadata.target_world())? } else if let Some(path) = resolution.metadata.target_path() { Self::target_local_path(resolution, &path, resolution.metadata.target_world())? } else { + empty_target = true; let (merged, world) = Self::target_empty_world(resolution); (merged, world, Vec::new()) }; @@ -243,6 +247,7 @@ impl<'a> BindingsGenerator<'a> { // Merge all component dependencies as interface imports for (id, dependency) in &resolution.resolutions { log::debug!("importing component dependency `{id}`"); + empty_target = false; let (mut resolve, component_world_id) = dependency .decode()? @@ -271,7 +276,10 @@ impl<'a> BindingsGenerator<'a> { )?; } - Ok((merged, world_id, source_files)) + if empty_target { + return Ok(None); + }; + Ok(Some((merged, world_id, source_files))) } fn target_package( diff --git a/src/commands/new.rs b/src/commands/new.rs index 0578d9ca..fa09a1b4 100644 --- a/src/commands/new.rs +++ b/src/commands/new.rs @@ -351,10 +351,7 @@ impl NewCommand { } None => { if self.is_command() { - Ok(r#"#[allow(warnings)] -mod bindings; - -fn main() { + Ok(r#"fn main() { println!("Hello, world!"); } "# diff --git a/src/lib.rs b/src/lib.rs index b558142f..000be812 100644 --- a/src/lib.rs +++ b/src/lib.rs @@ -820,6 +820,12 @@ async fn generate_package_bindings( return Ok(HashMap::new()); } + // If there is no wit files and no dependencies, stop generating the bindings file for it. + let (generator, import_name_map) = match BindingsGenerator::new(resolution)? { + Some(v) => v, + None => return Ok(HashMap::new()), + }; + // TODO: make the output path configurable let output_dir = resolution .metadata @@ -834,7 +840,6 @@ async fn generate_package_bindings( .then(|| last_modified_time(&bindings_path)) .transpose()?; - let (generator, import_name_map) = BindingsGenerator::new(resolution)?; match generator.reason(last_modified_exe, last_modified_output)? { Some(reason) => { log::debug!( diff --git a/tests/run.rs b/tests/run.rs index b5bb6c98..b4b9e692 100644 --- a/tests/run.rs +++ b/tests/run.rs @@ -14,9 +14,6 @@ fn it_runs_with_command_component() -> Result<()> { fs::write( project.root().join("src/main.rs"), r#" -#[allow(warnings)] -mod bindings; - fn main() { if std::env::args().any(|v| v == "--verbose") { println!("[guest] running component 'my:command'");