Consider a problem of minimizing $f(x)$ under the constraint that $g(x)=0$. The standard approach is to use Lagrange multiplier $$\mathcal L = f(x) -\lambda g(x)$$ and differentiate $\mathcal L$ to get $$f'(x) - \lambda g'(x) = 0.$$ Now, $g(x) =0$ is equivalent to $g^2(x) = 0$. So it seems we can consider the latter constraint instead, obtaining $$\mathcal L = f(x) -\lambda g^2(x)$$ and then $$f'(x) - 2\lambda g(x) g'(x)= 0.$$ But we know that $g(x)=0$ so this can be simplified to $$f'(x)=0$$ ...which does not make any sense: the constrained solution obviously does not need to satisfy $f'(x)=0$.
Where is the mistake in this reasoning?