@@ -21,7 +21,8 @@ use semver::Version;
21
21
use serde:: Deserialize ;
22
22
23
23
use crate :: {
24
- cfg_flag:: CfgFlag , CargoConfig , CargoFeatures , CargoWorkspace , InvocationStrategy , Package ,
24
+ cfg_flag:: CfgFlag , CargoConfig , CargoFeatures , CargoWorkspace , InvocationLocation ,
25
+ InvocationStrategy , Package ,
25
26
} ;
26
27
27
28
#[ derive( Debug , Default , Clone , PartialEq , Eq ) ]
@@ -55,10 +56,7 @@ impl BuildScriptOutput {
55
56
}
56
57
57
58
impl 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 > {
62
60
let mut cmd = match config. run_build_script_command . as_deref ( ) {
63
61
Some ( [ program, args @ ..] ) => {
64
62
let mut cmd = Command :: new ( program) ;
@@ -94,14 +92,11 @@ impl WorkspaceBuildScripts {
94
92
}
95
93
}
96
94
97
- if let Some ( workspace_root) = workspace_root {
98
- cmd. current_dir ( workspace_root) ;
99
- }
100
-
101
95
cmd
102
96
}
103
97
} ;
104
98
99
+ cmd. current_dir ( current_dir) ;
105
100
cmd. envs ( & config. extra_env ) ;
106
101
if config. wrap_rustc_in_build_scripts {
107
102
// Setup RUSTC_WRAPPER to point to `rust-analyzer` binary itself. We use
@@ -124,19 +119,19 @@ impl WorkspaceBuildScripts {
124
119
) -> io:: Result < WorkspaceBuildScripts > {
125
120
const RUST_1_62 : Version = Version :: new ( 1 , 62 , 0 ) ;
126
121
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 ( ) ;
128
127
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) {
134
129
Ok ( WorkspaceBuildScripts { error : Some ( error) , .. } )
135
130
if toolchain. as_ref ( ) . map_or ( false , |it| * it >= RUST_1_62 ) =>
136
131
{
137
132
// building build scripts failed, attempt to build with --keep-going so
138
133
// 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 ) ?;
140
135
cmd. args ( & [ "-Z" , "unstable-options" , "--keep-going" ] ) . env ( "RUSTC_BOOTSTRAP" , "1" ) ;
141
136
let mut res = Self :: run_per_ws ( cmd, workspace, progress) ?;
142
137
res. error = Some ( error) ;
@@ -154,7 +149,17 @@ impl WorkspaceBuildScripts {
154
149
progress : & dyn Fn ( String ) ,
155
150
) -> io:: Result < Vec < WorkspaceBuildScripts > > {
156
151
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 ( ) ) ?;
158
163
// NB: Cargo.toml could have been modified between `cargo metadata` and
159
164
// `cargo check`. We shouldn't assume that package ids we see here are
160
165
// exactly those from `config`.
0 commit comments