@@ -76,7 +76,7 @@ pub(crate) fn cargo_config_env(
76
76
// if successful we receive `env.key.value = "value" per entry
77
77
tracing:: debug!( "Discovering cargo config env by {:?}" , cargo_config) ;
78
78
utf8_stdout ( & mut cargo_config)
79
- . map ( |stdout| parse_output_cargo_config_env ( manifest, stdout) )
79
+ . map ( |stdout| parse_output_cargo_config_env ( manifest, & stdout) )
80
80
. inspect ( |env| {
81
81
tracing:: debug!( "Discovered cargo config env: {:?}" , env) ;
82
82
} )
@@ -86,7 +86,7 @@ pub(crate) fn cargo_config_env(
86
86
. unwrap_or_default ( )
87
87
}
88
88
89
- fn parse_output_cargo_config_env ( manifest : & ManifestPath , stdout : String ) -> Env {
89
+ fn parse_output_cargo_config_env ( manifest : & ManifestPath , stdout : & str ) -> Env {
90
90
let mut env = Env :: default ( ) ;
91
91
let mut relatives = vec ! [ ] ;
92
92
for ( key, val) in
@@ -112,12 +112,35 @@ fn parse_output_cargo_config_env(manifest: &ManifestPath, stdout: String) -> Env
112
112
// FIXME: The base here should be the parent of the `.cargo/config` file, not the manifest.
113
113
// But cargo does not provide this information.
114
114
let base = <_ as AsRef < Utf8Path > >:: as_ref ( manifest. parent ( ) ) ;
115
- for ( key, val) in relatives {
116
- if let Some ( val) = env. get ( & val) {
117
- env. insert ( key, base. join ( val) . to_string ( ) ) ;
118
- } else {
119
- env. insert ( key, base. to_string ( ) ) ;
115
+ for ( key, relative) in relatives {
116
+ if relative != "true" {
117
+ continue ;
118
+ }
119
+ if let Some ( suffix) = env. get ( key) {
120
+ env. insert ( key, base. join ( suffix) . to_string ( ) ) ;
120
121
}
121
122
}
122
123
env
123
124
}
125
+
126
+ #[ test]
127
+ fn parse_output_cargo_config_env_works ( ) {
128
+ let stdout = r#"
129
+ env.CARGO_WORKSPACE_DIR.relative = true
130
+ env.CARGO_WORKSPACE_DIR.value = ""
131
+ env.RELATIVE.relative = true
132
+ env.RELATIVE.value = "../relative"
133
+ env.INVALID.relative = invalidbool
134
+ env.INVALID.value = "../relative"
135
+ env.TEST.value = "test"
136
+ "#
137
+ . trim ( ) ;
138
+ let cwd = paths:: Utf8PathBuf :: try_from ( std:: env:: current_dir ( ) . unwrap ( ) ) . unwrap ( ) ;
139
+ let manifest = paths:: AbsPathBuf :: assert ( cwd. join ( "Cargo.toml" ) ) ;
140
+ let manifest = ManifestPath :: try_from ( manifest) . unwrap ( ) ;
141
+ let env = parse_output_cargo_config_env ( & manifest, stdout) ;
142
+ assert_eq ! ( env. get( "CARGO_WORKSPACE_DIR" ) . as_deref( ) , Some ( cwd. join( "" ) . as_str( ) ) ) ;
143
+ assert_eq ! ( env. get( "RELATIVE" ) . as_deref( ) , Some ( cwd. join( "../relative" ) . as_str( ) ) ) ;
144
+ assert_eq ! ( env. get( "INVALID" ) . as_deref( ) , Some ( "../relative" ) ) ;
145
+ assert_eq ! ( env. get( "TEST" ) . as_deref( ) , Some ( "test" ) ) ;
146
+ }
0 commit comments