Thousands of protesters marched against it. Hundreds of human rights groups implored lawmakers to reject it. Even the United Nations warned of its dangers.
But on Friday, Mexico's Congress hastily approved the Law of Internal Security, which gives the military broad new powers and solidifies its central role in the country's drug war.
President Enrique Peña Nieto is expected to sign the legislation despite criticism that it could fuel more violence.
In many ways, the measure enshrines into law what has been happening in practice for more than a decade.
It was late 2006 when then-President Felipe Calderon launched Mexico's war on drugs by sending thousands of troops to his home state of Michoacan, where powerful cartels were battling for turf. Military officers were regarded as less corrupt than poorly trained local and state police forces, some of whom collaborated with criminal groups, and in the subsequent years many thousands more soldiers and marines were deployed across the country.
The strategy has continued under the current president. Military officers patrol streets, operate checkpoints and detain suspects. But all this time, the military hasn't had legal authority to carry out law enforcement inside Mexico's borders.
The new law formally allows the president to deploy federal troops for military operations inside Mexico without the approval of Congress. The deployments are for up to one year but can be extended as many times as necessary. The law also defines domestic law enforcement as a national defense issue, meaning information about military operations could be classified as secret.
Human rights advocates say that instead of solidifying the presence of armed forces on Mexican streets, lawmakers should focus on initiatives to strengthen and professionalize Mexico's civilian police forces.
"Soldiers should not be out patrolling city streets trying to investigate and prevent crimes," said Maureen Meyer of the think tank Washington Office on Latin America. "Those are tasks for well-trained, professionalized police forces and a functional justice system."
Protesters marching in several large demonstrations against the law in recent weeks have pointed to the military's role in rights violations, including documented cases of soldiers raping, torturing and carrying out execution-style killings. There are also questions over the army's failure to stop the disappearance of 43 students near a military base in 2014.
Between January 2012 and August 2016, there were 5,541 complaints of human rights violations against the armed forces registered with the National Human Rights Commission. Only about 6% of those complaints resulted in criminal trials.
José María Tapia Franc, a senator from the ruling Institutional Revolutionary Party, said he voted for the law "to give certainty to the armed forces."
"It is absolutely false that this law is the militarization of the country," he said in a radio interview Friday. "The focus is on supporting local forces."
He said he wondered what critics of the law would do "if overnight the armed forces returned to their barracks and left the population unprotected?"
But critics of the new law question whether it will actually be effective at reducing violence.
Eleven years into the drug war, Mexico is on track to record more homicides in 2017 than in any year since authorities began publishing statistics 20 years ago. Officials say 2,371 homicide investigations were opened in October, making it Mexico's most murderous month on record.
The explosion in violence comes in part as a result of the military's "kingpin strategy." One by one, powerful drug lords, including Joaquin "El Chapo" Guzman, have been taken into custody or killed. While some security analysts say federal forces helped stop Mexico from being completely overtaken by drug cartels, the strategy also unleashed a wave of bloodshed as would-be kingpins fought for control of the cartels.
"The military's role has proven to be catastrophic," said Alejandro Madrazo, a researcher at the Center for Economic Investigation and Teaching think tank. "It has only exacerbated the violence, and this new law will exacerbate it further."
The law drew criticism from three former special rapporteurs of the United Nations, along with the Conference of Mexican Bishops, the directors of several of Mexico's top research institutes, and other national and international groups.
A statement issued Thursday by the U.N. high commissioner for human rights warned that the law gives too much power to the military, and without the necessary civilian checks and balances.
"We are concerned that the bill gives the armed forces a leadership and coordination role in certain circumstances, rather than limiting their role to aiding and assisting civilian authorities," the statement said.
Jaime Cárdenas, a law professor at the National Autonomous University of Mexico, said it also gives too much power to the president. "The executive has no counterweights," he tweeted, calling the new law "a coup d'état."