commitlint icon indicating copy to clipboard operation
commitlint copied to clipboard

no such file or directory COMMIT_EDITMSG on new checkout

Open Qard opened this issue 5 years ago • 18 comments

When checking out a repo, there is no COMMIT_EDITMSG file until a new commit is made. In the case of elastic-apm-node, we trigger the commit lint automatically, as part of the test process. This currently means a fresh checkout can't run the tests because @commitling/cli will crash due to being unable to find that file.

Expected Behavior

There was no commit to have a failing message on, so it should not be a failure.

Current Behavior

It crashes with this output:

/home/stephenbelanger/Code/elastic-apm-node/node_modules/@commitlint/cli/lib/cli.js:113
	throw err;
	^

Error: ENOENT: no such file or directory, open '/home/stephenbelanger/Code/elastic-apm-node/.git/COMMIT_EDITMSG'
npm ERR! code ELIFECYCLE
npm ERR! errno 1
npm ERR! [email protected] lint-commit: `test/lint-commits.sh`
npm ERR! Exit status 1
npm ERR! 
npm ERR! Failed at the [email protected] lint-commit script.
npm ERR! This is probably not a problem with npm. There is likely additional logging output above.

npm ERR! A complete log of this run can be found in:
npm ERR!     /home/stephenbelanger/.npm/_logs/2019-03-03T19_30_57_446Z-debug.log

Affected packages

  • [x] cli
  • [ ] core
  • [ ] prompt
  • [ ] config-angular

Possible Solution

Consider it a "pass" if the file is not found.

Steps to Reproduce (for bugs)

  1. Checkout any repo for the first time, which uses @commitlint/cli
  2. Trigger @commitlint/cli
  3. ???
  4. :cry:

Context

Currently this makes the contributing process painful, as new contributors will always run into this the first time they try to run our tests.

Qard avatar Mar 03 '19 22:03 Qard

Hey @Qard , I tried to reproducing your issue by doing:

  1. git clone [email protected]:elastic/apm-agent-nodejs.git (cd in)
  2. npm i
  3. ./node_modules/@commitlint/cli/lib/cli.js

Result:

@commitlint/[email protected] - Lint your commit messages

  [input] reads from stdin if --edit, --env, --from and --to are omitted
  ...

No error. I don't think this is what you mean. Can you provide more info to be able to recreate your issue? Thanks.

escapedcat avatar Mar 04 '19 02:03 escapedcat

Try npm run lint-commit or just commitlint --edit.

Qard avatar Mar 08 '19 08:03 Qard

The issue here is the unconditional usage of the --edit flag.

An excerpt from commitlint --help reveals the flag triggers commitlint to read from fs instead of stdin or the commit history:

--edit, -e   read last commit message from the specified file or fallbacks to ./.git/COMMIT_EDITMSG

Based on the comment in your linting script you want to call commitlint like this:

commitlint --from=HEAD^1

Let me know if this works for you.

marionebl avatar Mar 09 '19 20:03 marionebl

No, that's not exactly what we want, because we specifically want to look at if there is a new commit added locally. If there is, we want to lint it. If there is not, we want to skip it.

Regardless of that behavior though, it seems at least confusing that commitlint --edit just throws a missing file error when it's unable to find that file, leaking implementation details, rather than outputting something more useful, if an error is really the behavior you want there. 🤔

Qard avatar Mar 14 '19 08:03 Qard

Regardless of that behavior though, it seems at least confusing that commitlint --edit just throws a missing file error [...]

You are right, we should add handling for cases where commitlint --edit is called without an edit file being present and communicate clearly what we expect the user to do.

No, that's not exactly what we want, because we specifically want to look at if there is a new commit added locally. If there is, we want to lint it. If there is not, we want to skip it.

Ok - understood. I'd prefer keeping the behaviour of --edit. I'd consider something like --edit-skip-pristine (very open to name suggestions) which would signal commitlint to back of if the --edit file is not present. Thoughts?

marionebl avatar May 31 '19 08:05 marionebl

Maybe a separate flag that augments the --edit flag like --edit --skip-empty and then change the default to produce a more useful error when the file is not found?

Qard avatar Jun 03 '19 23:06 Qard

If this is still an issue we're happy for PR

escapedcat avatar May 12 '21 08:05 escapedcat

I checked out a new branch, and got this same error message.

gkatsanos avatar Nov 29 '21 08:11 gkatsanos

I faced this problem today, I've created the COMMIT_EDITMSG file on .git folder and my hook backs to work: touch ./.git/COMMIT_EDITMSG

Take a look at file permission, this need to be: -rw-rw-r-- 1 example example 9 jan 31 15:17 COMMIT_EDITMSG

iaurg avatar Jan 03 '22 20:01 iaurg

I have the same issue. Disabling commintlint until fixed.

I'm using it with husky

npx --no-install commitlint --edit $1

Also, sometimes it checks the PREVIOUS commit msg and not the one I'm currently trying to commit, resulting in an error if the previous one was in error.

https://intellij-support.jetbrains.com/hc/en-us/community/posts/360010605439/comments/4406713360018

Yohandah avatar Jan 17 '22 13:01 Yohandah

I have the same issue. Disabling commintlint until fixed.

I'm using it with husky

npx --no-install commitlint --edit $1

Also, sometimes it checks the PREVIOUS commit msg and not the one I'm currently trying to commit, resulting in an error if the previous one was in error.

https://intellij-support.jetbrains.com/hc/en-us/community/posts/360010605439/comments/4406713360018

{project}/node_modules/.pnpm/@[email protected]/node_modules/@commitlint/cli/lib/cli.js:114
        throw err;
        ^

[Error: ENOENT: no such file or directory, open '{project}/${1}'] {
  errno: -2,
  code: 'ENOENT',
  syscall: 'open',
  path: '{project}/${1}'
}
husky - commit-msg hook exited with code 1 (error)
#!/bin/sh
. "$(dirname "$0")/_/husky.sh"

# npx --no -- commitlint --edit "\${1}" # throw error
npx --no-install commitlint --edit $1 # it works

Thanks


ccforeverd avatar Jul 06 '22 17:07 ccforeverd

I have the same issue. Disabling commintlint until fixed. I'm using it with husky npx --no-install commitlint --edit $1 Also, sometimes it checks the PREVIOUS commit msg and not the one I'm currently trying to commit, resulting in an error if the previous one was in error. https://intellij-support.jetbrains.com/hc/en-us/community/posts/360010605439/comments/4406713360018

{project}/node_modules/.pnpm/@[email protected]/node_modules/@commitlint/cli/lib/cli.js:114
        throw err;
        ^

[Error: ENOENT: no such file or directory, open '{project}/${1}'] {
  errno: -2,
  code: 'ENOENT',
  syscall: 'open',
  path: '{project}/${1}'
}
husky - commit-msg hook exited with code 1 (error)
#!/bin/sh
. "$(dirname "$0")/_/husky.sh"

# npx --no -- commitlint --edit "\${1}" # throw error
npx --no-install commitlint --edit $1 # it works

Thanks

I suggest to put this on the documentation, so Windows users could save their time.

ShiftWatchOut avatar Jul 27 '22 07:07 ShiftWatchOut

@ShiftWatchOut happy for a PR

escapedcat avatar Jul 27 '22 08:07 escapedcat

I'm quite certain this is the result of a miss-configuration and not a fault in the commitlint itself.

This happens if you by mistake add commitlint to pre-commit instead of commit-msg hook (I know it by doing)

Megamannen avatar Sep 20 '22 06:09 Megamannen

I have the same issue. Disabling commintlint until fixed. I'm using it with husky npx --no-install commitlint --edit $1 Also, sometimes it checks the PREVIOUS commit msg and not the one I'm currently trying to commit, resulting in an error if the previous one was in error. https://intellij-support.jetbrains.com/hc/en-us/community/posts/360010605439/comments/4406713360018

{project}/node_modules/.pnpm/@[email protected]/node_modules/@commitlint/cli/lib/cli.js:114
        throw err;
        ^

[Error: ENOENT: no such file or directory, open '{project}/${1}'] {
  errno: -2,
  code: 'ENOENT',
  syscall: 'open',
  path: '{project}/${1}'
}
husky - commit-msg hook exited with code 1 (error)
#!/bin/sh
. "$(dirname "$0")/_/husky.sh"

# npx --no -- commitlint --edit "\${1}" # throw error
npx --no-install commitlint --edit $1 # it works

Thanks

I suggest to put this on the documentation, so Windows users could save their time.

Hi! I have the same issue and I'm a Windows user, but I'm using lefthook instead of husky and this solution doesn't work for me. What's the meaning of the $1 at the end? because removing it, works on powershell.

AmyLynn87 avatar Nov 23 '22 09:11 AmyLynn87

Same issue using github actions and commiting from within action.

  Error: Unexpected error: /home/runner/work/abc/node_modules/@commitlint/cli/lib/cli.js:112
          throw err;
          ^
  
  [Error: ENOENT: no such file or directory, open '/home/runner/work/abc/.git/COMMIT_EDITMSG'] {
    errno: -2,
    code: 'ENOENT',
    syscall: 'open',
    path: '/home/runner/work/abc/.git/COMMIT_EDITMSG'
  }
  husky - pre-commit hook exited with code 1 (error)

czlowiek488 avatar Nov 28 '22 01:11 czlowiek488

I guess a simple workaround solution is...

$GIT_REPO_PATH=/example

if [ ! -f "$GIT_REPO_PATH".git/COMMIT_EDITMSG ]; then
  git log -1 --pretty=%B > "$GIT_REPO_PATH".git/COMMIT_EDITMSG
  commitlint -e 
  rm "$GIT_REPO_PATH".git/COMMIT_EDITMSG
else
  commitlint -e
fi;

chrissound avatar May 03 '23 11:05 chrissound

For some reason, the commitlint script isn't looking in the correct place for the git message when I run the husky command.

A fix for me was using the PWD command to tell commitlint where the git message needs linting.

#!/usr/bin/env sh
. "${PWD}/$(dirname -- "$0")/_/husky.sh"

COMMIT_LOCATION="${PWD}/.git/COMMIT_EDITMSG"

npx --no-install commitlint --edit "${COMMIT_LOCATION}"

SampsonM avatar May 30 '23 11:05 SampsonM