-
Notifications
You must be signed in to change notification settings - Fork 30.4k
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
watch: fix reloading for rebuild/compiled files #45259
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change | ||||||||||||
---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|
|
@@ -6,9 +6,10 @@ import path from 'node:path'; | |||||||||||||
import { execPath } from 'node:process'; | ||||||||||||||
import { describe, it } from 'node:test'; | ||||||||||||||
import { spawn } from 'node:child_process'; | ||||||||||||||
import { writeFileSync, readFileSync } from 'node:fs'; | ||||||||||||||
import { writeFileSync, rmSync, readFileSync } from 'node:fs'; | ||||||||||||||
import { inspect } from 'node:util'; | ||||||||||||||
import { once } from 'node:events'; | ||||||||||||||
import { setTimeout } from 'node:timers/promises'; | ||||||||||||||
|
||||||||||||||
if (common.isIBMi) | ||||||||||||||
common.skip('IBMi does not support `fs.watch()`'); | ||||||||||||||
|
@@ -174,6 +175,41 @@ describe('watch mode', { concurrency: true, timeout: 60_0000 }, () => { | |||||||||||||
}); | ||||||||||||||
}); | ||||||||||||||
|
||||||||||||||
it('should watch changes to previously loaded dependencies', async () => { | ||||||||||||||
const dependencyContent = 'module.exports = {}'; | ||||||||||||||
const dependency = createTmpFile(dependencyContent); | ||||||||||||||
const relativeDependencyPath = `./${path.basename(dependency)}`; | ||||||||||||||
const dependant = createTmpFile(`console.log(require('${relativeDependencyPath}'))`); | ||||||||||||||
|
||||||||||||||
let stderr = ''; | ||||||||||||||
let stdout = ''; | ||||||||||||||
const child = spawn(execPath, ['--watch', '--no-warnings', dependant], { encoding: 'utf8' }); | ||||||||||||||
child.stdout.on('data', (data) => { stdout += data; }); | ||||||||||||||
child.stderr.on('data', (data) => { stderr += data; }); | ||||||||||||||
child.on('error', (err) => { throw err; }); | ||||||||||||||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more.
Suggested change
|
||||||||||||||
|
||||||||||||||
await once(child.stdout, 'data'); | ||||||||||||||
rmSync(dependency, { force: true }); | ||||||||||||||
|
||||||||||||||
await setTimeout(600); // throttle + 100 | ||||||||||||||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Can you use |
||||||||||||||
writeFileSync(dependency, dependencyContent); | ||||||||||||||
|
||||||||||||||
await setTimeout(600); // throttle + 100 | ||||||||||||||
child.kill(); | ||||||||||||||
await once(child, 'exit'); | ||||||||||||||
|
||||||||||||||
// TODO(ruyadorno): fs.watch is flaky when removing files from a watched | ||||||||||||||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Can you create an issue for this and assign it to me? And can you give platform information for this line, since fs.watch has different implementations on different OS |
||||||||||||||
// folder, in this test we want to assert the expected reload happened | ||||||||||||||
// after a missing file is recreated so we'll skip the assertions in case | ||||||||||||||
// the expected reload+failure never happened. | ||||||||||||||
// This should be an assertion for the failure instead of an if block once | ||||||||||||||
// the source of this flakyness gets resolved. | ||||||||||||||
if (stdout.match(/Failed/g)?.length) { | ||||||||||||||
assert.strictEqual(stdout.split('Failed')[1].match(/Restarting/g)?.length, 1, new Error('should have restarted after fail')); | ||||||||||||||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Can you also add an assert for the length of |
||||||||||||||
assert.ok(stderr.match(/MODULE_NOT_FOUND/g)?.length, new Error('should have failed for not finding the removed file')); | ||||||||||||||
Comment on lines
+207
to
+209
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more.
Suggested change
We should use There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Or |
||||||||||||||
} | ||||||||||||||
}); | ||||||||||||||
|
||||||||||||||
it('should restart multiple times', async () => { | ||||||||||||||
const file = createTmpFile(); | ||||||||||||||
const { stderr, stdout } = await spawnWithRestarts({ file, restarts: 3 }); | ||||||||||||||
|
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
does
path.relative(path.basename(dependency))
produces the same result for this line? if yes, can we use it?