| FazBrowse GitHub Viewer | Trending | | Home |
| Tools: [Download Repo ZIP] [Original HTTPS Page] |
1 parent d2ea8b7 commit a41b482
2 files changed
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
@@ -25,6 +25,7 @@ const { | |||
| 25 | 25 | join: pathJoin, | |
| 26 | 26 | relative: pathRelative, | |
| 27 | 27 | resolve: pathResolve, | |
| 28 | + sep: pathSep, | ||
| 28 | 29 | } = require('path'); | |
| 29 | 30 | ||
| 30 | 31 | let internalSync; | |
@@ -106,8 +107,10 @@ class FSWatcher extends EventEmitter { | |||
| 106 | 107 | #unwatchFiles(file) { | |
| 107 | 108 | this.#symbolicFiles.delete(file); | |
| 108 | 109 | ||
| 110 | + const childPrefix = file + pathSep; | ||
| 109 | 111 | for (const filename of this.#files.keys()) { | |
| 110 | - if (StringPrototypeStartsWith(filename, file)) { | ||
| 112 | + if (filename === file || | ||
| 113 | + StringPrototypeStartsWith(filename, childPrefix)) { | ||
| 111 | 114 | this.#files.delete(filename); | |
| 112 | 115 | this.#watchers.get(filename)?.close(); | |
| 113 | 116 | this.#watchers.delete(filename); | |
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
@@ -0,0 +1,70 @@ | |||
| 1 | + 'use strict'; | ||
| 2 | + | ||
| 3 | + // Regression test for https://github.com/nodejs/node/issues/58868 | ||
| 4 | + | ||
| 5 | + const common = require('../common'); | ||
| 6 | + | ||
| 7 | + if (common.isIBMi) { | ||
| 8 | + common.skip('IBMi does not support `fs.watch()`'); | ||
| 9 | + } | ||
| 10 | + | ||
| 11 | + if (common.isAIX) { | ||
| 12 | + common.skip('folder watch capability is limited in AIX.'); | ||
| 13 | + } | ||
| 14 | + | ||
| 15 | + // macOS and Windows use the native recursive watcher and are unaffected. | ||
| 16 | + if (common.isMacOS || common.isWindows) { | ||
| 17 | + common.skip('regression specific to the JS-based recursive watcher'); | ||
| 18 | + } | ||
| 19 | + | ||
| 20 | + const assert = require('assert'); | ||
| 21 | + const fs = require('fs'); | ||
| 22 | + const path = require('path'); | ||
| 23 | + const { setTimeout } = require('timers/promises'); | ||
| 24 | + | ||
| 25 | + const tmpdir = require('../common/tmpdir'); | ||
| 26 | + tmpdir.refresh(); | ||
| 27 | + | ||
| 28 | + (async () => { | ||
| 29 | + const root = fs.mkdtempSync(path.join(tmpdir.path, 'watch-prefix-')); | ||
| 30 | + | ||
| 31 | + // Sibling names that share the prefix `foo` with the entries to delete. | ||
| 32 | + fs.mkdirSync(path.join(root, 'foo_bar')); | ||
| 33 | + fs.writeFileSync(path.join(root, 'foo_bar', 'file.txt'), ''); | ||
| 34 | + fs.mkdirSync(path.join(root, 'foo_bar', 'somedir')); | ||
| 35 | + fs.writeFileSync(path.join(root, 'foo_'), ''); | ||
| 36 | + | ||
| 37 | + // `foo` (empty) exercises the exact-match branch of `#unwatchFiles`. | ||
| 38 | + fs.mkdirSync(path.join(root, 'foo')); | ||
| 39 | + | ||
| 40 | + // `foo2` has descendants and exercises the `file + sep` prefix branch. | ||
| 41 | + fs.mkdirSync(path.join(root, 'foo2')); | ||
| 42 | + fs.writeFileSync(path.join(root, 'foo2', 'inside.txt'), ''); | ||
| 43 | + fs.mkdirSync(path.join(root, 'foo2', 'sub')); | ||
| 44 | + | ||
| 45 | + const events = []; | ||
| 46 | + const watcher = fs.watch(root, { recursive: true }, (eventType, filename) => { | ||
| 47 | + events.push({ eventType, filename }); | ||
| 48 | + }); | ||
| 49 | + | ||
| 50 | + // Allow the watcher to fully attach to existing entries. | ||
| 51 | + await setTimeout(common.platformTimeout(200)); | ||
| 52 | + | ||
| 53 | + fs.rmdirSync(path.join(root, 'foo')); | ||
| 54 | + fs.rmSync(path.join(root, 'foo2'), { recursive: true }); | ||
| 55 | + | ||
| 56 | + // Wait long enough to capture any spurious follow-up events. | ||
| 57 | + await setTimeout(common.platformTimeout(500)); | ||
| 58 | + | ||
| 59 | + watcher.close(); | ||
| 60 | + | ||
| 61 | + const isSibling = (f) => | ||
| 62 | + f === 'foo_' || f === 'foo_bar' || | ||
| 63 | + f.startsWith('foo_bar' + path.sep); | ||
| 64 | + const spurious = events.filter((e) => isSibling(e.filename)); | ||
| 65 | + assert.deepStrictEqual( | ||
| 66 | + spurious, | ||
| 67 | + [], | ||
| 68 | + `unexpected events for prefix-sibling entries: ${JSON.stringify(spurious)}`, | ||
| 69 | + ); | ||
| 70 | + })().then(common.mustCall()); | ||
| Back | FazBrowse Home | New Git URL |
0 commit comments