First Canada was left out of a re-worked free trade deal with the U.S. and Mexico.

And now the country’s Federal Court of Appeal on Thursday halted the contentious Trans Mountain pipeline expansion that would nearly triple the flow of oil from the Alberta oil sands to the Pacific Coast — the latest setback that comes just as the government is buying the project.

“The government is deeply invested in this and it is going to cost the government a fortune.”

The court ordered the country’s National Energy Board to redo its review of the pipeline, saying the original study was flawed and lacked adequate consultations with First Nations peoples.

Prime Minister Justin Trudeau‘s government approved the Trans Mountain pipeline in 2016 and was so determined to see it built that it announced plans this spring to buy the pipeline. The sale will be finalized as early as Friday.

It faces stiff environmental opposition from British Columbia’s provincial government and activists. Houston-based Kinder Morgan earlier halted essential spending on the project and said it would cancel it altogether if the national and provincial governments could not guarantee it.

In a written decision, the court said the energy board’s review was so flawed that the federal government could not rely on it to approve the pipeline. The court concluded the federal government failed in its duty to engage in meaningful consultations with First Nations before approving it.

“Meaningful consultation is not intended simply to allow indigenous peoples “to blow off steam,” the decision said.

The court decision is a blow to Trudeau, whose government is having a bad week after Canada was left out of new free trade deal with the U.S. and Mexico. Talks to include Canada are now taking place in Washington.

“Now the incompetent Trudeau gov’t owns a pipeline it can’t build,” opposition deputy Conservative leader Lisa Raitt tweeted.

Kinder Morgan shareholders voted overwhelmingly, 99 percent, to approve the $4.5 billion Canadian (US$3.4 billion) sale of the pipeline to the government shortly after the court decision was announced.

Canadian Finance Minister Bill Morneau didn’t say whether the government would appeal the court decision, but said it will review the decision to ensure the environment is protected and that it meets obligations to consult with indigenous peoples.

“While we want to make sure the project proceeds, we want to make sure it proceeds in the right way,” Morneau said.

He said he expects the Kinder Morgan sale to be finalized on Friday and called it a good financial decision. He said the pipeline is critically important as Canada looks to diversify its oil exports.

The court ruling “is an incredible blow, especially with the government on the hook for buying it,” said Nelson Wiseman, a political science professor at the University of Toronto.

“The government is deeply invested in this and it is going to cost the government a fortune.”

Trudeau approved the expansion, arguing that it was “economically necessary,” and it enabled him to overcome opposition to a carbon tax plan that will help Canada cut its greenhouse emissions.

Alberta Premier Rachel Notley said her province is now pulling out of Trudeau’s carbon tax plan until the pipeline is back on track. Notley said the federal government needs to appeal the court’s decision to the Supreme Court and call an emergency session of Parliament.

The pipeline would allow Canada to diversify oil markets and vastly increase exports to Asia, where it could command a higher price. Canada has the world’s third largest oil reserves, but 99 percent of its exports now go to refiners in the U.S., where limits on pipeline and refinery capacity mean Canadian oil sells at a discount.

Analysts have said China is eager to get access to Canada’s oil, but largely gave up hope that a pipeline to the Pacific Coast would be built.

The court decision is a victory for indigenous leaders and environmentalists, who have pledged to do whatever necessary to thwart the pipeline, including chaining themselves to construction equipment. The Trans Mountain expansion would cause tanker traffic to balloon from about 60 to more than 400 vessels annually as the pipeline flow increases from 300,000 to 890,000 barrels per day.

Washington state Gov. Jay Inslee, an opponent of the project, said he was pleased that the court found there are serious impacts that have not yet been full considered.

Many indigenous people see the 620 miles (1,000 kilometers) of new pipeline as a threat to their lands, echoing concerns raised by Native Americans about the Keystone XL project in the U.S. Many in Canada say it also raises broader environmental concerns by enabling increased development of the carbon-heavy oil sands.

“This is a major victory for my community,” said Coldwater Indian Band Chief Lee Spahan. “The court has stepped in where Canada has failed to protect and respect our rights and our water.”

Stewart Phillip, the grand chief of the Union of British Columbia Indian Chiefs, said the ruling took him aback. “I was expecting to be talking about the need to carry on the battle,” he said. “I am absolutely elated.”

The Associated Press contributed to this report.