-
-
Notifications
You must be signed in to change notification settings - Fork 586
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
Renaming a folder which has subfolders throws exception on Windows #1380
Comments
Hi, I've attempted to resolve this issue, but it continues to throw an error unless we temporarily stop Chokidar while renaming the folder that contains a subfolder. |
Ye, that might help when you control the renaming action, but when some file is renamed outside the scope of your app, there is propably no way how to fix that. I wanted to use this package but after this error I was forced to use something else. |
I'm running into this issue as well. Only workaround seems to be to set |
ok sorry this took me a while to get to this isn't a chokidar problem it seems. see the reproduction below: import * as fs from 'node:fs/promises';
import {watch} from 'node:fs';
import * as path from 'node:path';
import {fileURLToPath} from 'node:url';
const __dirname = fileURLToPath(new URL('.', import.meta.url));
const rootFolder = path.join(__dirname, 'TEST_FOLDER');
async function main() {
await prepareFolderStructure();
// Watch a directory _under_ the one we're going to rename
watch(path.join(rootFolder, 'folder1'), console.log);
await renameFolders();
}
async function prepareFolderStructure() {
await fs.rm(rootFolder, {recursive: true, force: true});
await fs.rm(path.resolve(rootFolder, '../TEST_FOLDER2'), {force: true});
await fs.mkdir(rootFolder);
await fs.mkdir(path.join(rootFolder, 'folder1'));
}
async function renameFolders() {
await new Promise(resolve => setTimeout(resolve, 500));
await fs.rename(rootFolder, path.resolve(rootFolder, '../TEST_FOLDER2'));
}
main(); basically, if you try to rename the parent directory of a directory you're watching with node's i'm not sure this is fixable on our end (or by anyone). we'd have to remove our listeners just before a parent is removed (i.e. when we get another FS event telling us its happening, but it already happened by then i imagine) |
Thanks for looking into this! There is an issue on the nodejs github: nodejs/node#31702 but getting an error when the root folder that is being watched is deleted seems like reasonable behavior. This code works fine: import * as fs from 'node:fs/promises';
import {watch} from 'node:fs';
import * as path from 'node:path';
import {fileURLToPath} from 'node:url';
import { watch as chokidarWatch } from 'chokidar';
const __dirname = fileURLToPath(new URL('.', import.meta.url));
const rootFolder = path.join(__dirname, 'TEST_FOLDER');
async function main() {
await prepareFolderStructure();
// Watch a directory _under_ the one we're going to rename
watch(rootFolder, { recursive: true }, console.log);
// chokidarWatch(rootFolder).on('all', console.log);
await renameFolders();
}
async function prepareFolderStructure() {
await fs.rm(rootFolder, {recursive: true, force: true});
await fs.mkdir(rootFolder);
await fs.mkdir(path.join(rootFolder, 'folder1'));
await fs.mkdir(path.join(rootFolder, 'folder1', 'subfolder'));
}
async function renameFolders() {
await new Promise(resolve => setTimeout(resolve, 500));
await fs.rename(path.join(rootFolder, 'folder1'), path.resolve(rootFolder, 'folder1_renamed'));
}
main(); Switching line 14 to watch using chokidar gives the error. I take it that chokidar creates a separate watcher for each subdirectory rather than using the |
Your example works because you are watching the directory you are renaming My example specifically watches a directory below the one I'm renaming So yes indeed we don't use |
Getting an error when renaming a parent folder of the one we are watching is reasonable behavior - that is not the problem we are having on this issue.
If I am watching This is not a problem with node, because as highlighted in the code I put above, we can simply switch one line to watch using node instead of chokidar and it no longer crashes. |
You are renaming That is why you get the error (same as in my repro) One day, when we move to And no, you didn't just change it to use chokidar. You changed the path being watched. My repro specifically watches a directory below the one I'm renaming Your code changed that (look again, my code watches folder1 to repro the exact problem the same way it is happening in the original problem under the hood) |
What I am saying is that if you take the code I wrote above, and apply this diff which is a 1 line change: 14,15c14,15
< watch(rootFolder, { recursive: true }, console.log);
< // chokidarWatch(rootFolder).on('all', console.log);
---
> // watch(rootFolder, { recursive: true }, console.log);
> chokidarWatch(rootFolder).on('all', console.log); The error is reproduced, whereas originally when just using node there was no error. The problem is that chokidar is spinning up a node watcher for each subdirectory, blocking renames of those subdirectories. Agreed that switching to use |
Yes that code produces an error because of the exact repro code I posted Moving the parent directory of a directory being watched results in that error, as the repro I posted discovered The code you posted immediately after mine changed the directory being watched and is why it then began to work with node. Chokidar using a watcher per directory isn't a problem but is the reason you get this error. One day when we use I don't remember the problems with |
Renaming a folder which has subfolders throws exception on Windows.
To Reproduce:
Create following folder structure:
TEST_FOLDER\folder1
TEST_FOLDER\folder2
TEST_FOLDER\folder2\subfolder
Setup chokidar to watch folder 'TEST_FOLDER'
Rename folder "folder2" to "folder2_renamed"
Exception is thrown:
Test code:
Expected behavior
No exception, like it is on linux / macos
Additional context
CI:
Repo:
https://github.com/eyza-cod2/chokidar-bug
The text was updated successfully, but these errors were encountered: