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

Renaming a folder which has subfolders throws exception on Windows #1380

Open
eyza-cod2 opened this issue Oct 26, 2024 · 2 comments
Open

Renaming a folder which has subfolders throws exception on Windows #1380

eyza-cod2 opened this issue Oct 26, 2024 · 2 comments

Comments

@eyza-cod2
Copy link

Renaming a folder which has subfolders throws exception on Windows.

  • Chokidar version [4.0.1]
  • Node version [18, 20, 22]
  • OS version: [Windows 11]

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:

Error: EPERM: operation not permitted, rename 'C:\Users\tomas\chokidar-bug\TEST_FOLDER\folder2' -> 'C:\Users\tomas\chokidar-bug\TEST_FOLDER\folder2_renamed'
    at Object.renameSync (node:fs:1030:11)
    at renameFolders (C:\Users\tomas\chokidar-bug\index.js:50:8)
    at async main (C:\Users\tomas\chokidar-bug\index.js:18:9) {
  errno: -4048,
  code: 'EPERM',
  syscall: 'rename',
  path: 'C:\\Users\\tomas\\chokidar-bug\\TEST_FOLDER\\folder2',
  dest: 'C:\\Users\\tomas\\chokidar-bug\\TEST_FOLDER\\folder2_renamed'
}

Test code:

const chokidar = require('chokidar');
const fs = require('fs');
const path = require('path');

const rootFolder = path.join(__dirname, 'TEST_FOLDER');

async function main() {
    let watcher;
    try {
        prepareFolderStructure();

        watcher = chokidar.watch(rootFolder, { persistent: true });

        watcher.on('all', (event, path) => {
            console.log(event, path);
        });

        await renameFolders();

    } catch (error) {
        console.error(`Error: ${error.message}`);
        throw error;

    } finally {
        if (watcher) {
            await watcher.close();
            console.log('Watcher closed');
        }
    }
}

function prepareFolderStructure() {    
    if (fs.existsSync(rootFolder)) {
        fs.rmSync(rootFolder, { recursive: true });
    }
    // Create the directory structure
    fs.mkdirSync(rootFolder);
    fs.mkdirSync(path.join(rootFolder, 'folder1'));
    fs.mkdirSync(path.join(rootFolder, 'folder2'));
    fs.mkdirSync(path.join(rootFolder, 'folder2', 'subfolder'));
}

async function renameFolders() {
    await new Promise(resolve => setTimeout(resolve, 500));

    fs.renameSync(path.join(rootFolder, 'folder1'), path.join(rootFolder, 'folder1_renamed')); // Rename folder1 - OK

    await new Promise(resolve => setTimeout(resolve, 500));

    fs.renameSync(path.join(rootFolder, 'folder2'), path.join(rootFolder, 'folder2_renamed')); // Rename folder2 with subfolder - IT THROWS AN ERROR!

    await new Promise(resolve => setTimeout(resolve, 500));
}

main();

Expected behavior
No exception, like it is on linux / macos

Additional context
CI:
image

Repo:
https://github.com/eyza-cod2/chokidar-bug

@ketansahugit
Copy link

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.

@eyza-cod2
Copy link
Author

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.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants