I used to have this problem but I permanently dealt with it. The analogy for me is taking a hike. If you just leave your house without thinking, you are going to walk around a bit and come right back. This is like the dreaded 1-4 bar loop. But if you want to take a longer trip you need to plan out how to get back, or you will get lost. So I at least have some guideposts in mind about how I’m going to do that before even starting off. The longer the trip, the more you need to have these checkpoints.
Now I usually sketch out some really vague ideas about how the whole track is going to go before I even start programming at all. It never fails, because even if these ideas are really thin they lend a structure to build on.