Skip to content

Commit df94e73

Browse files
committed
Propagate type_ to sub-sources as to correctly determine 'dev' dependencies
1 parent 8c1fa02 commit df94e73

File tree

2 files changed

+77
-12
lines changed

2 files changed

+77
-12
lines changed

src/bsconfig.rs

Lines changed: 67 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -27,6 +27,13 @@ pub struct PackageSource {
2727
pub type_: Option<String>,
2828
}
2929

30+
pub fn get_type(s: &Source) -> Option<String> {
31+
match s {
32+
Source::Shorthand(_) => None,
33+
Source::Qualified(PackageSource { type_, .. }) => type_.clone(),
34+
}
35+
}
36+
3037
/// `to_qualified_without_children` takes a tree like structure of dependencies, coming in from
3138
/// `bsconfig`, and turns it into a flat list. The main thing we extract here are the source
3239
/// folders, and optional subdirs, where potentially, the subdirs recurse or not.
@@ -39,7 +46,7 @@ pub fn to_qualified_without_children(s: &Source, sub_path: Option<PathBuf>) -> P
3946
.to_string_lossy()
4047
.to_string(),
4148
subdirs: None,
42-
type_: None,
49+
type_: get_type(s),
4350
},
4451
Source::Qualified(PackageSource {
4552
dir,
@@ -74,6 +81,31 @@ pub enum Source {
7481
Shorthand(String),
7582
Qualified(PackageSource),
7683
}
84+
85+
impl Source {
86+
/// When reading, we should propagate the sources all the way through the tree
87+
pub fn get_type(&self) -> Option<String> {
88+
match self {
89+
Source::Shorthand(_) => None,
90+
Source::Qualified(PackageSource { type_, .. }) => type_.clone(),
91+
}
92+
}
93+
pub fn set_type(&self, type_: Option<String>) -> Source {
94+
match (self, type_) {
95+
(Source::Shorthand(dir), Some(type_)) => Source::Qualified(PackageSource {
96+
dir: dir.to_string(),
97+
subdirs: None,
98+
type_: Some(type_),
99+
}),
100+
(Source::Qualified(package_source), type_) => Source::Qualified(PackageSource {
101+
type_,
102+
..package_source.clone()
103+
}),
104+
(source, _) => source.clone(),
105+
}
106+
}
107+
}
108+
77109
impl Eq for Source {}
78110

79111
#[derive(Deserialize, Debug, Clone)]
@@ -412,6 +444,39 @@ mod tests {
412444
assert_eq!(config.get_module(), "es6");
413445
}
414446

447+
#[test]
448+
fn test_sources() {
449+
let json = r#"
450+
{
451+
"name": "@rescript/core",
452+
"version": "0.5.0",
453+
"sources": {
454+
"dir": "test",
455+
"subdirs": ["intl"],
456+
"type": "dev"
457+
},
458+
"suffix": ".mjs",
459+
"package-specs": {
460+
"module": "esmodule",
461+
"in-source": true
462+
},
463+
"bs-dev-dependencies": ["@rescript/tools"],
464+
"warnings": {
465+
"error": "+101"
466+
}
467+
}
468+
"#;
469+
470+
let config = serde_json::from_str::<Config>(json).unwrap();
471+
if let OneOrMore::Single(source) = config.sources {
472+
let source = to_qualified_without_children(&source, None);
473+
assert_eq!(source.type_, Some(String::from("dev")));
474+
} else {
475+
dbg!(config.sources);
476+
unreachable!()
477+
}
478+
}
479+
415480
#[test]
416481
fn test_detect_gentypeconfig() {
417482
let json = r#"
@@ -430,7 +495,7 @@ mod tests {
430495
"#;
431496

432497
let config = serde_json::from_str::<Config>(json).unwrap();
433-
assert_eq!(config.gentype_config.is_some(), true);
498+
assert!(config.gentype_config.is_some());
434499
assert_eq!(config.get_gentype_arg(), vec!["-bs-gentype".to_string()]);
435500
}
436501

src/build/packages.rs

Lines changed: 10 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -184,6 +184,9 @@ pub fn read_folders(
184184
fn get_source_dirs(source: bsconfig::Source, sub_path: Option<PathBuf>) -> AHashSet<bsconfig::PackageSource> {
185185
let mut source_folders: AHashSet<bsconfig::PackageSource> = AHashSet::new();
186186

187+
let source_folder = bsconfig::to_qualified_without_children(&source, sub_path.to_owned());
188+
source_folders.insert(source_folder.to_owned());
189+
187190
let (subdirs, full_recursive) = match source.to_owned() {
188191
bsconfig::Source::Shorthand(_)
189192
| bsconfig::Source::Qualified(bsconfig::PackageSource { subdirs: None, .. }) => (None, false),
@@ -197,15 +200,12 @@ fn get_source_dirs(source: bsconfig::Source, sub_path: Option<PathBuf>) -> AHash
197200
}) => (Some(subdirs), false),
198201
};
199202

200-
let source_folder = bsconfig::to_qualified_without_children(&source, sub_path.to_owned());
201-
source_folders.insert(source_folder.to_owned());
202-
203203
if !full_recursive {
204204
let sub_path = Path::new(&source_folder.dir).to_path_buf();
205205
subdirs
206206
.unwrap_or(vec![])
207207
.par_iter()
208-
.map(|subdir| get_source_dirs(subdir.to_owned(), Some(sub_path.to_owned())))
208+
.map(|subsource| get_source_dirs(subsource.set_type(source.get_type()), Some(sub_path.to_owned())))
209209
.collect::<Vec<AHashSet<bsconfig::PackageSource>>>()
210210
.into_iter()
211211
.for_each(|subdir| source_folders.extend(subdir))
@@ -453,12 +453,12 @@ pub fn get_source_files(
453453
if type_ != &Some("dev".to_string()) {
454454
match read_folders(filter, package_dir, path_dir, recurse) {
455455
Ok(files) => map.extend(files),
456-
Err(_e) if type_ == &Some("dev".to_string()) => {
457-
log::warn!(
458-
"Could not read folder: {}... Probably ok as type is dev",
459-
path_dir.to_string_lossy()
460-
)
461-
}
456+
// Err(_e) if type_ == &Some("dev".to_string()) => {
457+
// log::warn!(
458+
// "Could not read folder: {}... Probably ok as type is dev",
459+
// path_dir.to_string_lossy()
460+
// )
461+
// }
462462
Err(_e) => log::error!(
463463
"Could not read folder: {:?}. Specified in dependency: {}, located {:?}...",
464464
path_dir.to_path_buf().into_os_string(),

0 commit comments

Comments
 (0)