python-language-server icon indicating copy to clipboard operation
python-language-server copied to clipboard

Failed to find all references in import code

Open mozkileo opened this issue 5 years ago • 0 comments

Environment data

  • Version Info

    • MS Language Server: 0.5.45
    • OS: Windows10 19041 x64
    • Python: Python 3.8.3 64-bit
    • VS Code: 1.45.1
    • Python Extension:2020.5.80290
  • Settings

    {
        "python.jediEnabled": false,
        "python.analysis.memory.keepLibraryAst": true,
        "python.analysis.memory.keepLibraryLocalVariables": true
    }
    

Expected behaviour

When import <something> or from <module> import <something> , can find all references of <something> in the import code.

Actual behaviour

When find all references, just get “no results” or the package file of the module. But with Jedi enabled, everything works fine.

Steps to reproduce

Notice: In this gif, can't find ref of sleep in the main code, but succeeded in the later test.

1

Code Snippet

import random
from time import sleep

random.randint(1, 2)
sleep(1)
random.randint(1, 2)
sleep(1)
random.random()

Logs

Hover in file:///e:/Programming/Python/py-final-work/test.py at (7, 3)
Hover in file:///e:/Programming/Python/py-final-work/test.py at (0, 11)
Hover in file:///e:/Programming/Python/py-final-work/test.py at (0, 10)
Code Action in file:///e:/Programming/Python/py-final-work/test.py at (0, 11) - (0, 11)
References in file:///e:/Programming/Python/py-final-work/test.py at (0, 11)
Code Action in file:///c:/program files/windowsapps/pythonsoftwarefoundation.python.3.8_3.8.1008.0_x64__qbz5n2kfra8p0/lib/random.py at (0, 0) - (0, 0)
Code Action in file:///e:/Programming/Python/py-final-work/test.py at (0, 0) - (0, 0)
Code Action in file:///e:/Programming/Python/py-final-work/test.py at (0, 11) - (0, 11)
Hover in file:///e:/Programming/Python/py-final-work/test.py at (1, 9)
Hover in file:///e:/Programming/Python/py-final-work/test.py at (1, 7)
Hover in file:///e:/Programming/Python/py-final-work/test.py at (1, 19)
Document highlight in file:///e:/Programming/Python/py-final-work/test.py at (1, 19)
Code Action in file:///e:/Programming/Python/py-final-work/test.py at (1, 19) - (1, 19)
References in file:///e:/Programming/Python/py-final-work/test.py at (1, 19)
Hover in file:///e:/Programming/Python/py-final-work/test.py at (4, 2)
Document highlight in file:///e:/Programming/Python/py-final-work/test.py at (4, 2)
Code Action in file:///e:/Programming/Python/py-final-work/test.py at (4, 2) - (4, 2)
References in file:///e:/Programming/Python/py-final-work/test.py at (4, 2)
[Error - 下午1:50:14] Request textDocument/references failed.
  Message: Object reference not set to an instance of an object.
  Code: -32000
   at Microsoft.Python.LanguageServer.Sources.ReferenceSource.FindAllReferencesAsync(Uri uri, SourceLocation location, ReferenceSearchOptions options, CancellationToken cancellationToken) in E:\A\_work\3\s\src\LanguageServer\Impl\Sources\ReferenceSource.cs:line 74
   at Microsoft.Python.LanguageServer.Implementation.LanguageServer.FindReferences(JToken token, CancellationToken cancellationToken) in E:\A\_work\3\s\src\LanguageServer\Impl\LanguageServer.cs:line 212
Hover in file:///e:/Programming/Python/py-final-work/test.py at (3, 7)
Document highlight in file:///e:/Programming/Python/py-final-work/test.py at (3, 10)
Code Action in file:///e:/Programming/Python/py-final-work/test.py at (3, 10) - (3, 10)
References in file:///e:/Programming/Python/py-final-work/test.py at (3, 10)
Code Action in file:///c:/program files/windowsapps/pythonsoftwarefoundation.python.3.8_3.8.1008.0_x64__qbz5n2kfra8p0/lib/random.py at (0, 0) - (0, 0)
Code Action in file:///c:/program files/windowsapps/pythonsoftwarefoundation.python.3.8_3.8.1008.0_x64__qbz5n2kfra8p0/lib/random.py at (790, 0) - (790, 0)
Code Action in file:///e:/Programming/Python/py-final-work/test.py at (0, 0) - (0, 0)
Code Action in file:///e:/Programming/Python/py-final-work/test.py at (3, 10) - (3, 10)
Document highlight in file:///e:/Programming/Python/py-final-work/test.py at (3, 5)
Code Action in file:///e:/Programming/Python/py-final-work/test.py at (3, 5) - (3, 5)
Hover in file:///e:/Programming/Python/py-final-work/test.py at (3, 3)
Code Action in file:///e:/Programming/Python/py-final-work/test.py at (3, 3) - (3, 3)
References in file:///e:/Programming/Python/py-final-work/test.py at (3, 3)

Additional lnformation

The details are below:

import random  # got it's package file
from time import sleep  # got “no results”

random.randint(1, 2)  # can find all ref of random, inclue the import code
                      # when find all ref of randint, got it's definition in package file
sleep(1)  # same as random
random.randint(1, 2)
sleep(1)
random.random()  # same as random and randint

After testing codes above, I tried importing some other modules and found that find all ref is releated with go to def:

  • sleep and random in main code, go to def point to the import code, so I can find all ref
  • random in import code, go to def point to it's package file, so I got that
  • sleep and time im import code, can't find it's def, so I got “no results”

Is this a bug or by design?

The details are below, notice that from os.path import abspath, dirname can find all ref of abspath and dirname , it's interesting:

# --->
# codes below are like this
# from <module> import <something>

# ---> 1
# when find all ref of <module>, got its local package file
# when find all ref of <something>, got “no results”
from os import makedirs
from json import dumps, loads, JSONDecodeError
from urllib.parse import urljoin
from functools import wraps

# ---> 2
# both of <module> and <something> got “no results”
# neither of them can go to def
from time import sleep

# ---> 3
# <module> can neither find all ref nor go to def
# <something> can find all ref and go to def, it's the only one can find all ref in my test
# specially, go to def of <something> point to it's package file
from os.path import abspath, dirname


print(dirname("folder/test.py"))
print(abspath("test.py"))

And I noticed that when I reload the test file, I will got “nnresolved import warnings” at first, is this related with the find all ref failure?

I also tested those with Jedi enabled, and can find all ref everywhere and go to def correctly (i.e. imported things' definition point to it's package file not the import code)

mozkileo avatar Jun 02 '20 10:06 mozkileo