Hi
Thanks for great work!
So here are steps to repro:
new Query('Account').debug().countQuery();
throws:
Error on line 1587, column 1: System.QueryException: unexpected token: ')' Class.Query.countQuery: line 1587, column 1
After removing the debug() call it works fine:
new Query('Account').countQuery();
This is because debug() calls toQueryString() -> formQueryStringPreformat() -> formFieldString(), which does functionFieldList.isEmpty() too early and adds Id to field set.