The Remotes Commitlint's Travis CLI Never Fetched
On pull requests, @commitlint/travis-cli lints TRAVIS_COMMIT_RANGE with --from and --to. Those commits only resolve if the helper has fetched the base repository and, for fork PRs, the source repository. The helper was supposed to create those remotes, but it never did. The fetch calls were wrapped in arrow functions and handed to Promise.all:
await Promise.all([
() => fetch({ name: "base", url: `https://github.com/${REPO_SLUG}.git` }),
IS_PR
? () => fetch({ name: "source", url: `https://github.com/${PR_SLUG}.git` })
: () => Promise.resolve(),
]);
Promise.all was awaiting an array of functions, not promises. It resolved immediately with the functions, and neither fetch() ran. The remotes were never added, nothing was fetched, and the commit range could fail to resolve on the very PR builds the code was written for. The issue was conventional-changelog/commitlint#5197.
The first fix was small: call the functions and run them sequentially.
await fetch({ name: "base", url: `https://github.com/${REPO_SLUG}.git` }, remotes);
if (IS_PR) {
await fetch({ name: "source", url: `https://github.com/${PR_SLUG}.git` }, remotes);
}
Serializing mattered. Running git remote add concurrently produced intermittent failures:
error: could not lock config file .git/config: File exists
git remote add writes to .git/config under a lock, so two concurrent calls race. I reproduced 38 failures in 200 parallel runs. Sequential fetches avoid the lock entirely.
Once the fetches actually ran, the surrounding lifecycle bugs became reachable. A failed fetch would leave the temporary remote behind and leave the working tree stashed. The final version tracks the remotes it creates and cleans up in nested finally blocks:
const pop = await stash();
const remotes: string[] = [];
try {
await fetch({ name: "base", url: `https://github.com/${REPO_SLUG}.git` }, remotes);
if (IS_PR) {
await fetch({ name: "source", url: `https://github.com/${PR_SLUG}.git` }, remotes);
}
await pop();
await lint(/* ... */);
} finally {
try {
await pop();
} finally {
for (const remote of remotes) {
await removeRemote(remote);
}
}
}
stash() returns an idempotent pop, so the success path and the finally path can both call it without popping twice. It only stashes when the working tree is dirty, and -k -u keeps the index and untracked files.
The last review point was remote naming. An early cleanup pass removed commitlint-base and commitlint-source before adding them, which made repeat runs idempotent but deleted a user's remote with the same name. The final code never touches an existing remote; it creates a unique name and removes only that:
async function fetch({ name, url }, remotes) {
const remote = `commitlint-${name}-${randomUUID()}`;
await git(["remote", "add", remote, url]);
remotes.push(remote);
await git(["fetch", remote, "--quiet"]);
}
The tradeoff is a less readable name and a unique leftover remote if the process crashes hard, but it cannot clobber user configuration.
I also considered reusing the checkout's origin URL to preserve SSH authentication for private repositories. On Travis, origin is the base repository, so it only helps base and does nothing for a fork's source remote; a deploy key scoped to the base usually cannot read a private fork. Reusing it also risked copying credentials into a second remote. I dropped it and kept the fix scoped to the reported bug.
The tests use a fake git fixture that logs each invocation, plus a real shallow clone so the helper runs against a repository. They assert the exact sequence of stash, remote add, fetch, lint, pop, and remote removal, including that cleanup happens on the success path. The TDD history is split into failing-test and implementation commits for each behavior: fetch the remotes, clean up and restore the stash, then unique temporary names.