Skip to content

Commit dc86763

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

File tree

2 files changed

+70
-12
lines changed

2 files changed

+70
-12
lines changed

src/bsconfig.rs

Lines changed: 60 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -39,7 +39,7 @@ pub fn to_qualified_without_children(s: &Source, sub_path: Option<PathBuf>) -> P
3939
.to_string_lossy()
4040
.to_string(),
4141
subdirs: None,
42-
type_: None,
42+
type_: s.get_type(),
4343
},
4444
Source::Qualified(PackageSource {
4545
dir,
@@ -74,6 +74,31 @@ pub enum Source {
7474
Shorthand(String),
7575
Qualified(PackageSource),
7676
}
77+
78+
impl Source {
79+
/// When reading, we should propagate the sources all the way through the tree
80+
pub fn get_type(&self) -> Option<String> {
81+
match self {
82+
Source::Shorthand(_) => None,
83+
Source::Qualified(PackageSource { type_, .. }) => type_.clone(),
84+
}
85+
}
86+
pub fn set_type(&self, type_: Option<String>) -> Source {
87+
match (self, type_) {
88+
(Source::Shorthand(dir), Some(type_)) => Source::Qualified(PackageSource {
89+
dir: dir.to_string(),
90+
subdirs: None,
91+
type_: Some(type_),
92+
}),
93+
(Source::Qualified(package_source), type_) => Source::Qualified(PackageSource {
94+
type_,
95+
..package_source.clone()
96+
}),
97+
(source, _) => source.clone(),
98+
}
99+
}
100+
}
101+
77102
impl Eq for Source {}
78103

79104
#[derive(Deserialize, Debug, Clone)]
@@ -412,6 +437,39 @@ mod tests {
412437
assert_eq!(config.get_module(), "es6");
413438
}
414439

440+
#[test]
441+
fn test_sources() {
442+
let json = r#"
443+
{
444+
"name": "@rescript/core",
445+
"version": "0.5.0",
446+
"sources": {
447+
"dir": "test",
448+
"subdirs": ["intl"],
449+
"type": "dev"
450+
},
451+
"suffix": ".mjs",
452+
"package-specs": {
453+
"module": "esmodule",
454+
"in-source": true
455+
},
456+
"bs-dev-dependencies": ["@rescript/tools"],
457+
"warnings": {
458+
"error": "+101"
459+
}
460+
}
461+
"#;
462+
463+
let config = serde_json::from_str::<Config>(json).unwrap();
464+
if let OneOrMore::Single(source) = config.sources {
465+
let source = to_qualified_without_children(&source, None);
466+
assert_eq!(source.type_, Some(String::from("dev")));
467+
} else {
468+
dbg!(config.sources);
469+
unreachable!()
470+
}
471+
}
472+
415473
#[test]
416474
fn test_detect_gentypeconfig() {
417475
let json = r#"
@@ -430,7 +488,7 @@ mod tests {
430488
"#;
431489

432490
let config = serde_json::from_str::<Config>(json).unwrap();
433-
assert_eq!(config.gentype_config.is_some(), true);
491+
assert!(config.gentype_config.is_some());
434492
assert_eq!(config.get_gentype_arg(), vec!["-bs-gentype".to_string()]);
435493
}
436494

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)