Skip to content

Commit 33b3ea5

Browse files
committed
fix(bundler): write vswhere to a uniquely named temp file
1 parent 231df3a commit 33b3ea5

5 files changed

Lines changed: 33 additions & 24 deletions

File tree

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
---
2+
"tauri-bundler": "patch:bug"
3+
"tauri-cli": "patch:bug"
4+
"@tauri-apps/cli": "patch:bug"
5+
---
6+
7+
The bundled `vswhere.exe` is now written to a new, uniquely named temporary file on each use and removed afterwards, instead of running any existing `%TEMP%\vswhere.exe`.

‎crates/tauri-bundler/src/bundle.rs‎

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -32,9 +32,11 @@ pub use {
3232
PackageType, PlistKind, Position, RpmSettings, Settings, SettingsBuilder, Size,
3333
UpdaterSettings, WindowsSettings, WixLanguage, WixLanguageConfig, WixSettings,
3434
},
35-
windows::vswhere_path,
3635
};
3736

37+
#[cfg(windows)]
38+
pub use windows::vswhere_path;
39+
3840
const BUNDLE_VAR_TOKEN: &[u8] = b"__TAURI_BUNDLE_TYPE_VAR_UNK";
3941
/// Patch a binary with bundle type information
4042
fn patch_binary(binary: &PathBuf, package_type: &PackageType) -> crate::Result<()> {

‎crates/tauri-bundler/src/bundle/windows/mod.rs‎

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,9 @@ pub mod nsis;
1010
pub mod sign;
1111

1212
mod util;
13+
#[cfg(windows)]
14+
pub use util::vswhere_path;
1315
pub use util::{
1416
NSIS_OUTPUT_FOLDER_NAME, NSIS_UPDATER_OUTPUT_FOLDER_NAME, WIX_OUTPUT_FOLDER_NAME,
15-
WIX_UPDATER_OUTPUT_FOLDER_NAME, vswhere_path,
17+
WIX_UPDATER_OUTPUT_FOLDER_NAME,
1618
};

‎crates/tauri-bundler/src/bundle/windows/util.rs‎

Lines changed: 18 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -2,11 +2,12 @@
22
// SPDX-License-Identifier: Apache-2.0
33
// SPDX-License-Identifier: MIT
44

5+
#[cfg(windows)]
6+
use std::io::Write;
57
#[cfg(windows)]
68
use std::process::Command;
79
use std::{
810
fs,
9-
io::Write,
1011
path::{Path, PathBuf},
1112
};
1213
use ureq::ResponseExt;
@@ -26,6 +27,7 @@ pub const NSIS_UPDATER_OUTPUT_FOLDER_NAME: &str = "nsis-updater";
2627
pub const WIX_OUTPUT_FOLDER_NAME: &str = "msi";
2728
pub const WIX_UPDATER_OUTPUT_FOLDER_NAME: &str = "msi-updater";
2829

30+
#[cfg(windows)]
2931
const VSWHERE: &[u8] = include_bytes!("vswhere.exe");
3032
const VCTOOLS_REDIST_DIR_ENV_VAR: &str = "VCTOOLS_REDIST_DIR";
3133
#[cfg(windows)]
@@ -104,13 +106,9 @@ fn vc_runtime_arch(arch: Arch) -> crate::Result<&'static str> {
104106

105107
#[cfg(windows)]
106108
fn visual_studio_dir() -> crate::Result<PathBuf> {
107-
let Some(vswhere) = vswhere_path() else {
108-
return Err(crate::Error::GenericError(
109-
"failed to prepare bundled vswhere.exe".into(),
110-
));
111-
};
109+
let vswhere = vswhere_path()?;
112110

113-
let output = Command::new(vswhere)
111+
let output = Command::new(&*vswhere)
114112
.args([
115113
"-latest",
116114
"-prerelease",
@@ -212,20 +210,20 @@ fn glob_path(path: &Path, pattern: &str) -> String {
212210
.into_owned()
213211
}
214212

215-
/// Returns the bundled `vswhere.exe` path.
213+
/// Writes the bundled `vswhere.exe` to a new temporary file and returns its path.
216214
///
217-
/// The executable is written to a temporary file so callers do not depend on a system-installed
218-
/// `vswhere.exe`.
219-
pub fn vswhere_path() -> Option<PathBuf> {
220-
let mut vswhere = std::env::temp_dir();
221-
vswhere.push("vswhere.exe");
222-
223-
if !vswhere.exists() {
224-
let mut file = std::fs::File::create(&vswhere).ok()?;
225-
file.write_all(VSWHERE).ok()?;
226-
}
227-
228-
Some(vswhere)
215+
/// Each call creates a uniquely named file, so a pre-existing or half-written `vswhere.exe` in the
216+
/// temp directory is never executed. The file is deleted when the returned path is dropped.
217+
#[cfg(windows)]
218+
pub fn vswhere_path() -> crate::Result<tempfile::TempPath> {
219+
let mut file = tempfile::Builder::new()
220+
.prefix("vswhere-")
221+
.suffix(".exe")
222+
.tempfile()?;
223+
file.write_all(VSWHERE)?;
224+
file.as_file().sync_all()?;
225+
// close the handle so the executable can be launched
226+
Ok(file.into_temp_path())
229227
}
230228

231229
#[cfg(target_os = "windows")]

‎crates/tauri-cli/src/info/env_system.rs‎

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -24,7 +24,7 @@ fn build_tools_version() -> crate::Result<Vec<String>> {
2424

2525
// Check if there are Visual Studio installations that have the "MSVC - C++ Buildtools" and "Windows SDK" components.
2626
// Both the Windows 10 and Windows 11 SDKs work so we need to query it twice.
27-
let output_sdk10 = Command::new(&vswhere)
27+
let output_sdk10 = Command::new(&*vswhere)
2828
.args([
2929
"-prerelease",
3030
"-products",
@@ -43,7 +43,7 @@ fn build_tools_version() -> crate::Result<Vec<String>> {
4343
error,
4444
})?;
4545

46-
let output_sdk11 = Command::new(vswhere)
46+
let output_sdk11 = Command::new(&*vswhere)
4747
.args([
4848
"-prerelease",
4949
"-products",

0 commit comments

Comments
 (0)