With the risk of grossing everyone out, it could be that they were already there. Industry standards allow for a certain amount of insect eggs, parts and excrement in food articles like flour, cornmeal and the like. You also have the added issue of contamination during transport.
Foodstuffs should be rotated on a regular basis, particularly if one is planning on long-term storage. Any eggs will hatch, and the rotation allows you to weed it out of your supplies.
Out of curiosity, has this happened with a single type and brand of food item, or does it happen in everything?
I’ve noticeed it happening with stored flour and nuts. I’ve always assumed that the crafty insects wiggled through the seals as if their life depended on it. I never considered the possibilty that they were there already.
I used to buy those little Jiffy brand corn bread mixes at 3 for a buck. One time I opened the sealed wax paper bag and found a bunch of tiny worms crawing around. I never could eat the stuff again.