From c1a0f4878103b3db6dbb889e5f88864207c58b4e Mon Sep 17 00:00:00 2001 From: WillLillis Date: Sun, 23 Nov 2025 01:39:56 -0500 Subject: [PATCH] fix(cli): return error if `--wasm` flag is passed when the wasm feature is disabled This applies to the `parse` and `test` commands, but not `build` as it doesn't require the wasm feature. Also, hide the `--wasm` options if from the `--help` output if the feature is disabled. --- crates/cli/src/main.rs | 44 ++++++++++++++++++++++++++++-------------- 1 file changed, 30 insertions(+), 14 deletions(-) diff --git a/crates/cli/src/main.rs b/crates/cli/src/main.rs index 3f83bf5f..e97c9cce 100644 --- a/crates/cli/src/main.rs +++ b/crates/cli/src/main.rs @@ -223,7 +223,7 @@ struct Parse { #[arg(long, short = 'D')] pub debug_graph: bool, /// Compile parsers to Wasm instead of native dynamic libraries - #[arg(long)] + #[arg(long, hide = cfg!(not(feature = "wasm")))] pub wasm: bool, /// Output the parse data with graphviz dot #[arg(long = "dot")] @@ -323,7 +323,7 @@ struct Test { #[arg(long, short = 'D')] pub debug_graph: bool, /// Compile parsers to Wasm instead of native dynamic libraries - #[arg(long)] + #[arg(long, hide = cfg!(not(feature = "wasm")))] pub wasm: bool, /// Open `log.html` in the default browser, if `--debug-graph` is supplied #[arg(long)] @@ -589,6 +589,20 @@ pub enum Shell { Nushell, } +/// Complete `action` if the wasm feature is enabled, otherwise return an error +macro_rules! checked_wasm { + ($action:block) => { + #[cfg(feature = "wasm")] + { + $action + } + #[cfg(not(feature = "wasm"))] + { + Err(anyhow!("--wasm flag specified, but this build of tree-sitter-cli does not include the wasm feature"))?; + } + }; +} + impl InitConfig { fn run() -> Result<()> { if let Ok(Some(config_path)) = Config::find_config_file() { @@ -1005,13 +1019,14 @@ impl Parse { loader.debug_build(self.debug_build); loader.force_rebuild(self.rebuild || self.grammar_path.is_some()); - #[cfg(feature = "wasm")] if self.wasm { - let engine = tree_sitter::wasmtime::Engine::default(); - parser - .set_wasm_store(tree_sitter::WasmStore::new(&engine).unwrap()) - .unwrap(); - loader.use_wasm(&engine); + checked_wasm!({ + let engine = tree_sitter::wasmtime::Engine::default(); + parser + .set_wasm_store(tree_sitter::WasmStore::new(&engine).unwrap()) + .unwrap(); + loader.use_wasm(&engine); + }); } let timeout = self.timeout.unwrap_or_default(); @@ -1215,13 +1230,14 @@ impl Test { let mut parser = Parser::new(); - #[cfg(feature = "wasm")] if self.wasm { - let engine = tree_sitter::wasmtime::Engine::default(); - parser - .set_wasm_store(tree_sitter::WasmStore::new(&engine).unwrap()) - .unwrap(); - loader.use_wasm(&engine); + checked_wasm!({ + let engine = tree_sitter::wasmtime::Engine::default(); + parser + .set_wasm_store(tree_sitter::WasmStore::new(&engine).unwrap()) + .unwrap(); + loader.use_wasm(&engine); + }); } if self.lib_path.is_none() && self.lang_name.is_some() {