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

readdirSync get incorrect result #2285

Closed
yegao opened this issue Nov 11, 2019 · 6 comments
Closed

readdirSync get incorrect result #2285

yegao opened this issue Nov 11, 2019 · 6 comments

Comments

@yegao
Copy link

yegao commented Nov 11, 2019

Version: 11.15.0
Platform: Darwin

const rmdirSync = (path, options) => {
    if (options && options.recursive) {
        let files = [];
        if (fs.existsSync(path)) {
            if(fs.statSync(path).isDirectory()){
                files = fs.readdirSync(path);
                for (const file of files) {
                    const currentPath = $path.resolve(path, file);
                    if (fs.statSync(currentPath).isDirectory()) {
                        rmdirSync(currentPath, options);
                    } else {
                        fs.unlinkSync(currentPath);
                    }
                }
            }
            else{
                fs.unlinkSync(path);
            }
        }
    }
    return fs.rmdirSync(path);
};

when i use

fs.readdirSync('/project/node_modules/.bin')

I get files with in /project/node_modules/.bin as following

[
  'acorn',                'atob',
  'babylon',              'browserslist',
  'detect-libc',          'errno',
  'import-local-fixture', 'jsesc',
  'json5',                'loose-envify',
  'mg-service',           'miller-rabin',
  'mkdirp',               'needle',
  'node-pre-gyp',         'nopt',
  'parser',               'qrcode-terminal',
  'rc',                   'regjsparser',
  'rimraf',               'semver',
  'sha.js',               'terser',
  'uuid',                 'webpack',
  'webpack-cli',          'which'
]

When I open the directory .bin, there is no acorn in it

[
  'atob',
  'babylon',              'browserslist',
  'detect-libc',          'errno',
  'import-local-fixture', 'jsesc',
  'json5',                'loose-envify',
  'mg-service',           'miller-rabin',
  'mkdirp',               'needle',
  'node-pre-gyp',         'nopt',
  'parser',               'qrcode-terminal',
  'rc',                   'regjsparser',
  'rimraf',               'semver',
  'sha.js',               'terser',
  'uuid',                 'webpack',
  'webpack-cli',          'which'
]

as I do this:

rmdirSync($path.resolve(__dirname,"..","projects","test-empty","node_modules"),{recursive:true})

there is a Error: ENOTEMPTY: directory not empty, rmdir '/project/test-empty/node_modules/.bin'

@simone-sanfratello
Copy link

what is ls -lh of /project/node_modules/.bin?

@bnoordhuis bnoordhuis transferred this issue from nodejs/node Nov 12, 2019
@bnoordhuis
Copy link
Member

I'm moving this to nodejs/help because I see no indication that it's a bug with Node.js itself. Note that your example has a TOCTOU bug (fs.existsSync() followed by fs.statSync().)

@yegao
Copy link
Author

yegao commented Nov 21, 2019

what is ls -lh of /project/node_modules/.bin?

ls -lh ./node_modules/@babel/template/node_modules/.bin
// result is
total 0
lrwxr-xr-x  1 vvadmin  staff    35B 11 21 11:05 parser -> ../../../parser/bin/babel-parser.js

@yegao
Copy link
Author

yegao commented Nov 21, 2019

I'm moving this to nodejs/help because I see no indication that it's a bug with Node.js itself. Note that your example has a TOCTOU bug (fs.existsSync() followed by fs.statSync().)

fs.existsSync() could not be followed by fs.statSync()? Could you explain why in a little more detail :) , thank you ~~
Now I have resolved my problem with rimraf.But I don't understand why my implementation is problematic
When I use my implementation ,I get Error:

Error: ENOTEMPTY: directory not empty, rmdir '/Users/vvadmin/work/minigame-server/temp/e880a539-0ad2-403b-a347-7c959a0bfa65/node_modules/@babel/template/node_modules/.bin'
    at Object.rmdirSync (fs.js:763:3)
    at rmdirSync (/Users/vvadmin/work/minigame-server/utils/fs.js:157:15)
    at rmdirSync (/Users/vvadmin/work/minigame-server/utils/fs.js:143:29)
    at rmdirSync (/Users/vvadmin/work/minigame-server/utils/fs.js:143:29)
    at rmdirSync (/Users/vvadmin/work/minigame-server/utils/fs.js:143:29)
    at Object.rmdirSync (/Users/vvadmin/work/minigame-server/utils/fs.js:143:29)
    at bundle (/Users/vvadmin/work/minigame-server/build/index.js:174:24) {
  errno: -66,
  syscall: 'rmdir',
  code: 'ENOTEMPTY',
  path: '/Users/vvadmin/work/minigame-server/temp/e880a539-0ad2-403b-a347-7c959a0bfa65/node_modules/@babel/template/node_modules/.bin'
}

@PoojaDurgad
Copy link

@yegao - looks like this issue can be closed now .please let me know if that is not the case.

@PoojaDurgad
Copy link

inactive. closing!!

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

4 participants