Is there a risk of p-hacking when trying many models? I am familiar with the concept of p-hacking for statistical tests, but does p-hacking apply to model building? Say you are trying many different linear models with a variety of variables, interactions, splines, polynomials, etc. What about trying different types of models, like starting with a basic linear regression model and then trying a lasso regression?
If p-hacking does apply, how to go about mitigating it?

If you are trying out a variety of models for analysing the data then you are probably doing a preliminary study. There is no such thing as p-hacking in a preliminary study, but you will not be able to make any firm inferences until you have designed a secondary study of the interesting results and analysed new data. See this open access chapter for more details: https://link.springer.com/chapter/10.1007/164_2019_286#enumeration
On the other hand, if you are exploring a range of models in the hopes of finding a 'significant' result from one of them then you are greatly increasing the risk of false positive inferences exactly like "p-hacking".
