Integrate with your VCS
Цей контент ще не доступний вашою мовою.
The VCS (Version Control System) integration gives Biome additional features that only a VCS can provide, such as reading your ignore files and finding the files you changed. Biome supports only Git, and the integration is opt-in.
To enable the integration, set vcs.enabled and vcs.clientKind in the Biome configuration file:
{ "vcs": { "enabled": true, "clientKind": "git" }}This configuration does not change anything on its own. Opt in to the features you want, as described in the following sections.
Use the ignore file
Section titled “Use the ignore file”Enable vcs.useIgnoreFile to make Biome ignore all the files and directories listed in the project’s VCS ignore files, as well as in .ignore files.
For Git repositories, Biome reads .gitignore files and Git’s local exclude file at .git/info/exclude. In linked worktrees, Biome reads .git/info/exclude from the common Git directory, matching Git’s own behavior.
{ "vcs": { "enabled": true, "clientKind": "git", "useIgnoreFile": true }}Process only changed files
Section titled “Process only changed files”The --changed option processes only the files that have changed from one revision to another. It is available only in the CLI.
First, set the default branch in the vcs.defaultBranch field of your configuration file:
{ "vcs": { "enabled": true, "clientKind": "git", "useIgnoreFile": true, "defaultBranch": "main" }}Then, add the --changed option to your command. Biome asks the VCS which files changed between the main branch and your current revision, and processes only those files:
npx biome check --changedpnpm biome check --changedbunx biome check --changeddeno run -A npm:@biomejs/biome check --changedyarn biome check --changedbiome check --changed--changed compares only committed changes. It does not include staged or unstaged changes, so it is intended for CI. To check your changes locally before you commit them, process only staged files instead.
To compare against another branch, use the --since option. It takes precedence over vcs.defaultBranch. For example, to check your changes against the next branch:
npx biome check --changed --since=nextpnpm biome check --changed --since=nextbunx biome check --changed --since=nextdeno run -A npm:@biomejs/biome check --changed --since=nextyarn biome check --changed --since=nextbiome check --changed --since=nextProcess only staged files
Section titled “Process only staged files”Before you commit your changes, you can check formatting and linting for the files added to the index (also known as staged files), without running Biome on the entire project.
To process only the files that you are about to commit, pass the --staged option to the command:
npx biome check --stagedpnpm biome check --stagedbunx biome check --stageddeno run -A npm:@biomejs/biome check --stagedyarn biome check --stagedbiome check --stagedTo also format, lint, sort imports, and apply safe fixes to them, add --write:
npx biome check --write --stagedpnpm biome check --write --stagedbunx biome check --write --stageddeno run -A npm:@biomejs/biome check --write --stagedyarn biome check --write --stagedbiome check --write --stagedThe --staged option is not available in the ci command, because you usually do not commit changes during CI. To run it before every commit, use a Git hook.
Run Biome in Git hooks
Section titled “Run Biome in Git hooks”Git hooks run scripts during Git commands, for example to format and lint the files you staged before you commit or push them. You can write the hook script yourself, or call Biome from one of the common hook managers.
Most examples in this section also pass two flags:
--files-ignore-unknown=trueskips files that Biome does not support, so the hook handles every file type that Biome supports today and in later versions.--no-errors-on-unmatchedprevents an error when no files are processed, for example when you only staged files that Biome ignores.
Write your own hook
Section titled “Write your own hook”Shell scripts can behave differently across platforms, so a dedicated tool such as Lefthook or pre-commit is more reliable.
To check your staged files before every commit, save this script as .git/hooks/pre-commit:
#!/bin/shset -eu
npx @biomejs/biome check --staged --files-ignore-unknown=true --no-errors-on-unmatchedTo also apply safe fixes, and stage them:
#!/bin/shset -eu
if git status --short | grep --quiet '^MM'; then printf '%s\n' "ERROR: Some staged files have unstaged changes" >&2 exit 1;fi
npx @biomejs/biome check --write --staged --files-ignore-unknown=true --no-errors-on-unmatched
git update-index --againThis hook fails if a staged file also has unstaged changes, because git update-index --again would stage those changes along with the fixes.
Lefthook
Section titled “Lefthook”Lefthook is a fast, cross-platform hook manager without dependencies.
-
Install Lefthook, for example with npm.
-
Create
lefthook.ymlat the root of your Git repository. This configuration checks formatting and linting before every commit:lefthook.yml pre-commit:commands:check:run: npx @biomejs/biome check --no-errors-on-unmatched --files-ignore-unknown=true {staged_files} -
Run
lefthook installto set up the hooks.
To format, lint, and apply safe fixes before every commit, add --write. Then, stage_fixed: true adds the fixed files back to the staging area:
pre-commit: commands: check: run: npx @biomejs/biome check --write --no-errors-on-unmatched --files-ignore-unknown=true {staged_files} stage_fixed: trueTo check formatting and linting before every push instead, use the pre-push hook and the {push_files} placeholder:
pre-push: commands: check: run: npx @biomejs/biome check --no-errors-on-unmatched --files-ignore-unknown=true {push_files}To process only some files, add a Lefthook glob, such as glob: "*.{js,ts,jsx,tsx}", to the command. You then do not need --files-ignore-unknown=true.
Husky is a widely used hook manager in the JavaScript ecosystem. Husky does not hide unstaged changes and cannot provide the list of staged files, so it is often used together with lint-staged or git-format-staged.
If your project contains a package.json, you can set up the Husky hooks every time you install your packages with scripts.prepare:
{ "scripts": { "prepare": "husky" }}lint-staged
Section titled “lint-staged”lint-staged runs commands on your staged files, and is one of the most used tools in the JavaScript ecosystem.
-
Add the following Husky hook:
.husky/pre-commit lint-staged -
Configure lint-staged in
package.json. This configuration formats, lints, sorts imports, and applies safe fixes to every staged file that Biome supports:package.json {"lint-staged": {"*": ["biome check --write --no-errors-on-unmatched --files-ignore-unknown=true"]}}
lint-staged runs every command in the array, so each entry adds a step instead of offering an alternative. To change what the hook does, adjust the command:
- Remove
--writeto only report formatting and lint problems, without changing your files. - Add
--unsafeto also apply unsafe fixes. - Add
--assist-enabled=falseto format and apply safe fixes without running assist actions such as import sorting. - Use
biome format --writeto only format, orbiome lint --writeto only lint and apply safe fixes. - Replace
"*"with a glob such as"*.{js,ts,cjs,mjs,d.cts,d.mts,jsx,tsx,json,jsonc,css}"to process only some files. You then do not need--files-ignore-unknown=true.
Keep --no-errors-on-unmatched in every command, so that the hook does not fail when no files are processed.
git-format-staged
Section titled “git-format-staged”Unlike Lefthook, pre-commit, and lint-staged, git-format-staged does not use git stash internally. This avoids manual intervention when your unstaged changes conflict with the updated staged changes. See the comparison of git-format-staged with other tools.
To check formatting and linting before every commit:
git-format-staged --formatter 'biome check --files-ignore-unknown=true --no-errors-on-unmatched --stdin-file-path="{}"' '*'To format, lint, and apply safe fixes before every commit:
git-format-staged --formatter 'biome check --write --files-ignore-unknown=true --no-errors-on-unmatched --stdin-file-path="{}"' '*'pre-commit
Section titled “pre-commit”pre-commit is a hook manager for projects in any language. Biome provides four hooks in the biomejs/pre-commit repository:
| Hook ID | Description |
|---|---|
biome-ci |
Checks formatting, import sorting, and lint rules. |
biome-check |
Formats, sorts imports, lints, and applies safe fixes to the committed files. |
biome-format |
Formats the committed files. |
biome-lint |
Lints and applies safe fixes to the committed files. |
-
Create
.pre-commit-config.yamlat the root of your project. This configuration uses thebiome-checkhook, which runsbiome check --writeevery time you rungit commit:.pre-commit-config.yaml repos:- repo: https://github.com/biomejs/pre-commitrev: "v2.6.10"hooks:- id: biome-checkEach tag of the repository installs the Biome version with the same number. To upgrade to the latest version, run
pre-commit autoupdate. -
Run
pre-commit installin your repository.
If your project already installs Biome as an npm package, you then have to update both package.json and .pre-commit-config.yaml every time you upgrade Biome. To avoid this, use a local hook instead of the hooks that Biome provides. For example, if you use npm:
repos: - repo: local hooks: - id: local-biome-check name: biome check entry: npx @biomejs/biome check --write --files-ignore-unknown=true --no-errors-on-unmatched language: system types: [text] files: "\\.(jsx?|tsx?|c(js|ts)|m(js|ts)|d\\.(ts|cts|mts)|jsonc?|css)$"The files option is optional, because --files-ignore-unknown=true already makes Biome skip the files it does not support.
© 2023-present Biome developers and contributors.