Repository: fonttools/fonttools

Issue:
[feaLib.builder] Irrelevant "Must save GDEF when compiling a variable font" error when adding GSUB-only to VF
feaLib.builder insists on building GDEF, even if GPOS is not requested, for example if I only want to add GSUB to a VF.

I think the `elif self.varstorebuilder:` line below should read `elif "GPOS" in tables and self.varstorebuilder:`

https://github.com/fonttools/fonttools/blob/4139d8914abe788cc396dcb6ccc7c9cdb365ee89/Lib/fontTools/feaLib/builder.py#L227-L234

See also https://github.com/fonttools/fonttools/discussions/2690#discussioncomment-3131706


Discussion:
Even then it's possible that the GPOS not have any variations...
Maybe this would be more useful behavior:

```python
    gdef = self.buildGDEF():  # unconditionally
    if "GDEF" in tables:
        if gdef:
            self.font["GDEF"] = gdef
        elif "GDEF" in self.font: 
            del self.font["GDEF"]
    elif gdef:
        log.warning("GDEF is not requested, but is needed")
```
But why build if explicitly not requested?
The idea of this test is to prevent mistakes of this form:
- use variable .fea for GPOS
- request GPOS, but not GDEF
- without GDEF the variable GPOS won't work, so should error (or at least warn)

The current test is too strict, and one way (the easiest way?) to find out whether GDEF is in fact needed (so we can error or warn if it is not requested) is to simply build it.

If there's a different way to do this that avoids a pointless error as well as addresses Behdad's concern, we can consider of course.
I see, ok.

Regarding your proposed change in https://github.com/fonttools/fonttools/issues/2694#issuecomment-1183534216, should you not check both `if gdef and self.varstorebuilder`, rather than just `elif gdef:`?

```python
    elif gdef and self.varstorebuilder:
        log.warning("GDEF is not requested, but is needed when compiling variable font")
```
actually nevermind ignore my last comment, your suggested code is correct already. Please send a PR, thanks!
Ok, thanks!
Reopening, as now there still can be a needless warning, via 

https://github.com/googlefonts/ufo2ft/blob/036cb54e44e7cbdd1889d83c5f1bf02dee51a842/Lib/ufo2ft/util.py#L230

I *think* it is possible that the featureFile in that call contains .fea code that would produce GDEF, but we're not interested in that at all there. So a warning about GDEF not being requested is out of place. Will try to come up with a fix.

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
@@ -224,14 +224,12 @@ def build(self, tables=None, debug=False):
                 del self.font[tag]
         if any(tag in self.font for tag in ("GPOS", "GSUB")) and "OS/2" in self.font:
             self.font["OS/2"].usMaxContext = maxCtxFont(self.font)
-        gdef = self.buildGDEF()  # build unconditionally so we can warn below
         if "GDEF" in tables:
+            gdef = self.buildGDEF()
             if gdef:
                 self.font["GDEF"] = gdef
             elif "GDEF" in self.font:
                 del self.font["GDEF"]
-        elif gdef:
-            log.warning("GDEF is not requested, but is needed")
         if "BASE" in tables:
             base = self.buildBASE()
             if base:


Test patch: