Lsp-mode: Bizarre 'undefined variable' warnings in Python

Created on 1 Jan 2020  路  4Comments  路  Source: emacs-lsp/lsp-mode

Describe the bug
In certain situations, saving a file will cause mspyls to hallucinate that Python variables are not defined, even though they clearly are.

To Reproduce
Create the following Python script app.py (yes I know the contents don't make any sense, it is from a Flask app but I took out Flask to rule out installed packages as the culprit):

import json
import subprocess

app = subprocess.run(__name__)


@app.route("/")
def get_index():
    return subprocess.run("dist/index.html")


@app.route("/admin")
def get_admin():
    return subprocess.run("dist/admin.html")


@app.route("/<path>")
def get_static_file(path):
    if not path.endswith(".html"):
        return subprocess.run("dist", path)
    subprocess.run(404)


@app.route("/api/v1/admin/data")
def get_responses():
    try:
        with open("responses.json") as f:
            return subprocess.run(json.load(f))
    except (OSError, json.JSONDecodeError):
        return "Data not available", 500

Enable LSP with mspyls configured via lsp-python-ms. Now edit the buffer as follows:

  • Delete /data from the string literal on line 24, and save.
  • Add it back, and save.
  • Repeat the above steps a couple of times.
  • You will soon notice that errors have appeared in the buffer, even though none have been introduced. In particular, either f below or path above has been highlighted as a undefined variable. The error may appear and disappear each time you save the buffer.

With lsp-ui configured, it looks like this:

20191231173619

Using the provided scripts/lsp-start-plain.el:

image

The issue reproduces on Manjaro Linux and macOS, on 26.3 and Git build of 2019-12-24, and on a machine with no special configuration other than Emacs being installed from the package manager, so I think it is unlikely to be an artifact of my local environment.

Expected behavior
Well, mspyls shouldn't be hallucinating errors :)

Which Language Server did you use
mspyls

OS
Manjaro Linux and macOS

LSP log
See here. This is from the lsp-start-plain.el configuration.

I am more than happy to investigate&debug on my own if you have any pointers, because this bug really bothers me and I'm motivated to help fix it!

Most helpful comment

Another thing is to inspect closely the didChange notifications lsp-mode is sending - one possible root cause is emacs and the msserver being out of sync due to wrong notification.

All 4 comments

I would start with testing the same in vscode - if it reproduces it is issue in the server. If not, we could compare the client-server communication in both.

Another thing is to inspect closely the didChange notifications lsp-mode is sending - one possible root cause is emacs and the msserver being out of sync due to wrong notification.

Any update?

I have not done enough Python development to be bothered by the warnings, and it's possible that an update to the language server or other tooling has solved the problem in the meantime. I will close this issue, and re-open it with more information if I manage to reproduce the bug.

Was this page helpful?
0 / 5 - 0 ratings