@@ -11,6 +11,7 @@ use anyhow::{Context, Result};
1111use arena:: { Arena , Idx } ;
1212use base_db:: Edition ;
1313use cargo_metadata:: { BuildScript , CargoOpt , Message , MetadataCommand , PackageId } ;
14+ use itertools:: Itertools ;
1415use paths:: { AbsPath , AbsPathBuf } ;
1516use rustc_hash:: FxHashMap ;
1617
@@ -192,6 +193,9 @@ impl CargoWorkspace {
192193
193194 meta. packages . sort_by ( |a, b| a. id . cmp ( & b. id ) ) ;
194195 for meta_pkg in meta. packages {
196+ let id = meta_pkg. id . clone ( ) ;
197+ inject_cargo_env ( & meta_pkg, envs. entry ( id) . or_default ( ) ) ;
198+
195199 let cargo_metadata:: Package { id, edition, name, manifest_path, version, .. } =
196200 meta_pkg;
197201 let is_member = ws_members. contains ( & id) ;
@@ -385,3 +389,38 @@ fn is_dylib(path: &Path) -> bool {
385389 Some ( ext) => matches ! ( ext. as_str( ) , "dll" | "dylib" | "so" ) ,
386390 }
387391}
392+
393+ /// Recreates the compile-time environment variables that Cargo sets.
394+ ///
395+ /// Should be synced with <https://doc.rust-lang.org/cargo/reference/environment-variables.html#environment-variables-cargo-sets-for-crates>
396+ fn inject_cargo_env ( package : & cargo_metadata:: Package , env : & mut Vec < ( String , String ) > ) {
397+ // FIXME: Missing variables:
398+ // CARGO, CARGO_PKG_HOMEPAGE, CARGO_CRATE_NAME, CARGO_BIN_NAME, CARGO_BIN_EXE_<name>
399+
400+ let mut manifest_dir = package. manifest_path . clone ( ) ;
401+ manifest_dir. pop ( ) ;
402+ if let Some ( cargo_manifest_dir) = manifest_dir. to_str ( ) {
403+ env. push ( ( "CARGO_MANIFEST_DIR" . into ( ) , cargo_manifest_dir. into ( ) ) ) ;
404+ }
405+
406+ env. push ( ( "CARGO_PKG_VERSION" . into ( ) , package. version . to_string ( ) ) ) ;
407+ env. push ( ( "CARGO_PKG_VERSION_MAJOR" . into ( ) , package. version . major . to_string ( ) ) ) ;
408+ env. push ( ( "CARGO_PKG_VERSION_MINOR" . into ( ) , package. version . minor . to_string ( ) ) ) ;
409+ env. push ( ( "CARGO_PKG_VERSION_PATCH" . into ( ) , package. version . patch . to_string ( ) ) ) ;
410+
411+ let pre = package. version . pre . iter ( ) . map ( |id| id. to_string ( ) ) . format ( "." ) ;
412+ env. push ( ( "CARGO_PKG_VERSION_PRE" . into ( ) , pre. to_string ( ) ) ) ;
413+
414+ let authors = package. authors . join ( ";" ) ;
415+ env. push ( ( "CARGO_PKG_AUTHORS" . into ( ) , authors) ) ;
416+
417+ env. push ( ( "CARGO_PKG_NAME" . into ( ) , package. name . clone ( ) ) ) ;
418+ env. push ( ( "CARGO_PKG_DESCRIPTION" . into ( ) , package. description . clone ( ) . unwrap_or_default ( ) ) ) ;
419+ //env.push(("CARGO_PKG_HOMEPAGE".into(), package.homepage.clone().unwrap_or_default()));
420+ env. push ( ( "CARGO_PKG_REPOSITORY" . into ( ) , package. repository . clone ( ) . unwrap_or_default ( ) ) ) ;
421+ env. push ( ( "CARGO_PKG_LICENSE" . into ( ) , package. license . clone ( ) . unwrap_or_default ( ) ) ) ;
422+
423+ let license_file =
424+ package. license_file . as_ref ( ) . map ( |buf| buf. display ( ) . to_string ( ) ) . unwrap_or_default ( ) ;
425+ env. push ( ( "CARGO_PKG_LICENSE_FILE" . into ( ) , license_file) ) ;
426+ }
0 commit comments