-
Notifications
You must be signed in to change notification settings - Fork 15
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
provideDocumentFormattingEdits
called twice (formatting applied twice)
#18
Comments
Hi @zqstack. Sorrry for de delayed reply. Some weeks ago, a fix for formatters running more than once due to infinite loop of savings was added to de codebase, which happened, apparently, because the formatters were saving by themselves after format. Normally VSCode will handle the saving when a formatter runs after save if the In the code you provided you don't seem to be saving but, Did you manage to try with the last version of the extension? It should prevent the formatters to run twice, otherwise, I don't see what can be the issue, so I will ask you to provide more information, like if you have the |
I'm getting the same result. Wondering if this line is necessary since formatOnSave is already a "save" action that triggers the formatter. I'll clone the repo and take a closer look. |
Created a fresh extension and tried running it. Calling The save seems to cause an additional run. If you execute a save when there are formatting changes, the extension runs twice. If you execute a save when there are no changes, it only runs once. |
Thanks @a-laughlin. Lamentably, it seems that what you say isn't always the case, see the issue #2, that's why that line was added in the first place. Edit: Anyway, if there is a bug, is probably in these changes, since, as you can see in line 71 to 76 and 85 to 86, we already try to prevent to run the formatters twice even if format on save is enabled. |
I have added a commit that probably solves your issue. Could you test the last vesion of the app? |
Thanks for the update @jota0222. I also have some questions after testing MultiFormatter's core approach in a fresh plugin.
How sure are you that MultiFormatter caused the problem in #2? I can't reproduce that issue when I write a formatter plugin from scratch that runs multiple formatters in the same way. Nor can I find any instance of saving the document when I search vscode-prettier. My guess is that the cause of #2 was another plugin, not MultiFormatter. I think adding Are you able to reproduce the problem of not saving when formatOnSave is set and you remove |
Yes @a-laughlin, I was able to reproduce the issue when it was posted. Probably you are right and VSCode has received an update in which this doesn't happen anymore, however, I still think it's necessary to leave the save command there for older versions to work as expected. Also, as you may noticed, the issue was posted by someone and other person was who "solved" the issue, that suggests that it was happening to different people. Let me know if you still face the issue after the last update posted to see what can we do next. PS: This is not something I could confirm in the moment, but I think the issue was caused by the way the formatters are executed by the plugin, it changes the default formatter and runs the |
Interesting race condition case. VSCode not waiting for a formatter to finish seems like it would cause many plugin interaction problems for the VSCode team. My guess is that they'd have to wait somewhere.
I just did a test that will hopefully help. It uses a separate plugin that only logs saves: context.subscriptions.push(
workspace.onDidSaveTextDocument((document)=>{
if(!document.fileName.endsWith('foo.ts')) {return;}
logger.appendLine(`onDidSaveTextDocument (${document.fileName}) ~ document.isDirty:${document.isDirty}.`);
}),
workspace.onWillSaveTextDocument((e)=>{
if(!e.document.fileName.endsWith('foo.ts')) {return;}
logger.appendLine(`onWillSaveTextDocument (${e.document.fileName}) ~ document.isDirty:${e.document.isDirty}`);
})
); with no workspace settings, and these user settings:
Results of saving the document:
Results of "format selection" without saving:
It appears that MultiFormatter saves the file even when it shouldn't. It's possible to insert a check like for I get your desire for the plugin to work with older versions. At the same time, the effort of supporting multiple versions ends up being a lot, while introducing vectors for more bugs. You're welcome to do that if you want. Personally, I'd save the effort by telling folks with non-saving issues that they need to update their vscode or other formatters. |
@a-laughlin Can you tell me if the tests you did were using the version with the |
Somehow when adding my custom formatter extension for TypeScript to Multiple Formatters, it seems to be loaded twice.
provideDocumentFormattingEdits
is called twice as Iconsole.log()
, whileactivate()
is called only once.I wonder why this is happening. It does not happen when loaded directly as default formatter instead of using Multiple Formatters. Even in the most stripped-down version below.
settings.json (workspace - user does not have this setting)
extension.ts
package.json (extension - parts which may be relevant?)
The text was updated successfully, but these errors were encountered: