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

Precommit hook fails inside Visual Studio 2019 #673

Closed
lucipacurar opened this issue Feb 19, 2020 · 29 comments
Closed

Precommit hook fails inside Visual Studio 2019 #673

lucipacurar opened this issue Feb 19, 2020 · 29 comments
Labels

Comments

@lucipacurar
Copy link

lucipacurar commented Feb 19, 2020

Hi,

I have git version 2.24.1.windows.2, husky 4.2.3, VS2019 16.4.5, pretty-quick 2.0.1 and husky fails to run inside VS2019 only with the following message:

 husky > pre-commit (node v12.14.0)
/c/[my_project_path_here]/ClientApp/node_modules/.bin/pretty-quick: line 5: cygpath: command not found
internal/modules/cjs/loader.js:800
    throw err;
    ^

Error: Cannot find module 'C:\pretty-quick\bin\pretty-quick.js'
    at Function.Module._resolveFilename (internal/modules/cjs/loader.js:797:15)
    at Function.Module._load (internal/modules/cjs/loader.js:690:27)
    at Function.Module.runMain (internal/modules/cjs/loader.js:1047:10)
    at internal/main/run_main_module.js:17:11 {
  code: 'MODULE_NOT_FOUND',
  requireStack: []
}
husky > pre-commit hook failed (add --no-verify to bypass)

But it works perfectly fine from git bash and other git clients like SourceTree and Tortoise. I'm not sure how can I run husky in debug mode from VS so I can provide you with the debug log. With husky 3.1.0 it works just fine. Please let me know if you need more info. Thank you :)

@longcui
Copy link

longcui commented Mar 2, 2020

same issue here

@volser
Copy link

volser commented Mar 4, 2020

same issue but for GitHub Desktop for Windows

@riverwilliams94
Copy link

riverwilliams94 commented Mar 12, 2020

I also have this issue. As suggested, downgrading Husky to 3.1.0 appears to fix it

@vmo-mgosling
Copy link

vmo-mgosling commented Mar 16, 2020

Also having this issue with GitHub Desktop.

A few of our staff use GitHub Desktop, hopefully this issue gets fixed. 3.1.0 is quite old.

NotNoshy added a commit to NotNoshy/Aesthetically that referenced this issue Mar 27, 2020
…added

- Removed save flag in `npm i --save aesthetically` . [Redundant](https://stackoverflow.com/questions/19578796/what-is-the-save-option-for-npm-install)

- Removed default failing test

- Code formatting with [Prettier] (https://prettier.io) added. Rules added in package.json.

Pre-commit hook added with husky (to format code). Latest version not used because of an [issue](typicode/husky#673) with GitHub desktop.
@barisusakli
Copy link

This seems similar to https://stackoverflow.com/questions/61196309/husky-4-x-not-working-with-visual-studio-git, maybe try the solution from that link.

@Slettan
Copy link

Slettan commented Apr 25, 2020

This seems similar to https://stackoverflow.com/questions/61196309/husky-4-x-not-working-with-visual-studio-git, maybe try the solution from that link.

Thanks for the link, this worked for me:

"scripts": {
  "format-staged": "pretty-quick --staged"
},
"husky": {
  "hooks": {
    "pre-commit": "npm run format-staged"
  }
}

@nycgavin
Copy link

This seems similar to https://stackoverflow.com/questions/61196309/husky-4-x-not-working-with-visual-studio-git, maybe try the solution from that link.

Thanks for the link, this worked for me:

"scripts": {
  "format-staged": "pretty-quick --staged"
},
"husky": {
  "hooks": {
    "pre-commit": "npm run format-staged"
  }
}

Try
"husky": {
"hooks": {
"pre-commit": "npx pretty-quick --staged"
}
}

@aminya
Copy link

aminya commented Sep 23, 2020

This is not specific to Visual Studio.
I have the same error with commit lint on GitHub desktop or Atom.
conventional-changelog/commitlint#1665

@lloydtao
Copy link

i have this issue on GitHub Desktop.

it appears to be specific to commitlint (conventional-changelog/commitlint#1665).

removing this from my package.json instantly fixes the problem:

  "husky": {
    "hooks": {
      "commit-msg": "commitlint -E HUSKY_GIT_PARAMS",
      "pre-commit": "lint-staged"
    }
  },

the error log gives a small indication of what's happening.

it incorrectly looks in:

  • C:\Users\Lewis\AppData\Local\GitHubDesktop\app-2.5.5\resources\app\git\node_modules\yarn\bin\yarn.js

where the real path is:

  • C:\Users\Lewis\AppData\Roaming\npm\node_modules\yarn\bin\yarn.js

error log:

Commit failed - exit code 1 received, with output: '/c/Users/Lewis/AppData/Roaming/npm/yarn: line 5: cygpath: command not found
internal/modules/cjs/loader.js:1032
  throw err;
  ^

Error: Cannot find module 'C:\Users\Lewis\AppData\Local\GitHubDesktop\app-2.5.5\resources\app\git\node_modules\yarn\bin\yarn.js'
    at Function.Module._resolveFilename (internal/modules/cjs/loader.js:1029:15)
    at Function.Module._load (internal/modules/cjs/loader.js:898:27)
    at Function.executeUserEntryPoint [as runMain] (internal/modules/run_main.js:71:12)
    at internal/main/run_main_module.js:17:47 {
  code: 'MODULE_NOT_FOUND',
  requireStack: []
}'

@rlam3
Copy link

rlam3 commented Oct 7, 2020

@lloydtao Were you able to find a workaround solution?

@jmejiaxdev
Copy link

Hi! A brute solution is removing whatever hook (e.g. husky) folder located inside your .git folder

@aminya
Copy link

aminya commented Jan 20, 2021

Hi! A brute solution is removing whatever hook (e.g. husky) folder located inside your .git folder

Well, I actually do this. Other people use Husky in their projects, and when I want to contribute to their projects, I get rid of the hooks first so I can commit a change! 🙄

You can also remove the husky entry from package.json

@typicode
Copy link
Owner

Not sure but it seems to be a PATH issue. Could you try logging PATH?

In ~/.huskyrc:

echo "$PATH" > PATH.txt

@BarakMaozCIQ
Copy link

BarakMaozCIQ commented Jan 28, 2021

I have an issue here, someone knows how to solve it?
I'm using GitHub desktop and I want to use "pretty-quick", I tried to add it from package.json and I tried to add it on separate js files but it's still not working
*when I change the husky version to 3.1.0 it's working, so the issue is in husky probably
image

image

@lucipacurar
Copy link
Author

@BarakMaozCIQ you have some workarounds posted above. You can change the script to be run on precommit with npx pretty-quick --staged

@viceice
Copy link

viceice commented Apr 9, 2021

It's a visual studio issue, in the included git version the cygpath.exe is missing.

https://developercommunity.visualstudio.com/t/Missing-cygpathexe-in-git/1393876

@aminya
Copy link

aminya commented Apr 9, 2021

It is actually a Husky problem. I had the issue with GitHub Desktop, not Visual Studio. Husky v1 works fine for example.

@viceice
Copy link

viceice commented Apr 9, 2021

Nope, it's a problem of the used git distribution. cygpath seems also be missing in github desktop.

The included bash sets MSYS env, which causes the npm shell wrapper to use cygpath.

@lucksp
Copy link

lucksp commented Apr 15, 2021

committing on my terminal works fine. Committing on GitHub Desktop results in Error:

.husky/pre-commit: line 4: npx: command not found
husky - pre-commit hook exited with code 127 (error)

Followed the guide here: https://github.com/okonet/lint-staged#installation-and-setup

"husky": ">=6",
"lint-staged": ">=10",

@aminya
Copy link

aminya commented May 10, 2021

@viceice Microsoft has closed your issue meaning this is a Husky issue and it should be fixed independently of the environment that Husky is being used in (Visual Studio, GitHub Desktop, Atom, etc).

https://developercommunity.visualstudio.com/t/Missing-cygpathexe-in-git/1393876#T-N1414158

Hi Michael,

We only use the version of Git that we ship with Visual Studio because we want to make sure that our Git features within Visual Studio function as designed. > Different versions of git.exe can break us in three different ways:

If git.exe changes the command line parameters that its operations expected (unlikely), or
If git.exe changes the output that it writes to stdout and/or stderr (surprisingly likely), or
If git.exe changes the exit codes that its operations return (also, surprisingly likely).
Because git.exe can break us so easily, we don’t support versions of git.exe other than the one that ships with the version of Visual Studio that you have installed.

That being said, it’s your computer, and you can do what you want with it. You could try replacing the folder that contains the version of Git that we ship with Visual Studio with a version of Git for Windows that you download from https://gitforwindows.org/. I can’t state enough that this is NOT a supported scenario and some parts of Visual Studio’s Git integration may not work after you do this, but it might work for you. Something you should know, however, is that if you go this route, the next time your Visual Studio installation is updated, it will most likely replace the version of Git for Windows that you copy/install into the Visual Studio Git folder with the latest supported version of Git. This will most likely break your scenario again because the latest support version of Git that we ship won’t include cygpath.exe.

I really wish I could give you better news than this, but unfortunately, I think this is your best option at this point. You’re welcome to go to Git for Windows GitHub issues page and request cygpath.exe be added back to the tools that ship with MinGit, though.

Sorry and good luck.

@viceice
Copy link

viceice commented May 10, 2021

@aminya i know 😕 for me it looks like a npm issue too, because the generated shell wrapper scripts are not checking the existence of cygpath.

@aminya
Copy link

aminya commented May 10, 2021

Husky directly spawns git command without checking it the exe will work with Husky. I think the solution would be to first use which package to see where the git exe is and use the one which is guaranteed to work.

if (cp.spawnSync('git', ['rev-parse']).status !== 0) {

async function getGitExe() {
	if ("win32" !== process.platform) {
		return "git"
	}
	const which = await import('which') 
	let gitPaths = await which('git', { all: true })
	gitPaths = gitPaths.filter(gitPath => /* some condition */)
	if (gitPaths.length === 0) {
		return "git" // or throw an error
	}
	return gitPaths[0]
}

@Diaaz
Copy link

Diaaz commented Jun 8, 2021

Any update on this?

@xavierfoucrier
Copy link

Hello everyone,

I had the same error in Github Desktop and fix it by downgrading to a lower version of Git for Windows:
now using git version 2.30.1.windows.1 on Windows 7 Pro x64.

Still have the issue on Atom 1.57.0, but could probably fix it by reinstalling the software.

@mcaden
Copy link

mcaden commented Jun 22, 2021

I had this issue on an Angular project. It happened running yarn lint or just going straight to angular and using ng lint. However, npm run lint ran just fine.

I'm using nvm-for-windows using Nodejs 14.17.0 and npm 6.14.13.

@aminya
Copy link

aminya commented Jun 22, 2021

Those who still have this issue. I migrated to Lefthook, and I am pretty happy with it.

@stale
Copy link

stale bot commented Aug 21, 2021

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.

@stale stale bot added the wontfix label Aug 21, 2021
@stale stale bot closed this as completed Aug 28, 2021
@JBoothUA
Copy link

JBoothUA commented Jan 3, 2022

https://stackoverflow.com/questions/61196309/husky-4-x-not-working-with-visual-studio-git

@pajuka
Copy link

pajuka commented Jun 3, 2022

Look here, this fixed these issue for me: yarnpkg/berry#4095

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Projects
None yet
Development

No branches or pull requests