Skip to content

Commit 24a1b97

Browse files
rchen152meta-codesync[bot]
authored andcommitted
Don't add unnecessary [tool] section to pyproject.toml when running pyrefly init
Summary: Fixes #1948. Reviewed By: kinto0 Differential Revision: D89901293 fbshipit-source-id: c2a62c7ddabbe356cd1c02dbd89569cdcce0508d
1 parent cfc45cf commit 24a1b97

File tree

1 file changed

+29
-0
lines changed

1 file changed

+29
-0
lines changed

crates/pyrefly_config/src/pyproject.rs

Lines changed: 29 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -59,10 +59,14 @@ impl PyProject {
5959
if let Some(tool_table) = config_doc.get("tool")
6060
&& let Some(pyrefly_table) = tool_table.get("pyrefly")
6161
{
62+
let is_new_tool_table = !doc.contains_key("tool");
6263
let tool_entry = doc
6364
.entry("tool")
6465
.or_insert(toml_edit::Item::Table(toml_edit::Table::new()));
6566
if let Some(tool_table_mut) = tool_entry.as_table_mut() {
67+
if is_new_tool_table {
68+
tool_table_mut.set_implicit(true);
69+
}
6670
tool_table_mut.remove("pyrefly");
6771
let max_tool_pos = tool_table_mut
6872
.iter()
@@ -137,6 +141,31 @@ line-length = 88
137141
Ok(())
138142
}
139143

144+
#[test]
145+
fn test_add_pyrefly_config_to_existing_pyproject() -> anyhow::Result<()> {
146+
let tmp = tempfile::tempdir()?;
147+
let pyproject_path = tmp.path().join("pyproject.toml");
148+
149+
let existing_content = "\n";
150+
fs_anyhow::write(&pyproject_path, existing_content)?;
151+
152+
let config = ConfigFile {
153+
project_includes: Globs::new(vec!["new/path/**/*.py".to_owned()]).unwrap(),
154+
..Default::default()
155+
};
156+
PyProject::update(&pyproject_path, config)?;
157+
158+
let updated_content = fs_anyhow::read_to_string(&pyproject_path)?;
159+
160+
assert!(updated_content.contains("[tool.pyrefly]"));
161+
assert!(updated_content.contains("project-includes = [\"new/path/**/*.py\"]"));
162+
163+
// Regression test for bug where we would insert an unnecessary [tool] section
164+
assert!(!updated_content.contains("[tool]"));
165+
166+
Ok(())
167+
}
168+
140169
#[test]
141170
fn test_pyrefly_section_ordering() -> anyhow::Result<()> {
142171
let tmp = tempfile::tempdir()?;

0 commit comments

Comments
 (0)