jupyter-lsp / jupyterlab-lsp

Coding assistance for JupyterLab (code navigation + hover suggestions + linters + autocompletion + rename) using Language Server Protocol
https://jupyterlab-lsp.readthedocs.io
BSD 3-Clause "New" or "Revised" License
1.82k stars 148 forks source link

The 'Jump to definition' doesn't work properly inside the folder. #466

Open withsmilo opened 3 years ago

withsmilo commented 3 years ago

Description

The 'Jump to definition' doesn't work properly inside the folder.

Reproduce

  1. make a new directory named test_lsp and go into it.
  2. create a new Python file named test_func.py.
  3. create a new Notebook file named Untitled.ipynb.
  4. when I try to jump to bark definition, it failed with a No jump targets found error message.
스크린샷 2021-01-11 오후 4 44 12

Expected behavior

It should be jumped to test_func.py.

Context

Required: installed server extensions
Paste the output from running `jupyter server extension list` (JupyterLab >= 3)
or `jupyter serverextension list` (JupyterLab < 3) from the command line here.
You may want to sanitize the paths in the output.

config dir: /root/.jupyter
    jupyterlab_git  enabled
    - Validating...
      jupyterlab_git 0.22.2 OK
    jupyterlab_code_formatter  enabled
    - Validating...
      jupyterlab_code_formatter  OK
    jupyter_archive  enabled
    - Validating...
      jupyter_archive  OK
    jupyter_conda  enabled
    - Validating...
      jupyter_conda 3.4.1 OK
config dir: /opt/conda/etc/jupyter
    jupyter_lsp  enabled
    - Validating...
      jupyter_lsp 0.9.3 OK
    jupyter_conda  enabled
    - Validating...
      jupyter_conda 3.4.1 OK
    jupyterlab  enabled
    - Validating...
      jupyterlab 2.2.9 OK
    jupyterlab_git  enabled
    - Validating...
      jupyterlab_git 0.22.2 OK
    nbdime  enabled
    - Validating...
      nbdime 2.1.0 OK
    nbresuse  enabled
    - Validating...
      nbresuse  OK
Required: installed lab extensions
Paste the output from running `jupyter labextension list` from the command line here.
You may want to sanitize the paths in the output.

JupyterLab v2.2.9
Known labextensions:
   app dir: /opt/conda/share/jupyter/lab
        @arbennett/base16-gruvbox-dark v0.1.3  enabled  OK
        @arbennett/base16-gruvbox-light v0.1.3  enabled  OK
        @arbennett/base16-mexico-light v0.1.3  enabled  OK
        @arbennett/base16-monokai v0.1.3  enabled  OK
        @arbennett/base16-one-dark v0.1.3  enabled  OK
        @arbennett/base16-outrun v0.1.3  enabled  OK
        @arbennett/base16-solarized-dark v0.1.3  enabled  OK
        @arbennett/base16-solarized-light v0.1.3  enabled  OK
        @arbennett/base16-summerfruit-light v0.1.3  enabled  OK
        @hadim/jupyter-archive v0.7.0  enabled  OK
        @jupyter-widgets/jupyterlab-manager v2.0.0  enabled  OK
        @jupyterlab/git v0.22.2  enabled  OK
        @jupyterlab/toc v4.0.0  enabled  OK
        @karosc/jupyterlab_dracula v2.0.3  enabled  OK
        @krassowski/jupyterlab-lsp v2.1.2  enabled  OK
        @krassowski/jupyterlab_go_to_definition v1.0.0  enabled  OK
        @mohirio/jupyterlab-horizon-theme v2.0.0  enabled  OK
        @ryantam626/jupyterlab_code_formatter v1.3.6  enabled  OK
        @telamonian/theme-darcula v2.0.0  enabled  OK
        @yeebc/jupyterlab_neon_theme v2.0.1  enabled  OK
        jupyterlab-execute-time v1.0.0  enabled  OK
        jupyterlab-plotly v4.12.0  enabled  OK
        jupyterlab-python-file v0.4.0  enabled  OK
        jupyterlab-system-monitor v0.6.0  enabled  OK
        jupyterlab-tailwind-theme v0.4.4  enabled  OK
        jupyterlab-theme-solarized-dark v1.0.2  enabled  OK
        jupyterlab-topbar-extension v0.5.0  enabled  OK
        jupyterlab-topbar-text v0.5.1  enabled  OK
        jupyterlab_conda v2.2.4  enabled  OK
        jupyterlab_filetree v0.2.2  enabled  OK
        jupyterlab_theme_onedark v1.0.2  enabled  OK
        nbdime-jupyterlab v2.0.1  enabled  OK
Troubleshoot Output
Paste the output from running `jupyter troubleshoot` from the command line here.
You may want to sanitize the paths in the output.
Command Line Output
Paste the output from your command line running `jupyter lab` here, use `--debug` if possible.
Browser Output (recommended for all interface issues)
Paste the output from your browser JavaScript console replacing the text in here.

To learn how to open the developer tools in your browser:
https://developer.mozilla.org/en-US/docs/Learn/Common_questions/What_are_browser_developer_tools#How_to_open_the_devtools_in_your_browser
If too many messages accumulated after many hours of working in JupyterLab,
consider refreshing the window and then reproducing the bug to reduce the noise in the logs.

krassowski commented 3 years ago

I'm afraid that I cannot help with this one as the logic for target detection is implemented in jedi/python-language-server. The relevant lines in pyls are:

https://github.com/palantir/python-language-server/blob/a91a257d2c8687a7931721d387b2ffeb6aa71fc2/pyls/plugins/definition.py#L9-L26

and

https://github.com/palantir/python-language-server/blob/a91a257d2c8687a7931721d387b2ffeb6aa71fc2/pyls/workspace.py#L243-L279

it seems that jedi_script() has an argument that might help in your case: use_document_path; if it was turned to True it would include the test_lsp/ directory in Python PATH thus the test_func.py file would be checked for definitions. However, currently it is not set when jedi_script() is being called to get go-to-definition definitions so the default False is kept. You may be able to convince pyls maintainers to make it tuneable via a setting if you are willing to make a PR (this should be quite straightforward - see the follow_imports setting for an example).

withsmilo commented 3 years ago

@krassowski I solved my problem thanks to your kind answer. This is my workaround. I would like to hear your opinion if there is a better way.

https://github.com/palantir/python-language-server/blob/a91a257d2c8687a7931721d387b2ffeb6aa71fc2/pyls/plugins/definition.py#L9-L26

def pyls_definitions(config, document, position):
    ...
    definitions = document.jedi_script(use_document_path=True).goto(
        follow_imports=settings.get('follow_imports', True),
        follow_builtin_imports=settings.get('follow_builtin_imports', True),
        **code_position)
    ...

https://github.com/palantir/python-language-server/blob/a91a257d2c8687a7931721d387b2ffeb6aa71fc2/pyls/workspace.py#L243-L279

    def jedi_script(self, position=None, use_document_path=False):
    ...
        # Extend sys_path with document's path if requested
        if use_document_path:
            sys_path += [os.path.normpath(os.path.dirname(self.path.replace(".virtual_documents", "")))]
    ...
krassowski commented 3 years ago

Thank you, great to hear it helped! This seems like an interesting solution. If there is a need to strip away to the .virtual_documents prefix our extension should handle this somewhere. I will look into it next week (time permitting).

withsmilo commented 3 years ago

@krassowski Really thanks!

withsmilo commented 3 years ago

@krassowski May I close this issue?

krassowski commented 3 years ago

Let's keep it open for reference.