@@ -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