Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

isort does not pick up settings if they are not in the workspace folder #53

Closed
den-is opened this issue May 31, 2022 · 26 comments
Closed
Labels
bug Issue identified by VS Code Team member as probable bug needs investigation

Comments

@den-is
Copy link

den-is commented May 31, 2022

I have two VSCode dev environments:

One is my local workstation VScode (mac) with the latest isort and black available in $PATH.
The other one is pure devcontainer with controlled environment and the latest isort and black available as well.

Before the latest vscode isort and black extensions. I was able to pick the correct configuration using isort.cfg in the root of each project.

That way both VScode environments where not fighting with each other and were not applying different "standards".

With the installation of "ms-python.isort" the situation has changed:
Local vscode is using new "built-in" isort.
While I by default was not able to install dev-extenstion in dev-container - container continued using standard isort.

this is the only isort config included in both vscode settings and isort.cfg

"isort.args":[
		"--profile",
		"black",
		"--skip-gitignore",
		"--line-length",
		"99"
],

Now ms-python.isort is trying to apply this standard:

import logging

from config import settings as s
from selenium import webdriver

while native isort is trying to apply:

import logging

from selenium import webdriver

from config import settings as s

MS extension is putting local module with the third-party module in one section and alphabetically above it.

Your ms-python.isort built-in isort is using "wrong", non-default sections setting. Thas is documented here:
https://pycqa.github.io/isort/docs/configuration/custom_sections_and_ordering.html

I need this:

sections=FUTURE,STDLIB,FIRSTPARTY,THIRDPARTY,LOCALFOLDER

Providing "isort.path": ["/usr/local/bin/isort"] setting has completely broken Formatting. Will open separate issue.

@karthiknadig
Copy link
Member

@den-is The built in isort is the same one as the published here https://pypi.org/project/isort/5.10.1/. But isort is invoked using the standard input method, using runpy.run_method. Can you provide the layout of project?

Can you also try adding this?

"isort.args":["--profile", "black", "--skip-gitignore", "--line-length", "99", "--settings-file", "./isort.cfg"],

@karthiknadig karthiknadig added bug Issue identified by VS Code Team member as probable bug needs investigation and removed triage-needed Issue is not triaged. labels May 31, 2022
@den-is
Copy link
Author

den-is commented May 31, 2022

@karthiknadig
I'm not able to provide a project's structure. But it is extremely primitive.

regarding including "--settings-file", "./isort.cfg"
First I've got error, because I'm using "multi-folder" vscode project:

isort.exceptions.InvalidSettingsPath: isort was told to use the settings_path: /Users/den/work/sky/projects/isort.cfg as the base directory or file that represents the starting point of config file discovery, but it does not exist.

ok... let's fix it and add isort.cfg there.
my default isort.cfg had no "section" config i was always using default

[settings]
profile=black
line_length=99
skip_gitignore=true

Adding that setting still made to move my LOCALFILE and put it with THIRDPARTY

adding: sections=FUTURE,STDLIB,FIRSTPARTY,THIRDPARTY,LOCALFOLDER to isort.cfg did not work :/

these are all vscode settings related to python that I have:

    "python.languageServer": "Pylance",
    "python.formatting.provider": "black",
    "python.linting.pylintArgs": [
        "--disable=missing-docstring,unused-import",
        "--max-line-length=99"
    ],
    "python.linting.flake8Args": [
        "--max-line-length=99",
        "--ignore=E402,F841,F401"
    ],
    "python.linting.pylintPath": "/home/vscode/.local/bin/pylint",
    "[python]": {
        "editor.tabSize": 4,
        "editor.defaultFormatter": "ms-python.black-formatter",
        "editor.formatOnSave": true,
        "editor.codeActionsOnSave": {
            "source.organizeImports": true
        }
    },
    "black-formatter.args" : [
        "--line-length",
        "99"
    ],
    // "isort.path": ["/usr/local/bin/isort"],
    "isort.args": [
        "--profile",
        "black",
        "--line-length",
        "99",
        "--skip-gitignore",
        "--settings-file",
        "./.isort.cfg",
    ],

my complete .devcontainer settings, feel free to use it:
image definition https://github.com/den-is/vscode-dev-envs/tree/master/python

{

	"name": "py3",

	"image": "diskandarov/pydevenv",

	// "forwardPorts": [5000],

	"settings": {
		"editor.tabSize": 2,
		"terminal.integrated.defaultProfile.linux": "zsh",
		"python.defaultInterpreterPath": "/usr/local/bin/python",
		"python.linting.enabled": true,
		"python.linting.pylintEnabled": false,
		"python.linting.flake8Enabled": true,
		"python.formatting.provider": "black",
		"python.formatting.blackPath": "/usr/local/py-utils/bin/black",
		// required if not using "ms-python.black-formatter" ext
		"python.formatting.blackArgs": ["--line-length", "99"],
		"python.linting.flake8Path": "/usr/local/py-utils/bin/flake8",
		"python.sortImports.path": "/usr/local/py-utils/bin/isort",
		"python.linting.pylintPath": "/usr/local/py-utils/bin/pylint",
		"python.formatting.autopep8Path": "/usr/local/py-utils/bin/autopep8",
		"python.formatting.yapfPath": "/usr/local/py-utils/bin/yapf",
		"python.linting.banditPath": "/usr/local/py-utils/bin/bandit",
		"python.linting.mypyPath": "/usr/local/py-utils/bin/mypy",
		"python.linting.pycodestylePath": "/usr/local/py-utils/bin/pycodestyle",
		"python.linting.pydocstylePath": "/usr/local/py-utils/bin/pydocstyle",
		"python.languageServer": "Pylance",
		"[python]": {
			"editor.tabSize": 4,
			// requires "ms-python.black-formatter"
			// not able to install not-released "dev" extension into devcontainer
			// "editor.defaultFormatter": "ms-python.black-formatter",
			"editor.formatOnSave": true,
			"editor.codeActionsOnSave": {
					"source.organizeImports": true
			}
		},
		"black-formatter.args" : [
			"--line-length",
			"99"
		],
		"isort.args":[
				"--profile",
				"black",
				"--skip-gitignore",
				"--line-length",
				"99"
		],
		// specific linters settings should go in own config files in project's root
		"python.linting.pylintArgs": [
			"--max-line-length=99",
			"--disable=C0111,W0611"
		],
		"python.linting.flake8Args": [
			"--max-line-length=99",
			"--ignore=E402,F841,F401,W503"
		]
	},

	"extensions": [
		"ms-python.python",
		"ms-python.vscode-pylance",
		// not working, can't install unreleased dev extensions
		// "ms-python.black-formatter",
		// "ms-python.isort",
		"formulahendry.code-runner",
		"mrmlnc.vscode-duplicate",
		"redhat.vscode-yaml",
		"ahebrank.yaml2json",
		"mechatroner.rainbow-csv",
		"oderwat.indent-rainbow",
		"semgrep.semgrep",
		"esbenp.prettier-vscode"
	],

	// Comment out connect as root instead. More info: https://aka.ms/vscode-remote/containers/non-root.
	"remoteUser": "vscode",

	// Install globaly probably as root user.
	// "postCreateCommand": "pip install -r requirements.txt",

	// Install with user that runs container non-root.
	// Helps to avoid breaking dependencies and interfering with system libs.
	"postCreateCommand": ".devcontainer/startup.sh",

}

@karthiknadig
Copy link
Member

Would it be possible for you to provide a minimal repro of this, if you can't share the entire project? Note that there is a limitation in VS Code on installing pre-release versions of extension in dev container. See here on how to add them:
microsoft/vscode#149383 (comment)

@geiregjo
Copy link

geiregjo commented Jun 6, 2022

I ran into the same issue. I have black and isort extensions running in VSCode. Works like a charm. I also implemented a git pre-commit hook that runs black and isort. This can be used by teammates not using VSCode. One would expect the outcome to be the same. But the extension isort removes a new line in my imports and running it from command line adds one. I use the default settings for both.

@den-is
Copy link
Author

den-is commented Jun 7, 2022

I have really tried to come up with some trivial python package with a couple of modules in it and external dependencies. But it somehow did not work for a trivial package.

I have even tried to set a custom isort path for that extension, since you have fixed my previous issue - did not work.

I have removed that new isort extension and everything started to work as intended.

@karthiknadig
Copy link
Member

karthiknadig commented Jun 7, 2022

This might be something that I will need input from isort developer. There is a difference between how, vscode-python extension triggers this vs vscode-isort.

In vscode-python extension, we insert the path to isort bundled with the python extension and then call main on it.

isort_path = os.path.join(os.path.dirname(__file__), "lib", "python")
sys.path.insert(0, isort_path)

import isort.main

isort.main.main()

in vscode-python extension we get the diff and try to calculate the edits. The content of the file is passed in via stdin.

isort - --diff

# with --profile black
isort - --diff --profile black

Where as in vscode-isort, we insert the path to isort bundled with the python extension and then call runpy.run_module on it.

sys.path.append(str(pathlib.Path(__file__).parent.parent / "libs"))
result = utils.run_module(
                module=module, argv=argv, use_stdin=True, cwd=cwd, source=source
            )

in vscode-isort extension we get the formatted content and use it directly. The content of the file is passed in via stdin.

isort - --filename <path>

# with --profile black
isort - --filename <path> --profile black

So not really sure why there is any difference here.

@karthiknadig
Copy link
Member

Created PyCQA/isort#1940 on isort to get input on how this should be done.

@SandroJijavadze
Copy link

Ran into the same issue. Had to disable extension for everything to work as intended

@karthiknadig
Copy link
Member

@SandroJijavadze Can you try adding --show-config to isort.args and see if that shows where the extension is picking up config from? you should be able to see this in Output > isort panel.

Run that same thing isort --show-config ... from the terminal and share config details from both cases.

@SandroJijavadze
Copy link

@karthiknadig

I just reenabled isort to test your suggestion bu I can't seem to get it working at all anymore.

Restarted vscode, cleared settings.json, etc but still, I get shis error:

[2022-06-21 10:17:07.900] [renderer1] [error] Cannot read properties of null (reading 'map'): TypeError: Cannot read properties of null (reading 'map')
	at g (/home/buda/.vscode/extensions/ms-python.isort-2022.2.0/dist/extension.js:1:213014)
	at Object.asCodeAction (/home/buda/.vscode/extensions/ms-python.isort-2022.2.0/dist/extension.js:1:223954)
	at /home/buda/.vscode/extensions/ms-python.isort-2022.2.0/dist/extension.js:1:152688
	at async U.provideCodeActions (/opt/visual-studio-code/resources/app/out/vs/workbench/api/node/extensionHostProcess.js:83:103131)

@karthiknadig
Copy link
Member

karthiknadig commented Jun 21, 2022

@SandroJijavadze Can you try switching to the pre-release version? Do you see anything else in the Output > isort panel?

@SandroJijavadze
Copy link

@karthiknadig

Switched to pre-release, repeated steps (restart, test with/without options, etc) still get the same output:

[2022-06-21 10:42:13.425] [renderer1] [error] Cannot read properties of null (reading 'map'): TypeError: Cannot read properties of null (reading 'map')
	at g (/home/buda/.vscode/extensions/ms-python.isort-2022.3.11671003/dist/extension.js:1:213014)
	at Object.asCodeAction (/home/buda/.vscode/extensions/ms-python.isort-2022.3.11671003/dist/extension.js:1:223954)
	at /home/buda/.vscode/extensions/ms-python.isort-2022.3.11671003/dist/extension.js:1:152688
	at async U.provideCodeActions (/opt/visual-studio-code/resources/app/out/vs/workbench/api/node/extensionHostProcess.js:83:103131)

In isort panel:

isort - --profile black --line-length 99 --skip-gitignore --show-config --check --filename /../../../..path_to_py_file.py
CWD Formatter: /home/buda/code/py/projectdir

If I comment out settings.json then I get additional:

isort - --profile black --line-length 99 --skip-gitignore --show-config --check --filename /../../../..path_to_py_file.py
CWD Formatter: /home/buda/code/py/projectdir
CWD Format Server: /home/buda/code/py/projectdir
sys.path used to run Formatter:
    /home/buda/.vscode/extensions/ms-python.isort-2022.3.11671003/bundled/formatter
    /usr/lib/python310.zip
    /usr/lib/python3.10
    /usr/lib/python3.10/lib-dynload
    /home/buda/.local/lib/python3.10/site-packages
    /usr/lib/python3.10/site-packages
    /home/buda/.vscode/extensions/ms-python.isort-2022.3.11671003/bundled/libs

Settings used to run Formatter:
[
    {
        "workspace": "file:///home/buda/code/py/projectdir",
        "trace": "error",
        "args": [],
        "severity": {},
        "path": [],
        "interpreter": [
            "/bin/python"
        ]
    }
]

If I run isort --show-config in terminal

{
    "_known_patterns": null,
    "_section_comments": null,
    "_section_comments_end": null,
    "_skips": null,
    "_skip_globs": null,
    "_sorting_function": null,
    "py_version": "py3",
    "force_to_top": [],
    "skip": [
        "dist",
        "build",
        ".direnv",
        ".bzr",
        ".mypy_cache",
        "_build",
        ".pants.d",
        ".nox",
        ".eggs",
        "buck-out",
        "node_modules",
        ".git",
        ".hg",
        ".venv",
        "__pypackages__",
        ".tox",
        ".svn",
        "venv"
    ],
    "extend_skip": [],
    "skip_glob": [],
    "extend_skip_glob": [],
    "skip_gitignore": false,
    "line_length": 79,
    "wrap_length": 0,
    "line_ending": "",
    "sections": [
        "FUTURE",
        "STDLIB",
        "THIRDPARTY",
        "FIRSTPARTY",
        "LOCALFOLDER"
    ],
    "no_sections": false,
    "known_future_library": [
        "__future__"
    ],
    "known_third_party": [],
    "known_first_party": [],
    "known_local_folder": [],
    "known_standard_library": [
        "numbers",
        "zipapp",
        "locale",
        "configparser",
        "smtpd",
        "binascii",
        "calendar",
        "code",
        "subprocess",
        "binhex",
        "ntpath",
        "compileall",
        "rlcompleter",
        "cmath",
        "importlib",
        "signal",
        "turtle",
        "sunau",
        "time",
        "_thread",
        "errno",
        "msvcrt",
        "netrc",
        "syslog",
        "aifc",
        "formatter",
        "json",
        "imghdr",
        "sched",
        "xdrlib",
        "sre_parse",
        "ossaudiodev",
        "dummy_threading",
        "lib2to3",
        "readline",
        "token",
        "io",
        "fpectl",
        "logging",
        "socketserver",
        "platform",
        "plistlib",
        "datetime",
        "heapq",
        "argparse",
        "sre_compile",
        "traceback",
        "decimal",
        "bdb",
        "parser",
        "builtins",
        "html",
        "distutils",
        "_ast",
        "dataclasses",
        "sqlite3",
        "codeop",
        "py_compile",
        "os",
        "winreg",
        "tempfile",
        "array",
        "sre",
        "stat",
        "encodings",
        "keyword",
        "colorsys",
        "concurrent",
        "runpy",
        "graphlib",
        "pyclbr",
        "random",
        "sndhdr",
        "tty",
        "cgitb",
        "symtable",
        "curses",
        "sys",
        "termios",
        "telnetlib",
        "macpath",
        "tkinter",
        "contextlib",
        "mailcap",
        "trace",
        "fileinput",
        "zlib",
        "asyncore",
        "gettext",
        "ctypes",
        "multiprocessing",
        "threading",
        "http",
        "profile",
        "pathlib",
        "base64",
        "select",
        "math",
        "grp",
        "pdb",
        "tabnanny",
        "crypt",
        "gc",
        "operator",
        "socket",
        "csv",
        "copy",
        "cgi",
        "posix",
        "tracemalloc",
        "getpass",
        "mimetypes",
        "ast",
        "_dummy_thread",
        "marshal",
        "nis",
        "mailbox",
        "contextvars",
        "zoneinfo",
        "getopt",
        "ensurepip",
        "symbol",
        "reprlib",
        "dis",
        "ssl",
        "msilib",
        "shelve",
        "quopri",
        "pydoc",
        "secrets",
        "xmlrpc",
        "sre_constants",
        "selectors",
        "cmd",
        "fcntl",
        "atexit",
        "nntplib",
        "zipimport",
        "types",
        "asyncio",
        "itertools",
        "test",
        "filecmp",
        "imp",
        "winsound",
        "fnmatch",
        "audioop",
        "spwd",
        "codecs",
        "statistics",
        "bz2",
        "imaplib",
        "zipfile",
        "queue",
        "wsgiref",
        "faulthandler",
        "cProfile",
        "timeit",
        "struct",
        "pprint",
        "functools",
        "typing",
        "hmac",
        "linecache",
        "modulefinder",
        "shlex",
        "venv",
        "ipaddress",
        "site",
        "posixpath",
        "dbm",
        "pstats",
        "sysconfig",
        "doctest",
        "weakref",
        "re",
        "textwrap",
        "unittest",
        "uu",
        "pwd",
        "mmap",
        "ftplib",
        "xml",
        "difflib",
        "pty",
        "copyreg",
        "shutil",
        "optparse",
        "string",
        "pickle",
        "urllib",
        "tarfile",
        "enum",
        "pkgutil",
        "pickletools",
        "hashlib",
        "pipes",
        "abc",
        "gzip",
        "asynchat",
        "inspect",
        "turtledemo",
        "smtplib",
        "poplib",
        "stringprep",
        "collections",
        "fractions",
        "chunk",
        "uuid",
        "webbrowser",
        "tokenize",
        "wave",
        "glob",
        "unicodedata",
        "bisect",
        "lzma",
        "warnings",
        "resource",
        "email"
    ],
    "extra_standard_library": [],
    "known_other": {},
    "multi_line_output": "GRID",
    "forced_separate": [],
    "indent": "    ",
    "comment_prefix": "  #",
    "length_sort": false,
    "length_sort_straight": false,
    "length_sort_sections": [],
    "add_imports": [],
    "remove_imports": [],
    "append_only": false,
    "reverse_relative": false,
    "force_single_line": false,
    "single_line_exclusions": [],
    "default_section": "THIRDPARTY",
    "import_headings": {},
    "import_footers": {},
    "balanced_wrapping": false,
    "use_parentheses": false,
    "order_by_type": true,
    "atomic": false,
    "lines_before_imports": -1,
    "lines_after_imports": -1,
    "lines_between_sections": 1,
    "lines_between_types": 0,
    "combine_as_imports": false,
    "combine_star": false,
    "include_trailing_comma": false,
    "from_first": false,
    "verbose": false,
    "quiet": false,
    "force_adds": false,
    "force_alphabetical_sort_within_sections": false,
    "force_alphabetical_sort": false,
    "force_grid_wrap": 0,
    "force_sort_within_sections": false,
    "lexicographical": false,
    "group_by_package": false,
    "ignore_whitespace": false,
    "no_lines_before": [],
    "no_inline_sort": false,
    "ignore_comments": false,
    "case_sensitive": false,
    "sources": [
        {
            "py_version": "py3",
            "force_to_top": [],
            "skip": [
                "dist",
                "build",
                ".direnv",
                ".bzr",
                ".mypy_cache",
                "_build",
                ".pants.d",
                ".nox",
                ".eggs",
                "buck-out",
                "node_modules",
                ".git",
                ".hg",
                ".venv",
                "__pypackages__",
                ".tox",
                ".svn",
                "venv"
            ],
            "extend_skip": [],
            "skip_glob": [],
            "extend_skip_glob": [],
            "skip_gitignore": false,
            "line_length": 79,
            "wrap_length": 0,
            "line_ending": "",
            "sections": [
                "FUTURE",
                "STDLIB",
                "THIRDPARTY",
                "FIRSTPARTY",
                "LOCALFOLDER"
            ],
            "no_sections": false,
            "known_future_library": [
                "__future__"
            ],
            "known_third_party": [],
            "known_first_party": [],
            "known_local_folder": [],
            "known_standard_library": [
                "numbers",
                "zipapp",
                "locale",
                "configparser",
                "smtpd",
                "binascii",
                "calendar",
                "code",
                "subprocess",
                "binhex",
                "ntpath",
                "compileall",
                "rlcompleter",
                "cmath",
                "importlib",
                "signal",
                "turtle",
                "sunau",
                "time",
                "_thread",
                "errno",
                "msvcrt",
                "netrc",
                "syslog",
                "aifc",
                "formatter",
                "json",
                "imghdr",
                "sched",
                "xdrlib",
                "sre_parse",
                "ossaudiodev",
                "dummy_threading",
                "lib2to3",
                "readline",
                "token",
                "io",
                "fpectl",
                "logging",
                "socketserver",
                "platform",
                "plistlib",
                "datetime",
                "heapq",
                "argparse",
                "sre_compile",
                "traceback",
                "decimal",
                "bdb",
                "parser",
                "builtins",
                "html",
                "distutils",
                "_ast",
                "dataclasses",
                "sqlite3",
                "codeop",
                "py_compile",
                "os",
                "winreg",
                "tempfile",
                "array",
                "sre",
                "stat",
                "encodings",
                "keyword",
                "colorsys",
                "concurrent",
                "runpy",
                "graphlib",
                "pyclbr",
                "random",
                "sndhdr",
                "tty",
                "cgitb",
                "symtable",
                "curses",
                "sys",
                "termios",
                "telnetlib",
                "macpath",
                "tkinter",
                "contextlib",
                "mailcap",
                "trace",
                "fileinput",
                "zlib",
                "asyncore",
                "gettext",
                "ctypes",
                "multiprocessing",
                "threading",
                "http",
                "profile",
                "pathlib",
                "base64",
                "select",
                "math",
                "grp",
                "pdb",
                "tabnanny",
                "crypt",
                "gc",
                "operator",
                "socket",
                "csv",
                "copy",
                "cgi",
                "posix",
                "tracemalloc",
                "getpass",
                "mimetypes",
                "ast",
                "_dummy_thread",
                "marshal",
                "nis",
                "mailbox",
                "contextvars",
                "zoneinfo",
                "getopt",
                "ensurepip",
                "symbol",
                "reprlib",
                "dis",
                "ssl",
                "msilib",
                "shelve",
                "quopri",
                "pydoc",
                "secrets",
                "xmlrpc",
                "sre_constants",
                "selectors",
                "cmd",
                "fcntl",
                "atexit",
                "nntplib",
                "zipimport",
                "types",
                "asyncio",
                "itertools",
                "test",
                "filecmp",
                "imp",
                "winsound",
                "fnmatch",
                "audioop",
                "spwd",
                "codecs",
                "statistics",
                "bz2",
                "imaplib",
                "zipfile",
                "queue",
                "wsgiref",
                "faulthandler",
                "cProfile",
                "timeit",
                "struct",
                "pprint",
                "functools",
                "typing",
                "hmac",
                "linecache",
                "modulefinder",
                "shlex",
                "venv",
                "ipaddress",
                "site",
                "posixpath",
                "dbm",
                "pstats",
                "sysconfig",
                "doctest",
                "weakref",
                "re",
                "textwrap",
                "unittest",
                "uu",
                "pwd",
                "mmap",
                "ftplib",
                "xml",
                "difflib",
                "pty",
                "copyreg",
                "shutil",
                "optparse",
                "string",
                "pickle",
                "urllib",
                "tarfile",
                "enum",
                "pkgutil",
                "pickletools",
                "hashlib",
                "pipes",
                "abc",
                "gzip",
                "asynchat",
                "inspect",
                "turtledemo",
                "smtplib",
                "poplib",
                "stringprep",
                "collections",
                "fractions",
                "chunk",
                "uuid",
                "webbrowser",
                "tokenize",
                "wave",
                "glob",
                "unicodedata",
                "bisect",
                "lzma",
                "warnings",
                "resource",
                "email"
            ],
            "extra_standard_library": [],
            "known_other": {},
            "multi_line_output": "GRID",
            "forced_separate": [],
            "indent": "    ",
            "comment_prefix": "  #",
            "length_sort": false,
            "length_sort_straight": false,
            "length_sort_sections": [],
            "add_imports": [],
            "remove_imports": [],
            "append_only": false,
            "reverse_relative": false,
            "force_single_line": false,
            "single_line_exclusions": [],
            "default_section": "THIRDPARTY",
            "import_headings": {},
            "import_footers": {},
            "balanced_wrapping": false,
            "use_parentheses": false,
            "order_by_type": true,
            "atomic": false,
            "lines_before_imports": -1,
            "lines_after_imports": -1,
            "lines_between_sections": 1,
            "lines_between_types": 0,
            "combine_as_imports": false,
            "combine_star": false,
            "include_trailing_comma": false,
            "from_first": false,
            "verbose": false,
            "quiet": false,
            "force_adds": false,
            "force_alphabetical_sort_within_sections": false,
            "force_alphabetical_sort": false,
            "force_grid_wrap": 0,
            "force_sort_within_sections": false,
            "lexicographical": false,
            "group_by_package": false,
            "ignore_whitespace": false,
            "no_lines_before": [],
            "no_inline_sort": false,
            "ignore_comments": false,
            "case_sensitive": false,
            "sources": [],
            "virtual_env": "",
            "conda_env": "",
            "ensure_newline_before_comments": false,
            "directory": "",
            "profile": "",
            "honor_noqa": false,
            "src_paths": [],
            "old_finders": false,
            "remove_redundant_aliases": false,
            "float_to_top": false,
            "filter_files": false,
            "formatter": "",
            "formatting_function": null,
            "color_output": false,
            "treat_comments_as_code": [],
            "treat_all_comments_as_code": false,
            "supported_extensions": [
                "pyi",
                "pyx",
                "py",
                "pxd"
            ],
            "blocked_extensions": [
                "pex"
            ],
            "constants": [],
            "classes": [],
            "variables": [],
            "dedup_headings": false,
            "only_sections": false,
            "only_modified": false,
            "combine_straight_imports": false,
            "auto_identify_namespace_packages": true,
            "namespace_packages": [],
            "follow_links": true,
            "indented_import_headings": true,
            "honor_case_in_force_sorted_sections": false,
            "sort_relative_in_force_sorted_sections": false,
            "overwrite_in_place": false,
            "reverse_sort": false,
            "star_first": false,
            "git_ignore": {},
            "format_error": "{error}: {message}",
            "format_success": "{success}: {message}",
            "sort_order": "natural",
            "source": "defaults"
        }
    ],
    "virtual_env": "",
    "conda_env": "",
    "ensure_newline_before_comments": false,
    "directory": "/home/buda/code/py/projectdir",
    "profile": "",
    "honor_noqa": false,
    "src_paths": [
        "/home/buda/code/py/projectdir/src",
        "/home/buda/code/py/projectdir"
    ],
    "old_finders": false,
    "remove_redundant_aliases": false,
    "float_to_top": false,
    "filter_files": false,
    "formatter": "",
    "formatting_function": null,
    "color_output": false,
    "treat_comments_as_code": [],
    "treat_all_comments_as_code": false,
    "supported_extensions": [
        "pyi",
        "pyx",
        "py",
        "pxd"
    ],
    "blocked_extensions": [
        "pex"
    ],
    "constants": [],
    "classes": [],
    "variables": [],
    "dedup_headings": false,
    "only_sections": false,
    "only_modified": false,
    "combine_straight_imports": false,
    "auto_identify_namespace_packages": true,
    "namespace_packages": [],
    "follow_links": true,
    "indented_import_headings": true,
    "honor_case_in_force_sorted_sections": false,
    "sort_relative_in_force_sorted_sections": false,
    "overwrite_in_place": false,
    "reverse_sort": false,
    "star_first": false,
    "git_ignore": {},
    "format_error": "{error}: {message}",
    "format_success": "{success}: {message}",
    "sort_order": "natural"

@karthiknadig
Copy link
Member

@SandroJijavadze I see two other issues that I need to address before we can get to digging into the original problem here.

  1. --show-config should just work. I created isort should show config on start #78 to show what isort will use on start. So there should not be a need to run with --show-config.
  2. The TypeError, that seems to be a LS protocol error. TypeError: Cannot read properties of null (reading 'map') #77

I will post next steps on those issues. We will come back to this once we have that. It should make it easier to dig into the difference between bundled isort and isort in terminal.

@karthiknadig
Copy link
Member

After investigation it looks like this comes from isort not searching for configuration when using stdin with --filename. See PyCQA/isort#1989

@karthiknadig karthiknadig changed the title Built-in isort is using non default sections sorting order isort does not pick up settings if they are not in the workspace folder Nov 9, 2022
@karthiknadig karthiknadig pinned this issue Nov 9, 2022
@bluenote10
Copy link

bluenote10 commented Nov 10, 2022

FYI I've also just raised microsoft/vscode-python#20205 because this actually used to work for us before updating to VScode 1.73.1. Perhaps the minimal reproduction example I've created there may be of any help?

@karthiknadig
Copy link
Member

karthiknadig commented Nov 10, 2022

@bluenote10 the problem is with how isort works. We have a minimal repro see here: PyCQA/isort#1989 , this can be reproduced just via command line (even without the extension). The thing is we need this fixed in isort to properly support organize imports on save. Because the content is not yet written to disk, we have to send it via stdin. That is where this is failing. Please upvote that issue.

Python extension used to work around this by creating a temporary file and saving the content there, and then running isort on it. The issue with that approach was that as VS Code started to light up newer ways to work with repositories, we can't continue to support that workaround for isort.

Here is a work around, add this to your setting: "isort.args": ["--settings-path", "${workspaceFolder}/<project>/pyproject.toml"]

You could actually turn this into a multi root project, with your main directory, and your sub-directory as two roots. Like this:
Create a myproject.code-workspace, with this content, and open that with VS Code.:

{
    "folders": [
        {
            "path": "."
        },
        {
            "path": "./py_project_root"
        }
    ],
    "settings": {
           // common settings go here.
    }
}

That will allow isort to treat it as a individual project, and run isort with py_project_root as current working directory. depending on where the file is.

@karthiknadig
Copy link
Member

Another workaround for this using the tasks and keybinding:
microsoft/vscode-python#18714 (comment)

@brettcannon
Copy link
Member

Yeah, I'm wondering if providing a task as a fallback option makes sense? Otherwise we could have the language server fallback to subprocess if the config file is found somewhere else (although honestly I think it would be better if folks specified the config location instead).

@Olindholm
Copy link

Since PyCQA/isort#1992 is fixed now. Am I understanding it correctly that this should be also be resolved?

I installed latest isort, pre-release version of ms-python.isort. But still no luck, still experiencing this issue.

@karthiknadig
Copy link
Member

@Olindholm Please see the isort change log, it hasn't been released to pypi yet: https://github.com/PyCQA/isort/blob/main/CHANGELOG.md

@garysassano
Copy link

I always put the pyproject.toml file in the root folder of the workspace and yet vscode-isort wouldn't work by default: it doesn't run the isort command like you would manually do from the terminal, so it doesn't read the [tool.isort] section of the pyproject.toml file where all the settings are listed.

The only way I could make vscode-isort work is by adding "isort.path": ["isort"] to my VS Code settings.json file.

@matteoaletti
Copy link

Same problem here, I also tried to debug with the -v option passed as args in the settings, but the output is written to the file sorted instead of being written to the log.

@karthiknadig
Copy link
Member

@matteoaletti isort has not released the fix for this, so not much can be done about it. You could try and install directly from the github main. An alternative is to pass the configuration using command line arguments.

As for getting config info. you can set the log level for the isort extension to debug or trace and reload VS Code. The extension will get the --show-config output in the logs.

If people are looking for alternatives, look for Ruff extension. It is a multi-tool of python tools, it has import sorting, formatting support, and implements several linte rules (like pylint, flake8, pycodestyle, pydocstyle, bandit, pyflakes etc). It is implemented in rust and is blazingly fast. It is built on top of our templates for contributing python tools extensions for VS Code.
See docs for ruff: https://docs.astral.sh/ruff/

@matteoaletti
Copy link

@karthiknadig thank you for your answer, I had indeed tried the "develop" version of isort and it works!

@meshantz
Copy link

meshantz commented Oct 2, 2024

This appears to have been fixed as of isort 5.13.0 (though that version seems to have included all of its development dependencies 😂).

Updating the bundled isort to the latest isort should clear this up (as of this comment that is version 5.13.2)

Until that is done you should be able to work with a local (virtualenv installation) by installing said versions into your environment and using the "fromEnvironment" import strategy in the vscode/isort settings.

image

json for that setting is

{
  "isort.importStrategy": "fromEnvironment"
}

@den-is
Copy link
Author

den-is commented Nov 25, 2024

Found updates this thread in my email. Decided to share my workflow.
Already long time ago I've switched to ruff and the official ruff extension for VSCode.

Ruff formatting - formats imports only, also configured to use black style
Ruff linting - does linting
Black - formats everything else except imports

@den-is den-is closed this as completed Nov 25, 2024
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
bug Issue identified by VS Code Team member as probable bug needs investigation
Projects
None yet
Development

No branches or pull requests

10 participants