-
-
Notifications
You must be signed in to change notification settings - Fork 1k
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 setting wrong folder in monorepo (lerna/yarn workspace) #677
Comments
I'm also using the same setup (Yarn Workspaces + Lerna) and I've noticed the exact same issue. |
i have the same issue |
My setup is almost identical (using Yarn workspaces only, no Lerna) and I'm getting the same. This is what my structure looks like:
I just did some testing and it's only happening with the Changes the
to this (the hook stops running on commit):
|
Just tested this again as well and I can confirm it only seems to happen when running |
There is a special node on using husky with monorepos: https://github.com/typicode/husky#monorepos In short for monorepos, it is recommended to only install husky into the root package. Otherwise, all sorts of conflicts are expected when husky installation script is run for several packages in the repo. I use husky with lerna and yarn workspaces by installing husky to the root package via |
@eduard-malakhov , I have it installed with |
I only installed Husky in the root as well. |
@tad3j , I've tried the following:
May it be that when you were trying to remove husky, it was listed in any of the packages besides the root? If it is indeed and issue with removal and you can reproduce this situation, running |
The issue is not about removing Husky itself, but about removing another dependency from inside a package directory. |
@eduard-malakhov as @bertdeblock says...I also never tried to remove husky, this seems to happen when running yarn (maybe also lerna) in a sub directory. |
@tad3j , @bertdeblock thank you guys, now I see, I have reproduced the issue. Steps to reproduce
Expected result Presumably, the same behaviour can be observed with other commands that trigger package reinstallation, like I will take a closer look at this issue later today. Update: # Created by Husky v4.2.3 (https://github.com/typicode/husky#readme)
# At: 3/23/2020, 6:59:15 AM
# From: /workspaces/instrumentation/node_modules/husky (https://github.com/typicode/husky#readme)
packageManager=yarn
cd "." After: # Created by Husky v4.2.3 (https://github.com/typicode/husky#readme)
# At: 3/23/2020, 10:38:18 AM
# From: /workspaces/instrumentation/node_modules/husky (https://github.com/typicode/husky#readme)
packageManager=yarn
cd "packages/test_package/" |
@eduard-malakhov any updates on this? |
I can even reproduce it like this:
Being able to just run |
@naorzr, although we have a pretty good understanding of why this is happening, fixing this behavior is not trivial. As far as I can tell, this behavior is due to the way the hooks were designed in husky in the first place. Fixing the issue requires a substantial change in the way hooks are managed by husky and the risks are high that fixing this issue might break other use cases. Making the situation worse, if hooks stop working, it might not be immediately apparent to the users because hooks tend to fail without notice. Given the wide adoption of husky and the limited attention that this issue has received, I believe that fixing this issue at the risk of breaking mainstream scenarios is not worthwhile. To support this fix and make sure it doesn't break anything we'll need to extend our tests to cover many more use cases than we've got today for all supported package managers, which is quite a bit of work. I've started adding tests but I am not done yet. I have even considered releasing a separate package with the redesigned approach to hooks management, but it seems to be overkill to fork and maintain a new repository to only fix one issue. I am using husky in most of my monorepos myself and though, as @fixpunkt has correctly pointed out, "being able to just run yarn install from any place in the monorepo" is a valid use case, I think the best workaround, for now, is to avoid running yarn install and similar commands from anywhere except the repository root. |
Thanks so much for looking into this. From my limited perspective, everything that you said about the difficulty of this issue makes sense except for the fact that this works fine on version 3.1.0 (as far as I can tell). What do we make of that? |
@jparkrr, you're right about version 3.1.0. There was a major refactoring in version 4.0 that moved most of the common logic from respective |
Thanks for the feedbacks @eduard-malakhov 👌 I would like to clarify a point though. You said that the bug occurs "when Eg: $ yarn workspace MY_PACKAGE remove MODULE Under the hood, Yarn runs the |
Good point, @GuillaumeAmat, thank you! I have missed this case out. |
Someone has a workaround for this issue? I was thinking in create a npm scrip to exec yarn remove and reinstall husky, but I don't know if that is the better approach |
@dezkareid, my workaround on Linux: |
I didn’t test it sufficiently, but it seems like husky saves the location from which the initial cd "packages/web-app" which needs to be changed to cd "." in order for husky to run properly. My workaround for this is to add a postinstall script into the package.json of all workspaces: {
"scripts": {
"postinstall": "sed -i '' -E 's/cd \".+\"/cd \".\"/' ../../.git/hooks/husky.local.sh"
}
} |
How about a flag to prevents husky from overriding the
|
+1 for this issue's priority, a team I'm on is running into this issue often as well. The workaround of only running |
+1, git hooks have been a terrible problem in yarn workspaces. |
I commented back on the patch PR #728 - I think it is ready to merge. |
recently faced with this as well, I think @bnbarak PR should fix the problem and should have the priority for the next patch |
Just got caught on this issue. The postinstall script @dcastil posted above was the best workaround I found. |
@dcastil's solution fails if the build from inside the package. E.g. Netlify does this for monorepos. A slight adjustment to get it to work is:
|
post install script was set to all workspaces to reset husky.local.sh -> see typicode/husky#677
I thought I would add my two cents as I had a junior dev bypass the postinstall script suggestion @nickgieschen suggested using I created a import fs from 'fs';
const huskyHookLoc = '../../.git/hooks/husky.local.sh';
if (fs.existsSync(huskyHookLoc)) {
fs.readFile(huskyHookLoc, 'utf-8', (err, data) => {
if (err) throw err;
const newData = data.replace(/cd.*$/gim, 'cd "."');
fs.writeFile(huskyHookLoc, newData, 'utf-8', (err) => {
if (err) throw err;
// eslint-disable-next-line no-console
console.log('husky post-install fix applied');
});
});
}
process.exit(0); and then added it as the "postinstall" script in each repo: Your mileage may vary with the above (we're using TS & modules etc.) but the general principles are:
|
Fixes: atlasmap#3000 Fixes: atlasmap#3011 Disabled react-scripts preflight check Also downgraded husky to 4.2.3 as v6 has an issue with monorepo - typicode/husky#677
Fixes: #3000 Fixes: #3011 Disabled react-scripts preflight check Also downgraded husky to 4.2.3 as v6 has an issue with monorepo - typicode/husky#677
This issue has been automatically marked as stale because it has not had recent activity. It will be closed if no further activity occurs. Thank you for your contributions. |
Hey, just wanted to ping this thread to make sure it doesn't get closed as stale. We're experiencing this issue with Yarn Workspaces and would love to see it fixed. |
@Ashoat we found that if you upgrade to husky v6 it fixed this |
Summary: It [should fix](typicode/husky#677 (comment)) the issue where `yarn add` or `yarn remove` break `lint-staged`, which was occurring because we have a monorepo. Note that there were a good amount of breaking changes in this migration so the config looks pretty different. Test Plan: `yarn cleaninstall`, then make a commit and make sure `lint-staged` still runs Reviewers: palys-swm, atul Reviewed By: palys-swm, atul Subscribers: KatPo, Adrian Differential Revision: https://phabricator.ashoat.com/D1600
This issue has been automatically marked as stale because it has not had recent activity. It will be closed if no further activity occurs. Thank you for your contributions. |
Today I've noticed that Husky hooks suddenly stopped working in a monorepo. After investigating I've found out that file
husky.local.sh
inside.git\hooks
had an entry on last linecd "packages/shared/"
. After removing this line Husky hooks started to work again.My folder structure is like:
root:
-package.json (with Husky hooks setup)
-.git
--packages
---shared
-----package.json
---web
------package.json
Could this happen when I ran
yarn add/install
orlerna clean && lerna bootstrap
from a sub-directory? Is Husky aware of lerna/yarn workspaces? One solution to always use the right directory would be to check package.json forhusky
key.OS: Windows 10
Terminal: webstorm, CMD, cygwin
Since issue was pinpointed I'm not including command's outputs.
The text was updated successfully, but these errors were encountered: