Skip to content

Conversation

@VanTanev
Copy link
Contributor

@VanTanev VanTanev commented Mar 18, 2025

Fixes #990

There is no reasonable way to add tests for this, as far as I can figure.

@VanTanev
Copy link
Contributor Author

Tangential, but this doesn't work with knip --directory ../some-folder-one-level-up - This is because the gitHooksPath() function is called in a context, where the --directory parameter doesn't exist, and we can't pass cwd option to child_process.execSync()

@pkg-pr-new
Copy link

pkg-pr-new bot commented Mar 18, 2025

Open in StackBlitz

npm i https://pkg.pr.new/knip@991

commit: 6aec576

@webpro
Copy link
Member

webpro commented Mar 18, 2025

Fixes #990

Thanks for the PR!

There is no reasonable way to add tests for this, as far as I can figure.

We do have https://github.com/webpro-nl/knip/blob/main/packages/knip/test/plugins/lefthook-v1.test.ts + https://github.com/webpro-nl/knip/tree/main/packages/knip/fixtures/plugins/lefthook-v1 to prevent regression for regular .git/hooks lookups.

@VanTanev
Copy link
Contributor Author

Which now fails because it depended on the old static behavior... OK, let me look at it and see if I can come up with a test

@VanTanev
Copy link
Contributor Author

@webpro The tests are fairly ugly, but that is because git hook paths are resolved at knip import time, and do not resolve relatively to the cwd argument. This means the only way to set process.cwd() is to do process.chdir() before the import statement :-/

@VanTanev
Copy link
Contributor Author

VanTanev commented Mar 18, 2025

Leaving aside the biome formatting issue, this probably fails because the tests do not have execution isolation.

Running only the lefthook test on its own works fine.

So there is a module import that happens earlier, and the incorrect paths get cached here:

const gitHookPaths = getGitHookPaths();
const config = ['lefthook.yml', ...gitHookPaths];

This is not easy to fix, unless plugin config variable option turns into a function that gets knip options and returns an array.

const gitHookPaths = getGitHookPaths();

return ['lefthook.yml', ...gitHookPaths];
},
Copy link
Contributor Author

@VanTanev VanTanev Mar 18, 2025

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This is a workaround for configs getting calculated way too early with a static process.cwd()

@webpro
Copy link
Member

webpro commented Mar 18, 2025

How about adding a separate Actions workflow to assert git worktrees work properly? Could that simplify things, or maybe you have other ideas?

@VanTanev
Copy link
Contributor Author

You mean, go down to a terminal to execute the worktree tests instead of trying to run them within the test runner?

I think that would work well. There must be some best practices around doing that, I'll look around at some tools that need to manipulate git repositories at how they do their tests and come back to this.

@webpro
Copy link
Member

webpro commented Mar 18, 2025

There don't even necessarily need to be a test runner per se, only shell commands to set up the situation, add some files perhaps (maybe prepare a fixtures folder for this, but without a feature.test.ts file), and run knip. If all commands (including knip) exit with code zero then the workflow (which itself is the test!) succeeds.

@VanTanev
Copy link
Contributor Author

Yep! Will look into doing that if not tomorrow by the end of the week. Thanks for helping me get this to a good state!

@webpro webpro force-pushed the main branch 3 times, most recently from e764138 to d4c7379 Compare April 25, 2025 11:19
@webpro webpro force-pushed the main branch 7 times, most recently from ea72d80 to 7787123 Compare May 8, 2025 05:47
@webpro
Copy link
Member

webpro commented Jun 9, 2025

Is there still interest in finishing this PR? No worries if not, but I'm going to clean things up a bit around here.

@VanTanev
Copy link
Contributor Author

VanTanev commented Jun 9, 2025

I definitely want to push this through, as it's still a problem in my day-to-day work. It just seemed daunting every time I thought to return to it, as writing the correct tests seems quite complicated, even though the underlying fix is so straightforward: 2fe0a85

But please, keep it open for a while longer, and I'll make a best effort to get this fixed properly.

@webpro
Copy link
Member

webpro commented Jun 12, 2025

Sure, no hurries, just wanted to check if it's still interesting/relevant.

@webpro webpro force-pushed the main branch 4 times, most recently from 5a2d249 to 6bd250a Compare June 17, 2025 17:36
@VanTanev VanTanev force-pushed the fix-990 branch 3 times, most recently from 362f1be to a8931d6 Compare July 27, 2025 15:32
@VanTanev VanTanev force-pushed the fix-990 branch 5 times, most recently from 0cb770b to 4a08731 Compare July 27, 2025 16:09
@VanTanev
Copy link
Contributor Author

VanTanev commented Jul 27, 2025

Hey @webpro, I added a workflow that verifies worktree behavor and can correctly fail:
https://github.com/VanTanev/knip/actions/runs/16552992919/job/46810512675

This PR is good for re-review.

Let me know if you'd like me to improve this PR in any way before it's good for merge, and thank you for the patience!

@VanTanev
Copy link
Contributor Author

Huh, wonder what causes these failures, tests were running fine locally. Will investigate.

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

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

🐛 Knip fails in a git worktree + lefthook

2 participants