use std::path::{Path, PathBuf};
use crate::FallowConfig;
#[derive(Debug, Clone, PartialEq, Eq)]
pub enum ConfigFixPlan {
Edit {
config_path: PathBuf,
},
BlockedMonorepo {
workspace_root: PathBuf,
},
BlockedConfigLink {
config_path: PathBuf,
},
BlockedNoCreate {
target: PathBuf,
},
Create {
target: PathBuf,
},
}
#[must_use]
pub fn classify_config_fix_plan(
root: &Path,
explicit: Option<&PathBuf>,
no_create_config: bool,
) -> ConfigFixPlan {
if let Some(existing) = resolve_existing_config_path(root, explicit) {
let is_link = existing
.symlink_metadata()
.is_ok_and(|meta| meta.file_type().is_symlink());
if is_link
&& !fallow_types::read_guard::is_inside_root(
&crate::project_scope_base(root),
&existing,
)
{
return ConfigFixPlan::BlockedConfigLink {
config_path: existing,
};
}
return ConfigFixPlan::Edit {
config_path: existing,
};
}
let target = root.join(".fallowrc.json");
match target.symlink_metadata() {
Ok(meta) if meta.file_type().is_symlink() => {
return ConfigFixPlan::BlockedConfigLink {
config_path: target,
};
}
Ok(_) => return ConfigFixPlan::BlockedNoCreate { target },
Err(_) => {}
}
if let Some(workspace_root) = find_workspace_root_above(root) {
return ConfigFixPlan::BlockedMonorepo { workspace_root };
}
if no_create_config {
return ConfigFixPlan::BlockedNoCreate { target };
}
ConfigFixPlan::Create { target }
}
#[must_use]
pub fn is_config_fixable(root: &Path, explicit: Option<&PathBuf>) -> bool {
matches!(
classify_config_fix_plan(root, explicit, false),
ConfigFixPlan::Edit { .. } | ConfigFixPlan::Create { .. }
)
}
fn resolve_existing_config_path(root: &Path, explicit: Option<&PathBuf>) -> Option<PathBuf> {
if let Some(path) = explicit {
let absolute = if path.is_absolute() {
path.clone()
} else {
std::env::current_dir().map_or_else(|_| path.clone(), |cwd| cwd.join(path))
};
if absolute.exists() {
return Some(absolute);
}
return None;
}
FallowConfig::find_config_path(root)
}
fn find_workspace_root_above(start: &Path) -> Option<PathBuf> {
let mut current = start.parent()?;
loop {
if has_workspace_marker(current) {
return Some(current.to_path_buf());
}
current = current.parent()?;
}
}
fn has_workspace_marker(dir: &Path) -> bool {
const SENTINELS: &[&str] = &[
"pnpm-workspace.yaml",
"turbo.json",
"lerna.json",
"rush.json",
];
for name in SENTINELS {
if dir.join(name).exists() {
return true;
}
}
if crate::workspace::load_root_deno_workspace_patterns(dir)
.ok()
.flatten()
.is_some_and(|(_path, patterns)| !patterns.is_empty())
{
return true;
}
let pkg_path = dir.join("package.json");
if !pkg_path.exists() {
return false;
}
let Ok(content) = fallow_types::read_guard::read_to_string_bounded(
&pkg_path,
fallow_types::read_guard::DEFAULT_MAX_READ_BYTES,
) else {
return false;
};
let Ok(value) = serde_json::from_str::<serde_json::Value>(&content) else {
return false;
};
value
.get("workspaces")
.is_some_and(|v| v.is_array() || v.is_object())
}
#[cfg(test)]
mod tests {
use super::*;
#[cfg(unix)]
#[test]
#[cfg_attr(miri, ignore = "tempdir is blocked by Miri isolation")]
fn a_config_link_out_of_the_root_is_neither_edited_nor_replaced() {
let dir = tempfile::tempdir().unwrap();
let root = dir.path().join("project");
std::fs::create_dir_all(root.join(".git")).unwrap();
std::fs::write(dir.path().join("outside.json"), "{}").unwrap();
std::os::unix::fs::symlink(dir.path().join("outside.json"), root.join(".fallowrc.json"))
.unwrap();
let plan = classify_config_fix_plan(&root, None, false);
assert!(
matches!(plan, ConfigFixPlan::BlockedConfigLink { .. }),
"{plan:?}"
);
}
#[cfg(unix)]
#[test]
#[cfg_attr(miri, ignore = "tempdir is blocked by Miri isolation")]
fn an_untracked_config_link_out_of_the_root_is_not_edited() {
crate::set_tracked_link_probe(crate::config::test_tracked_link_probe);
let dir = tempfile::tempdir().unwrap();
let root = dir.path().join("project");
std::fs::create_dir_all(&root).unwrap();
let status = std::process::Command::new("git")
.arg("-C")
.arg(&root)
.args(["init", "-q"])
.env_remove("GIT_DIR")
.env_remove("GIT_WORK_TREE")
.status()
.expect("run git");
assert!(status.success(), "git init");
std::fs::write(dir.path().join("dotfiles.json"), "{}").unwrap();
std::os::unix::fs::symlink(
dir.path().join("dotfiles.json"),
root.join(".fallowrc.json"),
)
.unwrap();
let plan = classify_config_fix_plan(&root, None, false);
assert!(
matches!(plan, ConfigFixPlan::BlockedConfigLink { .. }),
"{plan:?}"
);
}
#[test]
fn config_fixable_true_when_config_exists() {
let dir = tempfile::tempdir().unwrap();
std::fs::write(dir.path().join(".fallowrc.json"), "{}").unwrap();
assert!(is_config_fixable(dir.path(), None));
}
#[test]
fn config_fixable_true_when_can_create_at_root() {
let dir = tempfile::tempdir().unwrap();
assert!(is_config_fixable(dir.path(), None));
}
#[test]
fn config_fixable_false_when_monorepo_subpackage() {
let dir = tempfile::tempdir().unwrap();
std::fs::write(
dir.path().join("pnpm-workspace.yaml"),
"packages:\n - packages/*\n",
)
.unwrap();
let sub = dir.path().join("packages/app");
std::fs::create_dir_all(&sub).unwrap();
assert!(!is_config_fixable(&sub, None));
}
#[cfg(unix)]
#[test]
#[cfg_attr(miri, ignore = "tempdir is blocked by Miri isolation")]
fn a_pipe_as_a_parent_manifest_does_not_block_the_check() {
let dir = tempfile::tempdir().unwrap();
let status = std::process::Command::new("mkfifo")
.arg(dir.path().join("package.json"))
.status()
.expect("run mkfifo");
assert!(status.success(), "mkfifo must create the test pipe");
let sub = dir.path().join("packages/app");
std::fs::create_dir_all(&sub).unwrap();
let (send, receive) = std::sync::mpsc::channel();
std::thread::spawn(move || {
let _ = send.send(is_config_fixable(&sub, None));
});
let fixable = receive
.recv_timeout(std::time::Duration::from_secs(10))
.expect("the check must not wait on a pipe");
assert!(fixable);
}
}