Glavin001 / atom-beautify

:mega: Help Wanted - Looking for Maintainer: https://github.com/Glavin001/atom-beautify/issues/2572 | :lipstick: Universal beautification package for Atom editor (:warning: Currently migrating to https://github.com/Unibeautify/ and have very limited bandwidth for Atom-Beautify Issues. Thank you for your patience and understanding :heart: )
http://unibeautify.com/
MIT License
1.5k stars 453 forks source link

"Sort imports" setting with Python doesn't work if file is not part of a project #2511

Open brettg0396 opened 4 years ago

brettg0396 commented 4 years ago

Description

iSort breaks with supplied parameters if the input file is not part of an existing atom project (e.g. if a standalone python file is edited with atom). Specifically, iSort will throw the error: "Error: arguments passed in without any paths or content."

Why this happens

The following code snippet comes from lines 57-60 in atom-beautify/src/beautifiers/autopep8.coffee

if options.sort_imports
          filePath = context.filePath
          projectPath = atom?.project.relativizePath(filePath)[0]
          @exe("isort").run(["-sp", projectPath, tempFile])

the function atom?.project.relativizePath(filePath) returns an array that has the project path at array[0] and the relativized file-path at array[1] if the file is part of a project, otherwise array[0] is null (see this issue). Therefore, if the file is not part of a project, then projectPath in the above code will equal null, and the -sp (settings path) parameter will not have an argument, resulting in the erroneous behavior.

Suggested fix

(Worked for me, anyway) Replace the above code with:

if options.sort_imports
          filePath = context.filePath
          console.log(filePath)
          projectPath = atom?.project.relativizePath(filePath)[0]
          if projectPath
            @exe("isort").run(["-sp", projectPath, tempFile])
          else
            @exe("isort").run([tempFile])

which forgoes the -sp parameter when the file is not part of a project (projectPath == null) and it is therefore unnecessary.

Steps to Reproduce

  1. Edit a python file without adding it to a project
  2. Tick the "Sort imports" setting in Atom Beautify > Python
  3. Run command Atom Beautify: Beautify Editor
  4. Receive error described above

Checklist

I have:

(I have not performed these last two actions as I have discovered the cause of the bug, but neither have I submitted a pull request as this bug may repeat in other beautifiers that make use of atom?.project.relativizePath() and the team may have a more appropriate fix in mind).

Cyberes commented 2 years ago

I'm having the same issue. This bug report is from 2020.