I feel like bayonets were useful obviously, but they were always short. Why weren’t there 20 inch sword blades attached to muskets?
Your standard musket of the period, regardless of the specific model and country of origin, was designed to take a socket bayonet. Socket bayonets of the era vary widely in dimensions, but generally they will be found with blade lengths between 13 and 18 inches. Using the French Mle 1766 and the British New Short Land Pattern Muskets as examples, those bayonets would be placed on guns with barrel lengths at roughly 44.5 inches (French), and 42 inches (English). Simply put, that is more than enough length to defend one's self against cavalry or to bring the "cold steel" to enemy infantrymen.
Sword bayonets didn't catch on en mass until just after the turn of the 19th century. They did see some use in the American Revolution in the hands of some of the German Jaegers, but their use was relatively minor in the grand scheme of things. Rifle Battalions were slow to arm themselves with saber bayonets because the open order fighting they commonly practiced did not lend its self to bayonet charges or even easy defense against cavalry.
Sword bayonets evolved as armies realized that you could make a dual purpose weapon that combined the traditional hanger/briquet short swords carried by rifle troops with a bayonet. However, the extra weight of a sword bayonet makes it very difficult to wield on a long barreled weapon, so their use was restricted to short rifles were they are more manageable. The ~24 inch blade of a sword bayonet coupled with a short 33 to 36 inch rifle barrel provides a comparable thrusting range to the longer barreled musket with a slightly shorter bayonet.