Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix(jest-haste-map): Make watchman existence check lazy+async #12675

Merged
merged 5 commits into from
Apr 16, 2022

Conversation

robhogan
Copy link
Contributor

Summary

Currently, jest-haste-map checks for watchman existence at import time (top level) with a blocking execSync call. This has a few downsides

  • Blocking at the top level holds up main thread (by 20-200ms) at a time when there's CPU work to be done.
  • execSync spawns a shell, which isn't necessary here - execFile is more efficient.
  • The exec happens totally unnecessarily even if the consumer specifies useWatchman: false.

This PR extracts this to an async utility function, executed lazily and memoized by the core class.

Test plan

  • New unit tests cover lib/isWatchmanInstalled.ts and lazy execution

  • Behaviour of execFile when watchman is installed:

$ node -e "util.promisify(child_process.execFile)('watchman', ['--version']).then(console.log)"
{ stdout: '20220411\n', stderr: '' }
  • And when we try to exec something unavailable:
$ node -e "util.promisify(child_process.execFile)('nonexistentthing', ['--version']).then(console.log)"

node:internal/errors:464
   ErrorCaptureStackTrace(err);
   ^

Error: spawn nonexistentthing ENOENT
   at Process.ChildProcess._handle.onexit (node:internal/child_process:282:19)
   at onErrorNT (node:internal/child_process:477:16)
   at processTicksAndRejections (node:internal/process/task_queues:83:21) {
 errno: -2,
 code: 'ENOENT',
 syscall: 'spawn nonexistentthing',
 path: 'nonexistentthing',
 spawnargs: [ '--version' ],
 cmd: 'nonexistentthing --version',
 stdout: '',
 stderr: ''
}

Copy link
Member

@SimenB SimenB left a comment

Choose a reason for hiding this comment

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

Ooh, great stuff! 👍

@SimenB SimenB merged commit defbc05 into jestjs:main Apr 16, 2022
@github-actions
Copy link

This pull request has been automatically locked since there has not been any recent activity after it was closed. Please open a new issue for related bugs.
Please note this issue tracker is not a help forum. We recommend using StackOverflow or our discord channel for questions.

@github-actions github-actions bot locked as resolved and limited conversation to collaborators May 17, 2022
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Projects
None yet
Development

Successfully merging this pull request may close these issues.

3 participants