forcedotcom / cli

Salesforce CLI
https://developer.salesforce.com/docs/atlas.en-us.sfdx_cli_reference.meta/sfdx_cli_reference/
BSD 3-Clause "New" or "Revised" License
493 stars 78 forks source link

Trying to commit but it error: cannot spawn .husky/pre-commit: No such file or directory #1108

Closed icrm-af closed 3 years ago

icrm-af commented 3 years ago

Summary

Getting error: cannot spawn .husky/pre-commit: No such file or directory when trying to commit, previously ran npm install. This started to happen when I upgraded to sfdx-cli/7.111.0.

Today I updated SFDX CLI but the error continues.

Steps To Reproduce:

  1. Run npm install
  2. Make some changes
  3. Git add .
  4. Git commit -m "Test this"

Repository to reproduce: NotificationApp

  1. Run npm install
  2. Make some changes to the files
  3. Run git add .
  4. Run git commit -m "Test this"

Expected result

Commit successfully with no errors.

Actual result

error: cannot spawn .husky/pre-commit: No such file or directory when trying to commit.

System Information

{
        "cliVersion": "sfdx-cli/7.112.0",
        "architecture": "win32-x64",
        "nodeVersion": "node-v14.17.4",
        "pluginVersions": [
                "@oclif/plugin-autocomplete 0.3.0 (core)",
                "@oclif/plugin-commands 1.3.0 (core)",
                "@oclif/plugin-help 3.2.2 (core)",
                "@oclif/plugin-not-found 1.2.4 (core)",
                "@oclif/plugin-plugins 1.10.1 (core)",
                "@oclif/plugin-update 1.4.0-3 (core)",
                "@oclif/plugin-warn-if-update-available 1.7.0 (core)",
                "@oclif/plugin-which 1.0.3 (core)",
                "@salesforce/sfdx-plugin-lwc-test 0.1.7 (core)",
                "@salesforce/sfdx-trust 3.6.0 (core)",
                "alias 1.1.10 (core)",
                "apex 0.2.3 (core)",
                "auth 1.7.1 (core)",
                "config 1.2.14 (core)",
                "custom-metadata 1.0.12 (core)",
                "data 0.6.0 (core)",
                "etcopydata 0.5.7",
                "generator 1.1.7 (core)",
                "limits 1.2.1 (core)",
                "org 1.6.7 (core)",
                "salesforce-alm 52.2.2 (core)",
                "schema 1.0.8 (core)",
                "sfdx-cli 7.112.0 (core)",
                "source 1.0.6 (core)",
                "telemetry 1.2.3 (core)",
                "templates 52.1.0 (core)",
                "user 1.4.0 (core)"
        ],
        "osVersion": "Windows_NT 10.0.19042"
}

Additional information

package.json file

{
  "name": "salesforce-app",
  "private": true,
  "version": "1.0.0",
  "description": "Salesforce App",
  "scripts": {
    "lint": "npm run lint:lwc && npm run lint:aura",
    "lint:aura": "eslint **/aura/**",
    "lint:lwc": "eslint **/lwc/**",
    "test": "npm run test:unit",
    "test:unit": "sfdx-lwc-jest",
    "test:unit:watch": "sfdx-lwc-jest --watch",
    "test:unit:debug": "sfdx-lwc-jest --debug",
    "test:unit:coverage": "sfdx-lwc-jest --coverage",
    "prettier": "prettier --write \"**/*.{cls,cmp,component,css,html,js,json,md,page,trigger,xml,yaml,yml}\"",
    "prettier:verify": "prettier --list-different \"**/*.{cls,cmp,component,css,html,js,json,md,page,trigger,xml,yaml,yml}\"",
    "postinstall": "husky install",
    "precommit": "lint-staged"
  },
  "devDependencies": {
    "@lwc/eslint-plugin-lwc": "^1.0.1",
    "@prettier/plugin-xml": "^0.13.1",
    "@salesforce/eslint-config-lwc": "^2.0.0",
    "@salesforce/eslint-plugin-aura": "^2.0.0",
    "@salesforce/eslint-plugin-lightning": "^0.1.1",
    "@salesforce/sfdx-lwc-jest": "^0.13.0",
    "eslint": "^7.29.0",
    "eslint-config-prettier": "^8.3.0",
    "eslint-plugin-import": "^2.23.4",
    "eslint-plugin-jest": "^24.3.6",
    "husky": "^6.0.0",
    "lint-staged": "^11.0.0",
    "prettier": "^2.3.1",
    "prettier-plugin-apex": "^1.9.1"
  },
  "lint-staged": {
    "**/*.{cls,cmp,component,css,html,js,json,md,page,trigger,xml,yaml,yml}": [
      "prettier --write"
    ],
    "**/{aura,lwc}/**": [
      "eslint"
    ]
  }
}
github-actions[bot] commented 3 years ago

Thank you for filing this issue. We appreciate your feedback and will review the issue as soon as possible. Remember, however, that GitHub isn't a mechanism for receiving support under any agreement or SLA. If you require immediate assistance, contact Salesforce Customer Support.

mshanemc commented 3 years ago

did you add husky or did it come from project:create ? What's in .husky folder or .huskyrc.js? When was the project created?

mshanemc commented 3 years ago

a quick solution would be to run force:project:create somewhere else and then move over the .husky directory that it creates to this project

icrm-af commented 3 years ago

I just tested this:

Created the project using sfdx:project:create, initialized a github repo using git init, created git repo using gh repo create, added files using git add ., committed changes via git commit -m "<MSG>", it worked fine, then I ran npm install, made some changes and repeated the process to commit and it errored.

@mshanemc

mshanemc commented 3 years ago

I can't get the error to appear.

Does the error reproduce for you only if you gh repo create or is that step irrelevant?

pozil commented 3 years ago

Hi @icrm-af, let's try to reproduce the issue with the simplest scenario. Can you run these commands:

sfdx force:project:create -n test
cd test
git init
npm install
git commit -a -m "Initial commit"

In order to figure out what's wrong, we need to see the output of all of these commands. Can you save them and share them here?

icrm-af commented 3 years ago

Log for:

sfdx force:project:create -n test

{
  "status": 0,
  "result": {
    "outputDir": "C:\\Users\\myUser\\Desktop\\VSCode Projects",
    "created": [
      "test\\config\\project-scratch-def.json",
      "test\\README.md",
      "test\\sfdx-project.json",
      "test\\.husky\\pre-commit",
      "test\\.vscode\\extensions.json",
      "test\\.vscode\\launch.json",
      "test\\.vscode\\settings.json",
      "test\\force-app\\main\\default\\lwc\\.eslintrc.json",
      "test\\force-app\\main\\default\\aura\\.eslintrc.json",
      "test\\scripts\\soql\\account.soql",
      "test\\scripts\\apex\\hello.apex",
      "test\\.eslintignore",
      "test\\.forceignore",
      "test\\.gitignore",
      "test\\.prettierignore",
      "test\\.prettierrc",
      "test\\jest.config.js",
      "test\\package.json"
    ],
    "rawOutput": "target dir = C:\\Users\\myUser\\Desktop\\VSCode Projects\n   create test\\config\\project-scratch-def.json\n   create test\\README.md\n   create test\\sfdx-project.json\n   create test\\.husky\\pre-commit\n   create test\\.vscode\\extensions.json\n   create test\\.vscode\\launch.json\n   create test\\.vscode\\settings.json\n   create test\\force-app\\main\\default\\lwc\\.eslintrc.json\n   create test\\force-app\\main\\default\\aura\\.eslintrc.json\n   create test\\scripts\\soql\\account.soql\n   create test\\scripts\\apex\\hello.apex\n   create test\\.eslintignore\n   create test\\.forceignore\n   create test\\.gitignore\n   create test\\.prettierignore\n   create test\\.prettierrc\n   create test\\jest.config.js\n   create test\\package.json\n"
  }
}

Log for:

git init

Initialized empty Git repository in C:/Users/myUser/Desktop/VSCode Projects/test/.git/

Log for:

npm install

`

core-js@3.16.1 postinstall C:\Users\myUser\Desktop\VSCode Projects\test\node_modules\core-js node -e "try{require('./postinstall')}catch(e){}"

Thank you for using core-js ( https://github.com/zloirock/core-js ) for polyfilling JavaScript standard library!

The project needs your help! Please consider supporting of core-js: > https://opencollective.com/core-js  > https://patreon.com/zloirock  > https://paypal.me/zloirock  > bitcoin: bc1qlea7544qtsmj2rayg0lthvza9fau63ux0fstcz 

Also, the author of core-js ( https://github.com/zloirock ) is looking for a good job -)

salesforce-app@1.0.0 postinstall C:\Users\myUser\Desktop\VSCode Projects\test husky install

husky - Git hooks installed added 961 packages from 378 contributors and audited 963 packages in 866.328s

89 packages are looking for funding run npm fund for details

found 0 vulnerabilities

`

Output for git commit:

git commit -a -m "Initial commit" >> git_commit_log.txt
error: cannot spawn .husky/pre-commit: No such file or directory

@pozil

icrm-af commented 3 years ago

I can't get the error to appear.

Does the error reproduce for you only if you gh repo create or is that step irrelevant?

That step is irrelevant.

icrm-af commented 3 years ago

I'll uninstall and install everything again.

icrm-af commented 3 years ago

I uninstalled Git, SFDX CLI, Node, Heroku, and many other tools, ran a cleanup, installed everything again, and the same issue, I know is related to my computer but I would like to find a way to fix this instead of living with it or reinstall my OS.

pozil commented 3 years ago

Thanks for sharing your logs @icrm-af, everything looks normal.

I saw that Husky updated it's commit hook template but I'm not sure if this has an impact. Can you replace the content of .husky/pre-commit with the following (the second line is new):

#!/bin/sh
. "$(dirname "$0")/_/husky.sh"

npm run precommit

Once the file is updated, try to commit something to check if this fixes the pre-commit hook.

icrm-af commented 3 years ago

Thanks for sharing your logs @icrm-af, everything looks normal.

I saw that Husky updated it's commit hook template but I'm not sure if this has an impact. Can you replace the content of .husky/pre-commit with the following (the second line is new):

#!/bin/sh
. "$(dirname "$0")/_/husky.sh"

npm run precommit

Once the file is updated, try to commit something to check if this fixes the pre-commit hook.

@pozil that worked.

But seems to be something wrong with my environment, because it doesn't fail in other instances.

pozil commented 3 years ago

Thanks for the update. I'm pushing this change to the new DX project template just in case.

pozil commented 3 years ago

My fix has been incorporated in the DX project template and should be available in the upcoming Salesforce CLI release.

@mshanemc we can close this issue.

icrm-af commented 3 years ago

@pozil issue came back, but now I'm on a different computer, clean OS.

After I do npm install on a fresh project and then try to commit changes, error: cannot spawn .husky/pre-commit: No such file or directory appears.

pozil commented 3 years ago

Damn :( My proposed fix hasn't landed on the CLI GA branch yet so I'm wondering if there's something else to it. Can you apply it manually (edit .husky/pre-commit) and check if it helps?

cristiand391 commented 3 years ago

I've tried creating a new project with sfdx force:project:create -n test-project and can see pozil's patch locally in the husky folder.

I guess when @icrm-af tried last time sfdx was still using an older version of the template. Can you see if you are still seeing this issue with the latest version? I'm using sfdx-cli/7.122.1 darwin-x64 node-v14.18.1 which includes templates 52.4.0 (core).

no-response[bot] commented 3 years ago

This issue has been automatically closed because there has been no response to our request for more information from the original author. Currently, there is not enough information provided for us to take action. Please reply and reopen this issue if you need additional assistance.