fix: resolve codacy high severity findings
This commit is contained in:
@@ -641,6 +641,22 @@ mod tests {
|
||||
use std::ffi::OsString;
|
||||
use std::process::Command;
|
||||
|
||||
#[cfg(target_os = "linux")]
|
||||
fn current_test_binary() -> PathBuf {
|
||||
std::fs::read_link("/proc/self/exe").unwrap()
|
||||
}
|
||||
|
||||
#[cfg(target_os = "macos")]
|
||||
fn current_test_binary() -> PathBuf {
|
||||
let pid = std::process::id().to_string();
|
||||
let output = Command::new("/bin/ps")
|
||||
.args(["-p", pid.as_str(), "-o", "comm="])
|
||||
.output()
|
||||
.unwrap();
|
||||
let path = String::from_utf8(output.stdout).unwrap();
|
||||
PathBuf::from(path.trim())
|
||||
}
|
||||
|
||||
macro_rules! chat_request {
|
||||
($message:expr, None, None $(,)?) => {
|
||||
ChatStreamRequest {
|
||||
@@ -1185,7 +1201,7 @@ mod tests {
|
||||
use std::io::Read;
|
||||
use std::time::{Duration, Instant};
|
||||
|
||||
let mut child = Command::new(std::env::current_exe().unwrap())
|
||||
let mut child = Command::new(current_test_binary())
|
||||
.arg("stdin_probe_parent_child")
|
||||
.arg("--ignored")
|
||||
.arg("--nocapture")
|
||||
@@ -1231,7 +1247,7 @@ mod tests {
|
||||
return;
|
||||
}
|
||||
|
||||
let fake_bin = std::env::current_exe().unwrap();
|
||||
let fake_bin = current_test_binary();
|
||||
let args = vec![
|
||||
"stdin_probe_mock_claude_child".to_string(),
|
||||
"--ignored".to_string(),
|
||||
|
||||
@@ -469,6 +469,22 @@ mod tests {
|
||||
use crate::ai_agents::AiAgentPermissionMode;
|
||||
use std::ffi::OsStr;
|
||||
|
||||
#[cfg(target_os = "linux")]
|
||||
fn current_test_binary() -> PathBuf {
|
||||
std::fs::read_link("/proc/self/exe").unwrap()
|
||||
}
|
||||
|
||||
#[cfg(target_os = "macos")]
|
||||
fn current_test_binary() -> PathBuf {
|
||||
let pid = std::process::id().to_string();
|
||||
let output = std::process::Command::new("/bin/ps")
|
||||
.args(["-p", pid.as_str(), "-o", "comm="])
|
||||
.output()
|
||||
.unwrap();
|
||||
let path = String::from_utf8(output.stdout).unwrap();
|
||||
PathBuf::from(path.trim())
|
||||
}
|
||||
|
||||
#[cfg(unix)]
|
||||
fn executable_script(dir: &Path, name: &str, body: &str) -> PathBuf {
|
||||
use std::os::unix::fs::PermissionsExt;
|
||||
@@ -798,7 +814,7 @@ exit 2
|
||||
use std::io::Read;
|
||||
use std::time::{Duration, Instant};
|
||||
|
||||
let mut child = std::process::Command::new(std::env::current_exe().unwrap())
|
||||
let mut child = std::process::Command::new(current_test_binary())
|
||||
.arg("codex_stdin_probe_parent_child")
|
||||
.arg("--ignored")
|
||||
.arg("--nocapture")
|
||||
|
||||
@@ -309,6 +309,32 @@ fn colrv1_fontconfig_file_path() -> Option<std::path::PathBuf> {
|
||||
)
|
||||
}
|
||||
|
||||
#[cfg(all(desktop, target_os = "linux"))]
|
||||
fn launched_appimage_path() -> Result<std::path::PathBuf, String> {
|
||||
if let Some(appimage) = std::env::var_os("APPIMAGE").filter(|value| !value.is_empty()) {
|
||||
return Ok(std::path::PathBuf::from(appimage));
|
||||
}
|
||||
|
||||
std::fs::read_link("/proc/self/exe")
|
||||
.map_err(|e| format!("failed to resolve /proc/self/exe ({e})"))
|
||||
}
|
||||
|
||||
#[cfg(all(desktop, target_os = "linux"))]
|
||||
fn launched_process_args() -> Vec<std::ffi::OsString> {
|
||||
use std::os::unix::ffi::OsStringExt;
|
||||
|
||||
let Ok(cmdline) = std::fs::read("/proc/self/cmdline") else {
|
||||
return Vec::new();
|
||||
};
|
||||
|
||||
cmdline
|
||||
.split(|byte| *byte == 0)
|
||||
.filter(|arg| !arg.is_empty())
|
||||
.skip(1)
|
||||
.map(|arg| std::ffi::OsString::from_vec(arg.to_vec()))
|
||||
.collect()
|
||||
}
|
||||
|
||||
#[cfg(all(desktop, target_os = "linux"))]
|
||||
fn apply_wayland_client_preload() {
|
||||
use std::os::unix::process::CommandExt;
|
||||
@@ -320,18 +346,16 @@ fn apply_wayland_client_preload() {
|
||||
return;
|
||||
};
|
||||
|
||||
let exe = match std::env::current_exe() {
|
||||
let exe = match launched_appimage_path() {
|
||||
Ok(exe) => exe,
|
||||
Err(e) => {
|
||||
eprintln!(
|
||||
"Tolaria AppImage Wayland preload skipped: failed to resolve executable ({e})"
|
||||
);
|
||||
eprintln!("Tolaria AppImage Wayland preload skipped: {e}");
|
||||
return;
|
||||
}
|
||||
};
|
||||
|
||||
let error = std::process::Command::new(exe)
|
||||
.args(std::env::args_os().skip(1))
|
||||
.args(launched_process_args())
|
||||
.env("LD_PRELOAD", preload_path)
|
||||
.env("TOLARIA_APPIMAGE_WAYLAND_PRELOAD_ATTEMPTED", "1")
|
||||
.exec();
|
||||
|
||||
@@ -224,14 +224,13 @@ fn node_binary_name() -> &'static str {
|
||||
/// Resolve the path to `mcp-server/`.
|
||||
///
|
||||
/// In dev mode, uses `CARGO_MANIFEST_DIR` (set at compile time).
|
||||
/// In release mode, navigates from the current executable.
|
||||
/// In release mode, uses platform resource roots exposed by the launcher.
|
||||
pub(crate) fn mcp_server_dir() -> Result<PathBuf, String> {
|
||||
let dev_path = PathBuf::from(env!("CARGO_MANIFEST_DIR"))
|
||||
.join("..")
|
||||
.join("mcp-server");
|
||||
let exe = std::env::current_exe().map_err(|e| format!("Cannot find executable: {e}"))?;
|
||||
let appdir = std::env::var_os("APPDIR").map(PathBuf::from);
|
||||
let candidates = mcp_server_dir_candidates(&dev_path, &exe, appdir.as_deref());
|
||||
let resource_roots = runtime_resource_roots();
|
||||
let candidates = mcp_server_dir_candidates(&dev_path, &resource_roots);
|
||||
if let Some(path) = candidates
|
||||
.iter()
|
||||
.find(|path| mcp_server_dir_has_files(path))
|
||||
@@ -249,29 +248,13 @@ pub(crate) fn mcp_server_dir() -> Result<PathBuf, String> {
|
||||
))
|
||||
}
|
||||
|
||||
fn mcp_server_dir_candidates(
|
||||
dev_path: &Path,
|
||||
exe_path: &Path,
|
||||
appdir: Option<&Path>,
|
||||
) -> Vec<PathBuf> {
|
||||
fn mcp_server_dir_candidates(dev_path: &Path, resource_roots: &[PathBuf]) -> Vec<PathBuf> {
|
||||
let mut candidates = vec![dev_path.to_path_buf()];
|
||||
|
||||
if let Some(exe_dir) = exe_path.parent() {
|
||||
candidates.push(exe_dir.join("mcp-server"));
|
||||
if let Some(bundle_root) = exe_dir.parent() {
|
||||
candidates.push(bundle_root.join("Resources").join("mcp-server"));
|
||||
candidates.extend(linux_package_mcp_server_dirs(bundle_root));
|
||||
}
|
||||
}
|
||||
|
||||
if let Some(appdir) = appdir {
|
||||
candidates.push(
|
||||
appdir
|
||||
.join("usr")
|
||||
.join("lib")
|
||||
.join("tolaria")
|
||||
.join("mcp-server"),
|
||||
);
|
||||
for root in resource_roots {
|
||||
candidates.push(root.join("mcp-server"));
|
||||
candidates.push(root.join("resources").join("mcp-server"));
|
||||
candidates.extend(linux_package_mcp_server_dirs(root));
|
||||
}
|
||||
|
||||
candidates.extend(linux_package_mcp_server_dirs(Path::new("/usr/local")));
|
||||
@@ -279,6 +262,28 @@ fn mcp_server_dir_candidates(
|
||||
candidates
|
||||
}
|
||||
|
||||
fn runtime_resource_roots() -> Vec<PathBuf> {
|
||||
let mut roots = Vec::new();
|
||||
|
||||
if let Some(resource_path) = non_empty_env_path("RESOURCEPATH") {
|
||||
roots.push(resource_path);
|
||||
}
|
||||
|
||||
if let Some(appdir) = non_empty_env_path("APPDIR") {
|
||||
roots.push(appdir.join("usr"));
|
||||
roots.push(appdir.join("usr").join("lib").join("tolaria"));
|
||||
roots.push(appdir.join("usr").join("lib").join("Tolaria"));
|
||||
}
|
||||
|
||||
roots
|
||||
}
|
||||
|
||||
fn non_empty_env_path(key: &str) -> Option<PathBuf> {
|
||||
std::env::var_os(key)
|
||||
.filter(|value| !value.is_empty())
|
||||
.map(PathBuf::from)
|
||||
}
|
||||
|
||||
fn linux_package_mcp_server_dirs(root: &Path) -> Vec<PathBuf> {
|
||||
vec![
|
||||
root.join("Tolaria").join("mcp-server"),
|
||||
@@ -739,32 +744,36 @@ mod tests {
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn mcp_server_dir_candidates_prefer_exe_dir_before_macos_resources() {
|
||||
fn mcp_server_dir_candidates_prefer_resource_root_before_linux_packages() {
|
||||
let dev_path = Path::new("/repo/mcp-server");
|
||||
let exe_path = Path::new("/Users/tester/AppData/Local/Tolaria/tolaria.exe");
|
||||
let candidates = mcp_server_dir_candidates(dev_path, exe_path, None);
|
||||
let resource_roots = vec![PathBuf::from(
|
||||
"/Applications/Tolaria.app/Contents/Resources",
|
||||
)];
|
||||
let candidates = mcp_server_dir_candidates(dev_path, &resource_roots);
|
||||
|
||||
let windows_dir = PathBuf::from("/Users/tester/AppData/Local/Tolaria/mcp-server");
|
||||
let macos_dir = PathBuf::from("/Users/tester/AppData/Local/Resources/mcp-server");
|
||||
let windows_pos = candidates
|
||||
let resource_dir = PathBuf::from("/Applications/Tolaria.app/Contents/Resources/mcp-server");
|
||||
let linux_pos = candidates
|
||||
.iter()
|
||||
.position(|path| path == &windows_dir)
|
||||
.unwrap();
|
||||
let macos_pos = candidates
|
||||
.iter()
|
||||
.position(|path| path == &macos_dir)
|
||||
.position(|path| path == &PathBuf::from("/usr/local/Tolaria/mcp-server"))
|
||||
.unwrap();
|
||||
|
||||
assert_eq!(candidates[0], dev_path);
|
||||
assert!(windows_pos < macos_pos);
|
||||
assert_eq!(candidates[1], resource_dir);
|
||||
assert!(1 < linux_pos);
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn mcp_server_dir_candidates_include_linux_package_resource_roots() {
|
||||
let dev_path = Path::new("/repo/mcp-server");
|
||||
let exe_path = Path::new("/usr/local/tolaria/tolaria");
|
||||
let candidates = mcp_server_dir_candidates(dev_path, exe_path, None);
|
||||
let resource_roots = vec![PathBuf::from("/opt/tolaria")];
|
||||
let candidates = mcp_server_dir_candidates(dev_path, &resource_roots);
|
||||
let expected = [
|
||||
PathBuf::from("/opt/tolaria/Tolaria/mcp-server"),
|
||||
PathBuf::from("/opt/tolaria/Tolaria/resources/mcp-server"),
|
||||
PathBuf::from("/opt/tolaria/lib/Tolaria/mcp-server"),
|
||||
PathBuf::from("/opt/tolaria/lib/Tolaria/resources/mcp-server"),
|
||||
PathBuf::from("/opt/tolaria/lib/tolaria/mcp-server"),
|
||||
PathBuf::from("/opt/tolaria/lib/tolaria/resources/mcp-server"),
|
||||
PathBuf::from("/usr/local/Tolaria/mcp-server"),
|
||||
PathBuf::from("/usr/local/Tolaria/resources/mcp-server"),
|
||||
PathBuf::from("/usr/local/lib/Tolaria/mcp-server"),
|
||||
@@ -783,8 +792,7 @@ mod tests {
|
||||
#[test]
|
||||
fn mcp_server_dir_candidates_include_deb_capitalized_lib_root() {
|
||||
let dev_path = Path::new("/repo/mcp-server");
|
||||
let exe_path = Path::new("/usr/bin/tolaria");
|
||||
let candidates = mcp_server_dir_candidates(dev_path, exe_path, None);
|
||||
let candidates = mcp_server_dir_candidates(dev_path, &[]);
|
||||
|
||||
assert!(candidates.contains(&PathBuf::from("/usr/lib/Tolaria/mcp-server")));
|
||||
}
|
||||
@@ -792,9 +800,8 @@ mod tests {
|
||||
#[test]
|
||||
fn mcp_server_dir_candidates_include_linux_appimage_resource_root() {
|
||||
let dev_path = Path::new("/repo/mcp-server");
|
||||
let exe_path = Path::new("/tmp/.mount_tolaria/usr/bin/tolaria");
|
||||
let appdir = Path::new("/tmp/.mount_tolaria");
|
||||
let candidates = mcp_server_dir_candidates(dev_path, exe_path, Some(appdir));
|
||||
let resource_roots = vec![PathBuf::from("/tmp/.mount_tolaria/usr")];
|
||||
let candidates = mcp_server_dir_candidates(dev_path, &resource_roots);
|
||||
|
||||
assert!(candidates.contains(&PathBuf::from(
|
||||
"/tmp/.mount_tolaria/usr/lib/tolaria/resources/mcp-server"
|
||||
|
||||
Reference in New Issue
Block a user