File tree Expand file tree Collapse file tree 3 files changed +49
-2
lines changed Expand file tree Collapse file tree 3 files changed +49
-2
lines changed Original file line number Diff line number Diff line change @@ -473,10 +473,24 @@ impl Config {
473
473
if let Some ( dir) = & self . target_dir {
474
474
Ok ( Some ( dir. clone ( ) ) )
475
475
} else if let Some ( dir) = env:: var_os ( "CARGO_TARGET_DIR" ) {
476
+ // Check if the CARGO_TARGET_DIR environment variable is set to an empty string.
477
+ if dir. to_string_lossy ( ) == "" {
478
+ anyhow:: bail!( "the target directory is set to an empty string in the `CARGO_TARGET_DIR` environment variable" )
479
+ }
480
+
476
481
Ok ( Some ( Filesystem :: new ( self . cwd . join ( dir) ) ) )
477
482
} else if let Some ( val) = & self . build_config ( ) ?. target_dir {
478
- let val = val. resolve_path ( self ) ;
479
- Ok ( Some ( Filesystem :: new ( val) ) )
483
+ let path = val. resolve_path ( self ) ;
484
+
485
+ // Check if the target directory is set to an empty string in the config.toml file.
486
+ if val. raw_value ( ) == "" {
487
+ anyhow:: bail!( format!(
488
+ "the target directory is set to an empty string in {}" ,
489
+ val. value( ) . definition
490
+ ) , )
491
+ }
492
+
493
+ Ok ( Some ( Filesystem :: new ( path) ) )
480
494
} else {
481
495
Ok ( None )
482
496
}
Original file line number Diff line number Diff line change @@ -10,6 +10,11 @@ use std::path::PathBuf;
10
10
pub struct ConfigRelativePath ( Value < String > ) ;
11
11
12
12
impl ConfigRelativePath {
13
+ /// Returns the underlying value.
14
+ pub fn value ( & self ) -> & Value < String > {
15
+ & self . 0
16
+ }
17
+
13
18
/// Returns the raw underlying configuration value for this key.
14
19
pub fn raw_value ( & self ) -> & str {
15
20
& self . 0 . val
Original file line number Diff line number Diff line change @@ -1460,3 +1460,31 @@ strip = 'debuginfo'
1460
1460
let strip = p. strip . unwrap ( ) ;
1461
1461
assert_eq ! ( strip, toml:: StringOrBool :: String ( "debuginfo" . to_string( ) ) ) ;
1462
1462
}
1463
+
1464
+ #[ cargo_test]
1465
+ fn cargo_target_empty_cfg ( ) {
1466
+ write_config (
1467
+ "\
1468
+ [build]
1469
+ target-dir = ''
1470
+ " ,
1471
+ ) ;
1472
+
1473
+ let config = new_config ( ) ;
1474
+
1475
+ assert_error (
1476
+ config. target_dir ( ) . unwrap_err ( ) ,
1477
+ "the target directory is set to an empty string in [..]/.cargo/config" ,
1478
+ ) ;
1479
+ }
1480
+
1481
+ #[ cargo_test]
1482
+ fn cargo_target_empty_env ( ) {
1483
+ let project = project ( ) . build ( ) ;
1484
+
1485
+ project. cargo ( "build" )
1486
+ . env ( "CARGO_TARGET_DIR" , "" )
1487
+ . with_stderr ( "error: the target directory is set to an empty string in the `CARGO_TARGET_DIR` environment variable" )
1488
+ . with_status ( 101 )
1489
+ . run ( )
1490
+ }
You can’t perform that action at this time.
0 commit comments