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

AutoSave is triggering editor.formatOnSave even when files.autoSave is afterDelay #206475

Closed
hb20007 opened this issue Feb 28, 2024 · 13 comments
Closed
Assignees
Labels
bug Issue identified by VS Code Team member as probable bug file-io File I/O insiders-released Patch has been released in VS Code Insiders verified Verification succeeded
Milestone

Comments

@hb20007
Copy link

hb20007 commented Feb 28, 2024

Does this issue occur when all extensions are disabled? Yes*

*The issue occurs when all extensions are disabled, except for Prettier, which I use for the opinionated formatting to demonstrate the severity of the issue. (It is, of course, also reproducible without Prettier; see my comments below.)

I am able to reproduce the issue on Windows (v1.87.0) as well as on the Insiders build on web using https://insiders.vscode.dev/.

Steps to Reproduce

I will provide the steps to reproduce it on the Insiders build:

  1. Launch a fresh build of VS Code.
  2. Download the Prettier extension.
  3. Add the following in user settings:
    user settings screenshot
    (It seems that the Insiders build has "files.autoSave": "afterDelay" as the default so there’s no need to add that. If using another version, you need to add it.)
  4. Create a new .js file and type the following:
    screenshot showing unformatted code
  5. Press Alt + Tab twice.
  6. Notice that the code has been autoformatted:
    screenshot showing formatted code

More Info

This is a major annoyance as "editor.formatOnSave": true is not meant to be triggered if the file is saved after delay, as the description of the property also says.

I see that someone has already reported this bug 3 weeks ago in #204884, but it was closed as the user did not confirm that the issue happens even when other extensions are disabled.

Also, I see that there is another open issue (#206434) related to "files.autoSave": "onFocusChange". Both issues might have been caused by the same commit.

System Info
Item Value
CPUs AMD Ryzen 7 5700U with Radeon Graphics (16 x 1797)
GPU Status 2d_canvas: enabled
canvas_oop_rasterization: enabled_on
direct_rendering_display_compositor: disabled_off_ok
gpu_compositing: enabled
multiple_raster_threads: enabled_on
opengl: enabled_on
rasterization: enabled
raw_draw: disabled_off_ok
skia_graphite: disabled_off
video_decode: enabled
video_encode: enabled
vulkan: disabled_off
webgl: enabled
webgl2: enabled
webgpu: enabled
Memory (System) 7.31GB (1.38GB free)
Screen Reader no
VM 0%
@starball5
Copy link

Related on Stack Overflow: When I hit alt+tab, VS code do auto save, and formats code, how to stop it?

@hb20007
Copy link
Author

hb20007 commented Feb 28, 2024

Related on Stack Overflow: When I hit alt+tab, VS code do auto save, and formats code, how to stop it?

I am aware of this too. The user there does not confirm that they aren't using "files.autoSave": "onWindowChange" so I wasn't sure if I should link to it.

@jrieken
Copy link
Member

jrieken commented Mar 4, 2024

The issue occurs when all extensions are disabled, except for Prettier, which I use for the opinionated formatting to demonstrate the issue.

How we do you know this isn't caused by prettier then? Please disable and configure one of the built-in formatters, e.g the one for JS/TS, and check if you can reproduce

@jrieken jrieken added the info-needed Issue requires more information from poster label Mar 4, 2024
@jrieken
Copy link
Member

jrieken commented Mar 4, 2024

Also, what do you have configured for files.autoSave? With off, prettier, and your steps I cannot reproduce this.

@hb20007
Copy link
Author

hb20007 commented Mar 4, 2024

@jrieken I already mentioned the configuration for files.autoSave… Please see step 3 in the steps to reproduce. The value should be "files.autoSave": "afterDelay".

@jrieken
Copy link
Member

jrieken commented Mar 4, 2024

It seems that the Insiders build has "files.autoSave": "afterDelay" as the default so there’s no need to add that. If using another version, you need to add it.

That's not correct - the default is off. You likely have changed this setting or a workspace or extension has.

@jrieken
Copy link
Member

jrieken commented Mar 4, 2024

Screenshot 2024-03-04 at 09 25 00

@jrieken jrieken closed this as completed Mar 4, 2024
@jrieken jrieken added *question Issue represents a question, should be posted to StackOverflow (VS Code) and removed info-needed Issue requires more information from poster labels Mar 4, 2024
@hb20007
Copy link
Author

hb20007 commented Mar 4, 2024

@jrieken On the Insiders build, I see this:

screenshot

As you can see, afterDelay is the predefined option.

@hb20007
Copy link
Author

hb20007 commented Mar 4, 2024

@jrieken,

Please reopen the issue. I have instructions to reproduce it without Prettier:

  1. Launch a fresh build of VS Code.
  2. Add the following in user settings:
    settings screenshot
    (Make sure that you also set "files.autoSave": "afterDelay" if it is not set for you by default.)
  3. Create a new .js file and type the following:
    screenshot showing unformatted code
  4. Press Alt + Tab twice.
  5. Notice that the code has been autoformatted (a semicolon was added):
    screenshot showing formatted code

If the issue does not get reproduced on the first attempt, try to manually save the file first and then try to reproduce it by removing the semicolon and pressing Alt + Tab.

@jrieken
Copy link
Member

jrieken commented Mar 4, 2024

@bpasero I see in code that afterDelay is the default for web (but not desktop). However, in the formatting save participant I explicitly check for AUTO and return early:

if (context.reason === SaveReason.AUTO) {

Is there a case, esp with Alt+Tab, that causes a save event with a different reason?

@bpasero
Copy link
Member

bpasero commented Mar 4, 2024

This actually seems to be a regression.

Web has auto save enabled by default because we were bold enough to decide this and saving in web felt wrong. It should have really been the default for desktop too, but is not, neither in stable nor insiders.

@bpasero bpasero reopened this Mar 4, 2024
@bpasero bpasero assigned bpasero and unassigned jrieken Mar 4, 2024
@bpasero bpasero added bug Issue identified by VS Code Team member as probable bug file-io File I/O and removed *question Issue represents a question, should be posted to StackOverflow (VS Code) labels Mar 4, 2024
@bpasero bpasero added this to the March 2024 milestone Mar 4, 2024
@bpasero bpasero closed this as completed in eaf5342 Mar 4, 2024
@VSCodeTriageBot VSCodeTriageBot added unreleased Patch has not yet been released in VS Code Insiders insiders-released Patch has been released in VS Code Insiders and removed unreleased Patch has not yet been released in VS Code Insiders labels Mar 4, 2024
@dhruvArora8053
Copy link

dhruvArora8053 commented Mar 5, 2024

Screenshot (1078)

Related on Stack Overflow: When I hit alt+tab, VS code do auto save, and formats code, how to stop it?

I am aware of this too. The user there does not confirm that they aren't using "files.autoSave": "onWindowChange" so I wasn't sure if I should link to it.

Yes I am not using the above instruction but problem still persists.

@hb20007
Copy link
Author

hb20007 commented Mar 5, 2024

Yes I am not using the above instruction but problem still persists.

The issue is no longer reproducible on the Insiders edition, so it should be fixed in the March release.

@bpasero bpasero added the verified Verification succeeded label Mar 5, 2024
yiliang114 pushed a commit to yiliang114/vscode that referenced this issue Mar 6, 2024
@microsoft microsoft locked and limited conversation to collaborators Jun 10, 2024
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
bug Issue identified by VS Code Team member as probable bug file-io File I/O insiders-released Patch has been released in VS Code Insiders verified Verification succeeded
Projects
None yet
Development

No branches or pull requests

6 participants