Skip to content

Sorting imports ignores configuration on unsaved files #4433

Closed

Description

@danpalmer

Environment data

  • VS Code version: 1.31.1
  • Extension version (available under the Extensions sidebar): 2019.1.0
  • OS and version: macOS 10.14.3
  • Python version (& distribution if applicable, e.g. Anaconda): 3.5.6
  • Type of virtual environment used (N/A | venv | virtualenv | conda | ...): virtualenv

Expected behaviour

When I edit a Python file and sort imports, I expect it to respect the isort configuration in my project.

Actual behaviour

When I have a file that has been edited and not yet saved, sorting imports uses the default isort configuration, rather than the project specific isort configuration in setup.cfg.

I understand that it is difficult or impossible to accurately determine the config file to use for an entirely unsaved file, however this is an edited file that does exist on disk, so an appropriate configuration file for isort should be able to be found.

Steps to reproduce:

  1. Create a Python file with the following content:
import b
import cc
  1. Create a setup.cfg file with the following content:
[isort]
length_sort = True
  1. Open the Python file, and add a 3rd line:
import b
import cc
import aaa

Note that this file is sorted according to the length_sort configuration, but is unsorted according to the isort default of alphabetical sorting of imports.

  1. Without saving the file, run "Sort Imports"

  2. The file will be:

import aaa
import b
import cc

It should have remained unchanged.

Logs

No logging output.

Console output:

[Extension Host] Python Extension: Cached data exists getEnvironmentVariables, test.py

Activity

  1. ghost added
    triage-needed
    on Feb 18, 2019
  2. karrtikr commented on Feb 19, 2019

    @karrtikr

    Closing in favour of #2980

  3. ghost removed
    triage-needed
    on Feb 19, 2019
  4. danpalmer commented on Feb 20, 2019

    @danpalmer
    Author

    Kartik Raj (@karrtikr) could we re-open this please, that is a separate issue.

    From the linked issue #2980:

    A few times people have asked about running commands like import sorting, formatting, etc. on unsaved, brand new files (i.e. Untitled-1). We should notify users that the command does not work on such files so they don't get a weird error message talking about e.g. EACCES.

    From this issue:

    I understand that it is difficult or impossible to accurately determine the config file to use for an entirely unsaved file, however this is an edited file that does exist on disk, so an appropriate configuration file for isort should be able to be found.

    I apologise if this wasn't clear enough, but I was already aware of #2980 and reported this specifically because it is a different issue. In this case we know what the filename is, therefore we can either save the file automatically before formatting, or use the filename to determine the correct config file to use for formatting. Also note that the isort integration in VSCode does not need the file to be saved, again different to the linked issue.

  5. danpalmer commented on Feb 22, 2019

    @danpalmer
    Author

    Kartik Raj (@karrtikr) ping on this, it needs to be re-opened.

  6. DonJayamanne commented on Feb 22, 2019

    @DonJayamanne

    Updated the original issue accordingly.

  7. danpalmer commented on Feb 22, 2019

    @danpalmer
    Author

    Don Jayamanne (@DonJayamanne) I do not agree that #2980 is the "original issue", I believe it to be a different issue, that is a much more complicated issue. It would be great to get this easier, and related but different issue solved separately, as I suspect the resolutions will be entirely distinct.

    For clarity: finding the correct configuration is trivial for modified files that already exist on disk, and very difficult for files that do not exist on disk at all. The former likely needs no user prompt, can always work correctly automatically, and is an obvious UX win. The latter is a complex UX issue.

    Grouping this issue in with #2980 essentially means it will take longer to be solved, and will likely be solved in a worse way as a result.

    I suspect that #3306 may be the same as this one, but that issue was also incorrectly closed as a duplicate of #2980.

    In general, it would be great to get a reasoning for closing issues, and for us to be open to discussion around when things are actually duplicates. So far this issue has received 2 responses from maintainers containing no information. This makes it very difficult to contribute, and signals that input is not valued, as it is not being listened to and discussion is being shut down. I'm sure this is not the intention at all, and VSCode has a great community around it, but this is how it may appear.

  8. DonJayamanne commented on Feb 22, 2019

    @DonJayamanne

    Dan Palmer (@danpalmer)
    They are the same. When running isort against a file that hasn't been saved, its the same as running it against a file that doesn't exist on disk.
    The way isort works is, it will look for the configuration files by walking up form the directory containing the file that needs to be formatted. When formatting files that haven't been saved, we create a temporary file in a temporary directory, and that's where things go wonky, at that point the file is NOT in your project directory.
    Again, whether the file has been saved or not or its a new file (& not saved), they are the same.

    Grouping this issue in with #2980 essentially means it will take longer to be solved, and will likely be solved in a worse way as a result.

    I'm sorry you feel this way, however please understand that we're doing our best to resolve issues in a timely manner.

    As a temporary solution you can enable auto save in vsc. That should fix it.

  9. danpalmer commented on Feb 22, 2019

    @danpalmer
    Author
  10. locked as resolved and limited conversation to collaborators on Mar 22, 2019
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions