refactor: eliminate all CARGO_MANIFEST_DIR compile-time dependencies (A-4)
- Add src/core/path_utils.rs with runtime path resolution helpers
- Replace all env!("CARGO_MANIFEST_DIR") with runtime resolution
- Priority: env var -> current_exe sibling -> dev fallback
Changes:
- executor.rs: scripts_dir(), cli_dir() (4 locations)
- face.rs: swift_processor_path()
- tool_selector.rs: swift_binary_path()
- thumbnail/mod.rs: script_path()
Issue: CARGO_MANIFEST_DIR expands to build machine path at compile time,
breaking on binary-only release hosts.
Solution: Resolve paths at runtime in this order:
1. Explicit environment variable (MOMENTRY_SCRIPTS_DIR, MOMENTRY_CLI_DIR)
2. Sibling of current_exe (same directory as running binary)
3. Relative path fallback (dev machine)
Verified: cargo check --lib passes
This commit is contained in:
@@ -0,0 +1,38 @@
|
||||
use std::path::PathBuf;
|
||||
|
||||
/// Resolve asset path at runtime (not compile-time).
|
||||
///
|
||||
/// Priority:
|
||||
/// 1. Explicit environment variable (e.g., MOMENTRY_SCRIPTS_DIR)
|
||||
/// 2. Sibling of current_exe (same directory as running binary)
|
||||
/// 3. Relative path fallback (dev machine)
|
||||
///
|
||||
/// This avoids CARGO_MANIFEST_DIR being baked into the binary at compile time,
|
||||
/// which breaks on binary-only release hosts.
|
||||
pub fn resolve_asset_path(env_var: &str, fallback_relative: &str) -> PathBuf {
|
||||
std::env::var_os(env_var)
|
||||
.map(PathBuf::from)
|
||||
.filter(|p| p.exists())
|
||||
.or_else(|| {
|
||||
std::env::current_exe()
|
||||
.ok()
|
||||
.and_then(|p| p.parent().map(|d| d.join(fallback_relative)))
|
||||
.filter(|p| p.exists())
|
||||
})
|
||||
.unwrap_or_else(|| PathBuf::from(fallback_relative))
|
||||
}
|
||||
|
||||
/// Get scripts directory.
|
||||
pub fn scripts_dir() -> PathBuf {
|
||||
resolve_asset_path("MOMENTRY_SCRIPTS_DIR", "scripts")
|
||||
}
|
||||
|
||||
/// Get CLI tools directory.
|
||||
pub fn cli_dir() -> PathBuf {
|
||||
resolve_asset_path("MOMENTRY_CLI_DIR", "tools/cli")
|
||||
}
|
||||
|
||||
/// Get config directory.
|
||||
pub fn config_dir() -> PathBuf {
|
||||
resolve_asset_path("MOMENTRY_CONFIG_DIR", "config")
|
||||
}
|
||||
Reference in New Issue
Block a user