Skip to content
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

Allow editor.codeActionsOnSave for autoSave afterDelay #200881

Closed
justschen opened this issue Dec 14, 2023 · 11 comments
Closed

Allow editor.codeActionsOnSave for autoSave afterDelay #200881

justschen opened this issue Dec 14, 2023 · 11 comments
Assignees
Labels
feature-request Request for new features or functionality insiders-released Patch has been released in VS Code Insiders under-discussion Issue is under discussion for relevance, priority, approach verification-needed Verification of issue is requested verified Verification succeeded
Milestone

Comments

@justschen
Copy link
Contributor

justschen commented Dec 14, 2023

cc. @bpasero @alexdima

From another conversation:

I have "files.autoSave": "afterDelay" and that seems to never run code actions. So every time I make changes, right before committing them, I need to manually click through each file, click on the right and press cmd+s to organize imports.

so I would love to run code actions perhaps on focus change or something, it just feels I don’t benefit at all from code actions on save, so I need to constantly run them manually

ATM, code actions on save already work for auto saves on focus change and window change, but not on auto saves. With the addition of being able to configure autoSave more granularly, can maybe consider adding back allowing code actions to run on auto save if the delay is significant enough.

Additional things to consider:

  • When this is enabled:
  • allow to skip auto save when the file has error markers

  • Will auto save be run on warnings (ie, imports that have not been used yet, linting), or is it limited to just errors?
  • If it doesn't run when there are warnings/errors, then auto save after delay would work perfectly and not accidentally delete imports that are recently added.
@justschen justschen self-assigned this Dec 14, 2023
@justschen justschen added this to the December / January 2024 milestone Dec 14, 2023
@bpasero
Copy link
Member

bpasero commented Dec 15, 2023

cc @jrieken

I think originally we wanted to disable code actions on auto save after delay to prevent altering the editor while you are typing in the editor. But I am easy to relax this, as a customer of auto save after delay, I would not mind to experiment with running formatter for example, maybe with a new setting, off by default.

@jrieken
Copy link
Member

jrieken commented Dec 15, 2023

I still believe that this isn't a good idea. There is the aspect of no synchronisation point, e.g formatting or code actions happen as you type, but worst is that save after delay often happens in the presence syntax errors (something that's less likely with manual save or editor switch) and tools, like eslint, perform very poorly in such cases, effectively destroying your sources.

@bpasero
Copy link
Member

bpasero commented Dec 15, 2023

I recently added a new setting to prevent auto save in the presence of errors in the document, I would think we could use a similar pattern here for code actions.

image

@jrieken
Copy link
Member

jrieken commented Dec 15, 2023

But isn't the point of code actions to remove errors before saving - like there is errors that code actions can fix and there are some that make code actions create more errors...

@bpasero
Copy link
Member

bpasero commented Dec 15, 2023

Yeah, maybe I misunderstand what the goal is here. I can tell from my use case: I often end up not being able to push my changes to Git because the file is not formatted. Then I have to manually save the file to trigger formatting on save. If formatting would run as part of auto save (after delay), this would not happen. I would be happy to try this out if possible from a new setting.

I think @alexdima had another scenario with organising imports, maybe he can comment as well.

@jrieken
Copy link
Member

jrieken commented Dec 15, 2023

I often end up not being able to push my changes to Git because the file is not formatted.

My understanding is that we add new save hooks like "save on git stage". Tho, save on focus change is covering almost all of those cases. It's only annoying when opening the command palette because I usually don't want to save then

@justschen
Copy link
Contributor Author

justschen commented Dec 18, 2023

I believe in @alexdima's case, he had manually added some imports in a mostly empty file, continued to type further down and the code actions on save (which had been enabled for auto saves after delay) deleted the imports since they had been unused.

The potential here could be something along the lines of allowing actions to be run on auto save after delay when there are no errors (when "files.autoSaveWhenNoErrors": true), but not super married to the idea though, just wanted to gauge how people felt about it 👍

@alexdima
Copy link
Member

If I understand things correctly, configuring "files.autoSave": "onFocusChange" makes it that on focus change, the file is saved and code actions are executed automatically. Meanwhile, configuring "files.autoSave": "afterDelay" makes it that the file is saved with a delay and code actions are never executed automatically.


I would like the possibility to have files saved with a delay and code actions executed automatically on focus change. I don't see why code actions must execute automatically only during save. Maybe a different way to think of it is that automatic code actions can be triggered by save but can be triggered also by other triggers, like focus change.

@justschen justschen added the under-discussion Issue is under discussion for relevance, priority, approach label Jan 23, 2024
@justschen justschen modified the milestones: December / January 2024, February 2024 Jan 23, 2024
@justschen justschen modified the milestones: February 2024, Backlog Feb 22, 2024
@tjx666
Copy link
Contributor

tjx666 commented Jul 9, 2024

@justschen can we get more details info about which codeAction is making saving sucks?

image

I would be helpful to provide a command to trigger codeActions, but not trigger save.

@justschen
Copy link
Contributor Author

@tjx666 see some of the comments talked about here! #221625

tldr: It's likely that the extension causing this is eslint and I've been investigating but do not have a consistent repro yet. feel free to open an issue there as well.

however, if you'd like to trigger code actions independently (ie, throught keybinding), you can run code actions via editor.action.sourceAction or editor.action.codeAction.

@justschen justschen added the feature-request Request for new features or functionality label Jul 22, 2024
@justschen justschen modified the milestones: Backlog, July 2024 Jul 23, 2024
@justschen justschen added the verification-needed Verification of issue is requested label Jul 23, 2024
@justschen
Copy link
Contributor Author

justschen commented Jul 23, 2024

The above PR is as follows:

Will trigger code actions for organize imports when the following conditions are applied:

  • file auto save is set to afterDelay
  • editor code actions (such as source.organizeImports) is set to always
  • setting: editor.codeActions.triggerOnFocusChange is true (default is false)

note: will still be triggered when afterDelay is on and we manually trigger a safe (same as current behavior)

We basically match the behavior of onFocusChange or onWindowChange save styles when code actions are always and our save style is afterDelay, but on these context changes, we don't immediately save.

cc. @alexdima

@justschen justschen added the insiders-released Patch has been released in VS Code Insiders label Jul 23, 2024
@joyceerhl joyceerhl added verified Verification succeeded and removed verified Verification succeeded labels Jul 23, 2024
@joyceerhl joyceerhl added the verified Verification succeeded label Jul 23, 2024
@vs-code-engineering vs-code-engineering bot locked and limited conversation to collaborators Sep 5, 2024
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
feature-request Request for new features or functionality insiders-released Patch has been released in VS Code Insiders under-discussion Issue is under discussion for relevance, priority, approach verification-needed Verification of issue is requested verified Verification succeeded
Projects
None yet
Development

No branches or pull requests

6 participants