zed icon indicating copy to clipboard operation
zed copied to clipboard

Autosave blocks format on save

Open KennyLindahl opened this issue 9 months ago • 5 comments

Summary

Autosave makes format on save not work. CMD + S does not count as saving as the autosave is already done and then won't trigger format on save.

Description

Steps to reproduce:

  1. Do a change in a python file
  2. Autosave triggers
  3. CMD + S (save)

Actual: Does NOT trigger formatting

Expected:

  • Required: SHOULD trigger formatting
  • Optional: SHOULD trigger format on save when auto saving (if configured)

I'm sure this is related since it works if you hit CMD + S:

  • Before autosave is triggered
  • Or if autosave is disabled

My settings:

click to expand
{
  "icon_theme": "VSCode Icons (Dark)",
  "theme": "One Dark",
  "features": {
    "edit_prediction_provider": "zed",
    "inline_completions": false,
    "show_inline_completions": true
  },
  "ui_font_size": 14,
  "buffer_font_size": 13,
  "autosave": {
    "after_delay": {
      "milliseconds": 1000
    }
  },
  "format_on_save": "on",
  "language_overrides": {
    "JavaScript": {
      "format_on_save": {
        "external": {
          "command": "/Users/kenny.lindahl/.nvm/versions/node/v18.16.0/bin/prettier",
          "arguments": ["--stdin-filepath", "{buffer_path}"]
        }
      }
    }
  },
  "languages": {
    "JavaScript": {
      "code_actions_on_format": {
        "source.fixAll.eslint": true
      }
    },
    "YAML": {
      "formatter": "language_server",
      "format_on_save": "on"
    },
    "Python": {
      "language_servers": ["pylsp", "!pyright"],
      "formatter": {
        "external": {
          "command": "/opt/homebrew/bin/ruff",
          "arguments": ["format", "--stdin-filename", "{buffer_path}", "-"]
        }
      },
      "format_on_save": "on"
    }
  },
  "lsp": {
    "yaml-language-server": {
      "command": "/Users/kenny.lindahl/.nvm/versions/node/v20.10.0/bin/yaml-language-server",
      "args": ["--stdio"],
      "language_ids": ["yaml"],
      "settings": {
        "yaml": {
          "format": {
            "enable": true
          }
        }
      }
    },
    "pylsp": {
      "command": "/opt/homebrew/bin/pylsp",
      "language_ids": ["python"],
      "settings": {
        "plugins": {
          "flake8": {
            "enabled": true,
            "executable": "/opt/homebrew/bin/flake8",
            "config": "./.flake8"
          },
          "pycodestyle": { "enabled": false },
          "pyflakes": { "enabled": false },
          "mccabe": { "enabled": false },
          "autopep8": { "enabled": false },
          "yapf": { "enabled": false }
        }
      }
    },
    "pyright": {
      "settings": {
        "python.analysis": {
          "diagnosticMode": "workspace",
          "typeCheckingMode": "off"
        }
      }
    }
  },
  "project_panel": {
    "indent_size": 10
  },
  "base_keymap": "VSCode"
}

Zed Version and System Specs

Zed: v0.185.9 (Zed) OS: macOS 15.4.1 Memory: 24 GiB Architecture: aarch64

KennyLindahl avatar May 07 '25 08:05 KennyLindahl

I don't think autoformat on autosave makes very much sense, but I'm open to being convinced otherwise. I do feel, however, that our current behavior of not running format on save if the file is already saved doesn't make sense either, and should probably be changed. For now, I recommend doing editor: format (can bind to a key if you like)

probably-neb avatar May 07 '25 18:05 probably-neb

Hi Zed team,

I'm experiencing an issue where the format_on_save option doesn't seem to trigger automatically for JavaScript and React files despite being configured properly.

Here is my full config:

{
  "assistant": {
    "default_model": {
      "provider": "google",
      "model": "gemini-2.5-pro-exp-03-25"
    },
    "version": "2"
  },
  "ui_font_size": 20,
  "buffer_font_size": 17.0,
  "theme": {
    "mode": "system",
    "light": "One Light",
    "dark": "One Dark"
  },
  "autosave": {
    "after_delay": {
      "milliseconds": 1050
    }
  },
  "format_on_save": "on",
  "formatter": "auto",
  "languages": {
    "JavaScript": {
      "format_on_save": "on",
      "formatter": "auto"
    },
    "JSX": {
      "format_on_save": "on",
      "formatter": "auto"
    },
    "JavaScriptReact": {
      "format_on_save": "on",
      "formatter": "auto"
    }
  }
}

Expected behavior: Code should automatically format when autosave is triggered (after ~1 second of inactivity).

Actual behavior: Code formatting only occurs when I explicitly press Ctrl+S. Autosave does save the file but doesn't format it.

Is this expected behavior? If not, could this be a bug with how autosave and format_on_save interact?

Thanks for the awesome editor!

Abdelkader-gnichi avatar May 07 '25 18:05 Abdelkader-gnichi

Hi @KennyLindahl i have the same problem her and i notice it from several days, the first time i ignore it, but now my code base is growing and i think that it is so annoying that the auto-save blocking the auto-formatting.

Abdelkader-gnichi avatar May 07 '25 18:05 Abdelkader-gnichi

@probably-neb The optional part is separate from the main bug report, but it's related. You may discuss this internally if you want to.

The required part however is still important to fix i believe. I'm pretty sure this worked before.

Thanks for the suggestion, i might use the key binding for now as a work around.

KennyLindahl avatar May 07 '25 19:05 KennyLindahl

Looks like duplicate of https://github.com/zed-industries/zed/issues/16588

mati865 avatar Jun 01 '25 17:06 mati865

Seems it is, thanks @mati865

probably-neb avatar Jun 03 '25 08:06 probably-neb