Skip to content

Support negated pattern-list syntax in fs.glob #63959

Description

@TheAlexLichter

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.

Metadata

Metadata

Assignees

No one assigned

    Labels

    feature requestIssues that request new features to be added to Node.js.

    Type

    No type

    Projects

    Status
    Awaiting Triage

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions