Skip to content

Commit 3a24702

Browse files
omarchehab98facebook-github-bot
authored andcommitted
Add extra_sources to project config
Reviewed By: voideanvalue, tyao1 Differential Revision: D56659647 fbshipit-source-id: 1912afd03b6dda5b99e38604fbca2a5190389d30
1 parent e9491a7 commit 3a24702

File tree

3 files changed

+80
-38
lines changed

3 files changed

+80
-38
lines changed

compiler/crates/relay-compiler/src/config.rs

Lines changed: 12 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -96,6 +96,10 @@ pub struct Config {
9696
pub root_dir: PathBuf,
9797
pub sources: FnvIndexMap<PathBuf, ProjectSet>,
9898
pub excludes: Vec<String>,
99+
/// Some projects may need to include extra source directories without being
100+
/// affected by exclusion globs from the `excludes` config (e.g. generated
101+
/// directories).
102+
pub generated_sources: FnvIndexMap<PathBuf, ProjectSet>,
99103
pub projects: FnvIndexMap<ProjectName, ProjectConfig>,
100104
pub header: Vec<String>,
101105
pub codegen_command: Option<String>,
@@ -422,6 +426,7 @@ impl Config {
422426
root_dir,
423427
sources: config_file.sources,
424428
excludes: config_file.excludes,
429+
generated_sources: config_file.generated_sources,
425430
projects,
426431
header: config_file.header,
427432
codegen_command: config_file.codegen_command,
@@ -580,6 +585,7 @@ impl fmt::Debug for Config {
580585
root_dir,
581586
sources,
582587
excludes,
588+
generated_sources,
583589
compile_everything,
584590
repersist_operations,
585591
projects,
@@ -604,6 +610,7 @@ impl fmt::Debug for Config {
604610
.field("root_dir", root_dir)
605611
.field("sources", sources)
606612
.field("excludes", excludes)
613+
.field("generated_sources", generated_sources)
607614
.field("compile_everything", compile_everything)
608615
.field("repersist_operations", repersist_operations)
609616
.field("projects", projects)
@@ -663,13 +670,17 @@ struct MultiProjectConfigFile {
663670
/// A mapping from directory paths (relative to the root) to a source set.
664671
/// If a path is a subdirectory of another path, the more specific path
665672
/// wins.
666-
sources: IndexMap<PathBuf, ProjectSet, fnv::FnvBuildHasher>,
673+
sources: FnvIndexMap<PathBuf, ProjectSet>,
667674

668675
/// Glob patterns that should not be part of the sources even if they are
669676
/// in the source set directories.
670677
#[serde(default = "get_default_excludes")]
671678
excludes: Vec<String>,
672679

680+
/// Similar to sources but not affected by excludes.
681+
#[serde(default)]
682+
generated_sources: FnvIndexMap<PathBuf, ProjectSet>,
683+
673684
/// Configuration of projects to compile.
674685
projects: FnvIndexMap<ProjectName, ConfigFileProject>,
675686

compiler/crates/relay-compiler/src/file_source/file_categorizer.rs

Lines changed: 7 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -149,10 +149,13 @@ impl FileCategorizer {
149149
for (path, project_set) in &config.sources {
150150
source_mapping.push((path.clone(), project_set.clone()));
151151
}
152+
for (path, project_set) in &config.generated_sources {
153+
source_mapping.push((path.clone(), project_set.clone()));
154+
}
152155

153156
let default_generated_dir = OsStr::new("__generated__");
154157
let mut generated_sources = vec![];
155-
for (path, _project_set) in &config.sources {
158+
for (path, _project_set) in &config.generated_sources {
156159
if in_relative_generated_dir(default_generated_dir, path) {
157160
generated_sources.push(path.clone());
158161
}
@@ -400,7 +403,9 @@ mod tests {
400403
"src/vendor": "public",
401404
"src/custom": "with_custom_generated_dir",
402405
"src/typescript": "typescript",
403-
"src/custom_overlapping": ["with_custom_generated_dir", "overlapping_generated_dir"],
406+
"src/custom_overlapping": ["with_custom_generated_dir", "overlapping_generated_dir"]
407+
},
408+
"generatedSources": {
404409
"src/resolver_codegen/__generated__": "public"
405410
},
406411
"projects": {

compiler/crates/relay-compiler/src/file_source/watchman_query_builder.rs

Lines changed: 61 additions & 35 deletions
Original file line numberDiff line numberDiff line change
@@ -7,48 +7,19 @@
77

88
use std::path::PathBuf;
99

10+
use fnv::FnvBuildHasher;
11+
use indexmap::IndexMap;
1012
use relay_typegen::TypegenLanguage;
1113
use watchman_client::prelude::*;
1214

15+
use crate::compiler_state::ProjectSet;
1316
use crate::config::Config;
1417
use crate::config::SchemaLocation;
1518

19+
type FnvIndexMap<K, V> = IndexMap<K, V, FnvBuildHasher>;
20+
1621
pub fn get_watchman_expr(config: &Config) -> Expr {
17-
let mut sources_conditions = vec![expr_any(
18-
config
19-
.sources
20-
.iter()
21-
.flat_map(|(path, project_set)| {
22-
project_set
23-
.iter()
24-
.map(|name| (path, &config.projects[name]))
25-
.collect::<Vec<_>>()
26-
})
27-
.map(|(path, project)| {
28-
Expr::All(vec![
29-
// Ending in *.js(x) or *.ts(x) depending on the project language.
30-
Expr::Suffix(match &project.typegen_config.language {
31-
TypegenLanguage::Flow | TypegenLanguage::JavaScript => {
32-
vec![PathBuf::from("js"), PathBuf::from("jsx")]
33-
}
34-
TypegenLanguage::TypeScript => {
35-
vec![
36-
PathBuf::from("js"),
37-
PathBuf::from("jsx"),
38-
PathBuf::from("ts"),
39-
PathBuf::from("tsx"),
40-
]
41-
}
42-
}),
43-
// In the related source root.
44-
Expr::DirName(DirNameTerm {
45-
path: path.clone(),
46-
depth: None,
47-
}),
48-
])
49-
})
50-
.collect(),
51-
)];
22+
let mut sources_conditions = vec![expr_any(get_sources_dir_exprs(config, &config.sources))];
5223
// not excluded by any glob
5324
if !config.excludes.is_empty() {
5425
sources_conditions.push(Expr::Not(Box::new(expr_any(
@@ -69,6 +40,11 @@ pub fn get_watchman_expr(config: &Config) -> Expr {
6940

7041
let mut expressions = vec![sources_expr];
7142

43+
let generated_sources_dir_exprs = get_sources_dir_exprs(config, &config.generated_sources);
44+
if !generated_sources_dir_exprs.is_empty() {
45+
expressions.push(expr_any(generated_sources_dir_exprs));
46+
}
47+
7248
let output_dir_paths = get_output_dir_paths(config);
7349
if !output_dir_paths.is_empty() {
7450
let output_dir_expr = expr_files_in_dirs(output_dir_paths);
@@ -103,17 +79,62 @@ pub fn get_watchman_expr(config: &Config) -> Expr {
10379
])
10480
}
10581

82+
fn get_sources_dir_exprs(
83+
config: &Config,
84+
paths_to_project: &FnvIndexMap<PathBuf, ProjectSet>,
85+
) -> Vec<Expr> {
86+
paths_to_project
87+
.iter()
88+
.flat_map(|(path, project_set)| {
89+
project_set
90+
.iter()
91+
.map(|name| (path, &config.projects[name]))
92+
.collect::<Vec<_>>()
93+
})
94+
.map(|(path, project)| {
95+
Expr::All(vec![
96+
// In the related source root.
97+
Expr::DirName(DirNameTerm {
98+
path: path.clone(),
99+
depth: None,
100+
}),
101+
// Match file extensions
102+
get_project_file_ext_expr(project.typegen_config.language),
103+
])
104+
})
105+
.collect()
106+
}
107+
108+
fn get_project_file_ext_expr(typegen_language: TypegenLanguage) -> Expr {
109+
// Ending in *.js(x) or *.ts(x) depending on the project language.
110+
Expr::Suffix(match &typegen_language {
111+
TypegenLanguage::Flow | TypegenLanguage::JavaScript => {
112+
vec![PathBuf::from("js"), PathBuf::from("jsx")]
113+
}
114+
TypegenLanguage::TypeScript => {
115+
vec![
116+
PathBuf::from("js"),
117+
PathBuf::from("jsx"),
118+
PathBuf::from("ts"),
119+
PathBuf::from("tsx"),
120+
]
121+
}
122+
})
123+
}
124+
106125
/// Compute all root paths that we need to query Watchman with. All files
107126
/// relevant to the compiler should be in these directories.
108127
pub fn get_all_roots(config: &Config) -> Vec<PathBuf> {
109128
let source_roots = get_source_roots(config);
129+
let extra_sources_roots = get_generated_sources_roots(config);
110130
let output_roots = get_output_dir_paths(config);
111131
let extension_roots = get_extension_roots(config);
112132
let schema_file_roots = get_schema_file_roots(config);
113133
let schema_dir_roots = get_schema_dir_paths(config);
114134
unify_roots(
115135
source_roots
116136
.into_iter()
137+
.chain(extra_sources_roots)
117138
.chain(output_roots)
118139
.chain(extension_roots)
119140
.chain(schema_file_roots)
@@ -127,6 +148,11 @@ fn get_source_roots(config: &Config) -> Vec<PathBuf> {
127148
config.sources.keys().cloned().collect()
128149
}
129150

151+
/// Returns all root directories of JS source files for the config.
152+
fn get_generated_sources_roots(config: &Config) -> Vec<PathBuf> {
153+
config.generated_sources.keys().cloned().collect()
154+
}
155+
130156
/// Returns all root directories of GraphQL schema extension files for the
131157
/// config.
132158
fn get_extension_roots(config: &Config) -> Vec<PathBuf> {

0 commit comments

Comments
 (0)