I have noticed that multiple non-socialist countries reference socialism in their constitution including Portugal, India and Sri Lanka. Why is this?
Propably because a sufficiently large power bloc within these countries was pushing for socialism at the time of creation of their constitutions. Portugal, for example, was prevented from declaring a Peoples Republic by NATO outright sending warships to the capital during their revolution.
Reactionaries then took over in these states
India & Sri Lanka were both in the Non-Aligned Movement and had good ties with USSR/Eastern Bloc. India was the first country where a Communist Party was elected in a bourgeois democracy system. So, Communists had significant influence on the state even if the state itself was aligned mainly with the bourgeoisie. Communism/Socialism wasn’t considered a no-no word in these countries unlike the West.
and also see this article on the resistance Israel faced trying to build ties with India
Socialism in popular consciousness is not always seen as the transitory phase towards communism. Sometimes it is conflated with the existence of a welfare state of some variety. I.e. when the government does stuff.
The socialism in Indian constitution comes mainly from how Nehru and his peers understood it. He was educated in England and hung out with utopian Fabian socialists.