-
-
Notifications
You must be signed in to change notification settings - Fork 460
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Plugin has a conflict with the built-in organize imports #716
Comments
I'm running into the same issue. I've disabled organize imports for now. |
Can this be solved in some way? I would like to help if I can to implement this. |
This only seems to have happened with VSCode's refactor/extension of how code-formatters are chosen & configured. It is a shame because prior to this VSCode version you could have a nice I've tried to mess with the timeouts of these actions, but no workaround seems to be consistent here. Turning this off for now :( |
After playing with some of the VSCode settings a bit more, you can make the editor behave in a REALLY strange way: And look at what happens if you repeatedly save the file: Yep, either Prettier or VSCode's |
Any member can chime in? |
I get severely corrupted files from this all the time. Looks like some kind of serious race condition |
same issue here, any workaround? |
@ntotten I think this is a bug, not an enhancement. It means Prettier VSCode is totally incompatible with one of VSCode's more useful features. |
I'm having this same issue too. I think I might have to disable this too. |
It seems weird that this is getting so little attention, considering it's the 3rd most upvoted issue of |
I need somebody to provide a github repo I can use to reproduce this. I do see the flicker where first the organize is applied, then prettier is applied, but I can't get it to actually cause any problems like "corrupted" files. |
It doesn't seem to be causing any bugs for me in the code. For example the module imports do not get distorted. The problem for me is that if I format on save and have this issue, then linting with prettier will fail. |
This is an issue for me as well. When I have the following settings w/ prettier:
It'd be nice if |
I found a workaround: a 6 core i7. No joke - my new macbook can run these two settings in tandem with no issues 🤔🙄 |
This happens to me frequently when my imports are long enough that Prettier needs to wrap them. I believe it's also related to the fact that I'm running a large project so my CPU is usually pretty throttled on save. What typically happens is, I press save, the imports are organized, then Prettier runs and reformats them, but then the file is still marked "dirty" - I have to save it again (or maybe 2 or 3 more times) before the little asterisk in the tab is gone. It feels like there's a race condition here that is exacerbated by the CPU demands of other TypeScript activities. |
I see exactly the same issue every day. |
@ntotten Not sure anyone reported corrupted files. Rather sometimes when saving a file it remains unsaved because changes to the imports are applied post-save. But in any case, isn't flickering when saving enough of an annoyance to treat that as the issue? |
I get that its an annoyance, but I am not even sure fixing it is possible. VS Code runes save actions that extensions provide, this extension doesn't know anything about nor have any influence on the order imports. |
Question: isn't the issue here the sequence of invocation? In other words, if one could make sure that the reshuffling of the imports always happened before the invocation of Prettier, we would be golden. |
I believe that formatting runs before code actions. I don't think there is any way to control that. |
This issue (files being set to "dirty" after save due to reformatting) affects me ~4/5 times when I press save (including when I then press save again). The truncation is a bit rarer but happens fairly frequently when the system is under heavy load. |
Playing around with this, one semi-workaround is to set prettier's |
Since Microsoft has closed the issue (microsoft/vscode#87096) that would have enabled us to solve this and has indicated they are not going to fix it, this isn't something that the extension can fix. As such I am closing this issue. |
Can I remove unused importer only? Needless resort. |
In the new VSCode v1.44.x, Explicit ordering for Code Actions on save was introduced. It looks like it might be the answer we've been searching for years. However, I tried playing with it but failed to get it to work. What I tried was: // settings.json
"editor.formatOnSave": false,
"editor.codeActionsOnSave": [
"source.organizeImports",
"editor.action.formatDocument" // does not work
], The problem is, I don't know what actions are available. I got the |
I disabled https://github.com/simonhaenisch/prettier-plugin-organize-imports The problem was fixed after a restart! |
Can confirm that @ZYinMD's solution works like a charm. |
Thanks @JonnyBurger! That's what we've boon looking for. Works perfect! |
@JonnyBurger I must be doing something wrong. I also disabled yarn add -D prettier-plugin-organize-imports prettier typescript After restarting VS Code and disordering some imports, nothing happened on save. |
@janosh are your files JS or Typescript? That plugin only works for Typescript files. |
@janosh try setting "editor.codeActionsOnSave[source.fixAll]": true and |
@Undistraction That must be it. I'm working with JS files. How come this limitation though? The plugin readme states:
"Organize Imports" in VS Code works for both TS and JS and uses (afaik) the same implementation. |
@janosh Could try this approach, #1277 (comment), but with organize-imports instead of eslint. This is similar to @ZYinMD approach above, but I could not get that to work. Install this VSCode extension. https://github.com/rohit-gohri/vscode-format-code-action/ This registers a custom codeAction: These settings would run organizeImports first and then prettier on save:
|
@rohit-gohri Thanks for the hint! It seems a little slower than |
@janosh Yes, they do run in sequence hence the little flash and speed difference. You could run them in parallel by providing an object to codeActionsOnSave but then we'll be back to the original problem of the issue.
That is due to VS Code's organize imports not supporting a max-line-length option: microsoft/TypeScript#22991 |
For anyone hoping to do:
I'm still seeing |
This issue has been automatically locked since there has not been any recent activity after it was closed. Please open a new issue for related bugs. |
As soon as VScode was updated to 1.31.0, it appears that this prettier extensions conflicts with the the built-in organize imports setting, when organize imports on save is enabled.
prettier:
"prettier": "^1.16.4",
(was initially on1.13.7
, tried to update)vscode:
Version 1.31.0 (1.31.0)
extension:
1.8.0
I first opened a ticket with vscode core, but was referred to the extension as the source of the problem:
microsoft/vscode#68193
Steps to reproduce
editor.codeActionsOnSave.source.organizeImports = true
(see below for example json)Expected behavior:
Actual behavior:
Note:
I can format the document manually using the command menu and the import is correctly formatted, but then on save it is unformatted and the file shows as modified by the file system.
Initial file
Format file manually, just to show that it works
After Save (cmd+s)
The text was updated successfully, but these errors were encountered: