@@ -15,15 +15,21 @@ pub struct Args {
1515 pub target_dir : PathBuf ,
1616 pub target : String ,
1717 pub env : HashMap < OsString , OsString > ,
18+ pub current_dir : PathBuf ,
1819}
1920
2021impl Args {
2122 pub fn parse_from (
2223 args : impl IntoIterator < Item = impl Into < OsString > + Clone > ,
2324 env : impl IntoIterator < Item = ( impl Into < OsString > , impl Into < OsString > ) > ,
25+ cwd : Option < impl Into < PathBuf > > ,
2426 ) -> Result < Args > {
2527 let mut args = ArgsImpl :: parse_from ( args) ;
2628 args. env = env. into_iter ( ) . map ( |( k, v) | ( k. into ( ) , v. into ( ) ) ) . collect ( ) ;
29+ args. current_dir = match cwd {
30+ Some ( cwd) => cwd. into ( ) ,
31+ None => env:: current_dir ( ) . context ( "Failed to get current directory" ) ?,
32+ } ;
2733 args. try_into ( )
2834 }
2935}
@@ -36,12 +42,12 @@ impl TryFrom<ArgsImpl> for Args {
3642
3743 let target_dir = match value. target_dir {
3844 Some ( dir) => dir,
39- None => resolve_target_dir ( & manifest_path, & value. env ) ?,
45+ None => resolve_target_dir ( & manifest_path, & value. env , & value . current_dir ) ?,
4046 } ;
4147
4248 let target = match value. target {
4349 Some ( triplet) => triplet,
44- None => resolve_target ( & value. env ) ?,
50+ None => resolve_target ( & value. env , & value . current_dir ) ?,
4551 } ;
4652
4753 let cwd = env:: current_dir ( ) . context ( "Failed to get current directory" ) ?;
@@ -52,6 +58,7 @@ impl TryFrom<ArgsImpl> for Args {
5258 target_dir,
5359 target,
5460 env : value. env ,
61+ current_dir : value. current_dir ,
5562 } )
5663 }
5764}
@@ -79,6 +86,9 @@ struct ArgsImpl {
7986
8087 #[ arg( skip) ]
8188 env : HashMap < OsString , OsString > ,
89+
90+ #[ arg( skip) ]
91+ pub current_dir : PathBuf ,
8292}
8393
8494#[ derive( Subcommand ) ]
@@ -99,10 +109,12 @@ struct CargoMetadata {
99109fn resolve_target_dir (
100110 manifest_path : & Option < PathBuf > ,
101111 env : & HashMap < OsString , OsString > ,
112+ cwd : & PathBuf ,
102113) -> Result < PathBuf > {
103114 let output = cargo ( )
104115 . env_clear ( )
105116 . envs ( env. iter ( ) )
117+ . current_dir ( cwd)
106118 . arg ( "metadata" )
107119 . manifest_path ( manifest_path)
108120 . arg ( "--format-version=1" )
@@ -116,10 +128,11 @@ fn resolve_target_dir(
116128 Ok ( metadata. target_directory )
117129}
118130
119- fn resolve_target ( env : & HashMap < OsString , OsString > ) -> Result < String > {
131+ fn resolve_target ( env : & HashMap < OsString , OsString > , cwd : & PathBuf ) -> Result < String > {
120132 let output = cargo ( )
121133 . env_clear ( )
122134 . envs ( env. iter ( ) )
135+ . current_dir ( cwd)
123136 . arg ( "config" )
124137 . arg ( "get" )
125138 . arg ( "--quiet" )
0 commit comments