Nazi Germany and the Soviet Union had 2 different ideologies. The Soviet Union had communism and the desire to spread it further in the world while Hitler hated communism and Nazism is about inferior vs superior peoples.
The Nazis thought Slavic people(and Jews) were subhuman and wanted to exterminate them and starve them to death and replace them with ethnic Germans where they lived. I don’t understand why Stalin would forge an alliance with Hitler when they had different ideologies and goals. I also read Stalin was shocked when Hitler decided to invade the Soviet Union and one of the reasons why the Nazis made such rapid gains is because it caught the Soviet Union completely by surprise and they had to mobilize troops.
How did Russia/USSR justify the Molotov Pact with Germany in WWII? written by u/kieslowskifan talks a bit about the "why" of signing