Skip to content

Commit fca4a31

Browse files
Fix aliased plugin imports in v1 migration (#15454)
1 parent 469a689 commit fca4a31

2 files changed

Lines changed: 49 additions & 7 deletions

File tree

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,8 @@
1+
---
2+
"tauri-cli": "patch:bug"
3+
"@tauri-apps/cli": "patch:bug"
4+
---
5+
6+
Fix `tauri migrate` generating invalid namespace imports for aliased pluginified imports from `@tauri-apps/api`.
7+
8+
Inputs like `import { cli as superCli } from "@tauri-apps/api"` now migrate to `import * as superCli from "@tauri-apps/plugin-cli"` instead of producing invalid ESM syntax. The migration tests also reparse migrated JS, Svelte, and Vue output so syntax regressions are caught directly.

crates/tauri-cli/src/migrate/migrations/v1/frontend.rs

Lines changed: 41 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -245,7 +245,7 @@ fn migrate_imports<'a>(
245245
// to:
246246
// ```
247247
// import * as dialog from "@tauri-apps/plugin-dialog"
248-
// import * as cli as superCli from "@tauri-apps/plugin-cli"
248+
// import * as superCli from "@tauri-apps/plugin-cli"
249249
// ```
250250
import if PLUGINIFIED_MODULES.contains(&import) && module == "@tauri-apps/api" => {
251251
let js_plugin: &str = MODULES_MAP[&format!("@tauri-apps/api/{import}")];
@@ -255,9 +255,7 @@ fn migrate_imports<'a>(
255255

256256
if specifier.local.name.as_str() != import {
257257
let local = &specifier.local.name;
258-
imports_to_add.push(format!(
259-
"\nimport * as {import} as {local} from \"{js_plugin}\""
260-
));
258+
imports_to_add.push(format!("\nimport * as {local} from \"{js_plugin}\""));
261259
} else {
262260
imports_to_add.push(format!("\nimport * as {import} from \"{js_plugin}\""));
263261
};
@@ -359,6 +357,39 @@ mod tests {
359357
use super::*;
360358
use pretty_assertions::assert_eq;
361359

360+
fn assert_migrated_output_parses(path: &Path, source: &str) {
361+
let has_partial_js = path
362+
.extension()
363+
.is_some_and(|ext| ext == "vue" || ext == "svelte");
364+
365+
let sources = if !has_partial_js {
366+
vec![(SourceType::from_path(path).unwrap(), source.to_string())]
367+
} else {
368+
partial_loader::PartialLoader::parse(
369+
path
370+
.extension()
371+
.unwrap_or_default()
372+
.to_str()
373+
.unwrap_or_default(),
374+
source,
375+
)
376+
.unwrap()
377+
.into_iter()
378+
.map(|s| (s.source_type, s.source_text.to_string()))
379+
.collect()
380+
};
381+
382+
for (source_type, script_source) in sources {
383+
let allocator = Allocator::default();
384+
let ret = Parser::new(&allocator, &script_source, source_type).parse();
385+
assert!(
386+
ret.errors.is_empty(),
387+
"migrated output did not parse: {:?}",
388+
ret.errors
389+
);
390+
}
391+
}
392+
362393
#[test]
363394
fn migrates_vue() {
364395
let input = r#"
@@ -404,7 +435,7 @@ mod tests {
404435
import * as fs from "@tauri-apps/plugin-fs";
405436
import "./App.css";
406437
import * as dialog from "@tauri-apps/plugin-dialog"
407-
import * as cli as superCli from "@tauri-apps/plugin-cli"
438+
import * as superCli from "@tauri-apps/plugin-cli"
408439
const appWindow = getCurrentWebviewWindow()
409440
</script>
410441
@@ -428,6 +459,7 @@ const appWindow = getCurrentWebviewWindow()
428459
.unwrap();
429460

430461
assert_eq!(migrated, expected);
462+
assert_migrated_output_parses(Path::new("file.vue"), &migrated);
431463

432464
assert_eq!(
433465
new_plugins,
@@ -479,7 +511,7 @@ const appWindow = getCurrentWebviewWindow()
479511
import * as fs from "@tauri-apps/plugin-fs";
480512
import "./App.css";
481513
import * as dialog from "@tauri-apps/plugin-dialog"
482-
import * as cli as superCli from "@tauri-apps/plugin-cli"
514+
import * as superCli from "@tauri-apps/plugin-cli"
483515
const appWindow = getCurrentWebviewWindow()
484516
</script>
485517
"#;
@@ -496,6 +528,7 @@ const appWindow = getCurrentWebviewWindow()
496528
.unwrap();
497529

498530
assert_eq!(migrated, expected);
531+
assert_migrated_output_parses(Path::new("file.svelte"), &migrated);
499532

500533
assert_eq!(
501534
new_plugins,
@@ -598,7 +631,7 @@ import { Store } from "@tauri-apps/plugin-store";
598631
import Database from "@tauri-apps/plugin-sql";
599632
import "./App.css";
600633
import * as dialog from "@tauri-apps/plugin-dialog"
601-
import * as cli as superCli from "@tauri-apps/plugin-cli"
634+
import * as superCli from "@tauri-apps/plugin-cli"
602635
const appWindow = getCurrentWebviewWindow()
603636
604637
function App() {
@@ -670,6 +703,7 @@ export default App;
670703
.unwrap();
671704

672705
assert_eq!(migrated, expected);
706+
assert_migrated_output_parses(Path::new("file.js"), &migrated);
673707

674708
assert_eq!(
675709
new_plugins,

0 commit comments

Comments
 (0)