Therein lies the problem. Since grants are appropriations, Congress needs to specify that cooperation with immigration officials is a requirement in exchange for the money.
You are correct - Congress needs to specify that cooperation with immigration officials is required for exchange for the money. Mandating a local government unit will do certain things w/o that provision is flawed. States are not sub units of the federal government, except when the judiciary has found it necessary to advance and impose the left’s agenda and programs (e.g. environmental, abortion, homo-marriage, Obamacare, etc). I would welcome a return to the federal government being one of limited, enumerated powers, with any change of said powers be done through the amendment process. Unfortunately that won’t happen. That ship has sailed, and in large part to what began during FDR tenure.
There is no Wickard v Filburn type of decision out there with regards to immigration. The real solution is for the do-nothing GOP to pass a law explicitly denying Sanctuary cities funding of all kinds. But of course they won’t; many still believe that Trump is wrong on illegal immigration and the broken legal immigration system.