Four years ago, Justin Trudeau promised his country “sunny ways”. This time there was no euphoria; simply relief, as the Liberals lost the popular vote but hung on as a minority government, just ahead of their Conservative rivals. It was a victory, but no triumph. This election was Mr Trudeau’s to lose – and he almost did.

He rose to power as a young, charismatic idealist, cloaked in the aura of his father Pierre, the long-serving prime minister. He appointed a gender-balanced and racially diverse cabinet. While his Conservative predecessor pulled the country out of the Kyoto protocol, he pledged decisive climate action. He welcomed tens of thousands of refugees as others shut their doors. The economy has boomed. His record looked even better when Donald Trump entered the White House.

The shine started to come off in 2018 as an official trip to India revived suggestions that he was a lightweight, and the government’s purchase of a pipeline angered those who had cheered his carbon tax scheme. But most Canadian governments manage a second term, and at the start of this year Mr Trudeau still looked unbeatable. Then came the SNC-Lavalin scandal. Even after Canada’s ethics watchdog ruled that Mr Trudeau violated laws by urging his attorney general – the first Indigenous person in the role – to halt the prosecution of an engineering company in a conflict of interest case, he shamefully refused to apologise. Then, weeks before the election, multiple images emerged of him in blackface. Both cases highlighted questions about his record on race and Indigenous issues.

His good luck was to face a charmless and uninspiring opposition under a socially conservative leader, Andrew Scheer, who struggled to even formulate his position on abortion and same-sex marriage. Mr Scheer’s vow to scrap the carbon tax cemented Conservative support in the western oil-producing regions, but alienated voters in suburban Ontario who have kept the Liberals in power. Many of the Liberals’ lost seats were taken not by the Conservatives but the Bloc Québécois.

The defeat of the Conservatives is good news, not only for Canada but for a world that needs a liberal counterbalance to a rightwing surge, and needs governments that will take action on the climate catastrophe, even if it falls short. The ignominious performance of the far-right People’s Party of Canada, which campaigned against “mass immigration” and ended up with no seats at all, is extremely welcome. Meanwhile, Canada has plenty of experience of minority governments, and their considerable achievements include universal healthcare. The Liberals’ most likely ally is the leftwing New Democratic party, despite its loss of 15 seats. There was little humility or introspection in Mr Trudeau’s speech on election night. But the Liberal win will be all the better if it is accompanied by increased scrutiny of the prime minister and a greater determination to hold him to account.