Caramelize the onions
Melt butter in a heavy pot over medium-low heat. Add sliced onions and salt. Cook slowly, stirring often, until onions soften and begin to turn golden. This will take about 30–40 minutes. Sprinkle in sugar if using and continue cooking until onions are deep brown and jam-like.
Build the flavor
Add garlic and cook for 30 seconds until fragrant. Pour in white wine and scrape the bottom of the pot. Let it simmer until mostly reduced.
Simmer the soup
Add warm beef stock, bay leaves, thyme, and black pepper. Bring to a gentle simmer. Cook uncovered for 20–25 minutes to allow flavors to blend. Remove herbs and bay leaves. Stir in sherry vinegar if using. Adjust seasoning.
Prepare the bread
Toast baguette slices with a little butter or olive oil until crisp and golden.
Assemble
Ladle soup into oven-safe bowls. Place toasted bread on top and cover generously with grated cheese.
Bake & broil
Place bowls under the broiler until cheese melts, bubbles, and turns lightly golden. Let cool slightly before serving.