SAN DIEGO — An appeals court on Wednesday upheld a freeze on Pentagon money to build a border wall with Mexico, casting doubt on President Donald Trump's ability to make good on a signature campaign promise before the 2020 election.

A divided three-judge panel of the 9th U.S. Circuit Court of Appeals in San Francisco agreed with a lower court ruling that prevented the government from tapping Defense Department counterdrug money to build high-priority sections of wall in Arizona and New Mexico.

The decision is a setback for Trump's ambitious plans. He ended a 35-day government shutdown in February after Congress gave him far less than he wanted. He then declared a national emergency that the White House said would free billions of dollars from the Pentagon.

The case may still be considered, but the administration cannot build during the legal challenge.

A freeze imposed by U.S. District Judge Haywood Gilliam Jr. of Oakland in May prevented work on two Pentagon-funded wall contracts — one spanning 46 miles in New Mexico and another covering 5 miles in Yuma, Arizona.

While the order applied only to those first-in-line projects, Gilliam made clear that he felt the American Civil Liberties Union and other groups were likely to prevail at trial in their argument that the president was ignoring Congress' wishes by diverting Defense Department money.

"Congress's 'absolute' control over federal expenditures — even when that control may frustrate the desires of the Executive Branch regarding initiatives it views as important — is not a bug in our constitutional system. It is a feature of that system, and an essential one," the judge wrote.

RELATED: Judge bars Trump from using $2.5B to build border wall

RELATED: House approves emergency funding bill for migrant care crisis

Gilliam went a step further Friday by ruling definitively that the administration couldn't use Pentagon counterdrug money for the two projects covered in his May order or to replace 63 miles in the Border Patrol's Tucson, Arizona, sector and 15 miles in its El Centro, California, sector.

Trump immediately vowed to appeal.

At stake is billions of dollars that would allow Trump to make progress on a major 2016 campaign promise heading into his race for a second term.

Trump declared a national emergency after losing a fight with the Democratic-led House that led to the 35-day shutdown. Congress agreed to spend nearly $1.4 billion on barriers in Texas' Rio Grande Valley, the busiest corridor for illegal crossings, which was well below the $5.7 billion the president requested.

Trump grudgingly accepted the money but declared the emergency to siphon money from other government accounts, finding up to $8.1 billion for wall construction. The money includes $3.6 billion from military construction funds, $2.5 billion from Defense Department counterdrug activities and $600 million from the Treasury Department's asset forfeiture fund.

Acting Defense Secretary Mark Esper has yet to approve transferring the military construction funds. The Treasury Department funds have so far survived legal challenges.

The president's adversaries say the emergency declaration was an illegal attempt to ignore Congress. The ACLU sued on behalf of the Sierra Club and the Southern Border Communities Coalition.

The administration said the U.S. needed emergency protection to fight drug smuggling. Its arguments did not mention illegal immigration or unprecedented numbers of Central American families seeking asylum at the U.S. border, which have dominated public attention in recent months.

Justice Department attorneys argued that the freeze on Pentagon funds showed a "fundamental misunderstanding of the federal appropriations process."

"The real separation-of-powers concern is the district court's intrusion into the budgeting process," they wrote.

The two sides argued before a three-judge panel in San Francisco on June 20, made up of Barack Obama appointee Michelle Friedland and George W. Bush appointees N. Randy Smith and Richard Clifton.

The administration has awarded $2.8 billion in contracts for barriers covering 247 miles, with all but 17 miles of that to replace existing barriers not expand coverage. It is preparing for a flurry of construction that the president is already celebrating at campaign-style rallies.

Trump inherited barriers spanning 654 miles, or about one-third of the border with Mexico. Of the miles covered under Trump-awarded contracts, more than half is with Pentagon money.

The Army Corps of Engineers recently announced several large Pentagon-funded contacts.

SLSCO Ltd. of Galveston, Texas, won a $789 million award to replace the New Mexico barrier. Southwest Valley Constructors of Albuquerque, New Mexico, won a $646 million award for the work in Tucson. Barnard Construction Co. of Bozeman, Montana, won a $141.8 million contract to replace barrier in Yuma and El Centro.