Left-Pad the World — JavaScript Bug Hunt
Inspired by the 11 lines that broke the internet in 2016, when left-pad vanished from npm and half the ecosystem stopped building.
- Language: JavaScript
- Layer: Backend
- Difficulty: Easy
- Concepts: Strings, Dependencies
- Modelled on: npm · 2016
- Visible tests: pads to the target length; long-enough strings pass through; default fill is a space
- Reward: 50 XP for a complete fix
Incident
- Severity: SEV-3
- Time to fix: 10 minutes, the target for an easy incident
Failing checks on the shipped build
- pads to the target length — expected "005" but got "0005"
- long-enough strings pass through — expected "hello" but got "000hello"
- default fill is a space — expected " 7" but got "undefinedundefinedundefined7"
How to work this incident
- Read the report: write down the expected behaviour and the actual one in a sentence each.
- Reproduce it: run the visible tests and find the failing check that matches the report.
- Read the logs and the failing checks for the first value that is wrong — the earliest wrong value is nearer the cause than the loudest error.
- Follow the request through the handler, the validation and the service it calls; find the first step whose output disagrees with the report.
- Fix the cause rather than the symptom, run the visible tests again, and submit against the hidden ones — then write the root cause down.
Briefing
Inspired by the 11 lines that broke the internet in 2016, when left-pad vanished from npm and half the ecosystem stopped building. The team vendored their own replacement — and got those 11 lines wrong.
leftPad.js: pad a string on the left to a target length.
Bug report
BUG-11LOC · Reported by: build infra
leftPad(str, len, ch):
- pads with ch (default " ") until the string is len long
- strings already at or beyond len come back untouched
Observed: leftPad("5", 3, "0") returns "0005" (pads len EXTRA chars) and the default fill inserts the literal text "undefined".
Logs
[ids] order number rendered as "undefined42"The code as shipped
src/util/leftPad.js (editable)
// Pads str on the left with ch until it reaches len characters.
exports.leftPad = function (str, len, ch) {
str = String(str);
var pad = "";
for (var i = 0; i < len; i++) {
pad += ch;
}
return pad + str;
};
Open the hunt to start the incident clock, edit the files, run the visible tests and submit against the hidden ones, then write the root cause down. More JavaScript bug hunts.