Congress cleared a sweeping $1.3 trillion spending bill early Friday and sent it to President Trump for his signature, staving off a government shutdown with less than 24 hours to spare.

Action by the Senate shortly before 1 a.m. capped a day of suspense, including the late-night revelation that the legislation had been stalled for hours partly because Sen. James E. Risch (R-Idaho) objected to the renaming of a federal wilderness area after a deceased political rival.

Sen. Rand Paul (R-Ky.) also held out against the bill for much of the day Thursday, voicing objections to what he viewed as unnecessary deficit spending while keeping colleagues in the dark about whether he would delay action on the legislation and force a brief government shutdown, as he did last month over an earlier spending deal.

Finally, after Senate Majority Leader Mitch McConnell (R-Ky.) intervened personally with Risch and Paul and the Senate passed a measure striking the provision Risch disliked, both men relented. The Senate passed the 2,232-page spending bill 65-32, about 12 hours after the House had also approved the legislation on a similarly wide bipartisan vote of 256-to-167.

“My principal responsibility is begging, pleading and cajoling,” McConnell ruefully remarked to colleagues on the Senate floor, shortly before the final vote was cast.

Late-night dramatics aside, the legislation sparked bitter disputes among Republicans in both chambers, on grounds both of policy and of process.

It abandons GOP claims of fiscal discipline in a stark reversal of the promises many Republicans ran on in capturing control of the House in 2010 and the Senate in 2014 as they railed against what they described as a profligate President Barack Obama. And in another about-face, GOP leaders tossed aside their own rules and past complaints about Democrats to rush the legislation through the House ahead of the Friday midnight government shutdown deadline. Lawmakers of both parties seethed, saying they had scant time to read the mammoth bill, which was released less than 17 hours before the House voted.

Nonetheless, leaders of both parties declared victory following the legislation’s passage, and at the White House, Office of Management and Budget Director Mick Mulvaney said Trump would sign it.

“Is it perfect? No. Is it exactly what we asked for in the budget? No. Were we ever going to get that? No. That’s not how the process works,” Mulvaney said. “This is what a bill looks like when you have 60 votes in the Senate and the Democrats get a chance to take their pound of flesh.”

The legislation funds the federal government for the remainder of the 2018 budget year, through Sept. 30, directing $700 billion toward the military and $591 billion to domestic agencies. The military spending is a $66 billion increase over the 2017 level, and the nondefense spending is $52 billion more than last year.

The spending bill is widely expected to be the last major legislation that Congress will pass before the November midterm elections, which had increased pressure to jam the bill full of odds and ends, with provisions addressing everything from gun safety to invasive carp.

Paul highlighted some of these measures in tweets throughout the day Thursday, and posted a picture of himself holding the hefty bill while glaring balefully at the camera. “Shame, shame. A pox on both Houses — and parties. Here’s the 2,232 page, $1.3 trillion, budget-busting Omnibus spending bill,” the caption read.

Paul was the focus of attention for hours after the House passed the legislation around mid-day Thursday, because the Senate’s rules grant a single senator significant latitude to hold up a vote and Paul had done so before. Given the short timeframe, he could have delayed the vote past the Saturday 12:01a.m. deadline and forced the third government shutdown of the year.

But finally, late Thursday night, Paul spoke on the phone with McConnell and subsequently relented, telling The Washington Post that despite his opposition he was prepared to allow votes to go forward. “I think if anything we’ve drawn more attention to our opposition to this bill by opposing it than we have in quite awhile, so I think it’s been successful from that point of view,” Paul said.

But just as Paul stood down, it emerged that he was not the only Republican senator objecting to the bill. Even as Paul was railing publicly against the legislation, Risch had been complaining behind the scenes, demanding that congressional leaders remove a provision in the bill naming the White Clouds Wilderness after former Democratic governor Cecil D. Andrus of Idaho, according to two congressional aides familiar with the dispute.

Risch and Andrus, who died last year, had clashed when both served in state government, with Andrus quoted in a 2006 story published by NewWest.net describing Risch as a “mean, snarly little guy.” The renaming provision passed the House in February and has the support of Rep. Mike Simpson (R-Idaho), chairman of a House Appropriations subcommittee.

Ultimately the Senate passed a stand-alone measure making the change Risch sought, although Senate Majority Whip John Cornyn (R-Texas) subsequently told reporters that the House was objecting to Risch’s change, meaning the renaming provision he objects to is likely to remain intact.

Nonetheless Risch relented and allowed the spending legislation to pass, refusing comment when senators approached him outside the Senate chamber.

“No. What part of ‘no’ don’t you understand? ... Do I have a problem with my English? I don’t have any comment,” Risch said.

The developments exasperated senators of both parties. “That was ridiculous,” Sen. Brian Schatz (D-Hawaii) remarked on Twitter after the final vote closed.

Throughout the maneuvering, the ultimate outcome was not in question: The legislation would pass, bringing budget increases to federal agencies large and small, from the National Institutes of Health to the National Park Service to the Election Assistance Commission.

“Sometimes you save the president from himself,” said Rep. Tom Cole (R-Okla.), arguing the administration would not want to be in the position of cutting something like the NIH budget if a new pandemic comes along. “Look, a new administration always runs on things, and may or may not know government intimately.”

Conservatives fumed at the generous increases for many agencies, with some arguing it undercut their party’s claims to fiscal restraint.

“The Democrats love this bill like the devil loves sin,” said Sen. John Neely Kennedy (R-La.). “I don’t understand why when President Obama does what we’re about to do, it’s bad for the country, but when we do it, it’s good for the country.”

Other Republicans, including House Speaker Paul D. Ryan (R-Wis.), argued the legislation fulfilled Trump’s governing agenda, including increasing military spending and funding a wall along the U.S.-Mexico border.

“This bill starts construction on the wall,” he told reporters. “It funds our war on opioids. It invests in infrastructure. It funds school safety and mental health. But what this bill is ultimately about, what we’ve fought for so long, is finally giving our military the tools and the resources it needs to do the job.”

House Minority Leader Nancy Pelosi (D-Calif.) called the bill “a tremendous victory for the American people,” one that keeps domestic agencies robustly funded while turning away Trump’s push for even more money for the border wall and immigration enforcement.

“If you want to think you’re getting a wall, just think it, and sign the bill,” she said.

The bill includes $1.6 billion in funding for construction of a border wall, but that number is far short of the $25 billion in long-term funding that the administration sought, and Democrats also won tight restrictions on how that money can be spent.

Despite weeks of negotiations, Democrats were unable to secure protections for young undocumented immigrants brought to the country as children who had been granted reprieves from deportation under an Obama-era directive. Trump announced in September he would end that program, known as Deferred Action for Childhood Arrivals, by March 5. That deadline was effectively nullified by recent court actions.

The legislation includes increased funding for priorities as diverse as arts agencies, the FBI, the IRS and federal apprenticeship programs. It greatly boosts funding to fight the opioid epidemic and orders the Army Corps of Engineers to keep working on trying to keep Asian carp, an invasive species, out of the Great Lakes.

Prodded by Trump, Republicans eliminated some provisions favoring the $30 billion Gateway project, a major New York-area infrastructure program backed by Senate Minority Leader Charles E. Schumer (D-N.Y.). The legislation also includes a fix for a provision in the new GOP tax law that favored grain cooperatives over traditional agricultural corporations. And it incorporates bipartisan legislation meant to improve the National Instant Criminal Background Check System (NICS) for gun buyers.

There were grumbles in all corners of Capitol Hill about the rapid process that has left lawmakers and aides poring through text to see exactly what the bill will do. House GOP leaders waived their own rules requiring any bill coming to the floor to be posted for at least three days, and none of more than a dozen lawmakers surveyed Thursday said they had read the entire bill.

“There’s no way humanly possible to read 2,232 pages,” said Rep. Thomas Massie (R-Ky.), who opposed the bill. “Sometimes they jam you, but they pretend to give you three days to read it. All the veneer is off now.”

Karoun Demirjian and John Wagner contributed to this report.