File Watchers not working in Intellij for isort and autoflake combination

792 views Asked by At

I have a Python file with multiline import statement:

from itertools import (
    accumulate,
    chain,
    combinations,
    combinations_with_replacement,
    compress,
    count,
    islice,
    product
)

from which I try to remove unused imports by first changing multiline import statement to single-line imports:

isort -sl totest.py
autoflake --remove-all-unused-imports --remove-unused-variables -i totest.py

It works great in command line, but when using File Watchers in Intellij it does not run autoflake when saving file for the first time. Basically, to get around it I need to:

  1. Make some edit, e.g., add some space
  2. Save file
  3. It gets formatted to:

    from itertools import accumulate
    from itertools import chain
    from itertools import combinations
    from itertools import combinations_with_replacement
    from itertools import compress
    from itertools import count
    from itertools import islice
    from itertools import product
    
  4. And then make another edit

  5. Save file
  6. And redundant imports gets removed

Steps 4 and 5 are redundant.

I wonder what could be the reason and where should I report that? To IDEA Intellij team, to isort or where?

Here's my watchers.xml to import into Intellij.

<TaskOptions>
  <TaskOptions>
    <option name="arguments" value="--remove-all-unused-imports --remove-unused-variables -i $FilePath$" />
    <option name="checkSyntaxErrors" value="true" />
    <option name="description" />
    <option name="exitCodeBehavior" value="ERROR" />
    <option name="fileExtension" value="py" />
    <option name="immediateSync" value="false" />
    <option name="name" value="Remove unused variables and imports" />
    <option name="output" value="$FilePath$" />
    <option name="outputFilters">
      <array />
    </option>
    <option name="outputFromStdout" value="false" />
    <option name="program" value="autoflake" />
    <option name="runOnExternalChanges" value="true" />
    <option name="scopeName" value="Project Files" />
    <option name="trackOnlyRoot" value="false" />
    <option name="workingDir" value="" />
    <envs />
  </TaskOptions>
  <TaskOptions>
    <option name="arguments" value="-sp $$ProjectFileDir$/.isort.cfg $FilePath$" />
    <option name="checkSyntaxErrors" value="true" />
    <option name="description" />
    <option name="exitCodeBehavior" value="ERROR" />
    <option name="fileExtension" value="py" />
    <option name="immediateSync" value="false" />
    <option name="name" value="Organize imports" />
    <option name="output" value="$FilePath$" />
    <option name="outputFilters">
      <array />
    </option>
    <option name="outputFromStdout" value="false" />
    <option name="program" value="isort" />
    <option name="runOnExternalChanges" value="true" />
    <option name="scopeName" value="Project Files" />
    <option name="trackOnlyRoot" value="false" />
    <option name="workingDir" value="" />
    <envs />
  </TaskOptions>
  <TaskOptions>
    <option name="arguments" value="--config=&quot;$ProjectFileDir$&quot;/pyproject.toml $FilePath$" />
    <option name="checkSyntaxErrors" value="true" />
    <option name="description" />
    <option name="exitCodeBehavior" value="ERROR" />
    <option name="fileExtension" value="py" />
    <option name="immediateSync" value="false" />
    <option name="name" value="Format code (with Black)" />
    <option name="output" value="$FilePath$" />
    <option name="outputFilters">
      <array />
    </option>
    <option name="outputFromStdout" value="false" />
    <option name="program" value="black" />
    <option name="runOnExternalChanges" value="true" />
    <option name="scopeName" value="Project Files" />
    <option name="trackOnlyRoot" value="false" />
    <option name="workingDir" value="" />
    <envs />
  </TaskOptions>
</TaskOptions>
0

There are 0 answers