DEV Community

Cover image for How we generate 100,000-line files with exactly the bugs we planted
dawn
dawn

Posted on

How we generate 100,000-line files with exactly the bugs we planted

We make Build Failed, a game about the bug that ate your evening. Every level is a code editor with a few broken characters hidden in it: a missing semicolon, a ) that became ], a 0 that is secretly the letter O, or a fullwidth ; that looks exactly like ;. You find the line, tap the wrong character, and ship.

👉 Try the web demo (no signup, nothing leaves your browser): https://dawncommitgames.com/demo/

The fun part to build was the code itself. Levels go from 19 lines to 100,000 lines with 500 errors, in five languages, and none of it is stored anywhere. Here's how.

1. Generate clean code first, then break it

Every file starts as clean code built from templates (JS, TS, Python, Java and SQL). Before a single error is planted, the clean output goes through a real parser:

  • JavaScript: acorn
  • TypeScript: the TypeScript parser
  • Python: ast
  • Java: javac

If anything fails to parse, the build stops. That gives us one guarantee we care about: the only bug in a file is the one we put there.

2. Precompute every place an error can go

Each template is cut into small blocks (a function, a config object, a class). For every line of every block we precompute the columns where each error type can be planted: the ; at the end of a statement, the ) of a call, a 0 inside a number, the , between arguments.

That turns "plant 500 errors in a 100,000-line file" into "pick 500 entries from a list", spread out so no two errors are closer than three lines.

3. Never store the file: rebuild it from a seed

A 100,000-line file would be megabytes of text. Instead, the device rebuilds it from a seed and a library of ~2,000 validated blocks. And it doesn't even build all of it: the file is a Proxy that looks like a string[] but makes a line only when the editor asks for it.

const lines = new Proxy([] as string[], {
  get(target, prop, receiver) {
    if (prop === 'length') return length;
    const i = Number(prop);
    if (Number.isInteger(i) && String(i) === prop) {
      return planted.get(i) ?? cleanAt(i); // build the line on demand
    }
    return Reflect.get(target, prop, receiver);
  },
});
Enter fullscreen mode Exit fullscreen mode

The editor only renders what's on screen, so only those lines ever exist as strings.

4. Variety without moving a single column

Rebuilding from the same ~2,000 blocks gets repetitive fast. So each block swaps its nouns: a TicketService becomes a CouponService somewhere else. The catch: every error column was precomputed, so the text can't shift.

The trick is to only swap words of the same length:

const NOUN_GROUPS = [
  ['user', 'item', 'cart', 'post', 'task'],
  ['order', 'token', 'event'],
  ['report', 'coupon', 'review', 'ticket'],
];
Enter fullscreen mode Exit fullscreen mode

ticket ↔ coupon keeps every column where it was, so the precomputed error positions stay valid after the swap.

5. Test the code like a reviewer would

Parsers catch syntax, not sense. A generated sort((a, b) => b.price - a.score) parses fine, but any developer will tell you that's a bug. So we generate every stage in CI and scan for things a reviewer would flag: comparisons between two different fields, duplicate keys, a fetchCoupon() calling /api/tickets/. If the scan finds one, the build fails.

What the game looks like

Besides the bug hunts, there's a 100-week "dev career" mode: standups, tickets, a PM who messages you at 11pm, promotions, and a lot of endings (not all of them good). The web demo has stages 1–20 and a 12-week career.

I'd love to hear which level got you, or how you'd generate "realistic" broken code. Questions about the generator are very welcome in the comments.

Top comments (1)

Collapse
 
dawncommit profile image
dawn •

Why we wrote this: every developer has lost an evening to a single missing character, and we wanted a game that makes that pain fun instead of frustrating.

The hardest part wasn't planting bugs, it was making sure there were no other bugs. Parsers catch syntax, not sense.

Questions we'd love to get:

  • How would you generate "realistic" broken code?
  • Which bug type would fool you the longest: ; vs ;, 0 vs O, or ) vs ]?

Ask anything about the generator below 👇