languages: Fix panic in python lsp adapters assuming settings shape (#38309)

Lukas Wirth created

Fixes ZED-1EV
Fixes ZED-S0
Fixes ZED-Q9

Release Notes:

- N/A

Change summary

crates/languages/src/python.rs | 16 ++++++++++------
1 file changed, 10 insertions(+), 6 deletions(-)

Detailed changes

crates/languages/src/python.rs 🔗

@@ -461,7 +461,7 @@ impl LspAdapter for PyrightLspAdapter {
                     pet_core::python_environment::PythonEnvironment,
                 >(toolchain.as_json.clone())
             {
-                if user_settings.is_null() {
+                if !user_settings.is_object() {
                     user_settings = Value::Object(serde_json::Map::default());
                 }
                 let object = user_settings.as_object_mut().unwrap();
@@ -492,9 +492,13 @@ impl LspAdapter for PyrightLspAdapter {
                 // Get or create the python section
                 let python = object
                     .entry("python")
-                    .or_insert(Value::Object(serde_json::Map::default()))
-                    .as_object_mut()
-                    .unwrap();
+                    .and_modify(|v| {
+                        if !v.is_object() {
+                            *v = Value::Object(serde_json::Map::default());
+                        }
+                    })
+                    .or_insert(Value::Object(serde_json::Map::default()));
+                let python = python.as_object_mut().unwrap();
 
                 // Set both pythonPath and defaultInterpreterPath for compatibility
                 python.insert(
@@ -1465,7 +1469,7 @@ impl LspAdapter for PyLspAdapter {
 
             // If user did not explicitly modify their python venv, use one from picker.
             if let Some(toolchain) = toolchain {
-                if user_settings.is_null() {
+                if !user_settings.is_object() {
                     user_settings = Value::Object(serde_json::Map::default());
                 }
                 let object = user_settings.as_object_mut().unwrap();
@@ -1787,7 +1791,7 @@ impl LspAdapter for BasedPyrightLspAdapter {
                     pet_core::python_environment::PythonEnvironment,
                 >(toolchain.as_json.clone())
             {
-                if user_settings.is_null() {
+                if !user_settings.is_object() {
                     user_settings = Value::Object(serde_json::Map::default());
                 }
                 let object = user_settings.as_object_mut().unwrap();