From ec940748175eca4e476ed29fa537319eb090356a Mon Sep 17 00:00:00 2001 From: Guillaume Gomez Date: Fri, 22 Dec 2023 00:47:01 +0100 Subject: [PATCH] Correctly take into account potential position of cargo command in `y.sh` --- build_system/src/cargo.rs | 26 ++++++++++++++++++++------ y.sh | 6 +++--- 2 files changed, 23 insertions(+), 9 deletions(-) diff --git a/build_system/src/cargo.rs b/build_system/src/cargo.rs index 06b543a6cad6..5f9de5e2eb65 100644 --- a/build_system/src/cargo.rs +++ b/build_system/src/cargo.rs @@ -6,6 +6,7 @@ use crate::utils::{ use std::collections::HashMap; use std::ffi::OsStr; +use std::path::PathBuf; fn args() -> Result>, String> { // We skip the binary and the "cargo" option. @@ -42,18 +43,31 @@ pub fn run() -> Result<(), String> { // We first need to go to the original location to ensure that the config setup will go as // expected. let current_dir = std::env::current_dir() + .and_then(|path| path.canonicalize()) .map_err(|error| format!("Failed to get current directory path: {:?}", error))?; let current_exe = std::env::current_exe() + .and_then(|path| path.canonicalize()) .map_err(|error| format!("Failed to get current exe path: {:?}", error))?; - let parent_dir = match current_exe.parent() { - Some(parent) => parent, - None => { + let mut parent_dir = current_exe + .components() + .map(|comp| comp.as_os_str()) + .collect::>(); + // We run this script from "build_system/target/release/y", so we need to remove these elements. + for to_remove in &["y", "release", "target", "build_system"] { + if parent_dir + .last() + .map(|part| part == to_remove) + .unwrap_or(false) + { + parent_dir.pop(); + } else { return Err(format!( - "Cannot get parent of current executable path `{}`", - current_exe.display() + "Build script not executed from `build_system/target/release/y` (in path {})", + current_exe.display(), )); } - }; + } + let parent_dir = PathBuf::from(parent_dir.join(&OsStr::new("/"))); std::env::set_current_dir(&parent_dir).map_err(|error| { format!( "Failed to go to `{}` folder: {:?}", diff --git a/y.sh b/y.sh index 188109743e3d..69d7917dd777 100755 --- a/y.sh +++ b/y.sh @@ -2,7 +2,7 @@ set -e echo "[BUILD] build system" 1>&2 -cd build_system +pushd $(dirname "$0")/build_system > /dev/null cargo build --release -cd .. -./build_system/target/release/y $@ +popd > /dev/null +$(dirname "$0")/build_system/target/release/y $@