0

Desired result: when I commit, prior to committing, run npm run build, which will generate some new files. I want those files to be added to the commit.

The current state of affairs: when I commit, it runs npm run build which generates files. And after the commit goes in, I have some staged changes.

I'm working with Solidity (and Forge) + npm. My repo is here if you wanna try: https://github.com/otterspace-xyz/otterspace-contracts

  1. checkout a new branch test-branch
  2. go to Badges.sol and add a variable string private test;
  3. save Badges.sol, run git add .
  4. git commit -m "commit to test husky"
  5. run git status

I expect that you'll see: modified: out/Badges.sol/Badges.json which makes sense (I guess). It seems like npm run build runs but it's not adding the resulting files to the commit.

torek
  • 448,244
  • 59
  • 642
  • 775
George Pickett
  • 71
  • 1
  • 1
  • 11
  • 1
    From the Git side of things, I will say only that it's not wise to attempt to *modify what will be committed* in a pre-commit hook. The hook should only *verify* that what is to be committed is correct, and produce an error if not. If you'd like to modify-then-commit, write your own non-Git command that does that: this command will verify and/or fix, then `git add`, then `git commit --no-verify` since there's no need to re-verify at this point. – torek Nov 26 '22 at 06:51
  • There are long and mostly boring reasons *why* "modify what is to be committed in the pre-commit hook" is a bad idea in general, and they'll allow you to decide when it's OK and when it's bad, but by inverting the order—verify, *then* add and commit; don't add-and-almost-commit-and-then-verify—you make the whole problem go away. It's like putting on the condom at the right time, instead of at the very last possible second. – torek Nov 26 '22 at 06:53

1 Answers1

0

You need to have git add . in the husky hook, manually. IIRC this was a change from way back around version 3 or 4 (it's been a while). And while I agree with torek's comments in principle, this is a major use case of Husky, so it's reasonable to expect a Husky user to be familiar with the risks of modifying code in a precommit.

Zac Anger
  • 6,983
  • 2
  • 15
  • 42