Rust library to generate static websites

fix: use same target finding function in assets

+59 -4
+8
Cargo.lock
··· 1960 1960 checksum = "2304e00983f87ffb38b55b444b5e3b60a884b5d30c0fca7d82fe33449bbe55ea" 1961 1961 1962 1962 [[package]] 1963 + name = "hot-reload-optimization" 1964 + version = "0.1.0" 1965 + dependencies = [ 1966 + "codspeed-divan-compat", 1967 + "maudit", 1968 + ] 1969 + 1970 + [[package]] 1963 1971 name = "http" 1964 1972 version = "1.4.0" 1965 1973 source = "registry+https://github.com/rust-lang/crates.io-index"
+51 -4
crates/maudit/src/build/options.rs
··· 1 - use std::{env, path::PathBuf}; 1 + use std::{fs, path::PathBuf}; 2 2 3 3 use crate::{assets::RouteAssetsOptions, is_dev, sitemap::SitemapOptions}; 4 4 ··· 165 165 tailwind_binary_path: "tailwindcss".into(), 166 166 assets_dir: "_maudit".into(), 167 167 image_cache_dir: { 168 - let target_dir = 169 - env::var("CARGO_TARGET_DIR").unwrap_or_else(|_| "target".to_string()); 170 - PathBuf::from(target_dir).join("maudit_cache/images") 168 + find_target_dir() 169 + .unwrap_or_else(|_| PathBuf::from("target")) 170 + .join("maudit_cache/images") 171 171 }, 172 172 hashing_strategy: if is_dev() { 173 173 AssetHashingStrategy::FastImprecise ··· 207 207 } 208 208 } 209 209 } 210 + 211 + /// Find the target directory using multiple strategies 212 + /// 213 + /// This function tries multiple approaches to locate the target directory: 214 + /// 1. CARGO_TARGET_DIR / CARGO_BUILD_TARGET_DIR environment variables 215 + /// 2. Local ./target directory 216 + /// 3. Workspace root target directory (walking up to find [workspace]) 217 + /// 4. Fallback to relative "target" path 218 + fn find_target_dir() -> Result<PathBuf, std::io::Error> { 219 + // 1. Check CARGO_TARGET_DIR and CARGO_BUILD_TARGET_DIR environment variables 220 + for env_var in ["CARGO_TARGET_DIR", "CARGO_BUILD_TARGET_DIR"] { 221 + if let Ok(target_dir) = std::env::var(env_var) { 222 + let path = PathBuf::from(&target_dir); 223 + if path.exists() { 224 + return Ok(path); 225 + } 226 + } 227 + } 228 + 229 + // 2. Look for target directory in current directory 230 + let local_target = PathBuf::from("target"); 231 + if local_target.exists() { 232 + return Ok(local_target); 233 + } 234 + 235 + // 3. Try to find workspace root by looking for Cargo.toml with [workspace] 236 + let mut current = std::env::current_dir()?; 237 + loop { 238 + let cargo_toml = current.join("Cargo.toml"); 239 + if cargo_toml.exists() 240 + && let Ok(content) = fs::read_to_string(&cargo_toml) 241 + && content.contains("[workspace]") { 242 + let workspace_target = current.join("target"); 243 + if workspace_target.exists() { 244 + return Ok(workspace_target); 245 + } 246 + } 247 + 248 + // Move up to parent directory 249 + if !current.pop() { 250 + break; 251 + } 252 + } 253 + 254 + // 4. Final fallback to relative path 255 + Ok(PathBuf::from("target")) 256 + }