Skip to content

fix: prevent duplicate generate:after hook execution#2036

Open
SushanthMusham wants to merge 2 commits intoasyncapi:masterfrom
SushanthMusham:fix-duplicate-after-hook
Open

fix: prevent duplicate generate:after hook execution#2036
SushanthMusham wants to merge 2 commits intoasyncapi:masterfrom
SushanthMusham:fix-duplicate-after-hook

Conversation

@SushanthMusham
Copy link
Copy Markdown

@SushanthMusham SushanthMusham commented Mar 29, 2026

Description

  • Fixed a bug where the generate:after hook was being executed twice when using the Generator API with output: 'fs' and a specified entrypoint.
  • Removed the redundant launchHook('generate:after') call inside handleEntrypoint in apps/generator/lib/generator.js to centralize hook execution and prevent duplicate side effects.
  • Added a strict regression test in apps/generator/test/generator.test.js to explicitly verify that the generate:after hook runs exactly once per generation lifecycle.

Related issue(s)
Fixes #1993

Summary by CodeRabbit

  • Bug Fixes

    • Fixed duplicate execution of the generate completion hook during file-system generation so the completion hook runs only once per generation.
  • Tests

    • Strengthened tests to assert the generate completion hook is invoked exactly once, preventing regressions that could trigger multiple invocations.

@changeset-bot
Copy link
Copy Markdown

changeset-bot bot commented Mar 29, 2026

⚠️ No Changeset found

Latest commit: e61c8e9

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

@asyncapi-bot
Copy link
Copy Markdown
Contributor

What reviewer looks at during PR review

The following are ideal points maintainers look for during review. Reviewing these points yourself beforehand can help streamline the review process and reduce time to merge.

  1. PR Title: Use a concise title that follows our Conventional Commits guidelines and clearly summarizes the change using imperative mood (it means spoken or written as if giving a command or instruction, like "add new helper for listing operations")

    Note - In Generator, prepend feat: or fix: in PR title only when PATCH/MINOR release must be triggered.

  2. PR Description: Clearly explain the issue being solved, summarize the changes made, and mention the related issue.

    Note - In Generator, we use Maintainers Work board to track progress. Ensure the PR Description includes Resolves #<issue-number> or Fixes #<issue-number> this will automatically close the linked issue when the PR is merged and helps automate the maintainers workflow.

  3. Documentation: Update the relevant Generator documentation to accurately reflect the changes introduced in the PR, ensuring users and contributors have up-to-date guidance.

  4. Comments and JSDoc: Write clear and consistent JSDoc comments for functions, including parameter types, return values, and error conditions, so others can easily understand and use the code.

  5. DRY Code: Ensure the code follows the Don't Repeat Yourself principle. Look out for duplicate logic that can be reused.

  6. Test Coverage: Ensure the new code is well-tested with meaningful test cases that pass consistently and cover all relevant edge cases.

  7. Commit History: Contributors should avoid force-pushing as much as possible. It makes it harder to track incremental changes and review the latest updates.

  8. Template Design Principles Alignment: While reviewing template-related changes in the packages/ directory, ensure they align with the Assumptions and Principles. If any principle feels outdated or no longer applicable, start a discussion these principles are meant to evolve with the project.

  9. Reduce Scope When Needed: If an issue or PR feels too large or complex, consider splitting it and creating follow-up issues. Smaller, focused PRs are easier to review and merge.

  10. Bot Comments: As reviewers, check that contributors have appropriately addressed comments or suggestions made by automated bots. If there are bot comments the reviewer disagrees with, react to them or mark them as resolved, so the review history remains clear and accurate.

@coderabbitai
Copy link
Copy Markdown

coderabbitai bot commented Mar 29, 2026

No actionable comments were generated in the recent review. 🎉

ℹ️ Recent review info
⚙️ Run configuration

Configuration used: Path: .coderabbit.yaml

Review profile: CHILL

Plan: Pro

Run ID: 07289256-6f6e-4ab4-a440-87139e99f076

📥 Commits

Reviewing files that changed from the base of the PR and between 1bee6b6 and e61c8e9.

📒 Files selected for processing (1)
  • apps/generator/test/generator.test.js
🚧 Files skipped from review as they are similar to previous changes (1)
  • apps/generator/test/generator.test.js

📝 Walkthrough

Walkthrough

Removed a duplicate invocation of generate:after from handleEntrypoint() so the generate:after hook runs only once via executeAfterHook() at the end of generate() when entrypoint is provided and output === 'fs'.

Changes

Cohort / File(s) Summary
Duplicate Hook Removal
apps/generator/lib/generator.js
Removed the extra this.launchHook('generate:after') call from handleEntrypoint(), preventing double execution of the generate:after lifecycle hook for filesystem output with an entrypoint.
Test Enhancement
apps/generator/test/generator.test.js
Strengthened #generate tests to assert generate:after is invoked exactly once by filtering gen.launchHook.mock.calls for 'generate:after' and checking the count equals 1.

Estimated code review effort

🎯 2 (Simple) | ⏱️ ~10 minutes

🚥 Pre-merge checks | ✅ 5
✅ Passed checks (5 passed)
Check name Status Explanation
Description Check ✅ Passed Check skipped - CodeRabbit’s high-level summary is enabled.
Title check ✅ Passed The title clearly and accurately describes the bug fix: preventing duplicate execution of the generate:after hook. It follows Conventional Commits guidelines with 'fix:' prefix and uses imperative mood as required.
Linked Issues check ✅ Passed The PR successfully addresses all coding requirements from issue #1993: removes the redundant launchHook call in handleEntrypoint(), centralizes hook execution, and adds regression tests asserting the hook runs exactly once.
Out of Scope Changes check ✅ Passed All changes are directly scoped to fixing the duplicate generate:after hook execution. Modifications to generator.js and test cases are focused on the reported issue with no extraneous changes.
Docstring Coverage ✅ Passed No functions found in the changed files to evaluate docstring coverage. Skipping docstring coverage check.

✏️ Tip: You can configure your own custom pre-merge checks in the settings.

✨ Finishing Touches
🧪 Generate unit tests (beta)
  • Create PR with unit tests

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 and usage tips.

Copy link
Copy Markdown

@coderabbitai coderabbitai bot left a comment

Choose a reason for hiding this comment

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

Actionable comments posted: 1

🧹 Nitpick comments (1)
apps/generator/test/generator.test.js (1)

138-159: Consider extending the hook count assertion to other #generate test cases.

For comprehensive regression coverage, consider adding the afterHookCalls.length === 1 assertion to the other test cases that verify generate:after is called (lines 174, 193, 213, 234, 255). This would ensure the "exactly once" invariant is consistently validated across all generation paths.

🤖 Prompt for AI Agents
Verify each finding against the current code and only fix it if needed.

In `@apps/generator/test/generator.test.js` around lines 138 - 159, The tests
currently assert that gen.launchHook was called with 'generate:after' in one
case; extend the same "exactly once" assertion to the other generate tests by
adding the same check used here (compute afterHookCalls =
gen.launchHook.mock.calls.filter(call => call[0] === 'generate:after') and
assert afterHookCalls.length === 1) inside each test that exercises
Generator.prototype.generate (i.e., the other it blocks that create a new
Generator, call gen.generate, and already assert gen.launchHook). Ensure you
reference the same mock function (gen.launchHook) and the same hook name
('generate:after') so each test verifies the hook is invoked exactly once.
🤖 Prompt for all review comments with AI agents
Verify each finding against the current code and only fix it if needed.

Inline comments:
In `@apps/generator/test/generator.test.js`:
- Around line 152-153: Add the same assertion that checks generate:after was
called once to the "entrypoint=set" test case (the test that sets output='fs',
forceWrite=false, install=false, entrypoint='set'); locate the test where
gen.launchHook is mocked and add: const afterHookCalls =
gen.launchHook.mock.calls.filter(call => call[0] === 'generate:after');
expect(afterHookCalls.length).toBe(1); so the regression for entrypoint=set with
output='fs' is covered (refer to gen.launchHook and the entrypoint=set test).

---

Nitpick comments:
In `@apps/generator/test/generator.test.js`:
- Around line 138-159: The tests currently assert that gen.launchHook was called
with 'generate:after' in one case; extend the same "exactly once" assertion to
the other generate tests by adding the same check used here (compute
afterHookCalls = gen.launchHook.mock.calls.filter(call => call[0] ===
'generate:after') and assert afterHookCalls.length === 1) inside each test that
exercises Generator.prototype.generate (i.e., the other it blocks that create a
new Generator, call gen.generate, and already assert gen.launchHook). Ensure you
reference the same mock function (gen.launchHook) and the same hook name
('generate:after') so each test verifies the hook is invoked exactly once.
🪄 Autofix (Beta)

Fix all unresolved CodeRabbit comments on this PR:

  • Push a commit to this branch (recommended)
  • Create a new PR with the fixes

ℹ️ Review info
⚙️ Run configuration

Configuration used: Path: .coderabbit.yaml

Review profile: CHILL

Plan: Pro

Run ID: 2a277ef6-ad99-4950-9cb3-e02327197887

📥 Commits

Reviewing files that changed from the base of the PR and between bd121dd and 1bee6b6.

📒 Files selected for processing (2)
  • apps/generator/lib/generator.js
  • apps/generator/test/generator.test.js
💤 Files with no reviewable changes (1)
  • apps/generator/lib/generator.js

@SHUBHANSHU602
Copy link
Copy Markdown
Contributor

Please read the guidelines, donot raise pr without maintainers consent

@sonarqubecloud
Copy link
Copy Markdown

sonarqubecloud bot commented Apr 4, 2026

Copy link
Copy Markdown
Member

@Adi-204 Adi-204 left a comment

Choose a reason for hiding this comment

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

@SushanthMusham the linting is failing you can run npm run lint to check whether it is passing or not.

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.

[BUG] generate:after hook is executed twice when using entrypoint with fs output

4 participants