Toss the aubergines with the salt, then tip into a colander set over a bowl for 30 mins, to extract any moisture.
Heat 2 tbsp of the oil in a casserole dish or saucepan. Tip in the onion and a pinch of salt, and cook over a low heat for 10 mins. Add the garlic and chilli, and cook for 1 min. Tip in the tomatoes and sugar, and bring to the simmer. Cook, uncovered, for 20 mins, stirring occasionally. Heat the oven to 220C/200C fan/gas 7.
Pat the aubergine dry with kitchen paper, then toss with the remaining oil. Spread out on a baking sheet and roast for 25-30 mins until softened and golden around the edges. Meanwhile, cook the pasta following pack instructions, reserving 100ml of the cooking water.
Stir the aubergine into the sauce with the basil and capers. Season to taste, then toss the spaghetti in the pan with the sauce and a splash of the pasta water. Divide between four bowls and top with the ricotta and extra basil, if using.