The front compartment, call it the boot for ease, has a conventional latch controlled by a lever inside the car and a second latch which is a spring loaded hook.
The hook is meant to engage on a small platform inside the boot lid, thus preventing the lid flying up or opening until the second latch is pushed back.
On mine, and on the example in the BMW Museum and possibly many others, that latch catches on a crease/fold in the metalwork on the underside of the lid at which point the lid is not high enough up to easily push the latch back.
The real cause of this seems to be that the pivot hole wears, allowing the latch to adopt a forward slope, taking the end of the hook too far forward and thus fouling.
The real cure is to be replace he welded on bracket. Which is obviously a bit too much work. The next best thing would be to sleeve the pivot hole, but that is a bit tricky too.
An easier solution is to slide a length of 1/4 inch vacuum rubber hose over the hook and down to the bottom so it rests against the stop, thus keeping the hook far enough back to clear correctly.