The Supreme Court has rejected Donald Trump's effort to end legal protections for 650,000 young immigrants.
The ruling means the young immigrants, known as Dreamers, retain their protection from deportation and their authorisation to work in the US.
The 5-4 outcome, in which Chief Justice John Roberts and the four liberal justices were in the majority, seems certain to elevate the issue in Mr Trump's campaign, given the anti-immigrant rhetoric of his first presidential run in 2016 and immigration restrictions his administration has imposed.
The justices rejected administration arguments that the eight-year-old Deferred Action for Childhood Arrivals Programme is illegal and that courts have no role to play in reviewing the decision to end DACA.
The programme covers people who have been in the United States since they were children and are in the country illegally. In some cases, they have no memory of any home other than the US.
Trump did not hold back in his assessment of the court's work, hitting hard at a political angle.
"These horrible & politically charged decisions coming out of the Supreme Court are shotgun blasts into the face of people that are proud to call themselves Republicans or Conservatives. We need more Justices or we will lose our 2nd Amendment & everything else. Vote Trump 2020!" he wrote on Twitter.
These horrible & politically charged decisions coming out of the Supreme Court are shotgun blasts into the face of people that are proud to call themselves Republicans or Conservatives. We need more Justices or we will lose our 2nd. Amendment & everything else. Vote Trump 2020!
— Donald J. Trump (@realDonaldTrump) June 18, 2020
In a second tweet, he wrote, "Do you get the impression that the Supreme Court doesn't like me?"
Mr Roberts wrote for the court that the administration did not pursue the end of the programme properly.
"We do not decide whether DACA or its rescission are sound policies," Mr Roberts wrote. "We address only whether the agency complied with the procedural requirement that it provide a reasoned explanation for its action. Here the agency failed to consider the conspicuous issues of whether to retain forbearance and what if anything to do about the hardship to DACA recipients."
The Department of Homeland Security can try again, he wrote. But any new order to end the programme, and the legal challenge it would provoke, would likely take months, if not longer.
"No way that's going to happen before November," said Stephen Yale-Loehr, a professor of immigration law practice at Cornell University Law School.
The court's four conservative justices dissented.
Justice Clarence Thomas, in a dissent joined by Justices Samuel Alito and Neil Gorsuch, wrote that DACA was illegal from the moment it was created under the Obama administration in 2012. Mr Thomas called the ruling "an effort to avoid a politically controversial but legally correct decision".
Mr Alito wrote that federal judges had prevented DACA from being ended "during an entire Presidential term. Our constitutional system is not supposed to work that way".
Justice Brett Kavanaugh wrote in a separate dissent that he was satisfied that the administration acted appropriately in trying to end the programme.
DACA recipients were elated by the ruling.
"We'll keep living our lives in the meantime," said Cesar Espinosa, who leads the Houston immigration advocacy group FIEL. "We're going to continue to work, continue to advocate."
Mr Espinosa said he got little sleep overnight in anticipation of a possible decision on Thursday. In the minutes since the decision was posted, he said his group was "flooded with calls with Dreamers, happy, with that hope that they're going to at least be in this country for a while longer".
From the Senate floor, the Democratic leader Chuck Schumer said of the DACA decision: "I cried tears of joy."
"Wow," he went on, choking up. "These kids, these families, I feel for them, and I think all of America does."