Skip to content

Comments

Add CLI command typo detection (#34836)#10

Open
MitchLewis930 wants to merge 1 commit intopr_040_beforefrom
pr_040_after
Open

Add CLI command typo detection (#34836)#10
MitchLewis930 wants to merge 1 commit intopr_040_beforefrom
pr_040_after

Conversation

@MitchLewis930
Copy link

PR_040

* Add CLI command typo detection

* Apply suggestions from code review

Co-authored-by: Steven <steven@ceriously.com>

* Apply suggestions from code review

* Apply suggestions from code review

Co-authored-by: Steven <steven@ceriously.com>
@greptile-apps
Copy link

greptile-apps bot commented Jan 30, 2026

Greptile Overview

Greptile Summary

This PR adds CLI command typo detection using Levenshtein distance to help users who mistype Next.js commands.

Key changes:

  • Implemented detectTypo() function using Levenshtein distance algorithm with a threshold of 2 edit operations
  • Exported the commands object from next.ts to make valid commands available for typo checking
  • Added typo detection in getProjectDir() error handler to catch misspelled commands and suggest corrections
  • Added comprehensive test coverage for common typos (buidlbuild, starrstart, etc.)

How it works:
When a user types an invalid command like next buidl, the CLI treats it as a directory argument to the default dev command. When getProjectDir() fails to resolve the path, it checks if the string matches any command names within 2 edit operations and suggests the closest match.

Confidence Score: 4/5

  • Safe to merge with minor optimization opportunity
  • Implementation is sound with good test coverage. The Levenshtein algorithm is correct and the integration works as intended. Minor style improvement possible (unused threshold parameter), but no functional issues found.
  • No files require special attention

Important Files Changed

Filename Overview
packages/next/lib/detect-typo.ts New file implementing Levenshtein distance algorithm for typo detection with default threshold of 2
packages/next/lib/get-project-dir.ts Added typo detection logic in ENOENT error handler to suggest correct commands before showing directory error

Sequence Diagram

sequenceDiagram
    participant User
    participant CLI as next.ts (CLI)
    participant Command as next-dev/build/etc
    participant GetDir as getProjectDir()
    participant Detector as detectTypo()
    
    User->>CLI: next buidl
    CLI->>CLI: Check if "buidl" in commands
    Note over CLI: foundCommand = false
    CLI->>CLI: Set command = "dev" (default)
    CLI->>CLI: Set forwardedArgs = ["buidl"]
    CLI->>Command: Execute dev(["buidl"])
    Command->>GetDir: getProjectDir("buidl")
    GetDir->>GetDir: Try fs.realpathSync("buidl")
    GetDir->>GetDir: Error: ENOENT (no such dir)
    GetDir->>Detector: detectTypo("buidl", ["build", "start", ...])
    Detector->>Detector: Calculate Levenshtein distance
    Detector->>Detector: Filter distance <= 2 and > 0
    Detector->>Detector: Sort by distance
    Detector-->>GetDir: Return "build"
    GetDir->>GetDir: Log.error("Did you mean 'next build'?")
    GetDir->>GetDir: process.exit(1)
    GetDir-->>User: Error message displayed
Loading

Copy link

@greptile-apps greptile-apps bot left a comment

Choose a reason for hiding this comment

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

2 files reviewed, 1 comment

Edit Code Review Agent Settings | Greptile

@@ -0,0 +1,44 @@
// the minimum number of operations required to convert string a to string b.
function minDistance(a: string, b: string, threshold: number): number {
Copy link

Choose a reason for hiding this comment

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

threshold parameter unused - could optimize by early-exiting when distance exceeds threshold

Prompt To Fix With AI
This is a comment left during a code review.
Path: packages/next/lib/detect-typo.ts
Line: 2:2

Comment:
`threshold` parameter unused - could optimize by early-exiting when distance exceeds threshold

How can I resolve this? If you propose a fix, please make it concise.

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.

2 participants