Suppose we have $$ x^{2} = (-x)^{2}.$$
I understand that this equation holds because $$\begin{aligned} (-x)^{2} & = (-1\cdot x)^{2} \\ & = (-1)^{2} \cdot x^{2} \\ & = x^{2}, \end{aligned}$$ and so $\sqrt{x^{2}} = \sqrt{x^{2}}$ becomes $x = x$ and everything is fine.
But what if we just take the square root first, like so: $$\sqrt{x^{2}} = \sqrt{(-x)^{2}}.$$ Wouldn't this gives $x = -x$, which is a contradiction?