Repository: gleam-lang/gleam (rust)

Issue:
Rename doesn't properly work for nested modules
I found a bug with the new module value renaming. If you have this code:
```gleam
// sub/mod.gleam
pub fn wibble() { todo }

// main.gleam
import sub/mod

pub fn main() {
  mod.wibble()
  //  ^ trigger rename
}
```
It becomes this:
```gleam
// sub/mod.gleam
pub fn wobble() { todo }

// main.gleam
import sub/mod

pub fn main() {
  mod.wibble()
}
```
Which is incorrect, it should rename the reference too. 

Pull request:
Fix renaming of aliased and nested modules
Fixes #4344 
Turns out `imported_modules` stores the locally used names of the modules, not the fully qualified ones.

Patch:
diff --git a/CHANGELOG.md b/CHANGELOG.md
index 204bf5b82..5fef48b58 100644
--- a/CHANGELOG.md
+++ b/CHANGELOG.md
@@ -11,7 +11,7 @@
 ### Build tool
 
 - Include a type annotation for the `main` function generated by `gleam new`.
-  ([Drew Olson](https://github.com/drewolson)]
+  ([Drew Olson](https://github.com/drewolson))
 
 ### Language server
 
diff --git a/compiler-core/src/analyse.rs b/compiler-core/src/analyse.rs
index 72a1ecb03..86875d728 100644
--- a/compiler-core/src/analyse.rs
+++ b/compiler-core/src/analyse.rs
@@ -342,7 +342,11 @@ impl<'a, A> ModuleAnalyzer<'a, A> {
                 documentation,
                 contains_echo: echo_found,
                 references: References {
-                    imported_modules: env.imported_modules.into_keys().collect(),
+                    imported_modules: env
+                        .imported_modules
+                        .values()
+                        .map(|(_location, module)| module.name.clone())
+                        .collect(),
                     value_references: env.references.into_locations(),
                 },
             },


Test patch:
diff --git a/compiler-core/src/language_server/tests/rename.rs b/compiler-core/src/language_server/tests/rename.rs
index 8b1766388..daffaac2d 100644
--- a/compiler-core/src/language_server/tests/rename.rs
+++ b/compiler-core/src/language_server/tests/rename.rs
@@ -1073,3 +1073,49 @@ pub fn main() {
         find_position_of("Wibble").to_selection()
     );
 }
+
+#[test]
+fn rename_value_in_nested_module() {
+    assert_rename!(
+        (
+            "sub/mod",
+            "
+pub fn wibble() {
+  wibble()
+}
+"
+        ),
+        "
+import sub/mod
+
+pub fn main() {
+  mod.wibble()
+}
+",
+        "some_function",
+        find_position_of("wibble").to_selection()
+    );
+}
+
+#[test]
+fn rename_value_in_aliased_module() {
+    assert_rename!(
+        (
+            "mod",
+            "
+pub fn wibble() {
+  wibble()
+}
+"
+        ),
+        "
+import mod as the_module
+
+pub fn main() {
+  the_module.wibble()
+}
+",
+        "some_function",
+        find_position_of("wibble").to_selection()
+    );
+}
diff --git a/compiler-core/src/language_server/tests/snapshots/gleam_core__language_server__tests__rename__rename_value_in_aliased_module.snap b/compiler-core/src/language_server/tests/snapshots/gleam_core__language_server__tests__rename__rename_value_in_aliased_module.snap
new file mode 100644
index 000000000..dab17d8c0
--- /dev/null
+++ b/compiler-core/src/language_server/tests/snapshots/gleam_core__language_server__tests__rename__rename_value_in_aliased_module.snap
@@ -0,0 +1,37 @@
+---
+source: compiler-core/src/language_server/tests/rename.rs
+expression: "\nimport mod as the_module\n\npub fn main() {\n  the_module.wibble()\n}\n"
+---
+----- BEFORE RENAME
+-- mod.gleam
+
+pub fn wibble() {
+  wibble()
+}
+
+
+-- app.gleam
+
+import mod as the_module
+
+pub fn main() {
+  the_module.wibble()
+             ↑       
+}
+
+
+----- AFTER RENAME
+-- mod.gleam
+
+pub fn some_function() {
+  some_function()
+}
+
+
+-- app.gleam
+
+import mod as the_module
+
+pub fn main() {
+  the_module.some_function()
+}
diff --git a/compiler-core/src/language_server/tests/snapshots/gleam_core__language_server__tests__rename__rename_value_in_nested_module.snap b/compiler-core/src/language_server/tests/snapshots/gleam_core__language_server__tests__rename__rename_value_in_nested_module.snap
new file mode 100644
index 000000000..5195eb174
--- /dev/null
+++ b/compiler-core/src/language_server/tests/snapshots/gleam_core__language_server__tests__rename__rename_value_in_nested_module.snap
@@ -0,0 +1,37 @@
+---
+source: compiler-core/src/language_server/tests/rename.rs
+expression: "\nimport sub/mod\n\npub fn main() {\n  mod.wibble()\n}\n"
+---
+----- BEFORE RENAME
+-- sub/mod.gleam
+
+pub fn wibble() {
+  wibble()
+}
+
+
+-- app.gleam
+
+import sub/mod
+
+pub fn main() {
+  mod.wibble()
+      ↑       
+}
+
+
+----- AFTER RENAME
+-- sub/mod.gleam
+
+pub fn some_function() {
+  some_function()
+}
+
+
+-- app.gleam
+
+import sub/mod
+
+pub fn main() {
+  mod.some_function()
+}