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

File formatter messes up imports if path dependent imports are used #298

Closed
Pandafishie opened this issue Dec 30, 2024 · 1 comment
Closed
Labels
bug Issue identified by VS Code Team member as probable bug triage-needed

Comments

@Pandafishie
Copy link

Diagnostic Data

  • Python version (& distribution if applicable, e.g., Anaconda): Python 3.13.1 @ conda 23.11.0
  • Type of virtual environment used (e.g., conda, venv, virtualenv, etc.): conda
  • Operating system (and version): Windows 11 Home ; OS build 26100.2605
  • Version of tool extension you are using:
    2024.0.0

Behaviour

Expected Behavior

Formatter does not move import order & lines past none-empty lines to prevent import issues

Actual Behavior

The formatter moves import lines to the very top of the file, regardless of any code in between. This can be fatal when importing relative packages from local paths using

sys.path.append(). As a result, imports imported from local added paths will no longer be able to be imported.

Reproduction Steps:

  • Write a sample script, eg:
import os
import sys
import numpy as np

sys.path.append(os.path.join(os.path.dirname(__file__), '..', 'local_packages_path'))

import local_package as lp
  • Execute formatting.
  • Expected output:
import os
import sys
import numpy as np

sys.path.append(os.path.join(os.path.dirname(__file__), '..', 'local_packages_path'))

import local_package as lp
  • Actual output:
import local_package as lp
import os
import sys
import numpy as np

sys.path.append(os.path.join(os.path.dirname(__file__), '..', 'local_packages_path'))

Logs:

Click here for detailed logs 2024-12-30 18:07:54.892 [info] [Trace - 6:07:54 PM] Sending request 'textDocument/formatting - (3)'. 2024-12-30 18:07:54.896 [info] [Trace - 6:07:54 PM] Received notification 'window/logMessage'. 2024-12-30 18:07:54.896 [info] CONDA_PATH\anaconda3\envs\VENV_NAME\python.exe -m autopep8 - 2024-12-30 18:07:54.896 [info] [Trace - 6:07:54 PM] Received notification 'window/logMessage'. 2024-12-30 18:07:54.896 [info] CWD Server: REPO_PATH 2024-12-30 18:07:55.119 [info] [Trace - 6:07:55 PM] Received response 'textDocument/formatting - (3)' in 227ms. 2024-12-30 18:07:55.127 [info] [Trace - 6:07:55 PM] Sending notification 'textDocument/didChange'.

Outcome When Attempting Debugging Steps:

Did running it from the command line work?
Yes, but same output.

Extra Details

workingdir/
├── dir1/
    ├── main_script.py
├── local_packages_path/
    ├── local_package.py
  • Screenshots expected vs actual output:
  • Image
  • Image
@Pandafishie Pandafishie added the bug Issue identified by VS Code Team member as probable bug label Dec 30, 2024
@karthiknadig
Copy link
Member

Did running it from the command line work?
Yes, but same output.

This means that the issue is in autopep8 itself. This repo is for the VS Code extension that wraps the autopep8 library to provide integration with VS Code, please file the issue here: https://github.com/hhatto/autopep8

@karthiknadig karthiknadig closed this as not planned Won't fix, can't repro, duplicate, stale Jan 6, 2025
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 triage-needed
Projects
None yet
Development

No branches or pull requests

2 participants