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

Fix Windows support by handling sh shebangs from pre-commit #1121

Merged
merged 2 commits into from
Feb 3, 2022

Conversation

jooh
Copy link
Contributor

@jooh jooh commented Feb 2, 2022

As of 2.17, pre-commit prepends a #!/bin/sh to its hooks under Windows. This breaks noxfile.py since activate_virtualenv_in_precommit_hooks only matches on bash in the first line of the script. I added a sh key to headers and that seems to have resolved the issue at my end.

pre-commit/pre-commit#2182

https://github.com/pre-commit/pre-commit/pull/2187/commits

Copy link
Owner

@cjolowicz cjolowicz left a comment

Choose a reason for hiding this comment

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

Thanks for catching this!

This function does a simple substring match on the shebang. With a two-letter command like sh, the check needs to become more specific. But not too specific, because shebangs can also contain options.

So how about matching entire words?

Instead of

            if executable in lines[0].lower():

we'd do this:

            if executable in lines[0].lower().split():

Update: Just realized this does not work because the new pre-commit scripts use #!/bin/sh (which is one word). Can you change the key from sh to /bin/sh? And we keep the substring matching as-is.

@cjolowicz cjolowicz added the bug Something isn't working label Feb 2, 2022
@jooh
Copy link
Contributor Author

jooh commented Feb 3, 2022

Just realized this does not work because the new pre-commit scripts use #!/bin/sh (which is one word). Can you change the key from sh to /bin/sh? And we keep the substring matching as-is.

Got it. In principle we could now miss #!/usr/bin/env sh shebangs, but given this code only runs on pre-commit-generated hooks I don't think that scenario is possible.

Copy link
Owner

@cjolowicz cjolowicz left a comment

Choose a reason for hiding this comment

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

Thank you!

@cjolowicz cjolowicz merged commit 5178654 into cjolowicz:main Feb 3, 2022
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

Successfully merging this pull request may close these issues.

None yet

2 participants