Skip to content

Conversation

@DougReeder
Copy link
Member

@DougReeder DougReeder commented Jun 24, 2025

What?

After an npm command, configures git to run hooks in .githooks. Adds pre-commit hook that runs automated tests.

Fixes lint errors from several PRs

Why?

There is no native way to include Git hooks in the checked-in code.
All solutions require either running a command or installing a 3rd party package.
As Hubs developers will almost always run npm install after cloning the repo, this approach requires no change in development steps.

npm run test was not run before the commits that introduced lint errors, and there was no Git pre-commit hook.

How to test

  1. run npm ci to activate the new hook
  2. create a new throwaway branch to test this
  3. introduce an error to a JavaScript file, such as adding spaces at the end of a line.
  4. use git add
  5. run git commit — observe that tests are run, an error message is displayed, and the commit does not complete
  6. undo the error
  7. use git add
  8. run git commit — observe that tests are run and the commit does complete

Documentation of functionality

No changes to docs, this just pushes validation to the left

Limitations

Note that developers can always prevent a hook from running by adding the flag -n, so git hooks just support fail-fast.
Enforcement has to occur in CI.

The pre-commit script also checks two things that are not currently enforced by anything:

  1. No non-ASCII characters in filenames
  2. No trailing spaces in lines.

Current code follows these rules, and they appeared good checks to add.
They could be removed if/when they get in the way.

Alternatives considered

Husky is an extra dependency and doesn't appear to allow any additional functionality of use.

Open questions

Should Husky be removed by this PR?

The tests take a noticeable amount of time to run, which can be annoying when updating a commit.
Should we move this to the pre-push hook, which is run less often?

@DougReeder DougReeder force-pushed the add-pre-commit branch 6 times, most recently from a472f40 to 32e5b34 Compare June 25, 2025 15:32
@DougReeder DougReeder requested review from Exairnous June 25, 2025 18:33
Copy link
Member

@Exairnous Exairnous left a comment

Choose a reason for hiding this comment

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

I've only done a slight bit of QA testing and still have most of that left to do, but here are some things from a mostly visual inspection.

  1. This needs documentation that it is present, how to manually trigger it, and that -n can be used to bypass the hook. This should probably be added to the readme.
  2. This appears to test everything (including untracked files) instead of only what's been staged for commit (ideally, only the staged changes should be tested/linted - for reference, the Spoke equivalent of this hook only works with the staged changes).
  3. The formatting changes present in this conflict with the formatting changes in #6559 and I kinda think it would be better to merge #6559 and then rebase this on top of it.
  4. I'm not sure, but given the hook is just a bash script I think it could potentially have issues on Windows. Has this been tested?

Should Husky be removed by this PR?

As far as I know, Husky isn't currently a dependency of this repository, so there's nothing to remove (at least I didn't find any sign of it in the package-lock files with a quick search).

Should we move this to the pre-push hook, which is run less often?

Not sure. My first thought is that if we're going to be enforcing this, then we should probably enforce it for every commit, but having it run before pushing would still be an improvement (it would probably result in a number of cleanup commits which wouldn't happen if it were required for every commit).

@DougReeder DougReeder force-pushed the add-pre-commit branch 3 times, most recently from f02d64a to b77ed54 Compare September 10, 2025 02:37
…that runs automated tests.

Why: There is no native way to include Git hooks in the checked-in code.
All solutions require either running a command or installing a 3rd party package.
As Hubs developers will almost always run `npm install` after cloning the repo, this approach requires no change in development steps.

Note that developers can always prevent a hook from running by adding the flag `-n`, so git hooks just support fail-fast.
Enforcement has to occur in CI.
@DougReeder
Copy link
Member Author

This needs documentation that it is present, how to manually trigger it, and that -n can be used to bypass the hook. This should probably be added to the readme.

Done.

This appears to test everything (including untracked files) instead of only what's been staged for commit (ideally, only the staged changes should be tested/linted - for reference, the Spoke equivalent of this hook only works with the staged changes).

Done.

The formatting changes present in this conflict with the formatting changes in https://github.com/Hubs-Foundation/hubs/pull/6559 and I kinda think it would be better to merge https://github.com/Hubs-Foundation/hubs/pull/6559 and then rebase this on top of it.

Done.

I'm not sure, but given the hook is just a bash script I think it could potentially have issues on Windows. Has this been tested?

We advise Windows users to use WSL

Copy link
Member

@Exairnous Exairnous left a comment

Choose a reason for hiding this comment

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

I have finished QA testing with the latest changes and it looks good to me. Thanks. Merging.

@Exairnous Exairnous merged commit a60b2be into Hubs-Foundation:master Sep 16, 2025
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.

2 participants