mirror of
https://github.com/storytold/craft-launcher.git
synced 2026-12-02 20:22:35 -05:00
Merge pull request #1377 from FredApps/fix/launch-bare-name-location
fix(launch): a bare app name is never a launchable location
This commit is contained in:
2 files changed
+53
-3
No files matched your search
@@ -890,7 +890,7 @@ pub fn launch(app: &CraftApp, installed: &Installed, paths: &Paths, args: &[Stri
|
||||
}
|
||||
_ if cfg!(target_os = "macos") && path.extension().is_some_and(|e| e == "app") => process::open_app(path, args),
|
||||
_ => {
|
||||
if !path.exists() {
|
||||
if !is_resolved_location(path) {
|
||||
if cfg!(windows) && installed.source == InstallSource::Msi && path.components().count() < 2 {
|
||||
return Err(format!(
|
||||
"{} is registered with Windows, but the launcher can't find where it put the program. Reinstall it from here.",
|
||||
@@ -905,6 +905,15 @@ pub fn launch(app: &CraftApp, installed: &Installed, paths: &Paths, args: &[Stri
|
||||
.map_err(|e| format!("Couldn't open {}: {e}", app.name))
|
||||
}
|
||||
|
||||
/// Whether `location` names a program that is really there. Only a full path counts: detection
|
||||
/// stores the bare app name (`WordCraft`) when it can't find the exe, and `exists()` on that
|
||||
/// looks in the launcher's working folder, which is its own install folder. With the apps in a
|
||||
/// custom folder beside the launcher, a folder of that name is there, so the bare name would be
|
||||
/// passed to spawn and fail with "program not found" instead of asking for a reinstall.
|
||||
fn is_resolved_location(location: &Path) -> bool {
|
||||
location.is_absolute() && location.exists()
|
||||
}
|
||||
|
||||
/// The environment a Craft App unpacked by the launcher runs with: its own `HOME` and the XDG
|
||||
/// base directories inside it. Apps read their settings from these, so everything they keep
|
||||
/// (settings, projects, caches) stays in that one folder.
|
||||
@@ -2419,6 +2428,18 @@ pub mod windows {
|
||||
mod tests {
|
||||
use super::*;
|
||||
|
||||
/// A bare app name is never a launchable location, even when the working folder holds a
|
||||
/// folder of that name (an app folder beside the launcher in a custom install folder).
|
||||
#[test]
|
||||
fn a_bare_name_location_is_not_found_even_when_the_working_folder_has_it() {
|
||||
let dir = tempfile::Builder::new().prefix("WordCraft-").tempdir_in(".").unwrap();
|
||||
let name = Path::new(dir.path().file_name().unwrap());
|
||||
assert!(name.exists(), "the bare name resolves against the working folder");
|
||||
assert!(!is_resolved_location(name));
|
||||
assert!(is_resolved_location(&std::fs::canonicalize(dir.path()).unwrap()));
|
||||
assert!(!is_resolved_location(&std::env::current_dir().unwrap().join("no-such-app")));
|
||||
}
|
||||
|
||||
/// A folder found only by the launch fallback, in another scope, never reaches the installer.
|
||||
#[cfg(windows)]
|
||||
#[test]
|
||||
|
||||
@@ -330,10 +330,21 @@ fn detect_discord_scheme_handler() -> bool {
|
||||
false
|
||||
}
|
||||
|
||||
/// Finds a program on `PATH`.
|
||||
/// Finds a program on `PATH`. The path returned is absolute: a relative `PATH` entry (`.`, `bin`)
|
||||
/// is taken from the working folder, as the shell does, so the result still names the same file
|
||||
/// after it is stored and used from elsewhere (launch accepts only absolute locations).
|
||||
pub(crate) fn which(name: &str) -> Option<std::path::PathBuf> {
|
||||
let path = std::env::var_os("PATH")?;
|
||||
std::env::split_paths(&path).map(|d| d.join(name)).find(|p| p.is_file())
|
||||
which_in(&path, std::env::current_dir().ok().as_deref(), name)
|
||||
}
|
||||
|
||||
/// [`which`] over the given `PATH` value, with relative entries taken from `cwd`. Without a
|
||||
/// working folder, relative entries are skipped: what they name can't be said.
|
||||
fn which_in(path: &std::ffi::OsStr, cwd: Option<&Path>, name: &str) -> Option<std::path::PathBuf> {
|
||||
std::env::split_paths(path)
|
||||
.filter_map(|d| if d.is_absolute() { Some(d) } else { cwd.map(|c| c.join(d)) })
|
||||
.map(|d| d.join(name))
|
||||
.find(|p| p.is_file())
|
||||
}
|
||||
|
||||
/// Starts an app binary from its own folder.
|
||||
@@ -570,6 +581,24 @@ mod tests {
|
||||
|
||||
use super::*;
|
||||
|
||||
/// A program found through a relative `PATH` entry comes back as an absolute path, so it is
|
||||
/// still a launchable location once stored.
|
||||
#[test]
|
||||
fn which_makes_a_relative_path_entry_absolute() {
|
||||
let dir = tempfile::tempdir().unwrap();
|
||||
std::fs::create_dir(dir.path().join("bin")).unwrap();
|
||||
std::fs::write(dir.path().join("bin").join("wordcraft"), b"").unwrap();
|
||||
let path = std::env::join_paths([Path::new("bin"), Path::new("other")]).unwrap();
|
||||
let found = which_in(&path, Some(dir.path()), "wordcraft").unwrap();
|
||||
assert!(found.is_absolute() && found.is_file(), "{found:?}");
|
||||
assert_eq!(found, dir.path().join("bin").join("wordcraft"));
|
||||
// An absolute entry is used as it is; without a working folder a relative one is skipped.
|
||||
let abs = std::env::join_paths([dir.path().join("bin")]).unwrap();
|
||||
assert_eq!(which_in(&abs, None, "wordcraft"), Some(dir.path().join("bin").join("wordcraft")));
|
||||
assert_eq!(which_in(&path, None, "wordcraft"), None);
|
||||
assert_eq!(which_in(&path, Some(dir.path()), "missing"), None);
|
||||
}
|
||||
|
||||
struct RunningProbe(Child);
|
||||
|
||||
impl RunningProbe {
|
||||
|
||||
Reference in new issue
Block a user