@@ -21,7 +21,8 @@ use semver::Version;
2121use serde:: Deserialize ;
2222
2323use crate :: {
24- cfg_flag:: CfgFlag , CargoConfig , CargoFeatures , CargoWorkspace , InvocationStrategy , Package ,
24+ cfg_flag:: CfgFlag , CargoConfig , CargoFeatures , CargoWorkspace , InvocationLocation ,
25+ InvocationStrategy , Package ,
2526} ;
2627
2728#[ derive( Debug , Default , Clone , PartialEq , Eq ) ]
@@ -55,10 +56,7 @@ impl BuildScriptOutput {
5556}
5657
5758impl WorkspaceBuildScripts {
58- fn build_command (
59- config : & CargoConfig ,
60- workspace_root : Option < & path:: Path > ,
61- ) -> io:: Result < Command > {
59+ fn build_command ( config : & CargoConfig , current_dir : & path:: Path ) -> io:: Result < Command > {
6260 let mut cmd = match config. run_build_script_command . as_deref ( ) {
6361 Some ( [ program, args @ ..] ) => {
6462 let mut cmd = Command :: new ( program) ;
@@ -94,14 +92,11 @@ impl WorkspaceBuildScripts {
9492 }
9593 }
9694
97- if let Some ( workspace_root) = workspace_root {
98- cmd. current_dir ( workspace_root) ;
99- }
100-
10195 cmd
10296 }
10397 } ;
10498
99+ cmd. current_dir ( current_dir) ;
105100 cmd. envs ( & config. extra_env ) ;
106101 if config. wrap_rustc_in_build_scripts {
107102 // Setup RUSTC_WRAPPER to point to `rust-analyzer` binary itself. We use
@@ -124,19 +119,19 @@ impl WorkspaceBuildScripts {
124119 ) -> io:: Result < WorkspaceBuildScripts > {
125120 const RUST_1_62 : Version = Version :: new ( 1 , 62 , 0 ) ;
126121
127- let workspace_root: & path:: Path = & workspace. workspace_root ( ) . as_ref ( ) ;
122+ let current_dir = match & config. invocation_location {
123+ InvocationLocation :: Project ( root) => root. as_path ( ) ,
124+ InvocationLocation :: Workspace => & workspace. workspace_root ( ) ,
125+ }
126+ . as_ref ( ) ;
128127
129- match Self :: run_per_ws (
130- Self :: build_command ( config, Some ( workspace_root) ) ?,
131- workspace,
132- progress,
133- ) {
128+ match Self :: run_per_ws ( Self :: build_command ( config, current_dir) ?, workspace, progress) {
134129 Ok ( WorkspaceBuildScripts { error : Some ( error) , .. } )
135130 if toolchain. as_ref ( ) . map_or ( false , |it| * it >= RUST_1_62 ) =>
136131 {
137132 // building build scripts failed, attempt to build with --keep-going so
138133 // that we potentially get more build data
139- let mut cmd = Self :: build_command ( config, Some ( workspace_root ) ) ?;
134+ let mut cmd = Self :: build_command ( config, current_dir ) ?;
140135 cmd. args ( & [ "-Z" , "unstable-options" , "--keep-going" ] ) . env ( "RUSTC_BOOTSTRAP" , "1" ) ;
141136 let mut res = Self :: run_per_ws ( cmd, workspace, progress) ?;
142137 res. error = Some ( error) ;
@@ -154,7 +149,17 @@ impl WorkspaceBuildScripts {
154149 progress : & dyn Fn ( String ) ,
155150 ) -> io:: Result < Vec < WorkspaceBuildScripts > > {
156151 assert_eq ! ( config. invocation_strategy, InvocationStrategy :: Once ) ;
157- let cmd = Self :: build_command ( config, None ) ?;
152+
153+ let current_dir = match & config. invocation_location {
154+ InvocationLocation :: Project ( root) => root,
155+ InvocationLocation :: Workspace => {
156+ return Err ( io:: Error :: new (
157+ io:: ErrorKind :: Other ,
158+ "Cannot run build scripts from workspace with invocation strategy `once`" ,
159+ ) )
160+ }
161+ } ;
162+ let cmd = Self :: build_command ( config, current_dir. as_path ( ) . as_ref ( ) ) ?;
158163 // NB: Cargo.toml could have been modified between `cargo metadata` and
159164 // `cargo check`. We shouldn't assume that package ids we see here are
160165 // exactly those from `config`.
0 commit comments