cspell icon indicating copy to clipboard operation
cspell copied to clipboard

[Bug]: Incorrect handling of negate rules in .gitignore

Open gdlol opened this issue 2 years ago • 3 comments

Info

Which Version

Version: 8.2.1

Issue with supporting library?

  • [x] cspell-glob -- library for matching glob patterns

Bug Description

Describe the bug

When using the useGitignore option, patterns in .gitignore seems not being processed in order. The issue seems to be that in this line where positive rules and negative rules are handled separately, hence not handling scenarios like ignore-then-unignore/unignore-then-ignore correctly according to Git behavior.

To Reproduce A minimal script to reproduce the bug: package.json

{
  "private": true,
  "type": "module",
  "dependencies": {
    "cspell-glob": "8.2.1"
  }
}

index.js

import { GlobMatcher } from "cspell-glob";

const globMatcher = new GlobMatcher(["*ignored/", "!un-ignored/", "node_modules/"], {
  root: "/test",
});

const check = (path, expected) => {
  const ignored = globMatcher.match(path);
  console.log(`path: ${path}, ignored: ${ignored}, expected: ${expected}`);
};

check("/test/test.txt", false);
check("/test/ignored/test.txt", true);
check("/test/node_modules/test.txt", true);
check("/test/un-ignored/test.txt", false);
check("/test/un-ignored/node_modules/test.txt", true);

Output (node index.js):

path: /test/test.txt, ignored: false, expected: false
path: /test/ignored/test.txt, ignored: true, expected: true
path: /test/node_modules/test.txt, ignored: true, expected: true
path: /test/un-ignored/test.txt, ignored: false, expected: false
path: /test/un-ignored/node_modules/test.txt, ignored: false, expected: true

By the way, please check the ignore package which should be more suitable for handling gitignore patterns than micromatch used in cspell-glob.

gdlol avatar Dec 22 '23 08:12 gdlol

@gdlol,

Thank you for pointing out the exact issue.

I'll take another look at ignore. It previously wasn't suitable for a couple of reasons:

  • It wasn't possible to report exactly which line in the .gitignore rejected the file.
  • Ignore had a hard dependency upon node:fs, make it impossible to use a virtual fs.
  • Ignore was file system based didn't work well with URLs.

In any case, I think the current implementation of GlobMather needs to be improved.

Jason3S avatar Dec 22 '23 08:12 Jason3S

@gdlol,

I was wrong in my comments above, node-ignore is not the package I had earlier evaluated. Thank you for the pointer.

Jason3S avatar Dec 22 '23 09:12 Jason3S