Skip to content

Commit 8d630bc

Browse files
authored
fix(cli.rs): fix workspace detection, fixes #2614, closes #2515 (#2644)
1 parent d8fe9d6 commit 8d630bc

File tree

5 files changed

+55
-24
lines changed

5 files changed

+55
-24
lines changed

.changes/fix-out-dir-detection.md

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
---
2+
"cli.rs": patch
3+
---
4+
5+
Fixes output directory detection when using Cargo workspaces.

tooling/cli.rs/Cargo.lock

Lines changed: 1 addition & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

tooling/cli.rs/Cargo.toml

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -46,6 +46,7 @@ terminal_size = "0.1"
4646
unicode-width = "0.1"
4747
tempfile = "3"
4848
zeroize = "1.4"
49+
glob = "0.3"
4950

5051
[target."cfg(windows)".dependencies]
5152
winapi = { version = "0.3", features = [ "winbase", "winuser", "consoleapi", "processenv", "wincon" ] }

tooling/cli.rs/src/build.rs

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -133,7 +133,11 @@ impl Build {
133133
.get_out_dir(self.target.clone(), self.debug)
134134
.with_context(|| "failed to get project out directory")?;
135135
if let Some(product_name) = config_.package.product_name.clone() {
136-
let bin_name = app_settings.cargo_package_settings().name.clone();
136+
let bin_name = app_settings
137+
.cargo_package_settings()
138+
.name
139+
.clone()
140+
.expect("Cargo manifest must have the `package.name` field");
137141
#[cfg(windows)]
138142
let (bin_path, product_path) = {
139143
(

tooling/cli.rs/src/interface/rust.rs

Lines changed: 43 additions & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -15,7 +15,7 @@ use anyhow::Context;
1515
use heck::KebabCase;
1616
use serde::Deserialize;
1717

18-
use crate::helpers::{app_paths::tauri_dir, config::Config, manifest::Manifest};
18+
use crate::helpers::{app_paths::tauri_dir, config::Config, manifest::Manifest, Logger};
1919
use tauri_bundler::{
2020
AppCategory, BundleBinary, BundleSettings, DebianSettings, MacOsSettings, PackageSettings,
2121
UpdaterSettings, WindowsSettings, WixSettings,
@@ -38,11 +38,11 @@ struct BinarySettings {
3838
#[derive(Debug, Clone, Deserialize)]
3939
pub struct CargoPackageSettings {
4040
/// the package's name.
41-
pub name: String,
41+
pub name: Option<String>,
4242
/// the package's version.
43-
pub version: String,
43+
pub version: Option<String>,
4444
/// the package's description.
45-
pub description: String,
45+
pub description: Option<String>,
4646
/// the package's homepage.
4747
pub homepage: Option<String>,
4848
/// the package's authors.
@@ -148,17 +148,22 @@ impl AppSettings {
148148
};
149149

150150
let package_settings = PackageSettings {
151-
product_name: config
152-
.package
153-
.product_name
154-
.clone()
155-
.unwrap_or_else(|| cargo_package_settings.name.clone()),
156-
version: config
157-
.package
158-
.version
151+
product_name: config.package.product_name.clone().unwrap_or_else(|| {
152+
cargo_package_settings
153+
.name
154+
.clone()
155+
.expect("Cargo manifest must have the `package.name` field")
156+
}),
157+
version: config.package.version.clone().unwrap_or_else(|| {
158+
cargo_package_settings
159+
.version
160+
.clone()
161+
.expect("Cargo manifest must have the `package.version` field")
162+
}),
163+
description: cargo_package_settings
164+
.description
159165
.clone()
160-
.unwrap_or_else(|| cargo_package_settings.version.clone()),
161-
description: cargo_package_settings.description.clone(),
166+
.unwrap_or_default(),
162167
homepage: cargo_package_settings.homepage.clone(),
163168
authors: cargo_package_settings.authors.clone(),
164169
default_run: cargo_package_settings.default_run.clone(),
@@ -208,7 +213,7 @@ impl AppSettings {
208213
.unwrap_or_else(|| "".to_string());
209214
for binary in bin {
210215
binaries.push(
211-
if binary.name.as_str() == self.cargo_package_settings.name
216+
if Some(&binary.name) == self.cargo_package_settings.name.as_ref()
212217
|| binary.name.as_str() == default_run
213218
{
214219
BundleBinary::new(
@@ -333,18 +338,33 @@ fn get_target_dir(
333338
pub fn get_workspace_dir(current_dir: &Path) -> PathBuf {
334339
let mut dir = current_dir.to_path_buf();
335340
let project_path = dir.clone();
341+
let logger = Logger::new("tauri:rust");
336342

337343
while dir.pop() {
338-
if let Ok(cargo_settings) = CargoSettings::load(&dir) {
339-
if let Some(workspace_settings) = cargo_settings.workspace {
340-
if let Some(members) = workspace_settings.members {
341-
if members
342-
.iter()
343-
.any(|member| dir.join(member) == project_path)
344-
{
345-
return dir;
344+
if dir.join("Cargo.toml").exists() {
345+
match CargoSettings::load(&dir) {
346+
Ok(cargo_settings) => {
347+
if let Some(workspace_settings) = cargo_settings.workspace {
348+
if let Some(members) = workspace_settings.members {
349+
if members.iter().any(|member| {
350+
glob::glob(&dir.join(member).to_string_lossy().into_owned())
351+
.unwrap()
352+
.any(|p| p.unwrap() == project_path)
353+
}) {
354+
return dir;
355+
}
356+
}
346357
}
347358
}
359+
Err(e) => {
360+
logger.warn(format!(
361+
"Found `{}`, which may define a parent workspace, but \
362+
failed to parse it. If this is indeed a parent workspace, undefined behavior may occur: \
363+
\n {:#}",
364+
dir.display(),
365+
e
366+
));
367+
}
348368
}
349369
}
350370
}

0 commit comments

Comments
 (0)