microsoft / vscode-isort

Import sorting for python using the isort library.
https://marketplace.visualstudio.com/items?itemName=ms-python.isort
MIT License
87 stars 21 forks source link

isort formatting error #32

Closed sudheer82 closed 2 years ago

sudheer82 commented 2 years ago

Unable to use isort extension. always facing the following error when reload the windows

Formatting error, please see Output > isort for more info: Traceback (most recent call last): File "c:\Users\user\.vscode\extensions\ms-python.isort-2022.1.11261002\bundled\formatter\format_server.py", line 140, in _run result = utils.run_module( File "c:\Users\user\.vscode\extensions\ms-python.isort-2022.1.11261002\bundled\formatter\utils.py", line 177, in run_module return _run_module(module, argv, use_stdin, source) File "c:\Users\user\.vscode\extensions\ms-python.isort-2022.1.11261002\bundled\formatter\utils.py", line 162, in _run_module runpy.run_module(module, run_name="__main__") File "C:\Users\user\AppData\Local\Programs\Python\Python310\lib\runpy.py", line 212, in run_module return _run_code(code, {}, init_globals, run_name, mod_spec) File "C:\Users\user\AppData\Local\Programs\Python\Python310\lib\runpy.py", line 86, in _run_code exec(code, run_globals) File "c:\Users\user\.vscode\extensions\ms-python.isort-2022.1.112610...

Settings:

"[python]": {
    "editor.formatOnSave": true,
    "editor.codeActionsOnSave": {
      "source.organizeImports": true
    },
    "editor.defaultFormatter": "ms-python.python"
  },
  "isort.args": [
    "--profile",
    "black"
  ],
karthiknadig commented 2 years ago

@sudheer82 Can you copy paste the contents of the Output> isort panel, the stack you have posted is incomplete, the output > python panel should have the complete stack? Please make sure that if you have isort installed in your environment it is >= 5.10.1. Run python -m pip list | findstr isort to get the isort version info from your environment.

sudheer82 commented 2 years ago

Hi Karthik,

here is the complete output of isort

Formatter Name: isort
Formatter Module: isort
CWD Format Server: c:\Users\<user>\
sys.path used to run Formatter:
    c:\Users\<user>\.vscode\extensions\ms-python.isort-2022.1.11261002\bundled\formatter
    C:\Users\<user>\AppData\Local\Programs\Python\Python310\python310.zip
    C:\Users\<user>\AppData\Local\Programs\Python\Python310\DLLs
    C:\Users\<user>\AppData\Local\Programs\Python\Python310\lib
    C:\Users\<user>\AppData\Local\Programs\Python\Python310
    C:\Users\<user>\AppData\Roaming\Python\Python310\site-packages
    C:\Users\<user>\AppData\Roaming\Python\Python310\site-packages\win32
    C:\Users\<user>\AppData\Roaming\Python\Python310\site-packages\win32\lib
    C:\Users\<user>\AppData\Roaming\Python\Python310\site-packages\Pythonwin
    C:\Users\<user>\AppData\Local\Programs\Python\Python310\lib\site-packages
    c:\Users\<user>\.vscode\extensions\ms-python.isort-2022.1.11261002\bundled\libs

Settings used to run Formatter:
[
    {
        "workspace": "file:///c%3A/Users/<user>",
        "trace": "error",
        "args": [
            "--profile",
            "black"
        ],
        "severity": {},
        "path": [],
        "interpreter": [
            "C:\\Users\\<user>\\AppData\\Local\\Programs\\Python\\Python310\\python.exe"
        ]
    }
]

isort --profile black --check --filename Untitled-1.ipynb -
CWD Formatter: c:\Users\<user>
[Error - 11:26:29 AM] Traceback (most recent call last):
  File "c:\Users\<user>\.vscode\extensions\ms-python.isort-2022.1.11261002\bundled\formatter\format_server.py", line 140, in _run
    result = utils.run_module(
  File "c:\Users\<user>\.vscode\extensions\ms-python.isort-2022.1.11261002\bundled\formatter\utils.py", line 177, in run_module
    return _run_module(module, argv, use_stdin, source)
  File "c:\Users\<user>\.vscode\extensions\ms-python.isort-2022.1.11261002\bundled\formatter\utils.py", line 162, in _run_module
    runpy.run_module(module, run_name="__main__")
  File "C:\Users\<user>\AppData\Local\Programs\Python\Python310\lib\runpy.py", line 212, in run_module
    return _run_code(code, {}, init_globals, run_name, mod_spec)
  File "C:\Users\<user>\AppData\Local\Programs\Python\Python310\lib\runpy.py", line 86, in _run_code
    exec(code, run_globals)
  File "c:\Users\<user>\.vscode\extensions\ms-python.isort-2022.1.11261002\bundled\libs\isort\__main__.py", line 3, in <module>
    main()
  File "c:\Users\<user>\.vscode\extensions\ms-python.isort-2022.1.11261002\bundled\libs\isort\main.py", line 1119, in main
    incorrectly_sorted = not api.check_stream(
  File "c:\Users\<user>\.vscode\extensions\ms-python.isort-2022.1.11261002\bundled\libs\isort\api.py", line 264, in check_stream
    changed: bool = sort_stream(
  File "c:\Users\<user>\.vscode\extensions\ms-python.isort-2022.1.11261002\bundled\libs\isort\api.py", line 189, in sort_stream
    raise FileSkipSetting(content_source)
isort.exceptions.FileSkipSetting: Untitled-1.ipynb was skipped as it's listed in 'skip' setting or matches a glob in 'skip_glob' setting
karthiknadig commented 2 years ago

Looks like instead of using the CLI, we should use the APIs available via isorts API module. This will allow us to skip the file check and get the sorted content. This is currently, occurring because isort does a file exists check on files which may not be on disk yet, because we are in the process of formatting before saving file to disk.

sudheer82 commented 2 years ago

In this case, Untitled-1.ipynb is not applied for save or format. the issue generally occurs when the save or format is being performed on other files on disk while Untitled-1.ipynb is open in one of the tabs.

karthiknadig commented 2 years ago

@sudheer82 We check all open files for import sorting issues. The underlying problem is the same, the file skip check in isort is to strict here, and causes this problem.

karthiknadig commented 2 years ago

https://github.com/microsoft/vscode-isort/pull/44 should address this