emacs-lsp / lsp-sourcekit

lsp-mode :heart: Apple's sourcekit
https://emacs-lsp.github.io/lsp-sourcekit
GNU General Public License v3.0
103 stars 13 forks source link

lsp-sourcekit can't get complete candidates #18

Open stardiviner opened 3 years ago

stardiviner commented 3 years ago

company-mode displays error in minibuffer:

Company: An error occurred in auto-begin
Company: backend company-capf error "request cancelled" with args (candidates let)

Then I enabled lsp-mode debug with:

(setq lsp-log-io t
      lsp-print-io t)

In buffer *lsp-log: sourcekit-ls:55049*:

[Trace - 05:53:52 PM] Received response 'textDocument/codeAction - (53)' in 28ms.
Result: []

[Trace - 05:53:58 PM] Sending request 'textDocument/codeAction - (54)'.
Params: {
  "textDocument": {
    "uri": "file:///Users/stardiviner/Documents/Programming/Swift/SwiftDemo/Sources/SwiftDemo/SwiftDemo.swift"
  },
  "range": {
    "start": {
      "line": 8,
      "character": 0
    },
    "end": {
      "line": 8,
      "character": 0
    }
  },
  "context": {
    "diagnostics": []
  }
}

[Trace - 05:53:58 PM] Received response 'textDocument/codeAction - (54)' in 22ms.
Result: []

[Trace - 05:53:58 PM] Sending notification 'textDocument/didChange'.
Params: {
  "textDocument": {
    "uri": "file:///Users/stardiviner/Documents/Programming/Swift/SwiftDemo/Sources/SwiftDemo/SwiftDemo.swift",
    "version": 54
  },
  "contentChanges": [
    {
      "range": {
        "start": {
          "line": 8,
          "character": 0
        },
        "end": {
          "line": 8,
          "character": 0
        }
      },
      "rangeLength": 0,
      "text": "l"
    }
  ]
}

[Trace - 05:53:58 PM] Received notification 'textDocument/publishDiagnostics'.
Params: {
  "diagnostics": [],
  "version": 54,
  "uri": "file:///Users/stardiviner/Documents/Programming/Swift/SwiftDemo/Sources/SwiftDemo/SwiftDemo.swift"
}

[Trace - 05:53:58 PM] Received notification 'textDocument/publishDiagnostics'.
Params: {
  "diagnostics": [
    {
      "relatedInformation": [],
      "severity": 1,
      "source": "sourcekitd",
      "message": "cannot find 'l' in scope",
      "range": {
        "end": {
          "character": 0,
          "line": 8
        },
        "start": {
          "character": 0,
          "line": 8
        }
      }
    },
    {
      "relatedInformation": [],
      "severity": 1,
      "source": "sourcekitd",
      "message": "expressions are not allowed at the top level",
      "range": {
        "end": {
          "character": 0,
          "line": 8
        },
        "start": {
          "character": 0,
          "line": 8
        }
      }
    }
  ],
  "version": 54,
  "uri": "file:///Users/stardiviner/Documents/Programming/Swift/SwiftDemo/Sources/SwiftDemo/SwiftDemo.swift"
}

[Trace - 05:53:58 PM] Sending notification 'textDocument/didChange'.
Params: {
  "textDocument": {
    "uri": "file:///Users/stardiviner/Documents/Programming/Swift/SwiftDemo/Sources/SwiftDemo/SwiftDemo.swift",
    "version": 55
  },
  "contentChanges": [
    {
      "range": {
        "start": {
          "line": 8,
          "character": 1
        },
        "end": {
          "line": 8,
          "character": 1
        }
      },
      "rangeLength": 0,
      "text": "e"
    }
  ]
}

[Trace - 05:53:58 PM] Received notification 'textDocument/publishDiagnostics'.
Params: {
  "diagnostics": [
    {
      "relatedInformation": [],
      "severity": 1,
      "source": "sourcekitd",
      "message": "cannot find 'l' in scope",
      "range": {
        "end": {
          "character": 0,
          "line": 8
        },
        "start": {
          "character": 0,
          "line": 8
        }
      }
    },
    {
      "relatedInformation": [],
      "severity": 1,
      "source": "sourcekitd",
      "message": "expressions are not allowed at the top level",
      "range": {
        "end": {
          "character": 0,
          "line": 8
        },
        "start": {
          "character": 0,
          "line": 8
        }
      }
    }
  ],
  "version": 55,
  "uri": "file:///Users/stardiviner/Documents/Programming/Swift/SwiftDemo/Sources/SwiftDemo/SwiftDemo.swift"
}

[Trace - 05:53:58 PM] Received notification 'textDocument/publishDiagnostics'.
Params: {
  "diagnostics": [
    {
      "relatedInformation": [],
      "severity": 1,
      "source": "sourcekitd",
      "message": "cannot find 'le' in scope",
      "range": {
        "end": {
          "character": 0,
          "line": 8
        },
        "start": {
          "character": 0,
          "line": 8
        }
      }
    },
    {
      "relatedInformation": [],
      "severity": 1,
      "source": "sourcekitd",
      "message": "expressions are not allowed at the top level",
      "range": {
        "end": {
          "character": 0,
          "line": 8
        },
        "start": {
          "character": 0,
          "line": 8
        }
      }
    }
  ],
  "version": 55,
  "uri": "file:///Users/stardiviner/Documents/Programming/Swift/SwiftDemo/Sources/SwiftDemo/SwiftDemo.swift"
}

[Trace - 05:53:58 PM] Sending notification 'textDocument/didChange'.
Params: {
  "textDocument": {
    "uri": "file:///Users/stardiviner/Documents/Programming/Swift/SwiftDemo/Sources/SwiftDemo/SwiftDemo.swift",
    "version": 56
  },
  "contentChanges": [
    {
      "range": {
        "start": {
          "line": 8,
          "character": 2
        },
        "end": {
          "line": 8,
          "character": 2
        }
      },
      "rangeLength": 0,
      "text": "t"
    }
  ]
}

[Trace - 05:53:58 PM] Received notification 'textDocument/publishDiagnostics'.
Params: {
  "diagnostics": [
    {
      "relatedInformation": [],
      "severity": 1,
      "source": "sourcekitd",
      "message": "expected pattern",
      "range": {
        "end": {
          "character": 3,
          "line": 8
        },
        "start": {
          "character": 3,
          "line": 8
        }
      }
    },
    {
      "relatedInformation": [],
      "severity": 1,
      "source": "sourcekitd",
      "message": "cannot find 'le' in scope",
      "range": {
        "end": {
          "character": 0,
          "line": 8
        },
        "start": {
          "character": 0,
          "line": 8
        }
      }
    },
    {
      "relatedInformation": [],
      "severity": 1,
      "source": "sourcekitd",
      "message": "expressions are not allowed at the top level",
      "range": {
        "end": {
          "character": 0,
          "line": 8
        },
        "start": {
          "character": 0,
          "line": 8
        }
      }
    }
  ],
  "version": 56,
  "uri": "file:///Users/stardiviner/Documents/Programming/Swift/SwiftDemo/Sources/SwiftDemo/SwiftDemo.swift"
}

[Trace - 05:53:58 PM] Received notification 'textDocument/publishDiagnostics'.
Params: {
  "diagnostics": [
    {
      "relatedInformation": [],
      "severity": 1,
      "source": "sourcekitd",
      "message": "expected pattern",
      "range": {
        "end": {
          "character": 3,
          "line": 8
        },
        "start": {
          "character": 3,
          "line": 8
        }
      }
    }
  ],
  "version": 56,
  "uri": "file:///Users/stardiviner/Documents/Programming/Swift/SwiftDemo/Sources/SwiftDemo/SwiftDemo.swift"
}

[Trace - 05:53:58 PM] Sending request 'textDocument/completion - (55)'.
Params: {
  "textDocument": {
    "uri": "file:///Users/stardiviner/Documents/Programming/Swift/SwiftDemo/Sources/SwiftDemo/SwiftDemo.swift"
  },
  "position": {
    "line": 8,
    "character": 3
  },
  "context": {
    "triggerKind": 3
  }
}

[Trace - 05:53:58 PM] Received response 'textDocument/completion - (55)' in 2ms.
Result: {
  "code": -32800,
  "message": "request cancelled"
}

[Trace - 05:53:58 PM] Sending request 'textDocument/completion - (56)'.
Params: {
  "textDocument": {
    "uri": "file:///Users/stardiviner/Documents/Programming/Swift/SwiftDemo/Sources/SwiftDemo/SwiftDemo.swift"
  },
  "position": {
    "line": 8,
    "character": 3
  },
  "context": {
    "triggerKind": 3
  }
}

[Trace - 05:53:58 PM] Received response 'textDocument/completion - (56)' in 42ms.
Result: {
  "code": -32800,
  "message": "request cancelled"
}

[Trace - 05:54:00 PM] Sending notification 'textDocument/didChange'.
Params: {
  "textDocument": {
    "uri": "file:///Users/stardiviner/Documents/Programming/Swift/SwiftDemo/Sources/SwiftDemo/SwiftDemo.swift",
    "version": 57
  },
  "contentChanges": [
    {
      "range": {
        "start": {
          "line": 8,
          "character": 0
        },
        "end": {
          "line": 8,
          "character": 3
        }
      },
      "rangeLength": 3,
      "text": ""
    }
  ]
}

[Trace - 05:54:00 PM] Received notification 'textDocument/publishDiagnostics'.
Params: {
  "diagnostics": [
    {
      "relatedInformation": [],
      "severity": 1,
      "source": "sourcekitd",
      "message": "expected pattern",
      "range": {
        "end": {
          "character": 3,
          "line": 8
        },
        "start": {
          "character": 3,
          "line": 8
        }
      }
    }
  ],
  "version": 57,
  "uri": "file:///Users/stardiviner/Documents/Programming/Swift/SwiftDemo/Sources/SwiftDemo/SwiftDemo.swift"
}

[Trace - 05:54:00 PM] Received notification 'textDocument/publishDiagnostics'.
Params: {
  "diagnostics": [],
  "version": 57,
  "uri": "file:///Users/stardiviner/Documents/Programming/Swift/SwiftDemo/Sources/SwiftDemo/SwiftDemo.swift"
}

[Trace - 05:54:00 PM] Sending notification 'textDocument/willSave'.
Params: {
  "textDocument": {
    "uri": "file:///Users/stardiviner/Documents/Programming/Swift/SwiftDemo/Sources/SwiftDemo/SwiftDemo.swift"
  },
  "reason": 1
}

[Trace - 05:54:00 PM] Sending notification 'textDocument/didSave'.
Params: {
  "textDocument": {
    "uri": "file:///Users/stardiviner/Documents/Programming/Swift/SwiftDemo/Sources/SwiftDemo/SwiftDemo.swift",
    "version": 57
  }
}
rksm commented 3 years ago

Running into the same problem.

The problem seems to be with lsp-completion--get-context that is used as a parameter to the completion request. For whatever reason, lsp-completion--cache gets set with an incomplete marker. The context sets specific :trigger-kind in this case which seems to break the completions.

Unfortunately, just deactivating caching with (setq lsp-completion-no-cache 't) doesn't seem to be enough, the :incomplete marker is still set.

So the quick'n dirty hack that might very well break other things:

(defun sourcekit-lsp-completion-fix (method &rest args)
  "Quick'n dirty fix for https://github.com/emacs-lsp/lsp-sourcekit/issues/18"
  (when (and (eq major-mode 'swift-mode) (string-equal method "textDocument/completion"))
    (message "%s" method)
    (lsp-completion--clear-cache)))

(advice-add 'lsp-request :before 'sourcekit-lsp-completion-fix)
stardiviner commented 3 years ago

@rksm Hi, thanks for diving into the problem. I tried your method. Still have problem. When lsp-sourcekit completing, got error Company: backend company-capf error "request cancelled" with args (candidates hel).

I enabled the lsp log, but have not got special output.

I guess current latest version company-capf incompatible with lsp-sourcekit or lsp-mode.

wibed commented 3 years ago

https://github.com/emacs-lsp/lsp-mode/issues/3028

and

https://github.com/emacs-lsp/lsp-sourcekit/issues/20

seem to be related to this issue. i too had no luck clearing the cache or disabling it for company as company still would block containing the cancel error

@stardiviner have you found a workaround?

stardiviner commented 3 years ago

I still don't got a solution. sorry @wibed

wibed commented 2 years ago

https://github.com/emacs-lsp/lsp-mode/issues/2970

this issue with pr seems related to the problem

stardiviner commented 2 years ago

After overview this issue, seems still no good solution yet. Temporary typing code literally.

wibed commented 2 years ago

@stardiviner over at https://github.com/emacs-lsp/lsp-mode/issues/3028 the actual problem has been stated

stardiviner commented 2 years ago

Glad the patch pr will coming soon. Thanks for your effort on this issue. đŸ˜„