A DRACONIAN Texas law mandating local law enforcement cooperation with federal immigration authorities, even to the extent of preventing police officers from speaking out against policies they disliked, was set take effect Friday. But after a federal judge ruled against the state in a lawsuit backed by several Texas cities, the statute has — rightly — been put on hold.

Much of Senate Bill 4, or SB 4, doesn't require law enforcement to cooperate with federal agents of Immigration and Customs Enforcement (ICE) to identify and deport undocumented immigrants. Instead, it largely declares that police departments can't set policies constraining that cooperation. But U.S. District Judge Orlando Garcia still held that SB 4's provisions compelling local police departments to allow officials to help ICE, even to assist in enforcing federal immigration law, were likely unconstitutional.

Mr. Garcia also ruled against a provision of SB 4 that would have prevented law enforcement from "endorsing" policies "materially limit[ing] the enforcement of immigration law" — phrasing so broad that it could curtail the free-speech rights of local officials seeking to voice their views of immigration enforcement, even through a wink. And most significantly, the judge halted SB 4's requirement that police departments cooperate with federal requests to detain undocumented immigrants on behalf of ICE. Mr. Garcia made a persuasive case that this practice, which has come into legal question nationwide, violates detainees' constitutional rights against unreasonable confinement.

Texas will still be able to roll out provisions of the law requiring police departments to allow officers to ask about the immigration status of a person under arrest or detention and to share that information with ICE. But Mr. Garcia's ruling is a blow to Texas and to the Justice Department, which backed the state's case. Following another judge's finding of a Texas voter registration law to be racially discriminatory, it's the second recent case in which the state, even with the support of the Justice Department, has lost a bid to enforce harsh measures against its minority population.

Texas has already appealed Mr. Garcia's decision. It's hard not to view the ongoing battle in context of President Trump's executive order withholding federal grants from sanctuary cities, itself halted in part by a federal judge. Yet this comparison can be taken too far. Mr. Trump's order only vaguely defines what constitutes a sanctuary city and seems to limit itself to pushing for information sharing with ICE, rather than mandating local cooperation with ICE detention requests.

Nevertheless, as litigation over both SB 4 and Mr. Trump's executive order goes forward, the Trump administration would be wise to take Mr. Garcia's ruling as a reminder of the constitutional limits on harsh immigration policies. Cities that take a reasonable approach to cooperating with federal agents — allowing deportations of dangerous criminals, for example — seek to enhance public safety by building trust between civilians and law enforcement. The Justice Department should work alongside these localities, not penalize them.