Skip to content
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

feat: Allow to discover additional config file names #42

Merged
merged 4 commits into from
Feb 26, 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
52 changes: 44 additions & 8 deletions src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -501,6 +501,7 @@ impl ConfigFile {
config_flag: &ConfigFlag,
maybe_config_path_args: Option<Vec<PathBuf>>,
cwd: &Path,
additional_config_file_names: Option<Vec<&str>>,
) -> Result<Option<ConfigFile>, AnyError> {
match config_flag {
ConfigFlag::Disabled => Ok(None),
Expand All @@ -517,12 +518,20 @@ impl ConfigFile {
if let Some(config_path_args) = maybe_config_path_args {
let mut checked = HashSet::new();
for f in config_path_args {
if let Some(cf) = Self::discover_from(&f, &mut checked)? {
if let Some(cf) = Self::discover_from(
&f,
&mut checked,
additional_config_file_names.as_ref(),
)? {
return Ok(Some(cf));
}
}
// From CWD walk up to root looking for deno.json or deno.jsonc
Self::discover_from(cwd, &mut checked)
Self::discover_from(
cwd,
&mut checked,
additional_config_file_names.as_ref(),
)
} else {
Ok(None)
}
Expand All @@ -533,6 +542,7 @@ impl ConfigFile {
pub fn discover_from(
start: &Path,
checked: &mut HashSet<PathBuf>,
additional_config_file_names: Option<&Vec<&str>>,
) -> Result<Option<ConfigFile>, AnyError> {
fn is_skippable_err(e: &AnyError) -> bool {
if let Some(ioerr) = e.downcast_ref::<std::io::Error>() {
Expand All @@ -554,6 +564,15 @@ impl ConfigFile {

/// Filenames that Deno will recognize when discovering config.
const CONFIG_FILE_NAMES: [&str; 2] = ["deno.json", "deno.jsonc"];
let config_file_names =
if let Some(additional) = additional_config_file_names {
CONFIG_FILE_NAMES
.into_iter()
.chain(additional.to_vec())
.collect::<Vec<_>>()
} else {
CONFIG_FILE_NAMES.to_vec()
};

// todo(dsherret): in the future, we should force all callers
// to provide a resolved path
Expand All @@ -565,7 +584,7 @@ impl ConfigFile {

for ancestor in start.ancestors() {
if checked.insert(ancestor.to_path_buf()) {
for config_filename in CONFIG_FILE_NAMES {
for config_filename in &config_file_names {
let f = ancestor.join(config_filename);
match ConfigFile::read(&f) {
Ok(cf) => {
Expand Down Expand Up @@ -1976,7 +1995,7 @@ mod tests {
let testdata = testdata_path();
let c_md = testdata.join("fmt/with_config/subdir/c.md");
let mut checked = HashSet::new();
let config_file = ConfigFile::discover_from(&c_md, &mut checked)
let config_file = ConfigFile::discover_from(&c_md, &mut checked, None)
.unwrap()
.unwrap();
assert!(checked.contains(c_md.parent().unwrap()));
Expand All @@ -1998,17 +2017,34 @@ mod tests {
}

// If we call discover_from again starting at testdata, we ought to get None.
assert!(ConfigFile::discover_from(testdata.as_path(), &mut checked)
.unwrap()
.is_none());
assert!(
ConfigFile::discover_from(testdata.as_path(), &mut checked, None)
.unwrap()
.is_none()
);
}

#[test]
fn discover_from_additional_config_file_names_success() {
let testdata = testdata_path();
let dir = testdata.join("additional_files/");
let mut checked = HashSet::new();
let config_file =
ConfigFile::discover_from(&dir, &mut checked, Some(&vec!["jsr.json"]))
.unwrap()
.unwrap();
assert!(checked.contains(&dir));
assert!(!checked.contains(testdata.as_path()));
assert_eq!(config_file.json.name.unwrap(), "@foo/bar");
}

#[test]
fn discover_from_malformed() {
let testdata = testdata_path();
let d = testdata.join("malformed_config/");
let mut checked = HashSet::new();
let err = ConfigFile::discover_from(d.as_path(), &mut checked).unwrap_err();
let err =
ConfigFile::discover_from(d.as_path(), &mut checked, None).unwrap_err();
assert!(err.to_string().contains("Unable to parse config file"));
}

Expand Down
3 changes: 3 additions & 0 deletions testdata/additional_files/jsr.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
{
"name": "@foo/bar"
}
Loading