Repository: fonttools/fonttools

Issue:
new Debg table breaks our font builds
A [commit](https://github.com/fonttools/fonttools/commit/2e112a464a649c330b12bad7e550cdfb80b4bdbb) in a recent [PR](https://github.com/fonttools/fonttools/pull/2052) broke many of my group's font builds. With the linked commit applied our tool, [`psfbuildfea`](https://github.com/silnrsi/pysilfont/blob/master/lib/silfont/scripts/psfbuildfea.py), outputs
```
  File "/home/ubuntu/.local/bin/psfbuildfea", line 11, in <module>
    load_entry_point('pysilfont', 'console_scripts', 'psfbuildfea')()
  File "/home/ubuntu/builds/pysilfont/lib/silfont/scripts/psfbuildfea.py", line 82, in cmd
    def cmd(): execute(None, doit, argspec)
  File "/home/ubuntu/builds/pysilfont/lib/silfont/core.py", line 631, in execute
    newfont = fn(args)
  File "/home/ubuntu/builds/pysilfont/lib/silfont/scripts/psfbuildfea.py", line 74, in doit
    builder.build()
  File "/home/ubuntu/builds/fonttools/Lib/fontTools/feaLib/builder.py", line 181, in build
    table = self.makeTable(tag)
  File "/home/ubuntu/builds/fonttools/Lib/fontTools/feaLib/builder.py", line 701, in makeTable
    self.lookup_locations[tag][ix][2] = key
KeyError: 0
```
Once the PR was merged the output is
```
  File "/home/ubuntu/.local/bin/psfbuildfea", line 11, in <module>
    load_entry_point('pysilfont', 'console_scripts', 'psfbuildfea')()
  File "/home/ubuntu/builds/pysilfont/lib/silfont/scripts/psfbuildfea.py", line 82, in cmd
    def cmd(): execute(None, doit, argspec)
  File "/home/ubuntu/builds/pysilfont/lib/silfont/core.py", line 631, in execute
    newfont = fn(args)
  File "/home/ubuntu/builds/pysilfont/lib/silfont/scripts/psfbuildfea.py", line 74, in doit
    builder.build()
  File "/home/ubuntu/builds/fonttools/Lib/fontTools/feaLib/builder.py", line 188, in build
    table = self.makeTable(tag)
  File "/home/ubuntu/builds/fonttools/Lib/fontTools/feaLib/builder.py", line 710, in makeTable
    self.lookup_locations[tag][str(ix)] = self.lookup_locations[tag][
KeyError: '0'
```
I don't know if the bug is in this commit, or if this commit expose a previously latent bug in `psfbuildfea`.


Discussion:
Pinging @simoncozens.
OK, I've reproduced this. Working on it now. 
thank you simon!
OK, I see the problem. `psfbuildfea` has subclassed the builder and provided its own implementation of `buildLookups_`. However, fontTools now stashes debugging information during `buildLookups_` so that it can be referenced later. `psfbuildfea`'s implementation of `buildLookups_` doesn't stash this debugging information, and so you get a KeyError when we try to find it later in `makeTable`.

This is life, unfortunately - override private methods, and you get end up getting blatted when the superclass implementation changes. Ideally you should add the debug-info-stashing code to your subclass, by putting this into `psfbuildfea` after line 42:

```
            self.lookup_locations[tag][str(bldr.lookup_index)] = LookupDebugInfo(
                location=str(bldr.location),
                name=self.get_lookup_name_(bldr),
                feature=None,
            )
```

I will also prepare a PR for fontTools to guard that the key is present during `makeTable`. 

Resolving patch:
diff --git a/Lib/fontTools/feaLib/builder.py b/Lib/fontTools/feaLib/builder.py
--- a/Lib/fontTools/feaLib/builder.py
+++ b/Lib/fontTools/feaLib/builder.py
@@ -30,6 +30,7 @@
 from collections import defaultdict
 import itertools
 import logging
+import warnings
 
 
 log = logging.getLogger(__name__)
@@ -707,9 +708,13 @@ def makeTable(self, tag):
                 continue
 
             for ix in lookup_indices:
-                self.lookup_locations[tag][str(ix)] = self.lookup_locations[tag][
-                    str(ix)
-                ]._replace(feature=key)
+                try:
+                    self.lookup_locations[tag][str(ix)] = self.lookup_locations[tag][
+                        str(ix)
+                    ]._replace(feature=key)
+                except KeyError:
+                    warnings.warn("feaLib.Builder subclass needs upgrading to "
+                        "stash debug information. See fonttools#2065.")
 
             feature_key = (feature_tag, lookup_indices)
             feature_index = feature_indices.get(feature_key)


Test patch: