简体   繁体   中英

Node.js fast-glob EACCES: permission denied

I am trying to get a list of directories using fast-glob , however there is an unknown number of directories were the user lacks read permissions so fast-glob ends up throwing an EACCES and then just hangs. Elevating the user's permissions or lowering the permissions of the glob matches is not an option since this will be in a VS Code extension.

import * as fg from 'fast-glob';
const globIncPaths: string[] = fg.sync(["/usr/lib/**"], {
      onlyDirectories: true,
      suppressErrors: true, // set this to false to raise a EACCES exception
    });

In my case /usr/lib/ssl/private will cause this to hang.

This can easily be recreated for any dir, eg

  1. mkdir -p temp/{public,private}
  2. chmod -r temp/private
  3. changing "/usr/lib/**" from the MWE to "your/cwd/temp/**"

Is there a way to suppress the error and continue the glob search simply excluding any matches where I don't have adequate permissions? Am I missing something obvious?

Alternatives to fast-glob are also welcome as long as they are not horribly slow/inefficient.

So the only reasonable solution that I found is using glob with strict set to false .

import { glob } from 'glob';
const globIncPaths: string[] = [];
for (const i of resIncludePaths) {
  // use '/' to match only directories and not files
  globIncPaths.push(...glob.sync(i + '/', { strict: false }));
}

There is a difference in performance for large matches, but there are optimisations to be made, like running glob only for the glob patterns that fast-glob failed, if performance is that crucial.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM