@@ -160,12 +160,12 @@ impl CargoConfigs {
160
160
pub fn new (
161
161
cli_configs : impl IntoIterator < Item = impl AsRef < str > > ,
162
162
) -> Result < Self , CargoConfigError > {
163
- let cli_configs = parse_cli_configs ( cli_configs. into_iter ( ) ) ?;
164
163
let cwd = std:: env:: current_dir ( )
165
164
. map_err ( CargoConfigError :: GetCurrentDir )
166
165
. and_then ( |cwd| {
167
166
Utf8PathBuf :: try_from ( cwd) . map_err ( CargoConfigError :: CurrentDirInvalidUtf8 )
168
167
} ) ?;
168
+ let cli_configs = parse_cli_configs ( & cwd, cli_configs. into_iter ( ) ) ?;
169
169
170
170
Ok ( Self {
171
171
cli_configs,
@@ -184,7 +184,7 @@ impl CargoConfigs {
184
184
cwd : & Utf8Path ,
185
185
terminate_search_at : & Utf8Path ,
186
186
) -> Result < Self , CargoConfigError > {
187
- let cli_configs = parse_cli_configs ( cli_configs. into_iter ( ) ) ?;
187
+ let cli_configs = parse_cli_configs ( cwd , cli_configs. into_iter ( ) ) ?;
188
188
189
189
Ok ( Self {
190
190
cli_configs,
@@ -214,15 +214,23 @@ impl CargoConfigs {
214
214
}
215
215
216
216
fn parse_cli_configs (
217
+ cwd : & Utf8Path ,
217
218
cli_configs : impl Iterator < Item = impl AsRef < str > > ,
218
219
) -> Result < Vec < ( CargoConfigSource , CargoConfig ) > , CargoConfigError > {
219
220
cli_configs
220
221
. into_iter ( )
221
222
. map ( |config_str| {
222
223
// Each cargo config is expected to be a valid TOML file.
223
224
let config_str = config_str. as_ref ( ) ;
224
- let config = parse_cli_config ( config_str) ?;
225
- Ok ( ( CargoConfigSource :: CliOption , config) )
225
+
226
+ let as_path = cwd. join ( config_str) ;
227
+ if as_path. exists ( ) {
228
+ // Read this config as a file.
229
+ load_file ( as_path)
230
+ } else {
231
+ let config = parse_cli_config ( config_str) ?;
232
+ Ok ( ( CargoConfigSource :: CliOption , config) )
233
+ }
226
234
} )
227
235
. collect ( )
228
236
}
@@ -387,27 +395,31 @@ fn discover_impl(
387
395
388
396
let configs = config_paths
389
397
. into_iter ( )
390
- . map ( |path| {
391
- let config_contents = std:: fs:: read_to_string ( & path) . map_err ( |error| {
392
- CargoConfigError :: ConfigReadError {
393
- path : path. clone ( ) ,
394
- error,
395
- }
396
- } ) ?;
397
- let config: CargoConfig =
398
- toml_edit:: easy:: from_str ( & config_contents) . map_err ( |error| {
399
- CargoConfigError :: ConfigParseError {
400
- path : path. clone ( ) ,
401
- error,
402
- }
403
- } ) ?;
404
- Ok ( ( CargoConfigSource :: File ( path) , config) )
405
- } )
398
+ . map ( load_file)
406
399
. collect :: < Result < Vec < _ > , CargoConfigError > > ( ) ?;
407
400
408
401
Ok ( configs)
409
402
}
410
403
404
+ fn load_file (
405
+ path : impl Into < Utf8PathBuf > ,
406
+ ) -> Result < ( CargoConfigSource , CargoConfig ) , CargoConfigError > {
407
+ let path = path. into ( ) ;
408
+
409
+ let config_contents =
410
+ std:: fs:: read_to_string ( & path) . map_err ( |error| CargoConfigError :: ConfigReadError {
411
+ path : path. clone ( ) ,
412
+ error,
413
+ } ) ?;
414
+ let config: CargoConfig = toml_edit:: easy:: from_str ( & config_contents) . map_err ( |error| {
415
+ CargoConfigError :: ConfigParseError {
416
+ path : path. clone ( ) ,
417
+ error,
418
+ }
419
+ } ) ?;
420
+ Ok ( ( CargoConfigSource :: File ( path) , config) )
421
+ }
422
+
411
423
#[ derive( Deserialize , Debug ) ]
412
424
pub ( crate ) struct CargoConfig {
413
425
#[ serde( default ) ]
@@ -567,6 +579,56 @@ mod tests {
567
579
} )
568
580
) ;
569
581
582
+ // --config <path> should be parsed correctly.
583
+ assert_eq ! (
584
+ find_target_triple(
585
+ & [
586
+ "extra-config.toml" ,
587
+ "build.target=\" x86_64-unknown-linux-musl\" " ,
588
+ ] ,
589
+ & dir_foo_path,
590
+ & dir_path
591
+ ) ,
592
+ Some ( TargetTriple {
593
+ triple: "aarch64-unknown-linux-gnu" . into( ) ,
594
+ source: TargetTripleSource :: CargoConfig {
595
+ source: CargoConfigSource :: File ( dir_foo_path. join( "extra-config.toml" ) ) ,
596
+ } ,
597
+ } )
598
+ ) ;
599
+ assert_eq ! (
600
+ find_target_triple(
601
+ & [
602
+ "../extra-config.toml" ,
603
+ "build.target=\" x86_64-unknown-linux-musl\" " ,
604
+ ] ,
605
+ & dir_foo_bar_path,
606
+ & dir_path
607
+ ) ,
608
+ Some ( TargetTriple {
609
+ triple: "aarch64-unknown-linux-gnu" . into( ) ,
610
+ source: TargetTripleSource :: CargoConfig {
611
+ source: CargoConfigSource :: File ( dir_foo_bar_path. join( "../extra-config.toml" ) ) ,
612
+ } ,
613
+ } )
614
+ ) ;
615
+ assert_eq ! (
616
+ find_target_triple(
617
+ & [
618
+ "build.target=\" x86_64-unknown-linux-musl\" " ,
619
+ "extra-config.toml" ,
620
+ ] ,
621
+ & dir_foo_path,
622
+ & dir_path
623
+ ) ,
624
+ Some ( TargetTriple {
625
+ triple: "x86_64-unknown-linux-musl" . into( ) ,
626
+ source: TargetTripleSource :: CargoConfig {
627
+ source: CargoConfigSource :: CliOption ,
628
+ } ,
629
+ } )
630
+ ) ;
631
+
570
632
assert_eq ! ( find_target_triple( & [ ] , & dir_path, & dir_path) , None ) ;
571
633
}
572
634
@@ -590,6 +652,11 @@ mod tests {
590
652
FOO_BAR_CARGO_CONFIG_CONTENTS ,
591
653
)
592
654
. wrap_err ( "error writing foo/bar/.cargo/config.toml" ) ?;
655
+ std:: fs:: write (
656
+ dir. path ( ) . join ( "foo/extra-config.toml" ) ,
657
+ FOO_EXTRA_CONFIG_CONTENTS ,
658
+ )
659
+ . wrap_err ( "error writing foo/extra-config.toml" ) ?;
593
660
594
661
Ok ( dir)
595
662
}
@@ -614,4 +681,9 @@ mod tests {
614
681
[build]
615
682
target = "x86_64-unknown-linux-gnu"
616
683
"# ;
684
+
685
+ static FOO_EXTRA_CONFIG_CONTENTS : & str = r#"
686
+ [build]
687
+ target = "aarch64-unknown-linux-gnu"
688
+ "# ;
617
689
}
0 commit comments