Skip to content

Commit

Permalink
fix(cli.rs): fix workspace detection, fixes #2614, closes #2515 (#2644)
Browse files Browse the repository at this point in the history
  • Loading branch information
lucasfernog committed Sep 24, 2021
1 parent d8fe9d6 commit 8d630bc
Show file tree
Hide file tree
Showing 5 changed files with 55 additions and 24 deletions.
5 changes: 5 additions & 0 deletions .changes/fix-out-dir-detection.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
---
"cli.rs": patch
---

Fixes output directory detection when using Cargo workspaces.
1 change: 1 addition & 0 deletions tooling/cli.rs/Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

1 change: 1 addition & 0 deletions tooling/cli.rs/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -46,6 +46,7 @@ terminal_size = "0.1"
unicode-width = "0.1"
tempfile = "3"
zeroize = "1.4"
glob = "0.3"

[target."cfg(windows)".dependencies]
winapi = { version = "0.3", features = [ "winbase", "winuser", "consoleapi", "processenv", "wincon" ] }
Expand Down
6 changes: 5 additions & 1 deletion tooling/cli.rs/src/build.rs
Original file line number Diff line number Diff line change
Expand Up @@ -133,7 +133,11 @@ impl Build {
.get_out_dir(self.target.clone(), self.debug)
.with_context(|| "failed to get project out directory")?;
if let Some(product_name) = config_.package.product_name.clone() {
let bin_name = app_settings.cargo_package_settings().name.clone();
let bin_name = app_settings
.cargo_package_settings()
.name
.clone()
.expect("Cargo manifest must have the `package.name` field");
#[cfg(windows)]
let (bin_path, product_path) = {
(
Expand Down
66 changes: 43 additions & 23 deletions tooling/cli.rs/src/interface/rust.rs
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@ use anyhow::Context;
use heck::KebabCase;
use serde::Deserialize;

use crate::helpers::{app_paths::tauri_dir, config::Config, manifest::Manifest};
use crate::helpers::{app_paths::tauri_dir, config::Config, manifest::Manifest, Logger};
use tauri_bundler::{
AppCategory, BundleBinary, BundleSettings, DebianSettings, MacOsSettings, PackageSettings,
UpdaterSettings, WindowsSettings, WixSettings,
Expand All @@ -38,11 +38,11 @@ struct BinarySettings {
#[derive(Debug, Clone, Deserialize)]
pub struct CargoPackageSettings {
/// the package's name.
pub name: String,
pub name: Option<String>,
/// the package's version.
pub version: String,
pub version: Option<String>,
/// the package's description.
pub description: String,
pub description: Option<String>,
/// the package's homepage.
pub homepage: Option<String>,
/// the package's authors.
Expand Down Expand Up @@ -148,17 +148,22 @@ impl AppSettings {
};

let package_settings = PackageSettings {
product_name: config
.package
.product_name
.clone()
.unwrap_or_else(|| cargo_package_settings.name.clone()),
version: config
.package
.version
product_name: config.package.product_name.clone().unwrap_or_else(|| {
cargo_package_settings
.name
.clone()
.expect("Cargo manifest must have the `package.name` field")
}),
version: config.package.version.clone().unwrap_or_else(|| {
cargo_package_settings
.version
.clone()
.expect("Cargo manifest must have the `package.version` field")
}),
description: cargo_package_settings
.description
.clone()
.unwrap_or_else(|| cargo_package_settings.version.clone()),
description: cargo_package_settings.description.clone(),
.unwrap_or_default(),
homepage: cargo_package_settings.homepage.clone(),
authors: cargo_package_settings.authors.clone(),
default_run: cargo_package_settings.default_run.clone(),
Expand Down Expand Up @@ -208,7 +213,7 @@ impl AppSettings {
.unwrap_or_else(|| "".to_string());
for binary in bin {
binaries.push(
if binary.name.as_str() == self.cargo_package_settings.name
if Some(&binary.name) == self.cargo_package_settings.name.as_ref()
|| binary.name.as_str() == default_run
{
BundleBinary::new(
Expand Down Expand Up @@ -333,18 +338,33 @@ fn get_target_dir(
pub fn get_workspace_dir(current_dir: &Path) -> PathBuf {
let mut dir = current_dir.to_path_buf();
let project_path = dir.clone();
let logger = Logger::new("tauri:rust");

while dir.pop() {
if let Ok(cargo_settings) = CargoSettings::load(&dir) {
if let Some(workspace_settings) = cargo_settings.workspace {
if let Some(members) = workspace_settings.members {
if members
.iter()
.any(|member| dir.join(member) == project_path)
{
return dir;
if dir.join("Cargo.toml").exists() {
match CargoSettings::load(&dir) {
Ok(cargo_settings) => {
if let Some(workspace_settings) = cargo_settings.workspace {
if let Some(members) = workspace_settings.members {
if members.iter().any(|member| {
glob::glob(&dir.join(member).to_string_lossy().into_owned())
.unwrap()
.any(|p| p.unwrap() == project_path)
}) {
return dir;
}
}
}
}
Err(e) => {
logger.warn(format!(
"Found `{}`, which may define a parent workspace, but \
failed to parse it. If this is indeed a parent workspace, undefined behavior may occur: \
\n {:#}",
dir.display(),
e
));
}
}
}
}
Expand Down

0 comments on commit 8d630bc

Please sign in to comment.