1
- use anyhow:: { Context , Result } ;
1
+ use anyhow:: Result ;
2
2
use serde:: Deserialize ;
3
3
use std:: {
4
4
borrow:: Cow ,
@@ -104,7 +104,7 @@ pub enum EnvOption {
104
104
}
105
105
106
106
impl EnvOption {
107
- /// Retrieve the value and canonicalize it relative to `config_parent` when [`EnvOption::Value::relative`] is set.
107
+ /// Retrieve the value and join it to `config_parent` when [`EnvOption::Value::relative`] is set.
108
108
///
109
109
/// `config_parent` is the directory containing `.cargo/config.toml` where this was parsed from.
110
110
pub fn resolve_value ( & self , config_parent : impl AsRef < Path > ) -> Result < Cow < ' _ , str > > {
@@ -113,16 +113,13 @@ impl EnvOption {
113
113
value,
114
114
relative : true ,
115
115
force : _,
116
- } => {
117
- let value = config_parent. as_ref ( ) . join ( value) ;
118
- let value = dunce:: canonicalize ( & value)
119
- . with_context ( || format ! ( "Failed to canonicalize `{}`" , value. display( ) ) ) ?;
120
- value
121
- . into_os_string ( )
122
- . into_string ( )
123
- . map_err ( VarError :: NotUnicode ) ?
124
- . into ( )
125
- }
116
+ } => config_parent
117
+ . as_ref ( )
118
+ . join ( value)
119
+ . into_os_string ( )
120
+ . into_string ( )
121
+ . map_err ( VarError :: NotUnicode ) ?
122
+ . into ( ) ,
126
123
Self :: String ( value) | Self :: Value { value, .. } => value. into ( ) ,
127
124
} )
128
125
}
@@ -225,46 +222,26 @@ CARGO_SUBCOMMAND_TEST_ENV_FORCED = { value = "forced", force = true }"#;
225
222
}
226
223
227
224
#[ test]
228
- fn test_env_canonicalization ( ) {
229
- use std:: ffi:: OsStr ;
230
-
225
+ fn test_env_relative ( ) {
231
226
let toml = r#"
232
227
[env]
233
228
CARGO_SUBCOMMAND_TEST_ENV_SRC_DIR = { value = "src", force = true, relative = true }
234
229
"# ;
235
230
236
231
let config = LocalizedConfig {
237
232
config : toml:: from_str :: < Config > ( toml) . unwrap ( ) ,
238
- workspace : PathBuf :: new ( ) ,
233
+ // Path does not have to exist
234
+ workspace : PathBuf :: from ( "my/work/space" ) ,
239
235
} ;
240
236
241
237
config. set_env_vars ( ) . unwrap ( ) ;
242
238
243
239
let path = std:: env:: var ( "CARGO_SUBCOMMAND_TEST_ENV_SRC_DIR" )
244
- . expect ( "Canonicalization for a known-to-exist ./src folder should not fail " ) ;
240
+ . expect ( "Relative env var should always be set " ) ;
245
241
let path = PathBuf :: from ( path) ;
246
- assert ! ( path. is_absolute( ) ) ;
247
- assert ! ( path. is_dir( ) ) ;
248
- assert_eq ! ( path. file_name( ) , Some ( OsStr :: new( "src" ) ) ) ;
249
-
250
- let toml = r#"
251
- [env]
252
- CARGO_SUBCOMMAND_TEST_ENV_INEXISTENT_DIR = { value = "blahblahthisfolderdoesntexist", force = true, relative = true }
253
- "# ;
254
-
255
- let config = LocalizedConfig {
256
- config : toml:: from_str :: < Config > ( toml) . unwrap ( ) ,
257
- workspace : PathBuf :: new ( ) ,
258
- } ;
259
-
260
- let e = config. set_env_vars ( ) . unwrap_err ( ) ;
261
-
262
- assert_eq ! (
263
- e. to_string( ) ,
264
- "Failed to canonicalize `blahblahthisfolderdoesntexist`"
265
- ) ;
266
- assert_eq ! (
267
- e. root_cause( ) . to_string( ) ,
268
- "No such file or directory (os error 2)"
242
+ assert ! (
243
+ path. is_relative( ) ,
244
+ "Workspace was not absolute so this shouldn't either"
269
245
) ;
246
+ assert_eq ! ( path, Path :: new( "my/work/space/src" ) ) ;
270
247
}
0 commit comments