Skip to content

🐞 - Propagate type_ to sub-sources as to correctly determine 'dev' dependencies #140

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 1 commit into from
Nov 15, 2024
Merged
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
62 changes: 60 additions & 2 deletions src/bsconfig.rs
Original file line number Diff line number Diff line change
Expand Up @@ -39,7 +39,7 @@ pub fn to_qualified_without_children(s: &Source, sub_path: Option<PathBuf>) -> P
.to_string_lossy()
.to_string(),
subdirs: None,
type_: None,
type_: s.get_type(),
},
Source::Qualified(PackageSource {
dir,
Expand Down Expand Up @@ -74,6 +74,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 +437,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 +488,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
Loading