Skip to content

watch: track worker thread entry files in --watch mode#62368

Merged
nodejs-github-bot merged 8 commits intonodejs:mainfrom
SudhansuBandha:watch-worker-support
Apr 28, 2026
Merged

watch: track worker thread entry files in --watch mode#62368
nodejs-github-bot merged 8 commits intonodejs:mainfrom
SudhansuBandha:watch-worker-support

Conversation

@SudhansuBandha
Copy link
Copy Markdown
Contributor

Currently, --watch mode only tracks dependencies from the main module graph (require/import). Worker thread entry points created via new Worker() are not included, so changes to worker files do not trigger restarts.

This change hooks into Worker initialization and registers the worker entry file with the watch mode, ensuring restarts when worker files change.

Fixes: #62275

@nodejs-github-bot nodejs-github-bot added needs-ci PRs that need a full CI run. worker Issues and PRs related to Worker support. labels Mar 21, 2026
@SudhansuBandha SudhansuBandha marked this pull request as ready for review March 21, 2026 17:55
@clemyan
Copy link
Copy Markdown

clemyan commented Mar 22, 2026

This solves the issue for the worker module itself, but not its dependencies. I.e. if a worker module imports/requires another module, modifying the latter still does not trigger re-run.

@SudhansuBandha
Copy link
Copy Markdown
Contributor Author

I have updated changes to watch dependencies for worker as well, so changes in imported/required modules will now trigger a re-run.
@clemyan

@clemyan
Copy link
Copy Markdown

clemyan commented Mar 23, 2026

This is the wrong way to solve this, for a myriad of reasons: (based on a cursory glance of the code. Did not bother to actual test it)

  1. It is impossible to properly parse JS with regex. This approach cannot even distinguish if the "import" is part of a string or a comment
  2. It only works for direct dependencies of the worker module, not transitively. (I.e. it does not watch dependencies of dependencies, and their dependencies, etc.)
  3. It only works for literals specifiers that are relative paths, not for absolute paths require('/path/to/file.js'), packages require('my-pkg/sub/path'), dynamic specifier require('./messages/'+language+'/string.json'), or paths that are resolved by customization hooks
  4. What about the worker module itself? If it is loaded by customization hooks, there isn't necessarily a file on disk to readSync from.

Any solution must be integrated into Node's loading pipeline

@SudhansuBandha
Copy link
Copy Markdown
Contributor Author

SudhansuBandha commented Mar 23, 2026

I will revert back these updates and work on the suggestions
@clemyan

Copy link
Copy Markdown

Choose a reason for hiding this comment

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

Submit the change.

@franciscoarturorivera371-cyber

This comment was marked as off-topic.

@franciscoarturorivera371-cyber

This comment was marked as off-topic.

@franciscoarturorivera371-cyber

This comment was marked as off-topic.

@SudhansuBandha
Copy link
Copy Markdown
Contributor Author

SudhansuBandha commented Mar 26, 2026

@clemyan
I have updated handling of dependencies from worker thread. For this I have also reverted my changes from the initial commit as well f9beab0
I have implemented the solution for both cjs and esm files.
Do let me know your feedback on the proposed solution and whether it makes sense to proceed further in this direction.

@aduh95
Copy link
Copy Markdown
Contributor

aduh95 commented Mar 30, 2026

The added test already passes on main, that means either the code change is not necessary, or that it's not actually testing what you're trying to achieve

@SudhansuBandha
Copy link
Copy Markdown
Contributor Author

Sure!! @aduh95 I will update the tests for this. Thanks for your feedback!!

@SudhansuBandha SudhansuBandha force-pushed the watch-worker-support branch 2 times, most recently from d4e5305 to 634a756 Compare March 31, 2026 09:42
@SudhansuBandha
Copy link
Copy Markdown
Contributor Author

@aduh95 Please do review the updated tests for the proposed changes based on behaviour of worker file and it's dependencies in --watch mode for both cjs and esm modules.

@aduh95
Copy link
Copy Markdown
Contributor

aduh95 commented Apr 1, 2026

Can you not put the tests in test/sequential/test-watch-mode.mjs? That file is enormous, which is becoming a maintenance issue/

Comment thread test/sequential/test-watch-mode.mjs Outdated
Comment thread test/sequential/test-watch-mode.mjs Outdated
@SudhansuBandha
Copy link
Copy Markdown
Contributor Author

SudhansuBandha commented Apr 2, 2026

@aduh95 As suggested I have removed tests from test/sequential/test-watch-mode.mjs to a new file and made few lint fixes.
Do let me know if any further changes are required

@SudhansuBandha SudhansuBandha requested a review from aduh95 April 2, 2026 09:45
@codecov
Copy link
Copy Markdown

codecov Bot commented Apr 4, 2026

Codecov Report

❌ Patch coverage is 94.54545% with 3 lines in your changes missing coverage. Please review.
✅ Project coverage is 89.66%. Comparing base (4e3a873) to head (8fa637a).
⚠️ Report is 2 commits behind head on main.

Files with missing lines Patch % Lines
lib/internal/modules/cjs/loader.js 87.50% 3 Missing ⚠️
Additional details and impacted files
@@            Coverage Diff             @@
##             main   #62368      +/-   ##
==========================================
- Coverage   91.47%   89.66%   -1.82%     
==========================================
  Files         357      707     +350     
  Lines      150883   219563   +68680     
  Branches    23778    42096   +18318     
==========================================
+ Hits       138026   196875   +58849     
- Misses      12582    14585    +2003     
- Partials      275     8103    +7828     
Files with missing lines Coverage Δ
lib/internal/modules/esm/loader.js 99.69% <100.00%> (+38.37%) ⬆️
lib/internal/worker.js 96.74% <100.00%> (+0.22%) ⬆️
lib/internal/modules/cjs/loader.js 98.25% <87.50%> (+18.64%) ⬆️

... and 467 files with indirect coverage changes

🚀 New features to boost your workflow:
  • ❄️ Test Analytics: Detect flaky tests, report on failures, and find test suite problems.
  • 📦 JS Bundle Analysis: Save yourself from yourself by tracking and limiting bundle sizes in JS merges.

Comment thread test/sequential/test-watch-mode-worker.mjs Outdated
@aduh95
Copy link
Copy Markdown
Contributor

aduh95 commented Apr 6, 2026

/cc @nodejs/fs

@MoLow
Copy link
Copy Markdown
Member

MoLow commented Apr 23, 2026

@SudhansuBandha ci failures might be related and allwys fail the same tests, can you kindly investigate?

@SudhansuBandha
Copy link
Copy Markdown
Contributor Author

@MoLow I have rebased this PR with main. Will this resolve those CI failures on re run?

Currently, --watch mode only tracks dependencies from the main
module graph (require/import). Worker thread entry points created
via new Worker() are not included, so changes to worker files do
not trigger restarts.

This change hooks into Worker initialization and registers the
worker entry file with watch mode, ensuring restarts when worker
files change.

Fixes: nodejs#62275
Signed-off-by: SudhansuBandha <sudhansu9.vssut@gmail.com>
@SudhansuBandha SudhansuBandha force-pushed the watch-worker-support branch 2 times, most recently from 02e6c2c to 4cba959 Compare April 28, 2026 07:29
@SudhansuBandha
Copy link
Copy Markdown
Contributor Author

SudhansuBandha commented Apr 28, 2026

@MoLow I have updated the event relays in worker to handle RACE conditions for specific scenarios. Kindly review it and provide any feedback.

@SudhansuBandha SudhansuBandha requested a review from MoLow April 28, 2026 07:48
@MoLow MoLow added the request-ci Add this label to start a Jenkins CI on a PR. label Apr 28, 2026
@SudhansuBandha
Copy link
Copy Markdown
Contributor Author

@MoLow can we proceed with next round of CI run since preliminary checks are successful?

@github-actions github-actions Bot removed the request-ci Add this label to start a Jenkins CI on a PR. label Apr 28, 2026
@nodejs-github-bot
Copy link
Copy Markdown
Collaborator

@nodejs-github-bot
Copy link
Copy Markdown
Collaborator

@SudhansuBandha
Copy link
Copy Markdown
Contributor Author

Is it good to be merged with main branch? @MoLow

@MoLow MoLow added commit-queue Add this label to land a pull request using GitHub Actions. commit-queue-squash Add this label to instruct the Commit Queue to squash all the PR commits into the first one. labels Apr 28, 2026
@nodejs-github-bot nodejs-github-bot removed the commit-queue Add this label to land a pull request using GitHub Actions. label Apr 28, 2026
@SudhansuBandha
Copy link
Copy Markdown
Contributor Author

SudhansuBandha commented Apr 28, 2026

Thank you @MoLow, @aduh95 and @clemyan for your guidance and support on this!!
This is my first ever PR in Nodejs and Openspource even
:)

@nodejs-github-bot nodejs-github-bot merged commit 7466249 into nodejs:main Apr 28, 2026
77 checks passed
@nodejs-github-bot
Copy link
Copy Markdown
Collaborator

Landed in 7466249

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

author ready PRs that have at least one approval, no pending requests for changes, and a CI started. commit-queue-squash Add this label to instruct the Commit Queue to squash all the PR commits into the first one. needs-ci PRs that need a full CI run. worker Issues and PRs related to Worker support.

Projects

None yet

Development

Successfully merging this pull request may close these issues.

Watch mode not triggered by worker module and dependencies

6 participants