Heat the oil in a large Dutch oven over medium heat and add the onions. Stir to coat the onions with the oil. Season the onions with the salt, pepper, and sugar.
Cook, stirring occasionally with a wooden spoon, until the onions are golden brown and caramelized, about 45 minutes. Take your time with this step as this is what’s going to provide the rich flavor for your soup. If the onions start to burn or catch bottom, add a small amount of water and scrape up any brown bits from the bottom of the pot with a wooden spoon.
Once the onions are caramelized, add the garlic, thyme and bay leaf. Cook another minute or two and then stir in the flour. Cook, stirring constantly, another minute.
Turn the heat up to high and add the wine. Cook until almost all of the liquid has evaporated and the onions are jammy. Add the beef broth. Bring to a boil and reduce to a simmer. Simmer the soup, partially covered, 30 minutes. When the soup is done, add the cognac, if using. Taste and adjust seasoning with salt and pepper. Remove thyme sprigs and bay leaf.
While soup is cooking, preheat oven to 400°F.
Cut the bread into large cubes. Place them on a baking sheet and bake in the oven until lightly toasted, 7-8 minutes. Remove from oven and set aside.
Place 6 oven-safe soup bowls on a baking sheet ladle the soup into the bowls. Top each bowl with a handful of toasted bread cubes and sprinkle evenly with the grated cheese. Place the baking sheet in the oven under the broiler and broil until cheese is melted and bubbly around the edges, 3-4 minutes. Let the soup cool a few minutes before serving.
If you don't have oven-proof bowls, you can sprinkle the cheese on the toasted bread cubes and place it under the broiler to melt. Then, top the soup with the cheesy bread.