Skip to content

Commit

Permalink
move root setting into config
Browse files Browse the repository at this point in the history
Summary:
The config path and root of the config are tightly coupled. This change makes it
so that the config has an optional `root` field to set the root of the compiler.

By default, the directory of the config is the root, otherwise the relative path
to the root is used.

Reviewed By: josephsavona

Differential Revision: D22498223

fbshipit-source-id: b857dfa77c9f815802a4b96dc07a4111af717aff
  • Loading branch information
kassens authored and facebook-github-bot committed Jul 13, 2020
1 parent ef7398f commit 00b1a1f
Show file tree
Hide file tree
Showing 3 changed files with 18 additions and 15 deletions.
25 changes: 16 additions & 9 deletions compiler/crates/relay-compiler/src/config.rs
Original file line number Diff line number Diff line change
Expand Up @@ -54,33 +54,27 @@ impl Config {
.filter(|project_config| project_config.enabled)
}

pub fn load(root_dir: PathBuf, config_path: PathBuf) -> Result<Self> {
pub fn load(config_path: PathBuf) -> Result<Self> {
let config_string =
std::fs::read_to_string(&config_path).map_err(|err| Error::ConfigFileRead {
config_path: config_path.clone(),
source: err,
})?;
Self::from_string(root_dir, config_path, &config_string, true)
Self::from_string(config_path, &config_string, true)
}

/// Loads a config file without validation for use in tests.
#[cfg(test)]
pub fn from_string_for_test(config_string: &str) -> Result<Self> {
Self::from_string(
"/virtual/root".into(),
"/virtual/root/virtual_config.json".into(),
config_string,
false,
)
}

/// `validate_fs` disables all filesystem checks for existence of files
fn from_string(
root_dir: PathBuf,
config_path: PathBuf,
config_string: &str,
validate_fs: bool,
) -> Result<Self> {
fn from_string(config_path: PathBuf, config_string: &str, validate_fs: bool) -> Result<Self> {
let config_file: ConfigFile =
serde_json::from_str(&config_string).map_err(|err| Error::ConfigFileParse {
config_path: config_path.clone(),
Expand Down Expand Up @@ -136,6 +130,14 @@ impl Config {
Ok((project_name, project_config))
})
.collect::<Result<HashMap<_, _>>>()?;

let config_file_dir = config_path.parent().unwrap();
let root_dir = if let Some(config_root) = config_file.root {
config_file_dir.join(config_root).canonicalize().unwrap()
} else {
config_file_dir.to_owned()
};

let config = Self {
root_dir,
sources: config_file.sources,
Expand Down Expand Up @@ -315,6 +317,11 @@ pub enum SchemaLocation {
#[derive(Debug, Deserialize)]
#[serde(deny_unknown_fields, rename_all = "camelCase")]
struct ConfigFile {
/// Root directory relative to the config file. Defaults to the directory
/// where the config is located.
#[serde(default)]
root: Option<PathBuf>,

#[serde(default)]
header: Vec<String>,
#[serde(default)]
Expand Down
5 changes: 1 addition & 4 deletions compiler/crates/relay-compiler/src/main.rs
Original file line number Diff line number Diff line change
Expand Up @@ -23,9 +23,6 @@ struct Opt {

/// Path to the compiler config file
config: PathBuf,

/// Root directory of the project
root: PathBuf,
}

#[tokio::main]
Expand All @@ -34,7 +31,7 @@ async fn main() {

let opt = Opt::from_args();

let config = match Config::load(opt.root, opt.config) {
let config = match Config::load(opt.config) {
Ok(config) => config,
Err(err) => {
error!("{}", err);
Expand Down
3 changes: 1 addition & 2 deletions compiler/crates/relay-lsp/src/server.rs
Original file line number Diff line number Diff line change
Expand Up @@ -151,8 +151,7 @@ fn load_config() -> Config {
"{}/fbsource/fbcode/relay/config/config.test.json",
home
));
let root_dir = PathBuf::from(format!("{}/fbsource", home));
let mut config = Config::load(root_dir, config_path).unwrap();
let mut config = Config::load(config_path).unwrap();
// Don't write artifacts by default
config.write_artifacts = false;
config
Expand Down

0 comments on commit 00b1a1f

Please sign in to comment.