biomejs/biome

๐Ÿ› Import sorting breaks with a particular import line when saving in VSCode

jpike88 opened this issue ยท 21 comments

Environment information

CLI:
  Version:                      1.5.1
  Color support:                true

Platform:
  CPU Architecture:             aarch64
  OS:                           macos

Environment:
  BIOME_LOG_DIR:                unset
  NO_COLOR:                     unset
  TERM:                         "xterm-256color"
  JS_RUNTIME_VERSION:           "v18.17.1"
  JS_RUNTIME_NAME:              "node"
  NODE_PACKAGE_MANAGER:         "npm/9.6.7"

Biome Configuration:
  Status:                       Loaded successfully
  Formatter disabled:           true
  Linter disabled:              false
  Organize imports disabled:    false
  VCS disabled:                 true

Workspace:
  Open Documents:               0

Biome VSCode extension version v2.1.1

What happened?

With organizeImports enabled and this config in workspace:

"editor.codeActionsOnSave": {
			"quickfix.biome": "explicit",
			"source.organizeImports.biome": "explicit"
		},

just add this particular import line to any script that has a bunch of existing imports.

import {MaxInteger32Value } from '@shared/MaxInteger32Value';

Minimal reproducible case below:

import {MaxInteger32Value } from '@shared/MaxInteger32Value';
import { a } from '@a';
import { b } from '@b';

Something about this particular string causes havoc by duplicating it, and replacing existing import lines below it. See screenshot

This only happens in VSCode, I can't reproduce it on the playground.

Expected result

Screenshot 2024-01-16 at 11 11 41โ€ฏAM

Code of Conduct

  • I agree to follow Biome's Code of Conduct

Seems to be problematic in this case too:

import { d } from '@dd';
import { a } from '@a';
import { b } from '@b';

But not in this:

import { d } from 'dd';
import { a } from '@a';
import { b } from '@b';

The @ symbol seems to be the reason here.

Can you share a playground link?

I can't repro on playground, just vscode

Then you'll have to share a reproduction. This will help us to identify the issue

source.organizeImports.biome breaks for me in VSCode constantly. It sometimes eats multiple lines of code and then spits out repeated import lines probably due to competing with source.addMissingImports? I've had to disable source.organizeImports.biome entirely since I just can't get it to work consistently without breaking code.

I don't have a working reproduction offhand since it seems to have to do with combination of quickfix.biome, source.organizeImports.biome, and source.addMissingImports, but will try to replicate it next time I see it.

@ematipico here's a simple reproduction showing the issue I'm having (maybe not related?):

Before save (swapped imports to be out of order)
Screenshot 2024-01-16 at 11 56 53โ€ฏPM
After save
Screenshot 2024-01-16 at 11 57 04โ€ฏPM

Settings
Screenshot 2024-01-16 at 11 58 29โ€ฏPM

fwiw I also just tried disabling source.addMissingImports and got the same result, so this seems like a bug isolated to source.organizeImports.biome. Update: interestingly restarting VSCode or resetting the extension host seems to fix things temporarily, so the example above works again until it starts breaking and then I can reproduce it consistently until I restart again. Maybe there's some sort of corrupted internal state of either VSCode or the extension?

Sorry @kainosnoema , this isn't a reproduction.

We need to be able to reproduce the issue. A repository would be great.

Without that , we can't help.

Iโ€™ll try enabling source.organizeImports.biome in my setup, see if I run into it.

Yeah, same issue here. It randomly happens as well. Reloading VSCode works. Sometimes the imports completely disappears, sometimes it completely changes
image

I have also been experiencing this issue. It seems to be because there is many Biome agents running and happens if I have left my Mac on overnight. If I hover over a Biome warning or error I can see the error repeated, sometimes 20 times.

Closing VSCode completely does fix the issue and Biome suggestions are no longer duplicated.

So I would suggest the issue is with Biome seemingly restarting without killing the previous process for some reason?

I wonder to what extent this issue and #1584 are related. I'm getting the impression both situations may be caused by having too many daemons running.

As far as I see, I also only see Mac users in these threads. So if there's a non-Mac user experiencing this, please raise your voice, since it might help us figure out in which direction to look.

We just published a nightly with a possible fix: https://github.com/biomejs/biome/releases/tag/cli/v1.5.2-nightly.91c7730

It would be great if someone can try it, and let us know.

Still a problem on 1.5.3

Hasn't happened to me since 1.5.2-dev and 1.5.3 ๐Ÿค”

I'm not sure how or why... but disabling reenabling plugin seemed to work afterwards... I'll keep you posted

Thank you all for confirming.

For anyone coming here , to apply the fix you must restart your VSCode after updating to Biome 1.5.3. There was an issue in the LSP.

Today I heard reports from several co-workers that seem to be running into this issue still, all on 1.5.3. Unfortunately, I haven't been able to witness it myself, but I did notice just now that I had 4 Biome formatter entries in VS Code, which was also one of the things my co-workers witnessed.

So while #1590 seems to have fixed #1584 (the issue that two Biome formatter entries appear in VS Code right away), I think there is still a lurking issue that causes additional entries to be registered over time. And my assumption here is that once there are multiple entries, they can still conflict and trigger this behavior.

I'm afraid I still have no additional information what might be the root cause however :(

And just like that, I finally witnessed the imports being messed up with my own eyes too! I'd been keeping the session with 4 Biome entries open, and it further reinforces my suspicion of the mangled imports and duplicate formatter entries being related: The mangled imports I witnessed had one of the imports being repeated 4 times:

mangled_imports

One thing of note though, this didn't happen to me on Save or by manually using Organize Imports. It happened when I renamed a symbol in one file and VS Code applied the refactoring to update that symbol in another file. Maybe that somehow triggered all four formatters to kick in, causing the renamed symbol to appear 4 times.

Okay, and now that it happened once, if I use Undo to revert to how the imports were before they got mangled, then hit Save (I have Organize Imports enabled on Save) it mangles it again.

Update: One more piece of information: If I invoke Organize Imports explicitly, I get to choose between 5 entries. I think one is VS Code's native Organize Imports, while the other 4 are marked as Biome:

organize_import_entries

I can select any of those 5 entries, and the imports get organized as expected. So the functionality itself is fine. But I hit Save, and it mangles again, presumably because multiple get invoked at the same time.

Update: If I reorder imports in another file and Save, it also mangles there. So somehow I've gotten into a state now where it will always mangle if I Save while there are imports that need sorting. Saving files where the imports are already sorted is still fine.

Is it possible that you enabled the rule https://biomejs.dev/linter/rules/use-import-type/ ?

It shouldn't be, no...