Skip to content

feat: add require-cd-guard rule - #12

Open
nzakas wants to merge 5 commits into
rule/require-read-rfrom
rule/require-cd-guard
Open

nzakas wants to merge 5 commits into
rule/require-read-rfrom
rule/require-cd-guard

Conversation

@nzakas

@nzakas nzakas commented Sep 18, 2026 •

Copy link
Copy Markdown
Member

Adds shell/require-cd-guard, which mirrors ShellCheck SC2164: if cd fails, the script keeps running in the wrong directory.

Behavior

  • Reports cd unless its exit status is used:
    • on the left of && or || (cd dir || exit, cd dir && make);
    • as an if, while, or until condition;
    • negated with !.
  • cd on the right of && is still reported.
  • Suggestion (not an autofix): append || exit. It's a suggestion because return may be the right choice inside functions. It isn't offered when the cd has redirects or runs in the background.
  • Recommended config: "error".

Known limitations

  • set -e isn't taken into account, so scripts that rely on errexit still get reports.
  • cd inside a subshell ((cd dir; make)) is reported too.

Documentation

Adds docs/rules/require-cd-guard.md, following the format of the @eslint/json, @eslint/css, and @eslint/markdown rule docs: description, background, rule details with incorrect and correct examples, options, when not to use it, and the ShellCheck reference. The rule's meta.docs.url points at that file, and its README table entry links to it.

Testing

10 RuleTester cases, including suggestion output, and 5 documentation checks (312 total); build, lint, and format checks pass.

🤖 Generated with Claude Code


Stack created with GitHub Stacks CLI • Give Feedback 💬

Summary by CodeRabbit

  • New Features

    • Added a rule that flags unguarded cd commands, based on ShellCheck SC2164.
    • Provides suggestions to append || exit where appropriate.
    • Enabled the rule as an error in the recommended configuration.
  • Documentation

    • Added usage guidance, examples, supported conditions, and configuration details for the new rule.
  • Tests

    • Added coverage for valid and invalid command patterns, diagnostics, and automated suggestions.

@coderabbitai

coderabbitai Bot commented Sep 18, 2026 •

Copy link
Copy Markdown

Review in Change Stack →

Warning

Review limit reached

You've used all free OSS reviews for now. Wait for the free limit to reset to keep reviewing this public repository.

Next included review available in 55 minutes.

Check out review usage here.

View limit details

Limit details: You’ve used the included review currently available.

Learn how review limits work.

Review configuration:

⚙️ Run configuration
  • Configuration used: Repository: eslint/coderabbit/.coderabbit.yaml
  • Review profile: CHILL
  • Plan: Advanced
  • Run ID: ff3af38f-b485-4bbc-9037-f21396848237
📥 Commits

Reviewing files that changed from the base of the PR and between 8437b3a and fa0e219.

📒 Files selected for processing (6)
  • README.md
  • docs/rules/require-cd-guard.md
  • src/index.spec.ts
  • src/index.ts
  • src/rules/require-cd-guard.spec.ts
  • src/rules/require-cd-guard.ts
ℹ️ Recent review info
⚙️ Run configuration

Configuration used: defaults

Review profile: CHILL

Plan: Free

Run ID: f06aef84-e9f8-4ae5-873f-c24020712732

📥 Commits

Reviewing files that changed from the base of the PR and between be6c318 and 8437b3a.

📒 Files selected for processing (6)
  • README.md
  • docs/rules/require-cd-guard.md
  • src/index.spec.ts
  • src/index.ts
  • src/rules/require-cd-guard.spec.ts
  • src/rules/require-cd-guard.ts

Included review availability: Your plan provides up to 1 included review per hour; 0 remain after this review.


📝 Walkthrough

Walkthrough

The PR adds the require-cd-guard rule, exports it, enables it in the recommended configuration, adds tests, and documents its behavior and suggestions.

Changes

Require CD Guard

Layer / File(s) Summary
Rule behavior and validation
src/rules/require-cd-guard.ts, src/rules/require-cd-guard.spec.ts
The rule reports unchecked cd commands and suggests `
Plugin registration and recommended configuration
src/index.ts, src/index.spec.ts
The plugin exports require-cd-guard, enables it as an error in the recommended configuration, and verifies the exported rule list.
Rule documentation
docs/rules/require-cd-guard.md, README.md
The documentation describes the checks, suggestions, examples, options, disabling guidance, ShellCheck SC2164, and the documented rule list.

Estimated code review effort: 3 (Moderate) | ~20 minutes


Thanks for using CodeRabbit! It's free for OSS, and your support helps us grow. If you like it, consider giving us a shout-out.

❤️ Share

Comment @coderabbitai help to get the list of available commands.

@nzakas
nzakas added this pull request to stack #16 September 18, 2026 15:00
@nzakas
nzakas marked this pull request as ready for review September 18, 2026 15:02
@nzakas
nzakas force-pushed the rule/require-cd-guard branch from f6eca2c to bd4436e Compare September 18, 2026 19:01
@nzakas
nzakas force-pushed the rule/require-cd-guard branch 2 times, most recently from 56a1409 to b797e24 Compare September 22, 2026 20:23
@nzakas
nzakas force-pushed the rule/require-cd-guard branch from b797e24 to 334383f Compare September 22, 2026 20:34
@nzakas
nzakas force-pushed the rule/require-cd-guard branch from 334383f to 8437b3a Compare September 22, 2026 21:21
@nzakas
nzakas requested a balanced review from Copilot September 28, 2026 18:25

Copilot AI left a comment

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Copilot review overview

🟡 Changes recommended

Status-use detection has false positives and negatives, and the suggested guard can incorrectly terminate scripts.

Review effort: Balanced
Findings: 1 Medium severity

Open (1)
What changed in this PR

Adds the bash/require-cd-guard rule based on ShellCheck SC2164.

Changes:

  • Detects unguarded cd commands and offers an || exit suggestion.
  • Enables the rule in the recommended configuration.
  • Adds tests and documentation.
File Description
src/​rules/​require-cd-guard.ts Implements detection and suggestions.
src/​rules/​require-cd-guard.spec.ts Tests rule behavior.
src/​index.ts Registers and enables the rule.
src/​index.spec.ts Verifies rule registration.
README.md Adds the rule to the catalog.
docs/​rules/​require-cd-guard.md Documents usage and behavior.

💡 Add a code-review agent skill or configure MCP servers for context-aware, tailored reviews. Learn more in the docs.

Comment thread src/rules/require-cd-guard.ts Outdated
@nzakas

nzakas commented Sep 29, 2026

Copy link
Copy Markdown
Member Author

@copilot address review comments. Be sure to add tests.

Copilot AI commented Sep 29, 2026

Copy link
Copy Markdown

@copilot address review comments. Be sure to add tests.

Updated the rule to walk enclosing logical-expression ancestors and added &&/|| chain regression tests. Committed as df636d5.

@nzakas
nzakas force-pushed the rule/require-cd-guard branch from df636d5 to abceb0a Compare October 1, 2026 18:07
@nzakas
nzakas force-pushed the rule/require-cd-guard branch from e8aaecc to aaebfa7 Compare October 2, 2026 16:05
@nzakas
nzakas force-pushed the rule/require-cd-guard branch 2 times, most recently from 266647f to 9e108b4 Compare October 6, 2026 19:09
@nzakas
nzakas force-pushed the rule/require-cd-guard branch from 9e108b4 to 5109a9b Compare October 6, 2026 19:26
@nzakas
nzakas force-pushed the rule/require-cd-guard branch 2 times, most recently from 1675915 to 8b66100 Compare October 8, 2026 19:46
nzakas and others added 5 commits October 8, 2026 16:49
Co-Authored-By: Claude Opus 5 <noreply@anthropic.com>
Co-authored-by: nzakas <38546+nzakas@users.noreply.github.com>
Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
The rule only listed shell/bash in meta.languages, so ESLint rejected it
when used with the shell/posix or shell/mksh language.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
@nzakas
nzakas force-pushed the rule/require-cd-guard branch from 8b66100 to fa0e219 Compare October 8, 2026 20:51
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

Projects

None yet

Development

Successfully merging this pull request may close these issues.

3 participants