Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Namespace alias shadows local variables #1718

Closed
brjann opened this issue Nov 3, 2023 · 1 comment
Closed

Namespace alias shadows local variables #1718

brjann opened this issue Nov 3, 2023 · 1 comment
Labels
bug Something isn't working editor Related to clojure-lsp on a text editor

Comments

@brjann
Copy link

brjann commented Nov 3, 2023

Bug discussed here
https://clojurians.slack.com/archives/CBE668G4R/p1699023626527349

Describe the bug
I'm using Calva. If a namespace alias shares name with a variable, argument or local binding, the autocompletion will only show the namespace alias. Hitting tab will add the aliased namespace as a dependency to the ns.

To Reproduce

;; aliased.clj
(ns aliased)

;; aliasing.clj
(ns aliasing
  (:require [aliased :as ns-alias]))

;; another_ns.clj
(ns another-ns)

(def ns-alias 0)

(defn bar
  []
  (ns-a)) ;; Autocomplete will only suggest "ns-alias alias to: aliased" LSP-OUTPUT

(defn foo
  [ns-alias]
  (ns-a)) ;; Autocomplete will only suggest "ns-alias alias to: aliased" 

(defn baz
  []
  (let [ns-alias 0]
    ns-a)) ;; Autocomplete will only suggest "ns-alias alias to: aliased" 

Expected behavior
In the cases above, the var, argument, and local binding, respectively, should be (1) suggested, and (2) the first suggestions in the autocompletion.

The Clojure LSP output below is for when I type l on the "LSP-OUTPUT" line

clojure-lsp output
[Trace - 17:03:56] Sending notification 'textDocument/didChange'.
Params: {
    "textDocument": {
        "uri": "file:///src/clj/another_ns.clj",
        "version": 141
    },
    "contentChanges": [
        {
            "text": "(ns another-ns)\n\n(def ns-alias 0)\n\n(defn bar\n  []\n  (ns-a)) ;; Autocomplete will only suggest \"ns-alias alias to: aliased\" \n\n(defn foo\n  [ns-alias]\n  (ns-a)) ;; Autocomplete will only suggest \"ns-alias alias to: aliased\" \n\n(defn baz\n  []\n  (let [ns-alias 0]\n    ns-a)) ;; Autocomplete will only suggest \"ns-alias alias to: aliased\" "
        }
    ]
}

[Trace - 17:03:56] Sending request 'textDocument/completion - (6525)'.
Params: {
"textDocument": {
"uri": "file:///src/clj/another_ns.clj"
},
"position": {
"line": 6,
"character": 7
},
"context": {
"triggerKind": 1
}
}

[Trace - 17:03:56] Received response 'textDocument/completion - (6525)' in 21ms.
Result: [
{
"label": "ns-alias",
"kind": 6,
"data": {
"unresolved": [
[
"documentation",
{
"name": "ns-alias",
"uri": "file:///src/clj/another_ns.clj",
"name-row": 3,
"name-col": 6
}
]
]
},
"score": 14
},
{
"label": "ns-alias",
"kind": 10,
"detail": "alias to: aliased",
"data": {
"unresolved": [
[
"alias",
{
"ns-to-add": "aliased",
"alias-to-add": "ns-alias",
"uri": "file:///src/clj/another_ns.clj"
}
]
]
},
"score": 9
},
{
"label": "ns-aliases",
"kind": 3,
"detail": "clojure.core/ns-aliases",
"data": {
"unresolved": [
[
"documentation",
{
"uri": "file:///clojure.core.clj",
"name": "ns-aliases",
"ns": "clojure.core"
}
]
]
},
"score": 6
}
]

[Trace - 17:03:56] Sending request 'completionItem/resolve - (6526)'.
Params: {
"label": "ns-alias",
"detail": "alias to: aliased",
"insertTextFormat": 1,
"kind": 10,
"data": {
"unresolved": [
[
"alias",
{
"ns-to-add": "aliased",
"alias-to-add": "ns-alias",
"uri": "file:///src/clj/another_ns.clj"
}
]
]
}
}

[Trace - 17:03:56] Received response 'completionItem/resolve - (6526)' in 1ms.
Result: {
"label": "ns-alias",
"detail": "alias to: aliased",
"insertTextFormat": 1,
"kind": 10,
"additionalTextEdits": [
{
"range": {
"start": {
"line": 0,
"character": 0
},
"end": {
"line": 0,
"character": 15
}
},
"newText": "(ns another-ns \n (:require [aliased :as ns-alias]))"
}
]
}

[Trace - 17:03:56] Received notification 'textDocument/publishDiagnostics'.
Params: {
"uri": "file:///src/clj/another_ns.clj",
"diagnostics": [
{
"range": {
"start": {
"line": 2,
"character": 5
},
"end": {
"line": 2,
"character": 13
}
},
"tags": [
1
],
"message": "Unused public var 'another-ns/ns-alias'",
"code": "clojure-lsp/unused-public-var",
"langs": [],
"severity": 3,
"source": "clojure-lsp"
},
{
"range": {
"start": {
"line": 4,
"character": 6
},
"end": {
"line": 4,
"character": 9
}
},
"tags": [
1
],
"message": "Unused public var 'another-ns/bar'",
"code": "clojure-lsp/unused-public-var",
"langs": [],
"severity": 3,
"source": "clojure-lsp"
},
{
"range": {
"start": {
"line": 6,
"character": 3
},
"end": {
"line": 6,
"character": 7
}
},
"tags": [],
"message": "Unresolved symbol: ns-a",
"code": "unresolved-symbol",
"langs": [],
"severity": 1,
"source": "clj-kondo"
},
{
"range": {
"start": {
"line": 8,
"character": 6
},
"end": {
"line": 8,
"character": 9
}
},
"tags": [
1
],
"message": "Unused public var 'another-ns/foo'",
"code": "clojure-lsp/unused-public-var",
"langs": [],
"severity": 3,
"source": "clojure-lsp"
},
{
"range": {
"start": {
"line": 9,
"character": 3
},
"end": {
"line": 9,
"character": 11
}
},
"tags": [
1
],
"message": "unused binding ns-alias",
"code": "unused-binding",
"langs": [],
"severity": 2,
"source": "clj-kondo"
},
{
"range": {
"start": {
"line": 10,
"character": 3
},
"end": {
"line": 10,
"character": 7
}
},
"tags": [],
"message": "Unresolved symbol: ns-a",
"code": "unresolved-symbol",
"langs": [],
"severity": 1,
"source": "clj-kondo"
},
{
"range": {
"start": {
"line": 12,
"character": 6
},
"end": {
"line": 12,
"character": 9
}
},
"tags": [
1
],
"message": "Unused public var 'another-ns/baz'",
"code": "clojure-lsp/unused-public-var",
"langs": [],
"severity": 3,
"source": "clojure-lsp"
},
{
"range": {
"start": {
"line": 14,
"character": 8
},
"end": {
"line": 14,
"character": 16
}
},
"tags": [
1
],
"message": "unused binding ns-alias",
"code": "unused-binding",
"langs": [],
"severity": 2,
"source": "clj-kondo"
},
{
"range": {
"start": {
"line": 15,
"character": 4
},
"end": {
"line": 15,
"character": 8
}
},
"tags": [],
"message": "Unresolved symbol: ns-a",
"code": "unresolved-symbol",
"langs": [],
"severity": 1,
"source": "clj-kondo"
}
]
}

[Trace - 17:03:56] Sending request 'textDocument/codeAction - (6527)'.
Params: {
"textDocument": {
"uri": "file:///src/clj/another_ns.clj"
},
"range": {
"start": {
"line": 6,
"character": 7
},
"end": {
"line": 6,
"character": 7
}
},
"context": {
"diagnostics": [
{
"range": {
"start": {
"line": 6,
"character": 3
},
"end": {
"line": 6,
"character": 7
}
},
"message": "Unresolved symbol: ns-a",
"code": "unresolved-symbol",
"severity": 1,
"source": "clj-kondo"
}
],
"triggerKind": 2
}
}

[Trace - 17:03:56] Received response 'textDocument/codeAction - (6527)' in 48ms.
Result: [
{
"title": "Create private function 'ns-a'",
"kind": "quickfix",
"command": {
"title": "Create function",
"command": "create-function",
"arguments": [
"file:///src/clj/another_ns.clj",
6,
3
]
}
},
{
"title": "Change coll to vector",
"kind": "refactor",
"command": {
"title": "Change coll",
"command": "change-coll",
"arguments": [
"file:///src/clj/another_ns.clj",
6,
7,
"vector"
]
}
},
{
"title": "Change coll to set",
"kind": "refactor",
"command": {
"title": "Change coll",
"command": "change-coll",
"arguments": [
"file:///src/clj/another_ns.clj",
6,
7,
"set"
]
}
},
{
"title": "Change coll to map",
"kind": "refactor",
"command": {
"title": "Change coll",
"command": "change-coll",
"arguments": [
"file:///src/clj/another_ns.clj",
6,
7,
"map"
]
}
},
{
"title": "Move to let",
"kind": "refactor.extract",
"command": {
"title": "Move to let",
"command": "move-to-let",
"arguments": [
"file:///src/clj/another_ns.clj",
6,
7,
"new-binding"
]
}
},
{
"title": "Cycle privacy",
"kind": "refactor.rewrite",
"command": {
"title": "Cycle privacy",
"command": "cycle-privacy",
"arguments": [
"file:///src/clj/another_ns.clj",
6,
7
]
}
},
{
"title": "Extract function",
"kind": "refactor.extract",
"command": {
"title": "Extract function",
"command": "extract-function",
"arguments": [
"file:///src/clj/another_ns.clj",
6,
7,
"new-function"
]
}
},
{
"title": "Extract to def",
"kind": "refactor.extract",
"command": {
"title": "Extract to def",
"command": "extract-to-def",
"arguments": [
"file:///src/clj/another_ns.clj",
6,
7,
null
]
}
},
{
"title": "Thread first all",
"kind": "refactor.rewrite",
"command": {
"title": "Thread first all",
"command": "thread-first-all",
"arguments": [
"file:///src/clj/another_ns.clj",
6,
7
]
}
},
{
"title": "Thread last all",
"kind": "refactor.rewrite",
"command": {
"title": "Thread last all",
"command": "thread-last-all",
"arguments": [
"file:///src/clj/another_ns.clj",
6,
7
]
}
},
{
"title": "Sort list",
"kind": "refactor.rewrite",
"command": {
"title": "Sort list",
"command": "sort-clauses",
"arguments": [
"file:///src/clj/another_ns.clj",
6,
7
]
}
},
{
"title": "Drag backward",
"kind": "refactor.rewrite",
"command": {
"title": "Drag backward",
"command": "drag-backward",
"arguments": [
"file:///src/clj/another_ns.clj",
6,
7
]
}
},
{
"title": "Introduce let",
"kind": "refactor.extract",
"command": {
"title": "Introduce let",
"command": "introduce-let",
"arguments": [
"file:///src/clj/another_ns.clj",
6,
7,
"new-binding"
]
}
},
{
"title": "Suppress 'unresolved-symbol' diagnostic",
"kind": "quickfix",
"command": {
"title": "Suppress diagnostic",
"command": "suppress-diagnostic",
"arguments": [
"file:///src/clj/another_ns.clj",
6,
3,
"unresolved-symbol"
]
}
},
{
"title": "Create test for 'bar'",
"kind": "refactor.rewrite",
"command": {
"title": "Create test",
"command": "create-test",
"arguments": [
"file:///src/clj/another_ns.clj",
6,
7
]
}
},
{
"title": "Clean namespace",
"kind": "source.organizeImports",
"command": {
"title": "Clean namespace",
"command": "clean-ns",
"arguments": [
"file:///src/clj/another_ns.clj",
6,
7
]
}
}
]

[Trace - 17:03:56] Sending request 'textDocument/semanticTokens/full - (6528)'.
Params: {
"textDocument": {
"uri": "file:///src/clj/another_ns.clj"
}
}

[Trace - 17:03:56] Received response 'textDocument/semanticTokens/full - (6528)' in 1ms.
Result: {
"data": [
0,
4,
10,
0,
0,
2,
1,
3,
3,
0,
0,
4,
8,
2,
1,
2,
1,
4,
3,
0,
0,
5,
3,
2,
1,
4,
1,
4,
3,
0,
0,
5,
3,
2,
1,
1,
3,
8,
6,
0,
3,
1,
4,
3,
0,
0,
5,
3,
2,
1,
2,
3,
3,
3,
0,
0,
5,
8,
6,
0
]
}

[Trace - 17:03:56] Sending request 'textDocument/documentSymbol - (6529)'.
Params: {
"textDocument": {
"uri": "file:///src/clj/another_ns.clj"
}
}

[Trace - 17:03:56] Received response 'textDocument/documentSymbol - (6529)' in 2ms.
Result: [
{
"name": "another-ns",
"kind": 3,
"range": {
"start": {
"line": 0,
"character": 0
},
"end": {
"line": 999999,
"character": 999999
}
},
"selectionRange": {
"start": {
"line": 0,
"character": 0
},
"end": {
"line": 0,
"character": 15
}
},
"children": [
{
"name": "ns-alias",
"kind": 13,
"range": {
"start": {
"line": 2,
"character": 0
},
"end": {
"line": 2,
"character": 16
}
},
"selectionRange": {
"start": {
"line": 2,
"character": 5
},
"end": {
"line": 2,
"character": 13
}
},
"tags": []
},
{
"name": "bar",
"kind": 12,
"range": {
"start": {
"line": 4,
"character": 0
},
"end": {
"line": 6,
"character": 9
}
},
"selectionRange": {
"start": {
"line": 4,
"character": 6
},
"end": {
"line": 4,
"character": 9
}
},
"tags": []
},
{
"name": "foo",
"kind": 12,
"range": {
"start": {
"line": 8,
"character": 0
},
"end": {
"line": 10,
"character": 9
}
},
"selectionRange": {
"start": {
"line": 8,
"character": 6
},
"end": {
"line": 8,
"character": 9
}
},
"tags": []
},
{
"name": "baz",
"kind": 12,
"range": {
"start": {
"line": 12,
"character": 0
},
"end": {
"line": 15,
"character": 10
}
},
"selectionRange": {
"start": {
"line": 12,
"character": 6
},
"end": {
"line": 12,
"character": 9
}
},
"tags": []
}
]
}
]

User details (please complete the following information):

  • OS: MacOS
  • Version: Sorry, I'm not too skilled with the terminal. % clojure-lsp --version => zsh: command not found: clojure-lsp
@brjann brjann added API Related to API / CLI bug Something isn't working labels Nov 3, 2023
@ericdallo ericdallo added editor Related to clojure-lsp on a text editor and removed API Related to API / CLI labels Nov 3, 2023
@ericdallo ericdallo added this to Low priority in clojure-lsp via automation Nov 3, 2023
@ericdallo
Copy link
Member

I tested with Emacs lsp-mode and it works, then I tested with calva and server correctly return the expected items in the correct order:

2023-11-03T16:55:59.247Z  DEBUG [clojure-lsp.server:55] - [Trace - 2023-11-03T16:55:59.247Z] Sending response 'textDocument/completion - (69)'. Request took 37ms.                            
Result: [ {                                                                                                                                                                                   
  "label" : "ns-alias",                                                                                                                                                                       
  "kind" : 6,                                                                                                                                                                                 
  "data" : {                                                                                                                                                                                  
    "unresolved" : [ [ "documentation", {                                                                                                                                                     
      "name" : "ns-alias",                                                                                                                                                                    
      "uri" : "file:///home/greg/dev/clojure-lsp/lib/src/clojure_lsp/feature/issue/another_ns.clj",                                                                                           
      "name-row" : 3,                                                                                                                                                                         
      "name-col" : 6                                                                                                                                                                          
    } ] ]                                                                                                                                                                                     
  },                                                                                                                                                                                          
  "score" : 14                                                                                                                                                                                
}, {                                                                                                                                                                                          
  "label" : "ns-alias",                                                                                                                                                                       
  "kind" : 10,                                                                                                                                                                                
  "detail" : "alias to: clojure-lsp.feature.issue.aliased",                                                                                                                                   
  "data" : {                                                                                                                                                                                  
    "unresolved" : [ [ "alias", {                                                                                                                                                             
      "ns-to-add" : "clojure-lsp.feature.issue.aliased",                                                                                                                                      
      "alias-to-add" : "ns-alias",                                                                                                                                                            
      "uri" : "file:///home/greg/dev/clojure-lsp/lib/src/clojure_lsp/feature/issue/another_ns.clj"                                                                                            
    } ] ]                                                                                                                                                                                     
  },                                                                                                                                                                                          
  "score" : 9                                                                                                                                                                                 
}, {                                                                                                                                                                                          
  "label" : "ns-aliases",                                                                                                                                                                     
  "kind" : 3,                                                                                                                                                                                 
  "detail" : "clojure.core/ns-aliases",
  "data" : {
    "unresolved" : [ [ "documentation", {
      "uri" : "file:///clojure.core.clj",
      "name" : "ns-aliases",
      "ns" : "clojure.core"
    } ] ]
  },
  "score" : 6
} ]

Closing this as this should be fixed on calva or some LSP dependency

clojure-lsp automation moved this from Low priority to Next release Nov 3, 2023
@ericdallo ericdallo removed this from Next release in clojure-lsp Nov 3, 2023
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
bug Something isn't working editor Related to clojure-lsp on a text editor
Projects
None yet
Development

No branches or pull requests

2 participants