fix(cli): validate that --cwd directory exists before execution#4658
Conversation
Review Summary by QodoValidate --cwd directory exists before git operations
WalkthroughsDescription• Validate --cwd directory exists before execution • Throw CliError with exit code 1 for non-existent directories • Add three test cases covering both flag variants and valid paths Diagramflowchart LR
A["User runs commitlint<br/>with --cwd flag"] --> B["normalizeFlags<br/>processes arguments"]
B --> C{"fs.existsSync<br/>check directory"}
C -->|"Directory exists"| D["Continue execution<br/>load config & lint"]
C -->|"Directory missing"| E["Throw CliError<br/>exit code 1"]
E --> F["Print error message<br/>to stderr"]
File Changes1. @commitlint/cli/src/cli.ts
|
Code Review by Qodo
1. CWD file path accepted
|
|
This pull request is automatically built and testable in CodeSandbox. To see build info of the built libraries, click here or the icon next to each commit SHA. |
There was a problem hiding this comment.
Pull request overview
Adds early validation for the --cwd/-d CLI option so commitlint fails fast (with a non-zero exit code and actionable message) when given an invalid working directory, addressing #4595.
Changes:
- Validate
flags.cwdviafs.statSync()and throw aCliErrorwhen it’s missing or not a directory. - Add CLI integration tests for non-existent
--cwd/-d, file-as-cwd, and valid directory behavior.
Reviewed changes
Copilot reviewed 2 out of 2 changed files in this pull request and generated 2 comments.
| File | Description |
|---|---|
| @commitlint/cli/src/cli.ts | Adds early --cwd directory validation and emits a CliError before any git/config operations run. |
| @commitlint/cli/src/cli.test.ts | Adds regression tests covering invalid/missing/valid --cwd and -d alias behavior. |
💡 Add Copilot custom instructions for smarter, more guided reviews. Learn how to get started.
You can also share your feedback on Copilot code review. Take the survey.
|
Please have a look at the copilot feedback, thanks! |
When a non-existent directory is passed via --cwd/-d, commitlint now prints a clear error message and exits with code 1 instead of silently returning exit code 0. Closes conventional-changelog#4595
Replaces existsSync with statSync + isDirectory to reject file paths that would cause uncontrolled errors downstream in git operations.
The catch-all handler for statSync failures reported all errors (EACCES, EPERM, ENOTDIR) as "does not exist". Now checks error.code and only uses that message for ENOENT; other errors surface the actual errno code.
556a942 to
0444e42
Compare
|
@escapedcat |
Problem
Running
commitlint -d doesNotExistsilently returns exit code 0 with no output. The non-existent directory is passed to git operations which fail silently.Fix
Added an
fs.existsSynccheck after flag normalization in themainfunction. If the directory doesn't exist, aCliErroris thrown with a clear message and exit code 1.This works because the check runs before any git operations or config loading, so the user gets an immediate, actionable error instead of silent success.
Tests
--cwdwith non-existent directory exits with code 1-dalias behaves the same--cwdwith valid directory works normallyFixes #4595
Closes #4595