But in the campaign to be the city’s next district attorney, candidates from the start attacked the raid — in which the police were investigating the leak of a police report about the mysterious death of Jeff Adachi, the city’s elected public defender — as a violation of the city’s principles, at a time when freedom of the press is under assault at the national level. (The current district attorney, George Gascon, in his first statement on the matter, said on Sunday, “I can’t imagine a situation in which a search warrant would be appropriate” to target a journalist.)

At a forum last week hosted by the Latino Democratic Club, each of the four candidates weighed in, offering condemnations that differed sharply from the tone of some city leaders.

Chesa Boudin, a public defender in the race, said, “A free press is a fourth branch of government and it’s under attack at the national level, and it cannot be and will not be under attack at the local level when I’m district attorney.”

Invoking President Trump’s attacks on a free press, Suzy Loftus, a former prosecutor in the race, said, “San Francisco is right to have a lot of questions about whether or not that was what was necessary.”

Leif Dautch, another candidate, who works in the office of the attorney general, said he was “extremely disturbed” by the raid, and said that “journalists play an incredibly important role in our society.” The fourth candidate, Nancy Tung, an assistant district attorney in Alameda County, said, “I think one of the most important rights we have is a right to a free press.” But Ms. Tung, who is running to the right of the other candidates, also said there were things in the search warrant, which is under seal, that “we are not privy to.”