They can: https://freemanlaw.com/immunity/
If you invoke your Fifth Amendment right not to self-incriminate you can be served with Statutory Immunity and then forced to testify, with contempt charges if you fail to testify.
There are also several different types of voluntary immunity, which is what you're describing. Those can't be compelled because they may not prevent the government from using your testimony against you in other venues, statutory immunity does not.