@@ -6,6 +6,7 @@ use crate::utils::{
66
77use std:: collections:: HashMap ;
88use std:: ffi:: OsStr ;
9+ use std:: path:: PathBuf ;
910
1011fn 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: {:?}" ,
0 commit comments