Sub topic 5
Finding the first real error
This chapter assumes you can read YAML, run commands in a shell, and open a workflow run in GitHub. You should also know that a workflow contains jobs, and a job contains steps. The goal here is a repeatable method for separating the root cause of a failure from the errors it produces afterward.
Why the last red line is rarely the cause
A failing run often shows several red marks. The log is long, and the loudest message is often near the bottom. That message is usually a symptom.
Steps in a job run in order. By default, when one step fails, GitHub skips the remaining steps unless they have a condition such as if: failure() or if: always(). So the first failed step in a job is normally the one to study. Later steps are marked skipped, not failed.
Inside a single step, the same idea applies. A script may print an error, keep running, and print more errors from the broken state. The first error in that step's output is the best candidate for the root cause.
The rule of the chapter: find the first failed step, then find the first error inside it.
The method
Work in this order.
- Open the run and find the job with the red cross. If a matrix produced several jobs, note which ones failed and which passed.
- Inside that job, find the first step with a red cross. Steps with a grey skipped icon ran nothing.
- Expand that step and read from the top, not the bottom.
- Look for the first line that reports an error, such as
Error:,fatal:,ENOENT,command not found, or a non-zero exit message. - Read the 10 to 20 lines before it. They often hold the command that was run and the values it received.
- Decide whether the error explains the exit code. If it does, you have the root cause. If not, keep reading upward or turn on debug logging.
A worked example
Take the baseline workflow from the plan. The repository keeps its code in an app/ subdirectory.
name: CI
on: [push, pull_request]
jobs:
test:
runs-on: ubuntu-latest
steps:
- uses: actions/checkout@v4
- uses: actions/setup-node@v4
with:
node-version: 20
- run: npm ci
- run: npm testThe run page shows the test job failed. Checkout and setup-node have green checks. The npm ci step is red. The npm test step is grey, meaning it was skipped.
Opening npm ci shows output close to this:
Run npm ci
npm error code ENOENT
npm error syscall open
npm error path /home/runner/work/repo/repo/package.json
npm error errno -2
npm error enoent Could not read package.json
Error: Process completed with exit code 254.Two lines matter. Could not read package.json is the root cause. Process completed with exit code 254 is how the runner reports that the command returned a non-zero status. The exit-code line is nearly always last, so it tells you that the step failed but not why.
The npm test step never ran, so it contributes nothing. Looking at it would waste time.
The fix is to run npm where package.json lives. This version sets a default working directory for run steps and points the cache at the lock file in that directory.
name: CI
on: [push, pull_request]
jobs:
test:
runs-on: ubuntu-latest
defaults:
run:
working-directory: app
steps:
- uses: actions/checkout@v4
- uses: actions/setup-node@v4
with:
node-version: 20
cache: npm
cache-dependency-path: app/package-lock.json
- run: npm ci
- run: npm testRoot cause versus follow-on errors
A follow-on error is a failure that exists only because an earlier thing went wrong. Recognizing the pattern saves a lot of time.
| Pattern | What you see | Where the cause is |
|---|---|---|
| Skipped steps | Grey steps after a red one | The red step before them |
| Cascading output | Many errors after one failed install | The first install error |
| Missing file after a failed build | `cannot find module './dist'` | The build step that should have created `dist` |
| Matrix siblings cancelled | Jobs marked cancelled | The one matrix job that failed first |
| Generic exit line | `Process completed with exit code 1` | The lines above it |
Cascades are common in scripts. Consider a build script that compiles, then bundles, then uploads. If compiling fails and the script keeps going, the bundler complains about missing input and the uploader complains about a missing artifact. All three messages appear in the log, but only the first one points at what you must change.
Where the first error can hide
The first error is not always the first line that looks alarming. Check these cases.
- Warnings that look like errors. Many tools print deprecation warnings in red or with
Error-like words. Check whether the step actually exited non-zero. A step with warnings can still be green. - Errors printed to a log group. The runner folds output into collapsible groups. An error inside a collapsed group is easy to miss. Use the expand-all control, or search the log with the search box.
- Errors in a different step than the one marked red. A step can fail because of a value set earlier, such as a variable written to
$GITHUB_ENVthat was empty. The failing command is the symptom, and the earlier step is the cause. Read the red step's log to see the bad value, then trace it back. - Silent failures that pass. A command followed by
|| true, or a step withcontinue-on-error: true, can fail without turning the job red. The real error then appears later as a follow-on. If the red step's error makes no sense, scan earlier green steps for error text. - Errors only in the exit code. Some tools print nothing useful before exiting with a non-zero code. In that case, the log gives you the command, and you must run it again with more verbosity.
Using the exit code as a clue
The exit code narrows the search. These values are common conventions, not guarantees, since each program chooses its own codes.
| Exit code | Usual meaning |
|---|---|
| 1 | General failure. The program decided something was wrong |
| 2 | Misuse of a shell command, or a tool-specific usage error |
| 126 | The file exists but is not executable |
| 127 | Command not found |
| 128 + N | The process was ended by signal N |
| 137 | Killed by signal 9, often out of memory |
| 143 | Ended by signal 15, often a cancellation or timeout |
A 127 sends you to the PATH or to a missing install step. A 126 on a script in the repository often means the file lost its execute bit, which chmod +x and git update-index --chmod=+x address. A 137 suggests a memory limit, so look for what grew before the kill.
Adding diagnostics when the log is not enough
When the first error does not explain the failure, add information instead of guessing. A step with if: failure() runs only after an earlier step has failed, so it is a safe place for diagnostics.
steps:
- name: Run checks
run: |
echo "::group::Install"
npm ci
echo "::endgroup::"
echo "::error file=app/index.js,line=10::Example error annotation"
exit 1
- name: Show diagnostics on failure
if: failure()
run: ls -laThe ::group:: and ::endgroup:: lines fold the install output into one collapsible block. The ::error line creates an annotation that appears on the run summary and can point at a file and line. The if: failure() step lists the working directory, which immediately shows a wrong path or a missing file.
Other useful diagnostics are pwd, env | sort (never print secrets, though masking will hide registered ones), node --version, and the contents of a lock file or generated config.
A short checklist
- Which job failed first, and did any pass?
- Which step is the first red one?
- What is the first error line in that step?
- Does that line explain the exit code?
- Could an earlier green step have set up the bad state?
- If still unclear, what diagnostic would confirm or rule out my best guess?
Form one hypothesis, change one thing, and re-run. Changing several things at once makes it hard to know which change worked.
Exercise
A job has five steps. Checkout and setup-node are green. The third step, npm run build, is red. Steps four and five are grey. The log of step three contains a TypeScript error on line 12 of its output, followed by Error: Cannot find module './dist/index.js' on line 40 and Process completed with exit code 1. Which line is the likely root cause, and why should you not investigate steps four and five?
Show answer
The TypeScript error on line 12 is the likely root cause. The missing ./dist/index.js on line 40 is a follow-on error, since the compile failure meant no output was created. The exit code line only reports that the step failed. Steps four and five are grey because they were skipped after the failure, so they ran no commands and have no log content to explain the problem.
Exercise
A step ends with Process completed with exit code 127, and the line above it says eslint: command not found. Name two possible causes and one change you could make for each.
Show answer
One cause is that the tool was never installed, for example because npm ci was skipped or ran in the wrong directory. Fix it by installing dependencies first or setting working-directory. Another cause is that the tool is installed locally but not on the PATH. Fix it by calling it through npx eslint or an npm script such as npm run lint.