Skip to content

Read project environment vars from config #8839

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Closed
wants to merge 1 commit into from
Closed
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
19 changes: 19 additions & 0 deletions src/cargo/core/compiler/custom_build.rs
Original file line number Diff line number Diff line change
Expand Up @@ -9,9 +9,11 @@ use crate::util::{self, internal, paths, profile};
use cargo_platform::Cfg;
use std::collections::hash_map::{Entry, HashMap};
use std::collections::{BTreeSet, HashSet};
use std::fs;
use std::path::{Path, PathBuf};
use std::str;
use std::sync::{Arc, Mutex};
use serde_json::Value;

const CARGO_WARNING: &str = "cargo:warning=";

Expand Down Expand Up @@ -208,6 +210,23 @@ fn build_work(cx: &mut Context<'_, '_>, unit: &Unit) -> CargoResult<Job> {
cmd.env("CARGO_MANIFEST_LINKS", links);
}

// Re-export environment variables declared in the project's environment.json
let env_path = Path::new(bcx.ws.root()).join("environment.json");
// TODO: cache the environment.json state with the Workspace so it doesn't
// need to be repeatedly read + parsed for each build script
if let Ok(env) = fs::read_to_string(env_path) {
let env: Value = serde_json::from_str(&env).expect("Failed to parse environment.json");
if let Some(env_map) = env.as_object() {
for key in env_map.keys() {
if let Some(env_value) = env[key].as_str() {
// Simply re-export as an actual environment variables so build scripts
// may continue to take their configuration from environment variables
cmd.env(key, env_value);
}
}
}
}

// Be sure to pass along all enabled features for this package, this is the
// last piece of statically known information that we have.
for feat in &unit.features {
Expand Down