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

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

Open
DaAitch opened this issue Jun 14, 2024 · 5 comments
Labels
bug Something isn't working

Comments

@DaAitch
Copy link

DaAitch commented Jun 14, 2024

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

if (options.gitignore) {
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 DaAitch added the bug Something isn't working label Jun 14, 2024
@webpro
Copy link
Collaborator

webpro commented Jun 15, 2024

Thanks for the bug report.

I wonder how it's possible the **/build pattern matching files outside the pattern influences the results? Any chance a small explanation or even reproduction could be set up?

That might be a reason to either improve Knip's docs and/or implementation: perhaps by extending or doing something similar to the source mapping (dist/index.jssrc/index.ts) that was recently introduced.

DaAitch added a commit to DaAitch/knip-issue-678 that referenced this issue Jun 16, 2024
DaAitch added a commit to DaAitch/knip-issue-678 that referenced this issue Jun 16, 2024
@DaAitch
Copy link
Author

DaAitch commented Jun 16, 2024

@webpro here it is: https://github.com/DaAitch/knip-issue-678

I simulated an absolute path, by checking in some folders.

The git-ignore rule builds/ in <PROJECT_ROOT>/Users/knipuser/cicd/builds/project42/.gitignore is translated to a glob-ignore for **/builds/**.

@webpro
Copy link
Collaborator

webpro commented Jun 16, 2024

Thanks, that helped a lot.

I think I found the issue, it seems to originate here in fast-glob: mrmlnc/fast-glob#441

Need to figure out whether it can/will be fixed downstream, or work around it in Knip.

DaAitch added a commit to DaAitch/knip that referenced this issue Jun 16, 2024
@DaAitch
Copy link
Author

DaAitch commented Jun 16, 2024

@webpro Okay interesting. Here is a red knip unit test DaAitch@5989d4a to reproduce

@webpro
Copy link
Collaborator

webpro commented Jun 16, 2024

Thanks, I'd prefer fixes and test coverage in the downstream lib, let's await that first: mrmlnc/fast-glob#445

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
bug Something isn't working
Projects
None yet
Development

No branches or pull requests

2 participants