The answer by user Herr K. is very sensible and in fact is what MWG p. 481 footnote 6 suggest to do in order to obtain the f.o.c.
But this approach begs the question: Then why on earth did we use the integrals in the first place, only to abandon them for the discrete formulation?
If our problem is formulated in terms of continuous profits, then profits are a continuous random variable, and considering cases "at each level of $\pi$ separately" (as MWG write in their footnote), is not possible because there are uncountably infinite "levels of profit". MWG attempt to rectify this by writing in the same footnote
To be rigorous, we should add that when we have a continuum of possible
levels of $\pi$, an optimal compensation scheme need only satisfy the
f.o.c at a set of profit levels that is of full measure.
Now, one should tells us how we can obtain a "set of full measure" by including in it a finite number of points from a set that is uncountably infinite (the continuum, that is).
So, once more: why then formulate the problem in continuous terms, only to change the formulation to discrete in order to obtain the f.o.c? Why not formulate the problem in discrete terms from the beginning?
Moreover the description of the situation is
- $\pi$ is a random variable
- $w$ is a function of $\pi$
- We want to choose the optimal $w$
But 2. means that $w$ is a random variable, so the only meaning 3. may have is that what we are going to choose is $w$ as a function of $\pi$, not $w$ as a number. Because if we choose $w$ as a number, we essentially eliminate its dependence on the random variable $\pi$...
...but this is exactly what we can do in order to arrive at the f.o.c. So, treat $w$ as a decision variable independent of $\pi$. We want to
$$\min_w \int_{\pi_{min}}^{\pi_{max}} w f(\pi\mid e)d\pi\,-\,\gamma \int_{\pi_{min}}^{\pi_{max}} v(w) f(\pi\mid e)d\pi\,.$$
Take the derivative with respect to $w$ and set it equal to zero:
$$\int_{\pi_{min}}^{\pi_{max}} f(\pi\mid e)d\pi\,-\,\gamma \int_{\pi_{min}}^{\pi_{max}} v'(w) f(\pi\mid e)d\pi = 0.$$
Because we treat $w$ as a decision variable independent of $\pi$, we can take it out of the integral,
$$\int_{\pi_{min}}^{\pi_{max}} f(\pi\mid e)d\pi\,-\,\gamma v'(w)\int_{\pi_{min}}^{\pi_{max}} f(\pi\mid e)d\pi = 0.$$
Both integrals now equal unity, since $f(\pi\mid e)$ is a proper density over the specific domain, so we end up with
$$1\,-\,\gamma v'(w) = 0 \implies \gamma = \frac{1}{v'(w)},$$
...which is exactly the solution one can find in MWG p. 481. So this f.o.c corresponds also to describing an optimization problem where $w$ is presented initially as a function of $\pi$, and then to solving the problem by treating $w$ as not being a function of $\pi$.
To recapitulate:
We formulated a problem over the continuum, and where the decision
variable is a function of a random variable.
In order to arrive at the f.o.c we either
a) Abandon the continuum formulation and look at a discrete version or
b) Abandon the assumption that the decision variable is a function
of a random variable
This rather twisted situation deserves some contemplation from the side of any interested reader, and I will leave them to it. See also https://economics.stackexchange.com/a/231/61.