Skip to content

Commit

Permalink
fix(windows): Fix Windows System User build failures by using the cur…
Browse files Browse the repository at this point in the history
…rent directory for bundling tools (fix: 9895) (#9914)


Co-authored-by: amrbashir <[email protected]>
  • Loading branch information
AnthonyNGarcia and amrbashir authored Jun 3, 2024
1 parent 5909662 commit a301be5
Show file tree
Hide file tree
Showing 13 changed files with 176 additions and 23 deletions.
5 changes: 5 additions & 0 deletions .changes/bundler-local-tools-directory.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
---
'tauri-bundler': 'patch:feat'
---

Add `Settings::local_tools_directory`.
7 changes: 7 additions & 0 deletions .changes/cli-use-local-tools-directory.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
---
"tauri-cli": "patch:feat"
"@tauri-apps/cli": "patch:feat"
---

Use cargo's target directory to store and cache bundling tools when `bundle > useLocalToolsDir` option is active.

5 changes: 5 additions & 0 deletions .changes/utils-use-local-tools-directory.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
---
'tauri-utils': 'patch:feat'
---

Add `use_local_tools_dir` option.
7 changes: 7 additions & 0 deletions core/tauri-config-schema/schema.json
Original file line number Diff line number Diff line change
Expand Up @@ -150,6 +150,7 @@
"release": "1"
},
"targets": "all",
"useLocalToolsDir": false,
"windows": {
"allowDowngrades": true,
"certificateThumbprint": null,
Expand Down Expand Up @@ -295,6 +296,7 @@
"release": "1"
},
"targets": "all",
"useLocalToolsDir": false,
"windows": {
"allowDowngrades": true,
"certificateThumbprint": null,
Expand Down Expand Up @@ -1124,6 +1126,11 @@
"null"
]
},
"useLocalToolsDir": {
"description": "Whether to use the project's `target` directory, for caching build tools (e.g., Wix and NSIS) when building this application. Defaults to `false`.\n\nIf true, tools will be cached in `target\\.tauri-tools`. If false, tools will be cached in the current user's platform-specific cache directory.\n\nAn example where it can be appropriate to set this to `true` is when building this application as a Windows System user (e.g., AWS EC2 workloads), because the Window system's app data directory is restricted.",
"default": false,
"type": "boolean"
},
"appimage": {
"description": "Configuration for the AppImage bundle.",
"default": {
Expand Down
12 changes: 12 additions & 0 deletions core/tauri-utils/src/config.rs
Original file line number Diff line number Diff line change
Expand Up @@ -775,6 +775,15 @@ pub struct BundleConfig {
/// A longer, multi-line description of the application.
#[serde(alias = "long-description")]
pub long_description: Option<String>,
/// Whether to use the project's `target` directory, for caching build tools (e.g., Wix and NSIS) when building this application. Defaults to `false`.
///
/// If true, tools will be cached in `target\.tauri-tools`.
/// If false, tools will be cached in the current user's platform-specific cache directory.
///
/// An example where it can be appropriate to set this to `true` is when building this application as a Windows System user (e.g., AWS EC2 workloads),
/// because the Window system's app data directory is restricted.
#[serde(default, alias = "use-local-tools-dir")]
pub use_local_tools_dir: bool,
/// Configuration for the AppImage bundle.
#[serde(default)]
pub appimage: AppImageConfig,
Expand Down Expand Up @@ -3609,6 +3618,7 @@ mod build {
let category = quote!(None);
let short_description = quote!(None);
let long_description = quote!(None);
let use_local_tools_dir = self.use_local_tools_dir;
let appimage = quote!(Default::default());
let deb = quote!(Default::default());
let rpm = quote!(Default::default());
Expand All @@ -3629,6 +3639,7 @@ mod build {
category,
short_description,
long_description,
use_local_tools_dir,
appimage,
deb,
rpm,
Expand Down Expand Up @@ -4072,6 +4083,7 @@ mod test {
category: None,
short_description: None,
long_description: None,
use_local_tools_dir: false,
appimage: Default::default(),
deb: Default::default(),
rpm: Default::default(),
Expand Down
13 changes: 6 additions & 7 deletions tooling/bundler/src/bundle/linux/appimage.rs
Original file line number Diff line number Diff line change
Expand Up @@ -55,15 +55,14 @@ pub fn bundle_project(settings: &Settings) -> crate::Result<Vec<PathBuf>> {
sh_map.insert("app_name", settings.main_binary_name());
sh_map.insert("app_name_uppercase", &upcase_app_name);
sh_map.insert("appimage_filename", &appimage_filename);
let tauri_tools_path = dirs_next::cache_dir().map_or_else(
|| output_path.to_path_buf(),
|mut p| {
p.push("tauri");
p
},
);

let tauri_tools_path = settings
.local_tools_directory()
.map(|d| d.join(".tauri"))
.unwrap_or_else(|| dirs_next::cache_dir().unwrap().join("tauri"));
std::fs::create_dir_all(&tauri_tools_path)?;
let tauri_tools_path_str = tauri_tools_path.to_string_lossy();

sh_map.insert("tauri_tools_path", &tauri_tools_path_str);
let larger_icon = icons
.iter()
Expand Down
28 changes: 26 additions & 2 deletions tooling/bundler/src/bundle/settings.rs
Original file line number Diff line number Diff line change
Expand Up @@ -543,6 +543,9 @@ pub struct Settings {
package_types: Option<Vec<PackageType>>,
/// the directory where the bundles will be placed.
project_out_directory: PathBuf,
/// the directory to place tools used by the bundler,
/// if `None`, tools are placed in the current user's platform-specific cache directory.
local_tools_directory: Option<PathBuf>,
/// the bundle settings.
bundle_settings: BundleSettings,
/// the binaries to bundle.
Expand All @@ -561,6 +564,7 @@ pub struct SettingsBuilder {
bundle_settings: BundleSettings,
binaries: Vec<BundleBinary>,
target: Option<String>,
local_tools_directory: Option<PathBuf>,
}

impl SettingsBuilder {
Expand All @@ -578,6 +582,16 @@ impl SettingsBuilder {
self
}

/// Sets the directory to place tools used by the bundler
/// when [`BundleSettings::use_local_tools_dir`] is true.
#[must_use]
pub fn local_tools_directory<P: AsRef<Path>>(mut self, path: P) -> Self {
self
.local_tools_directory
.replace(path.as_ref().to_path_buf());
self
}

/// Sets the package types to create.
#[must_use]
pub fn package_types(mut self, package_types: Vec<PackageType>) -> Self {
Expand Down Expand Up @@ -634,11 +648,16 @@ impl SettingsBuilder {

Ok(Settings {
log_level: self.log_level.unwrap_or(log::Level::Error),
package: self.package_settings.expect("package settings is required"),
package: self.package_settings.ok_or(crate::Error::GenericError(
"package settings is required".into(),
))?,
package_types: self.package_types,
project_out_directory: self
.project_out_directory
.expect("out directory is required"),
.ok_or(crate::Error::GenericError(
"out directory is required".into(),
))?,
local_tools_directory: self.local_tools_directory,
binaries: self.binaries,
bundle_settings: BundleSettings {
external_bin: self
Expand Down Expand Up @@ -901,6 +920,11 @@ impl Settings {
self.bundle_settings.long_description.as_deref()
}

/// Returns the directory for local tools path.
pub fn local_tools_directory(&self) -> Option<&Path> {
self.local_tools_directory.as_deref()
}

/// Returns the debian settings.
pub fn deb(&self) -> &DebianSettings {
&self.bundle_settings.deb
Expand Down
8 changes: 6 additions & 2 deletions tooling/bundler/src/bundle/windows/msi.rs
Original file line number Diff line number Diff line change
Expand Up @@ -26,8 +26,12 @@ const WIX_REQUIRED_FILES: &[&str] = &[
/// Runs all of the commands to build the MSI installer.
/// Returns a vector of PathBuf that shows where the MSI was created.
pub fn bundle_project(settings: &Settings, updater: bool) -> crate::Result<Vec<PathBuf>> {
let mut wix_path = dirs_next::cache_dir().unwrap();
wix_path.push("tauri/WixTools314");
let tauri_tools_path = settings
.local_tools_directory()
.map(|d| d.join(".tauri"))
.unwrap_or_else(|| dirs_next::cache_dir().unwrap().join("tauri"));

let wix_path = tauri_tools_path.join("WixTools314");

if !wix_path.exists() {
wix::get_and_extract_wix(&wix_path)?;
Expand Down
6 changes: 5 additions & 1 deletion tooling/bundler/src/bundle/windows/nsis.rs
Original file line number Diff line number Diff line change
Expand Up @@ -63,7 +63,11 @@ const NSIS_REQUIRED_FILES_HASH: &[(&str, &str, &str, HashAlgorithm)] = &[(
/// Runs all of the commands to build the NSIS installer.
/// Returns a vector of PathBuf that shows where the NSIS installer was created.
pub fn bundle_project(settings: &Settings, updater: bool) -> crate::Result<Vec<PathBuf>> {
let tauri_tools_path = dirs_next::cache_dir().unwrap().join("tauri");
let tauri_tools_path = settings
.local_tools_directory()
.map(|d| d.join(".tauri"))
.unwrap_or_else(|| dirs_next::cache_dir().unwrap().join("tauri"));

let nsis_toolset_path = tauri_tools_path.join("NSIS");

if !nsis_toolset_path.exists() {
Expand Down
84 changes: 77 additions & 7 deletions tooling/cli/Cargo.lock

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

7 changes: 7 additions & 0 deletions tooling/cli/schema.json
Original file line number Diff line number Diff line change
Expand Up @@ -150,6 +150,7 @@
"release": "1"
},
"targets": "all",
"useLocalToolsDir": false,
"windows": {
"allowDowngrades": true,
"certificateThumbprint": null,
Expand Down Expand Up @@ -295,6 +296,7 @@
"release": "1"
},
"targets": "all",
"useLocalToolsDir": false,
"windows": {
"allowDowngrades": true,
"certificateThumbprint": null,
Expand Down Expand Up @@ -1124,6 +1126,11 @@
"null"
]
},
"useLocalToolsDir": {
"description": "Whether to use the project's `target` directory, for caching build tools (e.g., Wix and NSIS) when building this application. Defaults to `false`.\n\nIf true, tools will be cached in `target\\.tauri-tools`. If false, tools will be cached in the current user's platform-specific cache directory.\n\nAn example where it can be appropriate to set this to `true` is when building this application as a Windows System user (e.g., AWS EC2 workloads), because the Window system's app data directory is restricted.",
"default": false,
"type": "boolean"
},
"appimage": {
"description": "Configuration for the AppImage bundle.",
"default": {
Expand Down
Loading

0 comments on commit a301be5

Please sign in to comment.