🐕

git-worktree内でeslintを使うとエラーになった。修正方法

2023/05/22に公開

概要

gitローカルリポジトリ内でgit-worktreeを使っていたら、eslintのデフォルト挙動である
「上位ディレクトリのeslintrcを全て見て、eslintrcがある全てのディレクトリでeslintを実行する」
で事故を起こしたので、修正をしました!

  • eslintの挙動 - Cascading and Hierarchy

  • git-worktreeとは

    • 簡単に言うと、特定のブランチ専用のディレクトリを、ローカルリポジトリの下に生成するものです
    • 例えば git clone foo && cd foo && git worktree add -b feature/bar feature/bar origin/developすると、./foo/feature/barが生成されます
    • ./foo/feature/barは、git-cloneした通常のローカルリポジトリと同じように扱えます

結論

'Cascading and Hierarchy'にある通り、root: trueを指定してあげて、上位ディレクトリの捜査をやめさせます。

./foo/feature/bar/.eslintrc.json
{
    "root": true,
    "extends": [
        ...
}

エラー内容

git-worktreeを使うと仕様上、eslintrcがあるディレクトリは少なくとも2つになります。

  • ./foo // ①
    • feature
      • bar // ②
$ cd ./foo/feature/bar

僕の場合、./fooでもyarn installをしていたので、まずは以下のエラーが出ました。
./fooでもeslintが実行されてしまっているからです。

$ eslint --fix --ext .ts src

Oops! Something went wrong! :(

ESLint: 8.39.0

ESLint couldn't determine the plugin "@typescript-eslint" uniquely.

- (リポジトリ)/feature/foo/node_modules/@typescript-eslint/eslint-plugin/dist/index.js (loaded in "--config")
- (リポジトリ)/node_modules/@typescript-eslint/eslint-plugin/dist/index.js (loaded in "../../.eslintrc.json")

Please remove the "plugins" setting from either config or remove either plugin installation.

If you still can't figure out the problem, please stop by https://eslint.org/chat/help to chat with the team.

こちらは単にこう直しました。

$ rm ../../node_modules

$ eslint --fix --ext .ts src

Oops! Something went wrong! :(

ESLint: 8.39.0

ESLint couldn't find the config "google" to extend from. Please check that the name of the config is correct.

The config "google" was referenced from the config file in "(リポジトリ)/.eslintrc.json".

If you still have problems, please stop by https://eslint.org/chat/help to chat with the team.

解決方法は'Cascading and Hierarchy'に書いてありました。

./foo/feature/bar/.eslintrc.json
{
    "root": true,
    "extends": [
        ...
}

おわり

おわり。

Discussion