Skip to content

Add AllowedPredicates configuration option to Rails/Env - #1617

Open
dduugg wants to merge 2 commits into
rubocop:masterfrom
dduugg:rails-env-allowed-predicates
Open

Add AllowedPredicates configuration option to Rails/Env#1617
dduugg wants to merge 2 commits into
rubocop:masterfrom
dduugg:rails-env-allowed-predicates

Conversation

@dduugg

@dduugg dduugg commented May 11, 2026

Copy link
Copy Markdown

Summary

Replace Rails/Env's hard-coded ALLOWED_LIST constant with a configurable AllowedPredicates option. The default value is the previous ALLOWED_LIST content — String / StringInquirer predicates like empty?, match?, between? — so out-of-the-box behavior is unchanged.

The motivation is to let users exempt additional predicates from the cop without forking or disabling it. Two concrete cases this addresses:

  • Rails.env.local? — the built-in alias for "development or test", introduced in Rails 7.1. Some users treat it as a legitimate guard rail for code that should only ever execute in dev or test (sanity checks, devtools, seed data) rather than as an environment-rollout mechanism, and want it exempt from the cop.
  • Custom predicates monkey-patched onto the environment inquirer. Apps with their own Rails.env.beta_user? / Rails.env.internal? style helpers can now opt those in.

The configured value fully replaces the default — it is not merged. Users who want to inherit the defaults and add to them can opt in via RuboCop's inherit_mode:

Rails/Env:
  AllowedPredicates:
    - empty?
    - match?
    - local?

Compatibility with Rails/EnvLocal

Rails/EnvLocal autocorrects Rails.env.development? || Rails.env.test?Rails.env.local?. Under the default AllowedPredicates, Rails/Env will still flag the autocorrected Rails.env.local?. Users who run both cops can now resolve that conflict by including local? in their AllowedPredicates rather than disabling either cop.

Test coverage

A new context block uses let(:cop_config) to override AllowedPredicates and verifies (a) a predicate in the override is not flagged, and (b) a predicate omitted from the override is flagged — confirming the override is exhaustive (no implicit merge with defaults).

Before submitting the PR make sure the following are checked:

  • The PR relates to only one subject with a clear title and description in grammatically correct, complete sentences.
  • Wrote good commit messages.
  • Commit message starts with [Fix #issue-number] (no related issue).
  • Feature branch is up-to-date with master.
  • Squashed related commits together.
  • Added tests.
  • Ran bundle exec rake default.
  • Added an entry to the changelog folder.
  • If this is a new cop, consider making a corresponding update to the Rails Style Guide (N/A — existing cop).

Replace the hard-coded `ALLOWED_LIST` constant with a configurable
`AllowedPredicates` option. The default value preserves the previous
behavior — `String` / `StringInquirer` predicates such as `empty?`,
`match?`, and `between?` are still exempt out of the box — but users
can now customize the list, for example to allow `Rails.env.local?`
or a custom predicate monkey-patched onto the environment inquirer.

The configured value fully replaces the default; users who want to
inherit the defaults and add to them can opt in via RuboCop's
`inherit_mode`.

  Rails/Env:
    AllowedPredicates:
      - empty?
      - match?
      - local?

A new spec block uses `let(:cop_config)` to verify that overriding
`AllowedPredicates` both allows the configured predicate and continues
to flag predicates that are no longer in the list.
@dduugg
dduugg force-pushed the rails-env-allowed-predicates branch from 6dc5bea to b7b27ad Compare May 11, 2026 17:57
@dduugg
dduugg marked this pull request as ready for review May 11, 2026 18:07
@corsonknowles

ghost commented May 11, 2026

Copy link
Copy Markdown
Contributor

Great fix! Thanks for the added versatility.

And we need this now as well, because we do allow .local?

@corsonknowles

ghost commented Jun 17, 2026

Copy link
Copy Markdown
Contributor

@Earlopain can you help us get this across the line? @fxn had asked us to support .local? but we didn't push that change upstream before it was merged.

The approach here allows everyone to adjust the cop accordingly so they can keep Rails/EnvLocal and also extend the behavior as needed

@fxn

ghost commented Jun 17, 2026

Copy link
Copy Markdown

It would be really convenient to have local?. As the description says, there are cases in which you want to 1) be intentional about the environments, 2) rule out completely anything else. In such cases, the behavior is not configurable, really, it has to be that way, conceptually hard-coded.

@Earlopain

ghost commented Jun 18, 2026

Copy link
Copy Markdown
Contributor

can you help us get this across the line

Sorry, you would be better to get the attention of a maintainer. I haven't done any rubocop this year.

@koic

ghost commented Jun 18, 2026

Copy link
Copy Markdown
Member

I'm not sure a public option like AllowedPredicates is needed here. Would it be sufficient to just add local? to the existing ALLOWED_LIST constant?

@corsonknowles

ghost commented Jun 18, 2026

Copy link
Copy Markdown
Contributor

I'm not sure a public option like AllowedPredicates is needed here. Would it be sufficient to just add local? to the existing ALLOWED_LIST constant?

Thanks @koic, I put together that approach here:

@dduugg

ghost commented Jun 22, 2026

Copy link
Copy Markdown
Author

I'm not sure a public option like AllowedPredicates is needed here. Would it be sufficient to just add local? to the existing ALLOWED_LIST constant?

I could live with that, though I would like to be able to drop the other allowed predicates (it’s slightly silly to ban production? while allowing start_with? 🙈). It would also be a breaking change for dependents that want to continue disallowing local?.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

5 participants