President Trump listens to guests including Commerce Secretary Wilbur Ross, left, before signing the Women’s Global Development and Prosperity Initiative in the Oval Office in Washington on Feb. 7, 2019. (Bill O'Leary/The Washington Post)

Spurred on by President Trump, government lawyers scrambled Thursday to find a legal path to add a controversial citizenship question to the 2020 Census, despite their conclusions in recent days that no such avenue exists.

Census officials and lawyers at the Justice and Commerce departments scrapped holiday plans and spent their Independence Day seeking new legal rationales for a citizenship question that critics say could lead to a steep undercount of immigrants, which could limit federal funding to some communities and skew congressional redistricting to favor Republicans.

A federal judge in Maryland overseeing one of three lawsuits on the citizenship question has given the Trump administration until 2 p.m. Friday to explain how it intends to proceed. The government has begun printing the census forms without the question, and that process will continue, administration officials said.

The question had seemed settled after the Supreme Court ruled last week against the Trump administration. As late as Tuesday evening, Commerce Secretary Wilbur Ross, who oversees the census, said the administration was dropping its effort and was printing the census forms without the citizenship question.

But Trump, in tweets Wednesday and Thursday, said he was not giving up. He tweeted Thursday morning: “So important for our Country that the very simple and basic ‘Are you a Citizen of the United States?’ question be allowed to be asked in the 2020 Census. Department of Commerce and the Department of Justice are working very hard on this, even on the 4th of July!”

The reversal came after Trump talked by phone with conservative allies who urged him not to give up the fight, according to a senior White House official and a Trump adviser, who both spoke on the condition of anonymity.

Trump was furious and thought the Commerce Department and the Justice Department — which has been arguing the case — had given up the fight too easily. He complained about Chief Justice John G. Roberts Jr.,who he said he thinks is lined up against him, the adviser and senior officials said. Trump also complained about Ross.

Before Trump’s tweets plunged their week into chaos, Justice officials thought the president understood how few legal options remained, according to people familiar with the matter. They had earlier told the White House that the case was a dead-end and that pursuing it would be a waste of time.

Those people said that Attorney General William P. Barr had talked to Trump and had tried to explain his limited options after the Supreme Court’s ruling.

After the Supreme Court called the government’s reason for the question “contrived,” many wondered how the government could suddenly come up with a new rationale.

“What were they going to say?” said Dale Ho, director of the Voting Rights Project at the American Civil Liberties Union and a lead attorney for plaintiffs in the New York lawsuit. “ ‘Here’s our real reason? Or here’s a new reason?’ Well, that’s kind of reverse engineering on a decision that’s already been made, which was the very definition of pretextual. . . . We had them in an inevitable checkmate.”

But since Trump’s tweet, Justice Department lawyers have been working furiously to find a way to appease him and devise a legal maneuver that would allow the question to be added with the Supreme Court’s blessing. As they deliberated options Thursday, the lawyers were pessimistic about their chances, according to people familiar with the matter, who spoke on the condition of anonymity to describe internal deliberations.

Not all Republicans agreed with Trump’s decision to plunge back into the fight. And some moderate Republicans and Trump advisers characterized it as a waste of time. “The census is part of the legacy of our Founding Fathers. There’s no reason to weaponize it in a country that was, is and will be composed of immigrants,” said Dan Eberhart, a prominent GOP donor.

But conservative legal figures — including Leonard Leo, head of the Federalist Society — have been especially vocal in urging Trump not to stop fighting for the citizenship question, according to advisers close to Trump.

Two top Republicans familiar with Trump’s discussions said the president continued to grouse to aides and friends Thursday in calls and conversations about his deep frustrations with Ross about the census issue.

Trump made it clear to several advisers Thursday that addressing the matter should be the administration’s priority in the coming days, saying he does not like the confusion that has gripped the administration. According to the two Republicans, the president has told aides repeatedly, “Fix this.”

Trump has talked of issuing an executive order to the Commerce Department to try to forge a new legal avenue for the citizenship question, having picked up that idea from conservative allies, according to a senior administration official.

In a Washington Post column on June 29, Hugh Hewitt urged Barr to “prepare an executive order for the president’s signature directing the commerce secretary to add any such questions, and be prepared to defend the questions on the grounds discussed in the meeting. Nothing more is required.”

Reached by phone, Hewitt argued that Justice and Commerce officials gave up too easily. “At a minimum, you could print two sets of census documents. The census does not fall apart because we go past July 5,” he said.

An executive order could give the government some leeway for argument, said Josh Blackman, a constitutional law professor at South Texas College of Law.

In the Supreme Court’s splintered ruling last week, Roberts said the government had provided a “contrived” reason for wanting the information, seemingly leaving open the door for the government to offer a new justification and see whether it satisfies the court. An executive order from Trump and a new rationale given by Ross on the basis of that order could give the administration something to take back to the justices.

A rule exists that would allow the government to file a motion for the Supreme Court to reconsider within 25 days of its ruling, Blackman said. “It’s un­or­tho­dox, but in theory, you could bypass having to fight it all over again in the lower courts and go right back to the Supreme Court,” he said.

For months, however, census officials have argued in court that not resolving the question by July 1 would impair their ability to administer the census.

With the printing of forms underway this week, each passing day of production makes a change of course more difficult and costly.

A change is not impossible, however, said John Thompson, a former Census Bureau director. “They can push it back pretty far, even by months. It’s a matter of how much it will cost,” he said.

The administration was considering some kind of addendum that could be printed separately, according to an administration official familiar with the discussions, although such a move would almost certainly carry additional costs, and the feasibility of the idea was not immediately clear.

Lawyers for plaintiffs in the New York and Maryland cases, several of whom had left for vacation after the Supreme Court ruling, also scrambled Thursday to prepare for the Trump administration’s new arguments.

Ho had been so relieved by the administration’s announcement Tuesday giving up the legal battle that the following day he went out with his wife for the day and purposely left his phone behind at their Montreal hotel. Then came Trump’s tweets, blowing up the best-laid plans of all involved in the 15-month court battle.

“It was kind of surreal,” Ho said.

Shankar Duraiswamy, one of the lead plaintiffs’ attorneys in the Maryland litigation, was vacationing with his family in Florida when he heard of the turnaround during a call with U.S. District Judge George J. Hazel and the Justice Department lawyers.

“The mystery to me is when did they receive that instruction . . . and who gave those instructions?” he said.

Duraiswamy said his team will seek to confer with the government’s lawyers Friday to find out whether they can agree to a stipulated order.

He added that he does not see what a path forward would look like for the government. “That may not be clear to the president or Secretary Ross, or they may not care, and so what we are seeing perhaps is tension between maybe the whims of the president and certain political appointees and the legal and practical realities,” he said.

As the wrangling continues, Hazel is poised to authorize litigants to start producing information in a case that could stop the question on different grounds from what the Supreme Court heard. New evidence suggests that a GOP political operative viewed the question as a way to help Republicans and white voters, and that operative was in touch with administration officials. If administration lawyers do not drop their pursuit of the question by Friday, Hazel is likely to allow that case to proceed.

Blackman, the constitutional law professor, said that like others, he was surprised to be working Thursday, trying to parse the Trump administration’s last-minute legal maneuvers as others fired up grills and prepared to watch fireworks. “In some ways though,” he said, “there’s nothing more patriotic than fighting over the Constitution on July Fourth.”

Robert Costa contributed to this report.