Read Stalin's Marxism and the National Question. https://www.marxists.org/reference/archive/stalin/works/1913/03a.htm
Nationalism derives from the bourgeoisie and intellectual middle class that develops as a result of capitalism, but national oppression can make it a cause of the working class. Not all "nationalism" is the same and must be evaluated dialectically on a case by case basis