Skip to content

STG-1671: chore: add prettier pre-commit hook via husky + lint-staged#1888

Open
shrey150 wants to merge 2 commits intomainfrom
shrey/add-prettier-precommit
Open

STG-1671: chore: add prettier pre-commit hook via husky + lint-staged#1888
shrey150 wants to merge 2 commits intomainfrom
shrey/add-prettier-precommit

Conversation

@shrey150
Copy link
Contributor

@shrey150 shrey150 commented Mar 25, 2026

Summary

  • Adds a pre-commit hook that runs prettier on staged files to catch formatting issues before CI
  • Uses husky for git hooks and lint-staged to scope prettier to only staged files
  • Preserves the existing prepare script behavior (node packages/core/scripts/prepare.js)

Changes

  • Added husky and lint-staged as root devDependencies
  • Created .husky/pre-commit hook that runs pnpm exec lint-staged
  • Configured lint-staged: { "*": "prettier --write --ignore-unknown" } (respects .prettierignore)

Test plan

  • Hook runs on commit — verified: badly formatted file auto-fixed during commit
  • pnpm install sets up hooks correctly via prepare script

🤖 Generated with Claude Code

Set up husky and lint-staged so formatting issues are caught before
commit instead of failing in CI. The pre-commit hook runs prettier
via lint-staged on all staged files, respecting .prettierignore.

Co-Authored-By: Claude Opus 4.6 (1M context) <noreply@anthropic.com>
@changeset-bot
Copy link

changeset-bot bot commented Mar 25, 2026

⚠️ No Changeset found

Latest commit: c95044f

Merging this PR will not cause a version bump for any packages. If these changes should not result in a new version, you're good to go. If these changes should result in a version bump, you need to add a changeset.

This PR includes no changesets

When changesets are added to this PR, you'll see the packages that this PR includes changesets for and the associated semver types

Click here to learn what changesets are, and how to add one.

Click here if you're a maintainer who wants to add a changeset to this PR

Copy link
Contributor

@cubic-dev-ai cubic-dev-ai bot left a comment

Choose a reason for hiding this comment

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

1 issue found across 3 files

Confidence score: 4/5

  • This PR looks safe to merge with minimal risk, but there is a moderate sequencing issue (5/10 severity, high confidence) rather than a runtime/product break.
  • In package.json, running Husky setup before the existing prepare step can skip packages/core/scripts/prepare.js if Husky fails, which may leave core preparation incomplete in affected environments.
  • Pay close attention to package.json and packages/core/scripts/prepare.js - script order should ensure prepare always runs even when Husky setup errors.
Prompt for AI agents (unresolved issues)

Check if these issues are valid — if so, understand the root cause of each and fix them. If appropriate, use sub-agents to investigate and fix each issue separately.


<file name="package.json">

<violation number="1" location="package.json:39">
P2: Run the existing prepare script before Husky setup. In the current order, a Husky failure skips `packages/core/scripts/prepare.js` entirely.</violation>
</file>
Architecture diagram
sequenceDiagram
    participant Dev as Developer
    participant PM as pnpm (Package Manager)
    participant Git as Git Engine
    participant Husky as Husky (Hooks)
    participant LS as lint-staged
    participant Prettier as Prettier (Formatter)
    participant Core as Core Prepare Script

    Note over Dev, Core: Initialization Flow (pnpm install)
    
    Dev->>PM: install
    PM->>PM: Run "prepare" script
    PM->>Husky: NEW: husky (Initialize hooks)
    PM->>Core: node packages/core/scripts/prepare.js
    Husky->>Git: Write .git/hooks/pre-commit

    Note over Dev, Core: Commit Flow (git commit)

    Dev->>Git: git commit -m "..."
    Git->>Husky: Trigger pre-commit hook
    Husky->>LS: NEW: Run lint-staged
    
    LS->>Git: Identify staged files
    Git-->>LS: List of files
    
    alt Files match glob (*)
        LS->>Prettier: NEW: prettier --write --ignore-unknown [files]
        Prettier->>Prettier: Format staged files
        Prettier-->>LS: Success
        LS->>Git: Re-stage formatted changes
    end

    LS-->>Husky: Exit code 0
    Husky-->>Git: Allow commit
    Git->>Git: Create commit object
Loading

Reply with feedback, questions, or to request a fix. Tag @cubic-dev-ai to re-run a review.

… failure

Co-Authored-By: Claude Opus 4.6 (1M context) <noreply@anthropic.com>
Copy link
Contributor Author

@shrey150 shrey150 left a comment

Choose a reason for hiding this comment

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

Addressed — swapped the order so prepare.js (build) runs first, and husky (hook setup) runs after. A husky failure no longer skips the build. See c95044f.

@shrey150 shrey150 changed the title chore: add prettier pre-commit hook via husky + lint-staged STG-1671: chore: add prettier pre-commit hook via husky + lint-staged Mar 25, 2026
Copy link
Member

@pirate pirate left a comment

Choose a reason for hiding this comment

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

Approved but I'd use oxlint and oxfmt over prettier

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.

3 participants