Speaker of the House Paul Ryan, R-Wisconsin, emerges from the chamber just after key conservatives in the rebellious House Freedom Caucus helped to kill passage of the farm bill which had been a priority for GOP leaders, at the Capitol in Washington, Friday, May 18, 2018. The 213-198 vote is an embarrassing blow to House Republican leaders, who had hoped to tout its new work requirements for recipients of food stamps. Credit: J. Scott Applewhite | AP

WASHINGTON — A sweeping farm bill failed in the House on Friday in a major embarrassment to GOP leaders who were unable to placate conservative lawmakers demanding commitments on immigration.

Leadership put the bill on the floor gambling it would pass despite unanimous Democratic opposition. They negotiated with members of the conservative House Freedom Caucus up to the last minutes.

But their gamble failed. The vote was 198-213, with 30 Republicans joining 183 Democrats in defeating bill.

“I’m glad this partisan legislation did not pass,” U.S. Rep. Chellie Pingree, a Democrat who represents Maine’s 1st District, said in a statement. “It would have taken food assistance away from millions of Americans, weakened environmental stewardship of the nation’s farmlands, and left Maine farmers and rural communities ill-prepared for the challenges and the opportunities of the next five years.”

Rep. Bruce Poliquin, a Republican who recommended some of its provisions, voted for the bill.

“Congressman Poliquin is going to continue to fight for Maine’s wild blueberries and common sense welfare reforms,” his spokesman said in a prepared statement after the vote.

The outcome exposed what is becoming an all-out war within the House GOP over immigration, a divisive fight the Republicans did not want to have heading into midterm elections in November that will decide control of Congress.

The bill’s collapse also revealed the intractable divisions within the GOP conference that have bedeviled House Speaker Paul Ryan, R-Wisconsin, and will be certain to dog the top lieutenants in line to replace him, Majority Leader Kevin McCarthy, R-California, and Majority Whip Steve Scalise, R-Louisiana.

With moderate Republicans maneuvering to force a vote on legislation offering citizenship to some younger immigrants who arrived in the country as children, conservatives revolted. The farm bill became a bargaining chip as they lobbied leadership for a vote on a hard-line immigration bill.

Leaders tried to come up with a compromise, but a series of 11th-hour negotiations, offers and counteroffers failed. Both McCarthy and Scalise will take their share of the blame for the failure, and their fortunes in the race to replace Ryan next year could suffer accordingly.

The farm bill itself became practically a sideshow, despite its importance to agriculture and the significant changes it would institute to food stamp programs.

On immigration, Scalise described a deal that would ensure a vote on a conservative immigration bill by Reps. Bob Goodlatte, R-Virginia, and Michael McCaul, R-Texas — while also allowing moderate Republicans the opportunity to negotiate on legislation that could win the support of President Donald Trump and resolve the status of immigrants who face losing protections offered by the Deferred Action for Childhood Arrivals program, or DACA.

“We came to an agreement that I think gives everybody what they want,” Scalise said ahead of the farm bill vote. “That’s a vote on Goodlatte-McCaul as well as an opportunity to try to work with the president on an alternative that can pass on DACA. We want to solve the DACA problem and secure the border and I still think there’s a path to get there working with the president.”

The solution may eventually emerge, but it didn’t do so in time to save the farm bill Friday.

Goodlatte-McCaul bill authorizes construction of a border wall, cracks down on “sanctuary cities” that protect immigrants against federal immigration authorities, and provides for three-year temporary guest work permits that don’t offer a chance at citizenship. Leaders and conservatives alike agree that it doesn’t command the votes to pass the House, but nonetheless conservatives want to vote on it.

The farm bill itself broke open partisan divisions in the House as Democrats abandoned negotiations with Republicans over the food stamp changes, which would require adults to spend 20 hours per week either working or participating in a state-run training program as a condition to receive benefits. Democrats argue that a million or more people would end up losing benefits as a result because most states don’t have the capacity to set up the training programs required.

House Democratic Leader Nancy Pelosi, D-California, described the legislation as “cruel” and argued that with the proposed changes to food stamps, “Republicans are taking food out of the mouths of families struggling to make ends meet.”

Republicans contend the food stamp changes are a reasonable approach that would help move able-bodied adults from poverty to work. “Our bill goes shoulder-to-shoulder with recipients to help get them the training and education they need to attain a job that can provide for them and their families,” said Agriculture Committee Chairman Michael Conaway, R-Texas.

The House farm bill would have been a non-starter in the Senate anyway, which is writing its own farm bill. Any legislation that ultimately makes it to President Trump’s desk will have to look more like the Senate version, where bipartisan support will be necessary for anything to pass and there is not sufficient support for the food stamp changes.

Trump had tweeted his support for the House bill late Thursday, writing: “Tomorrow, the House will vote on a strong Farm Bill, which includes work requirements. We must support our Nation’s great farmers!” So the president, too, shares in Friday’s failure.

The current farm bill expires Sept. 30 and the legislation would have reauthorized numerous programs and policies. In addition to the food stamps, flash points included an extension of supports for the sugar program, which a coalition of conservative lawmakers backed by free-market outside groups tried unsuccessfully to get rid of in an amendment defeated Thursday.

The legislation also would have extended the Agriculture Department’s subsidy program that compensates farmers when average crop prices fall below certain levels – and expanded by widening who counts as a “farmer,” for subsidy purposes.

Conaway pleaded for the legislation before the vote. “Times are not good right now in the heartland. Many of our nation’s farmers and ranchers, who have been struggling under the weight of a five-year recession, are just one bad year away from being forced out of business,” he said. “And in the face of these serious challenges, the last thing they need is the uncertainty of a prolonged debate over the 2018 farm bill.”

The Washington Post’s Caitlin Dewey contributed to this report.