aziznal/dart-import-sorter

Bug: Sort on Save garbles current file when refactoring

Opened this issue · 5 comments

1st: love the ext, love the sort on save feature.

But I had to switch it off - too annoying too often. If I e.g. rename a function, I get a block of imports (most probably the last sorted ones) on top of current file - often leads to a lot of confusing project errors. I can Cmd-Z it away, but it totally breaks my flow.

Here is what I think happens: The refactor does an auto-save itself. Somehow import-sorter is hooked in and applies cached imports. It may only apply, if multiple files get changed thru the refactoring.

Please fix :)

interesting issue. I've tried to recreate it on my side but wasn't able to. Could you share the exact scenario where you're seeing this behavior?

  • sort on save on, empty line between on
  • refactor/rename anything, that spans multiple files (current file must be saved, or sth)

Hope this helps

It happens with sortOnSave feature enabled, everytime you refactor a file (renaming, moving to a different folder...)

I can show you an example of the unexpected changes automatically made in one of my files:

Captura de pantalla 2024-05-07 a las 1 05 54

It looks to be adding new "random"(it may follow some logic but I have no clue) imports, replacing the top lines of the file.

Came to report the same. Extension is awesome, but I figured I'd check if there's a fix before I give up on it.

I hit the issue mainly when refactoring. It's hard to tell exactly when since the imports change at the top of the file not where I'm at. But I think it's related to renaming functions or classes. It looks like it's copying sorted imports from different file.

Observation: It's definitely happening often when using the VSCode right click, Rename Symbol (F2). It also doesn't appear to be copying an existing set of imports from another file. I also always get a lot of imports.

Theory: When you rename an item that exists in multiple files, it renames and saves all those files. I believe the import statements from all of the files referenced are all sorted and added to just the current open file.

So if a given item that's renamed exists in 3, 4, or more files, a lot of imports get added to the existing file.