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

Husky v4 doesn't load config when it is loaded using require #662

Closed
gurpreetatwal opened this issue Jan 30, 2020 · 11 comments · Fixed by DeviaVir/zenbot#2021
Closed

Husky v4 doesn't load config when it is loaded using require #662

gurpreetatwal opened this issue Jan 30, 2020 · 11 comments · Fixed by DeviaVir/zenbot#2021

Comments

@gurpreetatwal
Copy link

I believe Husky v4.0.0 might have a regression in regards to config file loading.

Given the following config file:
.huskyrc.js

'use strict';

module.exports = require('./configs').husky;

where ./configs is

'use strict';

module.exports = {
  husky: {
    hooks: {
      'pre-commit': 'lint-staged',
    },
  },
};

Husky prints the following (when running with HUSKY_DEBUG=1)

husky:debug husky v4.2.1 - pre-commit
husky:debug Current working directory is /home/gurpreet/projects/
husky:debug pre-commit config not found, skipping hook
husky:debug husky v4.2.1 - prepare-commit-msg
husky:debug Current working directory is /home/gurpreet/projects/
husky:debug prepare-commit-msg config not found, skipping hook
husky:debug husky v4.2.1 - commit-msg
husky:debug Current working directory is /home/gurpreet/projects
husky:debug commit-msg config not found, skipping hook
husky:debug husky v4.2.1 - post-commit
husky:debug Current working directory is /home/gurpreet/projects/
husky:debug post-commit config not found, skipping hook
husky:debug husky v4.2.1 - post-rewrite
husky:debug Current working directory is /home/gurpreet/projects/
husky:debug post-rewrite config not found, skipping hook

Oddly enough everything seems to work again as soon as I put the name of the hook that I want to run anywhere in the config file (even embedded between words in a comment). So for example the following config:

'use strict';

// garbagepre-commitgarbage
module.exports = require('./configs').husky;

gives:

husky:debug husky v4.2.1 - pre-commit
husky:debug Current working directory is /home/gurpreet/projects
husky:debug GIT_DIR environment variable is set to .git
husky:debug If you're getting "fatal: not a git repository" errors, check GIT_DIR value
husky > pre-commit (node v10.18.1)
No staged files match any of provided globs. 
husky:debug npx --no-install husky-run exited with 0 exit code
husky:debug husky v4.2.1 - prepare-commit-msg
husky:debug Current working directory is /home/gurpreet/projects
husky:debug prepare-commit-msg config not found, skipping hook
husky:debug husky v4.2.1 - commit-msg
husky:debug Current working directory is /home/gurpreet/projects
husky:debug commit-msg config not found, skipping hook
husky:debug husky v4.2.1 - post-commit
husky:debug Current working directory is /home/gurpreet/projects
husky:debug post-commit config not found, skipping hook
husky:debug husky v4.2.1 - post-rewrite
husky:debug Current working directory is /home/gurpreet/projects
husky:debug post-rewrite config not found, skipping hook

OS: Ubuntu 18.04.3 LTS
Terminal: alacritty 0.2.3
Shell: zsh 5.4.2 (x86_64-ubuntu-linux-gnu)

$ HUSKY_DEBUG=1 npm install husky --save-dev

> [email protected] preuninstall /home/gurpreet/projects//node_modules/husky
> node husky uninstall

husky > Uninstalling git hooks
husky:debug Current working directory is /home/gurpreet/projects/node_modules/husky
husky:debug INIT_CWD is set to /home/gurpreet/projects
husky:debug Git rev-parse command returned:
husky:debug --git-common-dir: .git
husky:debug --show-prefix:
husky > Done

[email protected] install /home/gurpreet/projects/node_modules/husky
node husky install

husky > Setting up git hooks
husky:debug Current working directory is /home/gurpreet/projects/node_modules/husky
husky:debug INIT_CWD is set to /home/gurpreet/projects
husky:debug Git rev-parse command returned:
husky:debug --git-common-dir: .git
husky:debug --show-prefix:
husky:debug Package manager: npm
{ hooks: { hooks: { 'pre-commit': 'lint-staged' } } }
husky:debug Installing hooks in /home/gurpreet/projects/.git/hooks
husky > Done

[email protected] postinstall /home/gurpreet/projects/node_modules/husky
opencollective-postinstall || exit 0

Thank you for using husky!
If you rely on this package, please consider supporting our open collective:

https://opencollective.com/husky/donate

@gurpreetatwal gurpreetatwal changed the title Husky doesn't seem to pick up the config when it is loaded using require Husky v4 doesn't load config when it is loaded using require Jan 30, 2020
@typicode
Copy link
Owner

Hi @gurpreetatwal,

Thanks for the report. This is related to #648

Husky v4.1.0 introduced an heuristic to "fast skip" hooks that don't have an associated script (it helped improve speed with Yarn 2). It simply greps config files (huskyrc, huskyrc.js, ...) for matching hooks.

But since there's no pre-commit in .huskyrc.js, it's skipped.

I understand this defeats the purpose of an external config file, but this would make pre-commit work:

'use strict';

// pre-commit
module.exports = require('./configs').husky;

@skeggse
Copy link

skeggse commented Jan 30, 2020

We distribute git hooks using a private package on npm across hundreds of repositories, and would like the freedom to introduce new hooks without going through and updating the barebones .huskyrc.js file in each one - is there some way we could introduce an opt-out for husky's heuristic, or provide a specific (relative) path to grep for the hooks?

@k2snowman69
Copy link

Could a possible fix for this be in sh/husky.sh to modify hookIsDefined with something like this (sorry my shell scripting abilities are super poor):

hookIsDefined () {
  grep -qs "$hookName\|require" \
    package.json \
    .huskyrc \
    .huskyrc.json \
    .huskyrc.yaml \
    .huskyrc.yml \
    .huskyrc.js \
    husky.config.js
}

This way for those who are using require (e.g. unknown hooks) then it grabs all the hooks and for those who aren't and are using specific hooks, only those get hooked.

@codyhamilton
Copy link

A robust solution might be to test for the existence of .huskyrc.js and husky.config.js.

If someone is generating config dynamically, it's not safe to shortcut. partly because require may not capture all dynamic scenarios but also because there may be side effects that are also skipped.

@typicode
Copy link
Owner

typicode commented Feb 2, 2020

@codyhamilton I agree, do you want to make a PR?

@JounQin
Copy link

JounQin commented Feb 11, 2020

@typicode Can we release a new patch version?

Besides, CHANGELOG seems is not synchronized.

@typicode
Copy link
Owner

typicode commented Feb 12, 2020

@JounQin just released it 👍 it includes @codyhamilton fix.

For recent versions, you can find changes here: https://github.com/typicode/husky/releases

@JounQin
Copy link

JounQin commented Feb 12, 2020

@typicode Thx, and I know we can find changes at releases, but if you do not want to maintain CHANGLOG file anymore, it's better to clarify in the documentation, right?

@antonsamper
Copy link

@typicode im not sure if this issue is fixed. I've upgraded to version 4.2.3 but my external hooks are still not running, they are all skipped.

Husky file in root of project: .huskyrc.js

module.exports = require('myexternalmodule/.huskyrc.js');

Should this work?

@JounQin
Copy link

JounQin commented Feb 14, 2020

@antonsamper It does work to me.

module.exports = require('@1stg/husky-config');

@antonsamper
Copy link

@JounQin @typicode I've re-tested and you're right! it does work when i use the command line but not the GUI (Sourcetree). I'll investigate further locally. I'm using Sourcetree and NVM so I'll probably just need to update the config.

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 a pull request may close this issue.

7 participants