Skip to content

Commit

Permalink
Propagate type_ to sub-sources as to correctly determine 'dev' depend…
Browse files Browse the repository at this point in the history
…encies
  • Loading branch information
rolandpeelen committed Nov 15, 2024
1 parent 8c1fa02 commit df94e73
Show file tree
Hide file tree
Showing 2 changed files with 77 additions and 12 deletions.
69 changes: 67 additions & 2 deletions src/bsconfig.rs
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,13 @@ pub struct PackageSource {
pub type_: Option<String>,
}

pub fn get_type(s: &Source) -> Option<String> {
match s {
Source::Shorthand(_) => None,
Source::Qualified(PackageSource { type_, .. }) => type_.clone(),
}
}

/// `to_qualified_without_children` takes a tree like structure of dependencies, coming in from
/// `bsconfig`, and turns it into a flat list. The main thing we extract here are the source
/// folders, and optional subdirs, where potentially, the subdirs recurse or not.
Expand All @@ -39,7 +46,7 @@ pub fn to_qualified_without_children(s: &Source, sub_path: Option<PathBuf>) -> P
.to_string_lossy()
.to_string(),
subdirs: None,
type_: None,
type_: get_type(s),
},
Source::Qualified(PackageSource {
dir,
Expand Down Expand Up @@ -74,6 +81,31 @@ pub enum Source {
Shorthand(String),
Qualified(PackageSource),
}

impl Source {
/// When reading, we should propagate the sources all the way through the tree
pub fn get_type(&self) -> Option<String> {
match self {
Source::Shorthand(_) => None,
Source::Qualified(PackageSource { type_, .. }) => type_.clone(),
}
}
pub fn set_type(&self, type_: Option<String>) -> Source {
match (self, type_) {
(Source::Shorthand(dir), Some(type_)) => Source::Qualified(PackageSource {
dir: dir.to_string(),
subdirs: None,
type_: Some(type_),
}),
(Source::Qualified(package_source), type_) => Source::Qualified(PackageSource {
type_,
..package_source.clone()
}),
(source, _) => source.clone(),
}
}
}

impl Eq for Source {}

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

#[test]
fn test_sources() {
let json = r#"
{
"name": "@rescript/core",
"version": "0.5.0",
"sources": {
"dir": "test",
"subdirs": ["intl"],
"type": "dev"
},
"suffix": ".mjs",
"package-specs": {
"module": "esmodule",
"in-source": true
},
"bs-dev-dependencies": ["@rescript/tools"],
"warnings": {
"error": "+101"
}
}
"#;

let config = serde_json::from_str::<Config>(json).unwrap();
if let OneOrMore::Single(source) = config.sources {
let source = to_qualified_without_children(&source, None);
assert_eq!(source.type_, Some(String::from("dev")));
} else {
dbg!(config.sources);
unreachable!()
}
}

#[test]
fn test_detect_gentypeconfig() {
let json = r#"
Expand All @@ -430,7 +495,7 @@ mod tests {
"#;

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

Expand Down
20 changes: 10 additions & 10 deletions src/build/packages.rs
Original file line number Diff line number Diff line change
Expand Up @@ -184,6 +184,9 @@ pub fn read_folders(
fn get_source_dirs(source: bsconfig::Source, sub_path: Option<PathBuf>) -> AHashSet<bsconfig::PackageSource> {
let mut source_folders: AHashSet<bsconfig::PackageSource> = AHashSet::new();

let source_folder = bsconfig::to_qualified_without_children(&source, sub_path.to_owned());
source_folders.insert(source_folder.to_owned());

let (subdirs, full_recursive) = match source.to_owned() {
bsconfig::Source::Shorthand(_)
| bsconfig::Source::Qualified(bsconfig::PackageSource { subdirs: None, .. }) => (None, false),
Expand All @@ -197,15 +200,12 @@ fn get_source_dirs(source: bsconfig::Source, sub_path: Option<PathBuf>) -> AHash
}) => (Some(subdirs), false),
};

let source_folder = bsconfig::to_qualified_without_children(&source, sub_path.to_owned());
source_folders.insert(source_folder.to_owned());

if !full_recursive {
let sub_path = Path::new(&source_folder.dir).to_path_buf();
subdirs
.unwrap_or(vec![])
.par_iter()
.map(|subdir| get_source_dirs(subdir.to_owned(), Some(sub_path.to_owned())))
.map(|subsource| get_source_dirs(subsource.set_type(source.get_type()), Some(sub_path.to_owned())))
.collect::<Vec<AHashSet<bsconfig::PackageSource>>>()
.into_iter()
.for_each(|subdir| source_folders.extend(subdir))
Expand Down Expand Up @@ -453,12 +453,12 @@ pub fn get_source_files(
if type_ != &Some("dev".to_string()) {
match read_folders(filter, package_dir, path_dir, recurse) {
Ok(files) => map.extend(files),
Err(_e) if type_ == &Some("dev".to_string()) => {
log::warn!(
"Could not read folder: {}... Probably ok as type is dev",
path_dir.to_string_lossy()
)
}
// Err(_e) if type_ == &Some("dev".to_string()) => {
// log::warn!(
// "Could not read folder: {}... Probably ok as type is dev",
// path_dir.to_string_lossy()
// )
// }
Err(_e) => log::error!(
"Could not read folder: {:?}. Specified in dependency: {}, located {:?}...",
path_dir.to_path_buf().into_os_string(),
Expand Down

0 comments on commit df94e73

Please sign in to comment.