From eb76df4c4fce173dd998f8734423513e882fa82e Mon Sep 17 00:00:00 2001 From: Amr Bashir Date: Wed, 26 Jun 2024 17:11:31 +0300 Subject: [PATCH] fix(cli/plugin/new): construct path from components iterator instead of `PathBuf::join` (#10128) fixes #10082 The problem that we were calling `PathBuf::join()` with value of collecting an empty iterator of path components which was equivalent to `PathBuf::from("ios").join("")` which will result in `ios/` with a trailing slash. This is fixed by chaining iterators of path components and collecting only once into `PathBuf`, which will never append empy path component and will never append trailing slash. ```rs [ Component::Normal(OsStr::new("ios")), Component::Normal(&some_folder_name), ] .into_iter() .chain(other_components_iterator) .collect::() ``` Co-authored-by: Lucas Nogueira --- tooling/cli/src/plugin/init.rs | 17 +++++++++++------ 1 file changed, 11 insertions(+), 6 deletions(-) diff --git a/tooling/cli/src/plugin/init.rs b/tooling/cli/src/plugin/init.rs index f5dd40b45..ffa3a7dd8 100644 --- a/tooling/cli/src/plugin/init.rs +++ b/tooling/cli/src/plugin/init.rs @@ -13,6 +13,7 @@ use clap::{Parser, ValueEnum}; use handlebars::{to_json, Handlebars}; use heck::{ToKebabCase, ToPascalCase, ToSnakeCase}; use include_dir::{include_dir, Dir}; +use std::ffi::{OsStr, OsString}; use std::{ collections::BTreeMap, env::current_dir, @@ -211,12 +212,16 @@ pub fn command(mut options: Options) -> Result<()> { "ios-xcode" if !matches!(ios_framework, IosFrameworkKind::Xcode) => return Ok(None), "ios-spm" | "ios-xcode" => { let folder_name = components.next().unwrap().as_os_str().to_string_lossy(); + let new_folder_name = folder_name.replace("{{ plugin_name }}", &plugin_name); + let new_folder_name = OsString::from(&new_folder_name); - path = Path::new("ios") - .join(Component::Normal(&std::ffi::OsString::from( - &folder_name.replace("{{ plugin_name }}", &plugin_name), - ))) - .join(components.collect::()); + path = [ + Component::Normal(OsStr::new("ios")), + Component::Normal(&new_folder_name), + ] + .into_iter() + .chain(components) + .collect::(); } "guest-js" | "rollup.config.js" | "tsconfig.json" | "package.json" if options.no_api => @@ -236,7 +241,7 @@ pub fn command(mut options: Options) -> Result<()> { File::create(path).map(Some) }, ) - .with_context(|| "failed to render plugin Android template")?; + .with_context(|| "failed to render plugin template")?; } let permissions_dir = template_target_path.join("permissions");