knip icon indicating copy to clipboard operation
knip copied to clipboard

Git-ignore rules inside a project should not match parts of the absolute path outside the project

Open DaAitch opened this issue 8 months ago • 5 comments

Knip (5.19.0) may behave differently on different environments, depending on

  • gitignore rules
  • the absolute path, where the project is checked out

During the block https://github.com/webpro-nl/knip/blob/7c9b6455d302193961816229c9d30ce0525a794f/packages/knip/src/util/globby.ts#L136 git ignore rules are translated to glob ignore rules. Rules are prefixed by ** which leads to the problem, that git ignore rules now may match outside the project.

Example:

# .gitignore
# ignore all files in builds folders in the project
builds/
// bug in knip

// doesn't match any file, because absolute path matches a gitignore rule
fastGlob(['src/index.ts'], {
  cwd: '/Users/xxx/builds/project',
  ignore: ['**/builds/**', '**/builds'] // generated from git-ignore
})
// maybe-fix in knip

fastGlob(['src/index.ts'], {
  cwd: '/Users/xxx/builds/project',
  ignore: ['/Users/xxx/builds/project/**/builds/**', '/Users/xxx/builds/project/**/builds'] // fix
})

DaAitch avatar Jun 14 '24 09:06 DaAitch