Skip to content

Commit ec94074

Browse files
Correctly take into account potential position of cargo command in y.sh
1 parent 2e52b08 commit ec94074

File tree

2 files changed

+23
-9
lines changed

2 files changed

+23
-9
lines changed

build_system/src/cargo.rs

+20-6
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@ use crate::utils::{
66

77
use std::collections::HashMap;
88
use std::ffi::OsStr;
9+
use std::path::PathBuf;
910

1011
fn args() -> Result<Option<Vec<String>>, String> {
1112
// We skip the binary and the "cargo" option.
@@ -42,18 +43,31 @@ pub fn run() -> Result<(), String> {
4243
// We first need to go to the original location to ensure that the config setup will go as
4344
// expected.
4445
let current_dir = std::env::current_dir()
46+
.and_then(|path| path.canonicalize())
4547
.map_err(|error| format!("Failed to get current directory path: {:?}", error))?;
4648
let current_exe = std::env::current_exe()
49+
.and_then(|path| path.canonicalize())
4750
.map_err(|error| format!("Failed to get current exe path: {:?}", error))?;
48-
let parent_dir = match current_exe.parent() {
49-
Some(parent) => parent,
50-
None => {
51+
let mut parent_dir = current_exe
52+
.components()
53+
.map(|comp| comp.as_os_str())
54+
.collect::<Vec<_>>();
55+
// We run this script from "build_system/target/release/y", so we need to remove these elements.
56+
for to_remove in &["y", "release", "target", "build_system"] {
57+
if parent_dir
58+
.last()
59+
.map(|part| part == to_remove)
60+
.unwrap_or(false)
61+
{
62+
parent_dir.pop();
63+
} else {
5164
return Err(format!(
52-
"Cannot get parent of current executable path `{}`",
53-
current_exe.display()
65+
"Build script not executed from `build_system/target/release/y` (in path {})",
66+
current_exe.display(),
5467
));
5568
}
56-
};
69+
}
70+
let parent_dir = PathBuf::from(parent_dir.join(&OsStr::new("/")));
5771
std::env::set_current_dir(&parent_dir).map_err(|error| {
5872
format!(
5973
"Failed to go to `{}` folder: {:?}",

y.sh

+3-3
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@
22

33
set -e
44
echo "[BUILD] build system" 1>&2
5-
cd build_system
5+
pushd $(dirname "$0")/build_system > /dev/null
66
cargo build --release
7-
cd ..
8-
./build_system/target/release/y $@
7+
popd > /dev/null
8+
$(dirname "$0")/build_system/target/release/y $@

0 commit comments

Comments
 (0)