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

--verbose command passed to isort via pyproject.toml or command line causes sorting to fail #143

Closed mthanded closed 2 years ago

mthanded commented 2 years ago

If --verbose is passed to isort the output of the verbose isort command ends up in the file instead of the transformed sort order

If I choose quick fix with a file containing the below:

import asyncio
from typing import List
import uvicorn
from aiokafka import AIOKafkaProducer
from fastapi import File, UploadFile

The sorted imports turn into

else-type place_module for asyncio returned STDLIB
from-type place_module for typing returned STDLIB
else-type place_module for uvicorn returned THIRDPARTY
from-type place_module for aiokafka returned THIRDPARTY
from-type place_module for fastapi returned THIRDPARTY
import asyncio
from typing import List

from aiokafka import AIOKafkaProducer
from fastapi import File, UploadFile
import uvicorn

This is the isort command with --verbose


                 _                 _
                (_) ___  ___  _ __| |_
                | |/ _/ / _ \/ '__  _/
                | |\__ \/\_\/| |  | |_
                |_|\___/\___/\_/   \_/

      isort your imports, so you don't have to.

                    VERSION 5.10.1

from-type place_module for typing returned STDLIB
else-type place_module for uvicorn returned THIRDPARTY
from-type place_module for aiokafka returned THIRDPARTY
from-type place_module for fastapi returned THIRDPARTY
Fixing /Users/mt/Projects/isort_tests/test_isort/test_isort/test.py
karthiknadig commented 2 years ago

This is by design. The way it works in the extension is, we take the plain string of text before the file is saved, and pass it to isort via stdin. isort then outputs the processed result.

If you need to use --verbose then I recommend disabling this extension and using the task.json to setup a task that does this for you using the isorts in-place edit.

Until there is a clear way to filter those out I don't see how we can support this when working with plain string buffers.

mthanded commented 2 years ago

@karthiknadig got it. This came up when debugging the sort order issue. Thanks for the clarity. Will use the task approach if needed in the future

karthiknadig commented 2 years ago

@mthanded Try setting isort.logLevel to debug. We pretty much output the entire thing we do. Should help with debugging problems in the extension.