@@ -76,7 +76,7 @@ pub(crate) fn cargo_config_env(
7676 // if successful we receive `env.key.value = "value" per entry
7777 tracing:: debug!( "Discovering cargo config env by {:?}" , cargo_config) ;
7878 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) )
8080 . inspect ( |env| {
8181 tracing:: debug!( "Discovered cargo config env: {:?}" , env) ;
8282 } )
@@ -86,7 +86,7 @@ pub(crate) fn cargo_config_env(
8686 . unwrap_or_default ( )
8787}
8888
89- fn parse_output_cargo_config_env ( manifest : & ManifestPath , stdout : String ) -> Env {
89+ fn parse_output_cargo_config_env ( manifest : & ManifestPath , stdout : & str ) -> Env {
9090 let mut env = Env :: default ( ) ;
9191 let mut relatives = vec ! [ ] ;
9292 for ( key, val) in
@@ -112,12 +112,35 @@ fn parse_output_cargo_config_env(manifest: &ManifestPath, stdout: String) -> Env
112112 // FIXME: The base here should be the parent of the `.cargo/config` file, not the manifest.
113113 // But cargo does not provide this information.
114114 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 ( ) ) ;
120121 }
121122 }
122123 env
123124}
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