Skip to content

Commit 98918df

Browse files
authored
fix(cli): do not auto-install npm packages for community plugins (#16112)
1 parent 6507d0b commit 98918df

3 files changed

Lines changed: 74 additions & 10 deletions

File tree

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,6 @@
1+
---
2+
"tauri-cli": patch:sec
3+
"@tauri-apps/cli": patch:sec
4+
---
5+
6+
`tauri add` no longer installs the npm package `tauri-plugin-<name>-api` for community plugins. npm and crates.io are separate namespaces, so that package may be unowned or squatted; the CLI now asks you to install the plugin's JavaScript bindings yourself. `tauri remove` only removes the plugin's JS package when `package.json` lists it, and a failure there no longer skips the capability cleanup.

‎crates/tauri-cli/src/add.rs‎

Lines changed: 8 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -59,11 +59,7 @@ pub fn run(options: Options, dirs: &Dirs) -> Result<()> {
5959

6060
let plugin_snake_case = plugin.replace('-', "_");
6161
let crate_name = format!("tauri-plugin-{plugin}");
62-
let npm_name = if is_known {
63-
format!("@tauri-apps/plugin-{plugin}")
64-
} else {
65-
format!("tauri-plugin-{plugin}-api")
66-
};
62+
let npm_name = format!("@tauri-apps/plugin-{plugin}");
6763

6864
if !is_known && (options.tag.is_some() || options.rev.is_some() || options.branch.is_some()) {
6965
crate::error::bail!(
@@ -95,7 +91,13 @@ pub fn run(options: Options, dirs: &Dirs) -> Result<()> {
9591
})?;
9692

9793
if !metadata.rust_only {
98-
if let Some(manager) = frontend_dir.map(PackageManager::from_project) {
94+
// npm and crates.io are separate namespaces, so a community plugin's crate name says nothing
95+
// about who owns a similarly named npm package: only install the JS bindings of known plugins
96+
if !is_known {
97+
log::info!(
98+
"If `{crate_name}` has JavaScript bindings, install them with your package manager, see the plugin's documentation for the package name."
99+
);
100+
} else if let Some(manager) = frontend_dir.map(PackageManager::from_project) {
99101
let npm_version_req = version.map(ToString::to_string).or_else(|| {
100102
metadata.version_req.as_ref().map(|v| match manager {
101103
PackageManager::Npm => format!(">={v}"),

‎crates/tauri-cli/src/remove.rs‎

Lines changed: 60 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -23,7 +23,10 @@ pub fn command(options: Options) -> Result<()> {
2323
let crate_name = format!("tauri-plugin-{plugin}");
2424

2525
let mut plugins = crate::helpers::plugins::known_plugins();
26-
let metadata = plugins.remove(plugin.as_str()).unwrap_or_default();
26+
let (metadata, is_known) = plugins
27+
.remove(plugin.as_str())
28+
.map(|metadata| (metadata, true))
29+
.unwrap_or_default();
2730

2831
let frontend_dir = resolve_frontend_dir();
2932

@@ -43,9 +46,27 @@ pub fn command(options: Options) -> Result<()> {
4346
})?;
4447

4548
if !metadata.rust_only {
46-
if let Some(manager) = frontend_dir.map(PackageManager::from_project) {
47-
let npm_name = format!("@tauri-apps/plugin-{plugin}");
48-
manager.remove(&[npm_name], dirs.frontend)?;
49+
// `tauri add` only installs the JS bindings of known plugins; for community plugins, only
50+
// remove the package older CLI versions installed, and only if the project actually uses it
51+
let npm_name = if is_known {
52+
format!("@tauri-apps/plugin-{plugin}")
53+
} else {
54+
format!("tauri-plugin-{plugin}-api")
55+
};
56+
if let Some(frontend_dir) = frontend_dir {
57+
let package_json = std::fs::read(dirs.frontend.join("package.json"))
58+
.ok()
59+
.and_then(|content| serde_json::from_slice::<serde_json::Value>(&content).ok());
60+
if package_json
61+
.as_ref()
62+
.is_some_and(|package_json| lists_dependency(package_json, &npm_name))
63+
{
64+
let manager = PackageManager::from_project(frontend_dir);
65+
// not fatal: the ACL cleanup below must still run
66+
if let Err(e) = manager.remove(std::slice::from_ref(&npm_name), dirs.frontend) {
67+
log::warn!("Failed to remove `{npm_name}`, remove it manually: {e}");
68+
}
69+
}
4970
}
5071

5172
acl::permission::rm::command(acl::permission::rm::Options {
@@ -57,3 +78,38 @@ pub fn command(options: Options) -> Result<()> {
5778

5879
Ok(())
5980
}
81+
82+
/// Whether the given `package.json` lists `name` as any kind of dependency.
83+
fn lists_dependency(package_json: &serde_json::Value, name: &str) -> bool {
84+
[
85+
"dependencies",
86+
"devDependencies",
87+
"optionalDependencies",
88+
"peerDependencies",
89+
]
90+
.iter()
91+
.any(|key| {
92+
package_json
93+
.get(key)
94+
.and_then(|deps| deps.get(name))
95+
.is_some()
96+
})
97+
}
98+
99+
#[cfg(test)]
100+
mod tests {
101+
use super::lists_dependency;
102+
use serde_json::json;
103+
104+
#[test]
105+
fn finds_dependency_in_any_section() {
106+
let package_json = json!({
107+
"dependencies": { "@tauri-apps/plugin-fs": "~2" },
108+
"devDependencies": { "tauri-plugin-foo-api": "^1" },
109+
});
110+
assert!(lists_dependency(&package_json, "@tauri-apps/plugin-fs"));
111+
assert!(lists_dependency(&package_json, "tauri-plugin-foo-api"));
112+
assert!(!lists_dependency(&package_json, "@tauri-apps/plugin-os"));
113+
assert!(!lists_dependency(&json!({}), "@tauri-apps/plugin-fs"));
114+
}
115+
}

0 commit comments

Comments
 (0)