Skip to content

Current working directory included in import path #3092

@sjkillen

Description

@sjkillen

Describe the bug

When run with python3 -m black (used by vscode) black keeps the current working directory inside sys.path which can result in a crash if there are name conflicts between black's imports and the files in the current working directory

To Reproduce

  • touch ast.py
  • Run python3 -m black ast.py

Output:

python3 -m black ast.py 
Traceback (most recent call last):
  File "/usr/lib/python3.9/runpy.py", line 188, in _run_module_as_main
    mod_name, mod_spec, code = _get_module_details(mod_name, _Error)
  File "/usr/lib/python3.9/runpy.py", line 147, in _get_module_details
    return _get_module_details(pkg_main_name, error)
  File "/usr/lib/python3.9/runpy.py", line 111, in _get_module_details
    __import__(pkg_name)
  File "src/black/__init__.py", line 1, in <module>
  File "/usr/lib/python3.9/asyncio/__init__.py", line 8, in <module>
    from .base_events import *
  File "/usr/lib/python3.9/asyncio/base_events.py", line 39, in <module>
    from . import coroutines
  File "/usr/lib/python3.9/asyncio/coroutines.py", line 5, in <module>
    import inspect
  File "/usr/lib/python3.9/inspect.py", line 778, in <module>
    class _ClassFinder(ast.NodeVisitor):
AttributeError: module 'ast' has no attribute 'NodeVisitor'

Expected behaviour

For the current working directory to be removed from sys.path when black is run like this.

Environment

  • Black's version: black, 22.3.0 (compiled: yes)
  • OS and Python version: Linux/Python 3.9.7

Metadata

Metadata

Assignees

No one assigned

    Labels

    R: rejectedThis will not be worked onT: bugSomething isn't working
    No fields configured for issues without a type.

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions