microsoft / vscode-flake8

Linting support for python using the flake8 library.
https://marketplace.visualstudio.com/items?itemName=ms-python.flake8
MIT License
41 stars 30 forks source link

vscode over remote with flake8 extension+python extension #209

Closed jensenja closed 1 year ago

jensenja commented 1 year ago

I'm running VSCode on my M1 Mac and using the remote extension to connect to a Linux laptop. VSCode information:

Version: 1.81.1
Commit: 6c3e3dba23e8fadc360aed75ce363ba185c49794
Date: 2023-08-09T22:40:25.698Z
Electron: 22.3.18
ElectronBuildId: 22689846
Chromium: 108.0.5359.215
Node.js: 16.17.1
V8: 10.8.168.25-electron.0
OS: Darwin arm64 22.6.0

I have a workspace settings.json with the following contents:

{
    "python.analysis.autoSearchPaths": true,
    "python.analysis.enablePytestSupport": false,
    "python.analysis.extraPaths": [
        "dist/export/python/virtualenvs/python-default/3.11.4/lib/python3.11/site-packages"
    ],
    "python.analysis.typeCheckingMode": "off",
    "python.defaultInterpreterPath": "dist/export/python/virtualenvs/python-default/3.11.4/bin/python",
    "python.envFile": "${workspaceFolder}/.env",
    "python.languageServer": "Pylance",
    "python.terminal.activateEnvironment": true,
    "python.testing.cwd": "${workspaceFolder}",
    "python.testing.pytestEnabled": false,
    "python.testing.pytestPath": "${workspaceFolder}/dist/export/python/virtualenvs/pytest/3.11.4/bin/pytest",
    "python.testing.unittestEnabled": false,
    "black-formatter.importStrategy": "fromEnvironment",
    "black-formatter.interpreter": [
        "${workspaceFolder}/dist/export/python/virtualenvs/black/3.11.4/bin/python"
    ],
    "flake8.importStrategy": "fromEnvironment",
    "flake8.interpreter": [
        "${workspaceFolder}/dist/export/python/virtualenvs/flake8/3.11.4/bin/python"
    ],
    "isort.importStrategy": "fromEnvironment",
    "isort.interpreter": [
        "${workspaceFolder}/dist/export/python/virtualenvs/isort/3.11.4/bin/python"
    ],
    "mypy-type-checker.importStrategy": "fromEnvironment",
    "mypy-type-checker.interpreter": [
        "${workspaceFolder}/dist/export/python/virtualenvs/mypy/3.11.4/bin/python"
    ]
}

The virtual environments are set up by Pants. In the output logs for the Flake8 extension, I get a couple of errors but things seem to start up fine:

2023-08-21 18:02:44.078 [info] Name: Flake8
2023-08-21 18:02:44.078 [info] Module: flake8
2023-08-21 18:02:46.160 [error] Python version undefined.undefined is not supported.
2023-08-21 18:02:46.160 [error] Selected python path: undefined
2023-08-21 18:02:46.160 [error] Supported versions are 3.7 and above.
2023-08-21 18:02:46.169 [info] Server run command: /home/jjensen/code/nom/new-gitlab/technodrome-backend/dist/export/python/virtualenvs/flake8/3.11.4/bin/python /home/jjensen/.vscode-server/extensions/ms-python.flake8-2023.6.0/bundled/tool/lsp_server.py
2023-08-21 18:02:46.170 [info] Server: Start requested.
2023-08-21 18:02:46.669 [info] CWD Server: /home/jjensen/code/nom/new-gitlab/technodrome-backend
2023-08-21 18:02:46.669 [info] sys.path used to run Server:
   /home/jjensen/.vscode-server/extensions/ms-python.flake8-2023.6.0/bundled/tool
   /home/jjensen/.pyenv/versions/3.11.4/lib/python311.zip
   /home/jjensen/.pyenv/versions/3.11.4/lib/python3.11
   /home/jjensen/.pyenv/versions/3.11.4/lib/python3.11/lib-dynload
   /home/jjensen/code/nom/new-gitlab/technodrome-backend/dist/export/python/virtualenvs/flake8/3.11.4/lib/python3.11/site-packages
   /home/jjensen/.vscode-server/extensions/ms-python.flake8-2023.6.0/bundled/libs
2023-08-21 18:02:46.669 [info] Settings used to run Server:
[
    {
        "cwd": "/home/jjensen/code/nom/new-gitlab/technodrome-backend",
        "workspace": "file:///home/jjensen/code/nom/new-gitlab/technodrome-backend",
        "args": [],
        "severity": {
            "E": "Error",
            "F": "Error",
            "I": "Information",
            "W": "Warning"
        },
        "path": [],
        "interpreter": [
            "/home/jjensen/code/nom/new-gitlab/technodrome-backend/dist/export/python/virtualenvs/flake8/3.11.4/bin/python"
        ],
        "importStrategy": "fromEnvironment",
        "showNotifications": "off"
    }
]

2023-08-21 18:02:46.669 [info] Global settings:
{
    "cwd": "/home/jjensen",
    "workspace": "/home/jjensen",
    "args": [],
    "severity": {
        "E": "Error",
        "F": "Error",
        "I": "Information",
        "W": "Warning"
    },
    "path": [],
    "interpreter": [],
    "importStrategy": "useBundled",
    "showNotifications": "off"
}

2023-08-21 18:02:46.670 [info] /home/jjensen/code/nom/new-gitlab/technodrome-backend/dist/export/python/virtualenvs/flake8/3.11.4/bin/python -m flake8 --version
2023-08-21 18:02:46.670 [info] CWD Linter: /home/jjensen/code/nom/new-gitlab/technodrome-backend
2023-08-21 18:02:46.694 [info] 
5.0.4 (mccabe: 0.7.0, pycodestyle: 2.9.1, pyflakes: 2.5.0) CPython 3.11.4 on
Linux

2023-08-21 18:02:46.694 [info] Version info for linter running for /home/jjensen/code/nom/new-gitlab/technodrome-backend:
5.0.4 (mccabe: 0.7.0, pycodestyle: 2.9.1, pyflakes: 2.5.0) CPython 3.11.4 on
Linux

2023-08-21 18:02:46.694 [info] SUPPORTED flake8>=5.0.0
FOUND flake8==5.0.4

But - I do get errors in the "Python" output:

2023-08-21 18:02:43.467 [info] Experiment 'pythonPromptNewFormatterExt' is active
2023-08-21 18:02:43.468 [info] Experiment 'pythonPromptNewToolsExt' is active
2023-08-21 18:02:43.468 [info] Experiment 'pythonTestAdapter' is active
2023-08-21 18:02:43.833 [info] Test server listening.
2023-08-21 18:02:43.972 [warning] Failed to check if ${workspaceFolder}/dist/export/python/virtualenvs/flake8/3.11.4/bin/python is an executable [Error: ENOENT: no such file or directory, lstat '${workspaceFolder}/dist/export/python/virtualenvs/flake8/3.11.4/bin/python'] {
  errno: -2,
  code: 'ENOENT',
  syscall: 'lstat',
  path: '${workspaceFolder}/dist/export/python/virtualenvs/flake8/3.11.4/bin/python'
}
2023-08-21 18:02:44.049 [warning] Identifier for virt-virtualenv failed to identify ${workspaceFolder}/dist/export/python/virtualenvs/flake8/3.11.4/bin/python [Error: ENOENT: no such file or directory, scandir '${workspaceFolder}/dist/export/python/virtualenvs/flake8/3.11.4/bin'] {
  errno: -2,
  code: 'ENOENT',
  syscall: 'scandir',
  path: '${workspaceFolder}/dist/export/python/virtualenvs/flake8/3.11.4/bin'
}
2023-08-21 18:02:44.073 [info] > ./dist/export/python/virtualenvs/python-default/3.11.4/bin/python -I ~/.vscode-server/extensions/ms-python.python-2023.14.0/pythonFiles/get_output_via_markers.py ~/.vscode-server/extensions/ms-python.python-2023.14.0/pythonFiles/interpreterInfo.py
2023-08-21 18:02:44.101 [info] Python interpreter path: ./dist/export/python/virtualenvs/python-default/3.11.4/bin/python
2023-08-21 18:02:44.139 [info] > ${workspaceFolder}/dist/export/python/virtualenvs/flake8/3.11.4/bin/python -I ~/.vscode-server/extensions/ms-python.python-2023.14.0/pythonFiles/get_output_via_markers.py ~/.vscode-server/extensions/ms-python.python-2023.14.0/pythonFiles/interpreterInfo.py
2023-08-21 18:02:44.148 [error] [Error: Command failed: ${workspaceFolder}/dist/export/python/virtualenvs/flake8/3.11.4/bin/python -I /home/jjensen/.vscode-server/extensions/ms-python.python-2023.14.0/pythonFiles/get_output_via_markers.py /home/jjensen/.vscode-server/extensions/ms-python.python-2023.14.0/pythonFiles/interpreterInfo.py
/bin/sh: line 1: /dist/export/python/virtualenvs/flake8/3.11.4/bin/python: No such file or directory

    at ChildProcess.exithandler (node:child_process:400:12)
    at ChildProcess.emit (node:events:513:28)
    at maybeClose (node:internal/child_process:1093:16)
    at Socket.<anonymous> (node:internal/child_process:451:11)
    at Socket.emit (node:events:513:28)
    at Pipe.<anonymous> (node:net:757:14)] {
  code: 127,
  killed: false,
  signal: null,
  cmd: '${workspaceFolder}/dist/export/python/virtualenvs/flake8/3.11.4/bin/python -I /home/jjensen/.vscode-server/extensions/ms-python.python-2023.14.0/pythonFiles/get_output_via_markers.py /home/jjensen/.vscode-server/extensions/ms-python.python-2023.14.0/pythonFiles/interpreterInfo.py'
}
2023-08-21 18:02:45.413 [info] Starting Pylance language server.
2023-08-21 18:02:46.157 [info] > ${workspaceFolder}/dist/export/python/virtualenvs/flake8/3.11.4/bin/python -I ~/.vscode-server/extensions/ms-python.python-2023.14.0/pythonFiles/get_output_via_markers.py ~/.vscode-server/extensions/ms-python.python-2023.14.0/pythonFiles/interpreterInfo.py
2023-08-21 18:02:46.160 [error] [Error: Command failed: ${workspaceFolder}/dist/export/python/virtualenvs/flake8/3.11.4/bin/python -I /home/jjensen/.vscode-server/extensions/ms-python.python-2023.14.0/pythonFiles/get_output_via_markers.py /home/jjensen/.vscode-server/extensions/ms-python.python-2023.14.0/pythonFiles/interpreterInfo.py
/bin/sh: line 1: /dist/export/python/virtualenvs/flake8/3.11.4/bin/python: No such file or directory

    at ChildProcess.exithandler (node:child_process:400:12)
    at ChildProcess.emit (node:events:513:28)
    at maybeClose (node:internal/child_process:1093:16)
    at Socket.<anonymous> (node:internal/child_process:451:11)
    at Socket.emit (node:events:513:28)
    at Pipe.<anonymous> (node:net:757:14)] {
  code: 127,
  killed: false,
  signal: null,
  cmd: '${workspaceFolder}/dist/export/python/virtualenvs/flake8/3.11.4/bin/python -I /home/jjensen/.vscode-server/extensions/ms-python.python-2023.14.0/pythonFiles/get_output_via_markers.py /home/jjensen/.vscode-server/extensions/ms-python.python-2023.14.0/pythonFiles/interpreterInfo.py'
}

Now - those errors go away (in both the Python extension output as well as the Flake8 extension output) as soon as I change flake8.interpreter in my settings.json to the full absolute path of the Python interpreter in my flake8 virtualenv:

    "flake8.interpreter": [
        "/home/jjensen/code/nom/new-gitlab/technodrome-backend/dist/export/python/virtualenvs/flake8/3.11.4/bin/python"
    ]

EDIT: I get the errors in both extensions if i also set flake8.interpreter to a relative path from the repository root, ie:

    "flake8.interpreter": [
        "dist/export/python/virtualenvs/flake8/3.11.4/bin/python"
    ]

Since this is intended to be workspace settings (and are checked into my repo) this obviously isn't ideal - can someone point me in the correct direction of how to resolve this?

karthiknadig commented 1 year ago

@jensenja The errors on Python extension are from python extension probing the various python installs it finds on your system. You don't need to do anything in particular as long as your flake8 extension is running and working.

You only need to set flake8.interpreter if you want to use a specific interpreter for that particular extension. This setting is there for the rare cases where users want to use custom pythons. Also, to set relative paths, you can use ${workspaceFolder} as a variable to represent your project, and you can use relative pathing from that.

But, based on what you have shown in the logs, you don't need that setting.

FYI, If you want to try out a faster version of flake8 try ruff, whihc also has a similar extension in the VS Code marketplace.

jensenja commented 1 year ago

Thanks @karthiknadig -

The errors on Python extension are from python extension probing the various python installs it finds on your system. You don't need to do anything in particular as long as your flake8 extension is running and working.

Understood - so I take it that the error messages regarding the Python extension not being able to stat files for Flake8 are cosmetic?

You only need to set flake8.interpreter if you want to use a specific interpreter for that particular extension. This setting is there for the rare cases where users want to use custom pythons.

That's right - As I'd mentioned I'm working on a Python monorepo that's using Pants so I'd like to be using the same version of Flake8 that comes bundled with it, hence me exporting its included version of Flake8 as a virtual environment, and then pointing the Flake8 extension to use that. This seems to work perfectly fine with Black/MyPy/isort - Flake8 is the only extension that seems to be causing the Python extension to spit out these errors.

karthiknadig commented 1 year ago

Python extension not being able to stat files for Flake8 are cosmetic?

That is correct. You can ignore those errors.

jensenja commented 1 year ago

Great - thank you @karthiknadig - I'll go ahead and close.