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

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

Open
1 task done
gdlol opened this issue Dec 22, 2023 · 2 comments
Open
1 task done

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

gdlol opened this issue Dec 22, 2023 · 2 comments

Comments

@gdlol
Copy link

gdlol commented Dec 22, 2023

Info

Which Version

Version: 8.2.1

Issue with supporting library?

  • 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.

@Jason3S
Copy link
Collaborator

Jason3S commented Dec 22, 2023

@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
Copy link
Collaborator

Jason3S commented Dec 22, 2023

@gdlol,

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

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

No branches or pull requests

2 participants