---
title: "在 GitHub Actions 中讓 Rubocop 與 ESLint 忽略 bundled gems"
description: "Rails 專案在 GitHub actions 中通常會把 gems 安裝到 vendor 目錄，方便在後續執行時使用快取。"
canonical: https://oozou.com/til/ignore-bundled-gems-for-rubocop-and-eslint-in-github-actions-154
locale: zh-Hant
date: 2022-01-19
topic: ruby-on-rails
---

# 在 GitHub Actions 中讓 Rubocop 與 ESLint 忽略 bundled gems

> Rails 專案在 GitHub actions 中通常會把 gems 安裝到 vendor 目錄，方便在後續執行時使用快取。

Published: 2022-01-19 · Topic: [ruby on rails](https://oozou.com/zh-Hant/til-topics/ruby-on-rails) ([markdown](https://oozou.com/zh-Hant/til-topics/ruby-on-rails.md))

Rails 專案在 GitHub actions 中通常會把 gems 安裝到 vendor 目錄，方便在後續執行時使用快取。

如果你在 action 中使用程式碼 linter，就需要排除 `vendor/bundle/\*`，以避免誤報  

ESLint 可以透過 ignorePatterns 來設定

```json
// ./.eslintrc.json
{
  "ignorePatterns": [
    "vendor/bundle/*"
  ]
}
```

Rubocop 則可以透過 AllCops 設定來排除

```yaml
# ./.rubocop.yml
AllCops:
  Exclude:
    - 'vendor/**/*'
```

# 在程式碼 linter 中忽略 bundled gems 的重要性

在軟體開發中，維持高程式碼品質至關重要。程式碼 linter 透過靜態程式碼分析協助達成這個目標，能偵測潛在錯誤並強制一致的程式碼風格。然而，在 GitHub Actions 這類持續整合環境中使用 linter 時，正確設定以避免誤報非常重要。

Bundled gems 通常存放在 vendor 目錄中，屬於外部程式碼相依套件，如果沒有妥善排除，就可能被程式碼 linter 標記。忽略這些目錄可以確保 linter 專注在你的原始碼而不是外部函式庫，進而維持程式碼分析的正確性。

# 設定 linter 排除 bundled gems

如果是使用 ESLint 的 JavaScript 專案，你可以透過 .eslintrc.json 檔案中的 ignorePatterns 設定，讓 linter 忽略 vendor/bundle/\* 目錄。這能確保只檢查你自己的程式碼，在沒有不必要干擾的情況下提升程式碼品質。

```
// ./.eslintrc.json
{
  "ignorePatterns": [
    "vendor/bundle/*"
  ]
}
```

同樣地，如果是使用 Rubocop 的 Ruby 專案，你可以在 .rubocop.yml 設定檔的 AllCops 底下加入 Exclude 規則來排除 vendor 目錄。這有助於讓靜態程式碼分析聚焦在你實際的原始碼上。

```
# ./.rubocop.yml
AllCops:
  Exclude:
    - 'vendor/**/*'
```

透過設定程式碼 linter 排除 bundled gems，你可以在避免靜態程式碼分析誤報的同時，維持高標準的程式碼品質。  

[#RUBY-ON-RAILS](https://oozou.com/til-topics/ruby-on-rails)

---

Contact OOZOU: hello@oozou.com · +66 2544 0000 · https://oozou.com/zh-Hant/contact

More for AI agents: https://oozou.com/llms.txt · every page has a markdown twin at its URL plus ".md"
