Work done on a gas paradox 
The work done by a gas on a piston, by definition:
$$W_{gas} = \int \vec{F} \cdot \vec{dr}$$
Where $\vec{F}$ is the net force on the piston, $\vec{dr}$ is the infinitesimal displacement of this piston.
Since the force on the piston, is against the direction of displacement (for my coordinate system), F and dr are in opposite directions hence the work done on the gas
$$W_{gas} = \int -|F||dr|$$
Assuming that I move my piston in the purely x direction
$$W_{gas} = \int -|F|dx$$
I then move my piston from its initial position relative to my coordinates, to a distance x, thus the work done by the gas on the piston is
$$W_{gas} = \int_{0}^{x} -|F|dx$$
Therefore, the negative of this quantity is the work done by the piston on the gas (work done on the gas):
$$W = \int_{0}^{x} |F|dx$$
Let's forget about converting this into a pressure volume integral for simplicity
From here I am having trouble reconciling the standard formula for the work done on a gas.
$$W= -\int_{v_{i}}^{v_{f}} \rho dv$$
This converted into linear form is
$$W=-\int_{l_{1
I}}^{l_{f}} |F| dx$$
Let's take a look at the different formulas.
$$W = \int_{0}^{x} |F|dx$$
$$W=-\int_{l_{i}}^{l_{f}} |F| dx$$
One is integrating from the pistons initial position, to the pistons final position.
One is integrating from the remaining length when the piston is at its starting position, to the remaining length when the piston has extended.
(Much like the volume form is the remaining  volume, and the remaining volume after the piston extends)
From first principles, the one that I derived, only matches the given formula  given that $\rho$( and therefore F) is constant.
$$\int_{0}^{x} |F|dx = -\int_{l_{1
I}}^{l_{f}} |F| dx$$
$$|F|x - |F|0 = |F|(l_{i} -l_{f})$$
From the diagram $l_{i} -l_{f} = x$
$$|F|x = |F|x$$
The formulas differ when F is not constant.
Which formula is correct? Have i made any mistakes?
 A: The general expression for work:
$$W=\int_{xi}^{xf}\vec F\cdot \vec {dx}\tag{1}$$
Since the force $\vec F$ is in the same direction as $\vec {dx}$ (i.e., $\theta=0$), the work is simply $\int Fdx$. Then, for a piston of cross section area $A$ and the pressure $P=F/A$. A differential change in volume is $dV=Adx$, or $dx=dV/A$. Substituting these into equation (1)
$$W=\int_{vi}^{vf}(PA)(dV/A)=\int_{vi}^{vf}PdV$$
Work done by the system (gas)is negative if the final volume is less than the initial (compression work) and positive if the final volume is greater than the initial (expansion work).
Hope this helps.
A: 
The formulas differ when F is not constant.

Specifically, they differ when $F$ is a function of $x$. Otherwise, as you note, $F$ can be brought outside the integral and the difference $l_{i}-l_{f}$ replaced with the identical difference $x-0$.
And of course we do expect the resistance $F$ to depend on the compression $x$ when we compress a material. The apparent paradox arises because you've used $F$ to refer to two different functions: $F(x,0)$ and $F(l_{i},l_{f})$. They aren't the same because you've measured $l$ increasing leftward from the right end of the container and $x$ moving rightward from the left end of the container. These inputs to $F$ differ by an offset and by a sign, which makes the integrals appear to differ as well.
If you formulate $F$ appropriately for one of these coordinate systems (say, $F(l)=\frac{nRT}{L-l_i+l_f}$, using the ideal gas law, where $L$ is the total container length), then you can always apply a coordinate transformation to $F$ and to the limits of integration to work in another coordinate system, obtaining identical results for the work done.
A: I think that your confusion is down to your choice of variables.
Let the system be the gas inside the cylinder/piston.
Rather than $L_{\rm f}$ I would like to introduce the variable $\ell$ such that $x=L_{\rm i} - \ell$.
If one differentiates this expression one gets $dx = -d\ell$.
The change of volume of the gas is $d\ell \,A = -dx\,A$ where $A$ is the area of the piston.
Note that $dx\,A$ is the change in volume outside the system.
Let $\hat \ell$ be the unit vector pointing to the left in your diagram and displacement $\vec \ell$ be measured from the right-hand end of the cylinder.
The work done by the gas undergoing a change from $\ell =L_{\rm i}$ to $\ell = L_{\rm f}$ is $\displaystyle \int^{L_{\rm f}}_{L_{\rm i}}\vec F_{\rm gas}\cdot d\vec \ell = \int^{L_{\rm f}}_{L_{\rm i}}F_{\rm gas}\,\hat \ell\cdot d\ell \,\hat\ell = \int^{L_{\rm f}}_{L_{\rm i}}F_{\rm gas}\,d\ell$.
With a change of variable, $x=L_{\rm i} - \ell$, work done by gas becomes $\displaystyle -\int^{x_{\rm f}}_0F_{\rm gas}\,dx$.
Another possible change of variable is $V= A\,\ell \Rightarrow dV = A\,d\ell$ and the work done by the gas becomes $\displaystyle \int^{V_{\rm f}}_{V_{\rm i}}P\,dV$
