In VS Code, how do you set up the ESLint extension so it applies ESLint's fixes on save in a repository with several packages?
answer
- a code action, not a formatter
- editor.codeActionsOnSave
- the fixAll source for ESLint
- all versus problems mode
- working directories in a monorepo
basics
~10 sAdd source.fixAll.eslint set to explicit under editor.codeActionsOnSave; the extension then applies ESLint's fixes on each save. eslint.codeActionsOnSave.mode picks all, the default, or the faster problems, and eslint.workingDirectories says where ESLint runs.
solid answer
~40 sThe VS Code ESLint extension runs the ESLint package installed in the workspace folder, falling back to a global install, so the editor uses the project's own config and rules. Fix-on-save is a **code action on save**: put `"source.fixAll.eslint": "explicit"` inside `editor.codeActionsOnSave`, ideally in the repository's `.vscode/settings.json` so everyone gets it. `eslint.codeActionsOnSave.mode` controls the depth: `all`, the default, revalidates the file and follows the same code path as `eslint --fix`; `problems` applies only the currently known fixes whose edits do not overlap, which is faster but often partial. `eslint.run` decides when problems are shown, `onType` by default or `onSave`. In a repository where you would `cd` into a package before running ESLint in a terminal, list those folders in `eslint.workingDirectories`.
code
json · 10 lines{
"editor.codeActionsOnSave": {
"source.fixAll.eslint": "explicit"
},
"eslint.codeActionsOnSave.mode": "all",
"eslint.workingDirectories": [
"./apps/operator-dashboard",
"./services/locker-events"
]
}go deeper
Remember the one setting that turns on fixing: source.fixAll.eslint set to explicit inside editor.codeActionsOnSave, and that the extension uses the project's own ESLint.
Explain the save action's depth, all against problems mode, the rules filter, and the difference between when ESLint runs, eslint.run, and what a save fixes.
Set up a monorepo so the editor agrees with the terminal: shared .vscode settings, eslint.workingDirectories per package, and checking the output channel when fixes do not apply.
Treat editor settings as part of the toolchain: commit them with the repository so every developer's save applies the same fixes the CI run would demand.
## What the extension runs The VS Code ESLint extension does not bundle its own ESLint. It uses the **ESLint library installed in the opened workspace folder**, and only falls back to a globally installed copy when the folder has none. That is why a local `devDependency` on `eslint` is the recommended install: the editor then runs the same ESLint version, the same `eslint.config.js` and the same plugins as the terminal and the CI job. For ESLint 10 there is nothing to switch on for flat config. The extension's `eslint.useFlatConfig` setting is ignored from ESLint 10.0.0 on, because flat config is the only format and cannot be turned off. ## Fixing on save VS Code runs **code actions on save**, and the extension contributes one called `source.fixAll.eslint`. Enable it in settings: ```json { "editor.codeActionsOnSave": { "source.fixAll.eslint": "explicit" } } ``` The extension also respects the generic `source.fixAll` action, so a team can turn on every fixer and then switch off ESLint's alone with `"source.fixAll.eslint": "never"`. The old `eslint.autoFixOnSave` setting was deprecated in version 2.0.0 of the extension in favour of `editor.codeActionsOnSave`. Putting these settings in the repository's `.vscode/settings.json` makes fix-on-save part of the project rather than a personal preference. ## How much a save fixes Two extension settings narrow what the save action does: | Setting | Values | Effect | |---|---|---| | `eslint.codeActionsOnSave.mode` | `all` (default), `problems` | `all` revalidates the file and runs the same code path as `eslint --fix`, so it can take time; `problems` applies only the currently known fixable problems whose edits do not overlap, which is much faster but often fixes only part of them | | `eslint.codeActionsOnSave.rules` | list of rule patterns | limits which rules are considered on save; ignored when the mode is `problems` | Saving fixes only what ESLint rules can fix. Layout such as indentation and line breaks belongs to a formatter, and how a formatter's save hook sits beside ESLint's is a Prettier question rather than an ESLint one. ## When problems appear - `eslint.run` is `onType` by default, so problems update as you type; `onSave` lints only when the file is saved. - `eslint.quiet`, off by default, hides warnings in the editor. - `eslint.validate`, `null` by default, restricts validation to the listed language identifiers, comparable to the CLI's `--ext`; `eslint.probe` lists the languages the extension tries by default and stays silent if validation fails for one of them. - The extension lints the open file. To lint a whole folder from the editor, `eslint.lintTask.enable` contributes a task that runs ESLint over the workspace. ## Monorepos and working directories `eslint.workingDirectories` tells the extension which directory to treat as ESLint's working directory for the files under it. The extension's own rule of thumb: if running ESLint in a terminal requires you to change into a subfolder first, you usually need this setting. Its forms: 1. `[{ "mode": "location" }]`, the default, uses the workspace folder, or the file's location when no folder is open. 2. `[{ "mode": "auto" }]` infers a directory from where files such as `package.json` and `eslint.config.js` sit; it works often but can surprise. 3. A list of paths, such as `["./apps/operator-dashboard", "./services/locker-events"]`, or a pattern such as `{ "pattern": "./packages/*/" }` for every package folder. By default the extension also changes the process's working directory to the chosen folder; adding `"!cwd": true` to an entry keeps ESLint's working directory without changing the process's. ESLint 10 already finds `eslint.config.*` by searching upward from each linted file, so this setting matters most for anything that still resolves from the working directory, such as relative paths a config builds from `process.cwd()`. ## Checking that it works - Open a file with a known fixable problem and save: the fix should appear and the problem disappear. - If nothing happens, turn on `eslint.debug` and read the ESLint output channel, which shows verbose information about how ESLint validates the file. - Compare with `npx eslint --fix <file>` run from the same directory; in the default `all` mode the two should agree, since both run the project's ESLint and config.
- Saving fixes only some of a file's fixable ESLint problems. Which setting would you check first?`eslint.codeActionsOnSave.mode`. In `problems` mode the extension applies only the currently known fixes whose edits do not overlap, which is fast but often partial. The default `all` revalidates the file through the same path as `eslint --fix`. Also check `eslint.codeActionsOnSave.rules`, which can limit the rules considered on save.
- Does the extension need `eslint.useFlatConfig` for a project on ESLint 10?No. The extension ignores that setting from ESLint 10.0.0 on, because flat config is the only format ESLint 10 supports. The setting only mattered for ESLint 8.57 through 9.x, where flat config could still be switched.
- Setting eslint.run to onSave is what makes the extension fix problems on save.
- The extension ships its own ESLint, so the project's installed version does not matter.
- problems mode is the default and fixes everything eslint --fix would.
- eslint.autoFixOnSave is the current setting for fixing on save.
- eslint.validate is how you point the extension at each package of a monorepo.