Skip to content
Open
Show file tree
Hide file tree
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
Original file line number Diff line number Diff line change
Expand Up @@ -180,6 +180,7 @@ fn extract_schema_documents_for_resolvers<'a>(
let mut errors = vec![];
let mut type_asts = vec![];
let mut field_asts_and_definitions = FxHashMap::default();
println!("docblock_ast_sources: {:?}", docblock_ast_sources);

if let (Some(docblocks), Some(graphql_asts)) = docblock_ast_sources {
for (file_path, docblock_sources) in docblocks.get_all() {
Expand Down
90 changes: 42 additions & 48 deletions compiler/crates/relay-compiler/src/config.rs
Original file line number Diff line number Diff line change
Expand Up @@ -76,6 +76,7 @@ use crate::compiler_state::ProjectSet;
use crate::errors::ConfigValidationError;
use crate::errors::Error;
use crate::errors::Result;
use crate::path_validator::PathValidator;
use crate::source_control_for_root;
use crate::status_reporter::ConsoleStatusReporter;
use crate::status_reporter::StatusReporter;
Expand Down Expand Up @@ -344,6 +345,14 @@ impl Config {
}
};

let config_file_dir = config_path.parent().unwrap();

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

let MultiProjectConfigFile {
feature_flags: config_file_feature_flags,
projects,
Expand All @@ -354,8 +363,12 @@ impl Config {
.map(|(project_name, config_file_project)| {
let schema_location =
match (config_file_project.schema, config_file_project.schema_dir) {
(Some(schema_file), None) => Ok(SchemaLocation::File(schema_file)),
(None, Some(schema_dir)) => Ok(SchemaLocation::Directory(schema_dir)),
(Some(schema_file), None) => Ok(SchemaLocation::File(
normalize_relative_path(&root_dir, &schema_file),
)),
(None, Some(schema_dir)) => Ok(SchemaLocation::Directory(
normalize_relative_path(&root_dir, &schema_dir),
)),
_ => Err(Error::ConfigFileValidation {
config_path: config_path.clone(),
validation_errors: vec![
Expand Down Expand Up @@ -407,7 +420,11 @@ impl Config {
name: project_name,
base: config_file_project.base,
enabled: true,
schema_extensions: config_file_project.schema_extensions,
schema_extensions: config_file_project
.schema_extensions
.into_iter()
.map(|extension_dir| normalize_relative_path(&root_dir, &extension_dir))
.collect(),
extra_artifacts_config: None,
extra: config_file_project.extra,
excludes_extensions: excludes_extensions_set,
Expand Down Expand Up @@ -440,14 +457,6 @@ impl Config {
})
.collect::<Result<FnvIndexMap<_, _>>>()?;

let config_file_dir = config_path.parent().unwrap();

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

let config = Self {
name: config_file.name,
artifact_writer: Box::new(ArtifactFileWriter::new(
Expand Down Expand Up @@ -558,7 +567,7 @@ impl Config {
}

/// Validates that all paths actually exist on disk.
fn validate_paths(&self, errors: &mut Vec<ConfigValidationError>) {
pub fn validate_paths(&self, errors: &mut Vec<ConfigValidationError>) {
if !self.root_dir.is_dir() {
errors.push(ConfigValidationError::RootNotDirectory {
root_dir: self.root_dir.clone(),
Expand All @@ -567,52 +576,31 @@ impl Config {
return;
}

let mut validator = PathValidator::new(self.root_dir.clone(), &self.excludes);

// each source should point to an existing directory
for source_dir in self.sources.keys() {
let abs_source_dir = self.root_dir.join(source_dir);
if !abs_source_dir.exists() {
errors.push(ConfigValidationError::SourceNotExistent {
source_dir: abs_source_dir.clone(),
});
} else if !abs_source_dir.is_dir() {
errors.push(ConfigValidationError::SourceNotDirectory {
source_dir: abs_source_dir.clone(),
});
}
validator.assert_is_included_source_dir(source_dir, "source");
}

for (&project_name, project) in &self.projects {
for (_, project) in &self.projects {
match &project.schema_location {
SchemaLocation::File(schema_file) => {
let abs_schema_file = self.root_dir.join(schema_file);
if !abs_schema_file.exists() {
errors.push(ConfigValidationError::SchemaFileNotExistent {
project_name,
schema_file: abs_schema_file.clone(),
});
} else if !abs_schema_file.is_file() {
errors.push(ConfigValidationError::SchemaFileNotFile {
project_name,
schema_file: abs_schema_file.clone(),
});
}
validator.assert_is_included_schema_file(schema_file, "schema file");
}
SchemaLocation::Directory(schema_dir) => {
let abs_schema_dir = self.root_dir.join(schema_dir);
if !abs_schema_dir.exists() {
errors.push(ConfigValidationError::SchemaDirNotExistent {
project_name,
schema_dir: abs_schema_dir.clone(),
});
} else if !abs_schema_dir.is_dir() {
errors.push(ConfigValidationError::SchemaDirNotDirectory {
project_name,
schema_dir: abs_schema_dir.clone(),
});
}
validator.assert_is_included_schema_dir(schema_dir, "schema directory");
}
}

// Validate schema extensions
for extension_path in &project.schema_extensions {
validator
.assert_is_included_schema_dir(extension_path, "schema extension directory");
}
}

errors.extend(validator.into_errors());
}

/// Compute all root paths that we need to query. All files relevant to the
Expand Down Expand Up @@ -748,6 +736,12 @@ mod test {
}
}

fn normalize_relative_path(root_dir: &Path, path: &PathBuf) -> PathBuf {
let absolute = root_dir.join(path);

absolute.strip_prefix(root_dir).unwrap().to_path_buf()
}

impl fmt::Debug for Config {
fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
let Config {
Expand Down Expand Up @@ -902,7 +896,7 @@ pub struct SingleProjectConfigFile {

/// Directories to ignore under src
/// default: ['**/node_modules/**', '**/__mocks__/**', '**/__generated__/**'],
#[serde(alias = "exclude")]
#[serde(default = "get_default_excludes")]
pub excludes: Vec<String>,

/// List of directories with schema extensions.
Expand Down
47 changes: 23 additions & 24 deletions compiler/crates/relay-compiler/src/errors.rs
Original file line number Diff line number Diff line change
Expand Up @@ -207,22 +207,6 @@ pub enum ConfigValidationError {
schema_file: PathBuf,
},

#[error(
"The `schema` configured for project `{project_name}` to be `{schema_file}` is not a file."
)]
SchemaFileNotFile {
project_name: ProjectName,
schema_file: PathBuf,
},

#[error(
"The `schema_dir` configured for project `{project_name}` does not exist at `{schema_dir}`."
)]
SchemaDirNotExistent {
project_name: ProjectName,
schema_dir: PathBuf,
},

#[error(
"The `schemaExtensions` configured for project `{project_name}` does not exist at `{extension_path}`."
)]
Expand All @@ -231,14 +215,6 @@ pub enum ConfigValidationError {
extension_path: PathBuf,
},

#[error(
"The `schema_dir` configured for project `{project_name}` to be `{schema_dir}` is not a directory."
)]
SchemaDirNotDirectory {
project_name: ProjectName,
schema_dir: PathBuf,
},

#[error("The regex in `{key}` for project `{project_name}` is invalid.\n {error}.")]
InvalidRegex {
key: &'static str,
Expand All @@ -257,6 +233,29 @@ pub enum ConfigValidationError {
name: &'static str,
action: &'static str,
},

#[error("The `{file_type}` at `{path}` is not within the project root `{project_root}`.")]
FileNotInRoot {
file_type: String,
path: PathBuf,
project_root: PathBuf,
},

#[error("The `{file_type}` at `{path}` matches an exclude pattern `{pattern}`.")]
FileMatchesExclude {
file_type: String,
path: PathBuf,
pattern: String,
},

#[error("The `{file_type}` at `{path}` does not exist.")]
FileNotExistent { file_type: String, path: PathBuf },

#[error("The `{file_type}` at `{path}` is not a directory.")]
FileNotDirectory { file_type: String, path: PathBuf },

#[error("The `{file_type}` at `{path}` is not a file.")]
FileNotFile { file_type: String, path: PathBuf },
}

#[derive(Debug, Error, serde::Serialize)]
Expand Down
21 changes: 10 additions & 11 deletions compiler/crates/relay-compiler/src/file_source/file_categorizer.rs
Original file line number Diff line number Diff line change
Expand Up @@ -116,18 +116,16 @@ fn categorize_non_watchman_files(
.par_iter()
.filter(|file| file_filter.is_file_relevant(&file.name))
.filter_map(|file| {
let file_group = categorizer
.categorize(&file.name, config)
.map_err(|err| {
warn!(
"Unexpected error in file categorizer for file `{}`: {}.",
file.name.to_string_lossy(),
err
);
let file_group = categorizer.categorize(&file.name, config).map_err(|err| {
warn!(
"Unexpected error in file categorizer for file `{}`: {}.",
file.name.to_string_lossy(),
err
})
.ok()?;
Some((file_group, file.clone()))
);
err
});

Some((file_group.ok()?, file.clone()))
})
.collect::<Vec<_>>()
}
Expand Down Expand Up @@ -245,6 +243,7 @@ impl FileCategorizer {
/// `FileCategorizer`.
pub fn categorize(&self, path: &Path, config: &Config) -> Result<FileGroup, Cow<'static, str>> {
let extension = path.extension();
// TODO: Here

let in_generated_sources = self
.generated_sources
Expand Down
1 change: 1 addition & 0 deletions compiler/crates/relay-compiler/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,7 @@ pub mod file_source;
mod get_programs;
mod graphql_asts;
mod operation_persister;
mod path_validator;
mod red_to_green;
pub mod status_reporter;
mod utils;
Expand Down
Loading
Loading