nodejs / nodejs/node

Support negated pattern-list syntax in `fs.glob`

Open
#63,959 4 comments 3 reactions 0 assignees View on GitHub

Nobody has claimed this yet.

feature request
Dominant language
JavaScript
Stars
122k
Forks
37.3k
Avg merge
4d 2h
Merged PRs (30d)
283

Description

What problem would this feature solve?

Node's fs.glob() accepts an array of patterns, but entries beginning with ! are not interpreted as exclusions:

globSync([
  'src/**/*.js',
  '!src/generated/**',
]);

Node.js does support negative extglobs such as !(pattern), but that is a different feature. An extglob negates part of a single pattern while negated pattern-list syntax applies an exclusion to the matches (based on other entries in the array).

Node.js also provides options.exclude, but it cannot fully represent ordered pattern lists where a later positive pattern re-includes a path.

Ecosystem

Leading-! entries are supported by packages such as globby and tinyglobby:

await globby(['src/**/*.js', '!src/generated/**']);
await glob(['src/**/*.js', '!src/generated/**']); // tinyglobby

Supporting this syntax would make migration to the built-in API easier and allow existing pattern arrays from configuration files and CLI arguments to be reused unchanged.

What feature are you proposing?

When pattern is an array, interpret entries beginning with ! as negated pattern-list entries:

globSync([
  'src/**/*.js',
  '!src/generated/**',
]);

Patterns should be processed in order:

  • A positive pattern adds matching paths.
  • A negated pattern removes matching paths.
  • A later positive pattern can re-include previously removed paths.

For example:

globSync([
  'src/**/*.js',
  '!src/generated/**',
  'src/generated/keep.js',
]);

This should include src/generated/keep.js.

The existing options.exclude API should remain available for global exclusions and exclusion callbacks.

What alternatives have you considered?

Applications can split positive and negative entries:

globSync(include, { exclude });

However, this loses ordering semantics and cannot represent later re-inclusion without additional processing.

Contributor guide

Open the contributing guide

First steps

  1. Read the whole issue, then the project's contributing guide.
  2. Comment on the issue to say you are picking it up — it saves two people doing the same work.
  3. Fork the repository and make your change on a branch.
  4. Open a pull request that references the issue number.

Research direction

Start with the fs.glob and globSync APIs described in the issue, then run the provided pattern-array examples to establish current behavior. Done means leading-! entries remove matches in order, later positive entries re-include them, and options.exclude remains available for global exclusions and callbacks.

Written by the indexing model from the issue text.

Assessment

Tech stack
javascript
Domain
operating-systems
Issue type
Feature
Difficulty
4/5
Estimated time
3-5 days
Activity status
Active
Clarity
Mostly clear
Newbie friendliness
62/100

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.