I realize it can be interpreted in different lights and the ending takes on a more pious note but it’s hard to see how that doesn’t seem somewhat tacked on. With other biblical texts being taken out and declared non-canon over time, I just wonder how this survived.
Your question as a whole is difficult to answer, as it is a lot of work to nail down why a great many different people made the decisions that they did. Given that some early Christians (commonly referred to as Marcionites, after their founder Marcion) wanted to rejected the Old Testament entirely, some have ejected Ecclesiastes from their canon, though not for the reasons you described.
By way of a partial answer, let me say something about how early, medieval and early modern Christians read Ecclesiastes. By far, the most dominant reading of Ecclesiastes in the Middle Ages was as a defense of the monastic life. This might seem strange at first blush, but one of the core concepts of monasticism was a withdrawal from the world driven by a contempt for worldly things, referred to as the contemptus mundi. The idea was that the things and concerns of the world—everything from wealth and power to material security and well-being—tempted one to sin and distracted from one focusing on the truly important, spiritual things. As Ambrose of Milan, an important 4th century bishop theologian, wrote on the famous opening lines of Ecclesiastes:
"All things are vanity!" as Ecclesiastes said, all things that are in this world. Accordingly, let the person who wishes to be saved ascend above the world, let him seek the Word who is with God, let him flee from this world and depart from the earth. For a man cannot comprehend that which exists and exists always, unless he has first fled from here." Flight from the World 1.4.
In this way, what you identify as the text's nihilism was read by early Christians as a criticism of worldliness and an admonition not to seek after material things nor put their trust in human labors or endeavors but to seek after what is heavenly and place their trust solely in God. Consider how John Chrysostom, one of the most famous preachers of the early church fathers, applies this to material wealth:
"And what is the use of understanding, you will say, to the poor person? As might be expected you are ignorant; for neither does the blind person know what is the advantage of light. Listen to Solomon, saying, "As far as light excels darkness, so does wisdom excel folly." [Eccl. 2:13] But how shall we instruct him in darkness? For the love of money is darkness, permitting nothing that is to appear as it is, but otherwise. For much as one in darkness, though he should see a golden vessel, though a precious stone, though purple garments, supposes them to be nothing, for he does not see their beauty So also he that is covetous, knows not as he ought the beauty of those things that are worthy of our care. Disperse then I pray you the mist that arises from this passion, and then will you see the nature of things." John Chrysostom, Homilies on the Gospel of Matthew 83.3.
See how Chrysostom turns the text's lamentations to be about the contempt of wealth (a major theme of his preaching and theology) and how it obscures true sight. One of the reason many early church theologians held views like this and therefore found Ecclesiastes compelling was because of the prevalence of Platonism in the philosophy of the day. A very simplified but sufficient overview of Platonism would describe it as holding the spiritual to be of a higher and preceding order to the material. We can see this philosophy at work in how Origen of Alexandria, the most influential early church interpreter of Scripture, read Ecclesiastes 1:2:
"Let us see what is the 'vanity' to which the creation was subjected. My own opinion is, that this is nothing else than the possession of bodies, for even though teh stars are composde of ether they are nevertheless material. this, it seems to me, is teh reason why Solomon arraigns the whole bodily universe in a way burdensome and as impeding teh activity of spirits, thus: 'Vanity of vanities, all is vanity, said the Preacher; all is vanity.' 'For,' he adds, 'I looked and I saw all things that are under the sun, and behold, all is vanity." On First Principles 1.7.5.
So, as Origen reads Ecclesiastes, Solomon it basically a proto-Platonist, setting the whole bodily universe as an impediment to the spiritual.
In these ways, Ecclesiastes became a text which gave support to the central project of monasticism of withdrawing from the world, whether from marital and familial relationships through celibacy, wealth through vows of poverty, or broader social life through cloisters or hermitages.
The above readings of Ecclesiastes held sway until the 16th century when the Protestant Reformation took aim at the monastic life. There, we see Ecclesiastes reborn in a number of ways, such as as a form of proto-Stoicism or a mirror to Princes (that is, a book advising a prince on good governance). In the eyes of someone like Martin Luther, who wrote very critically of monasticism and its contempt of the world, he explained that:
"Solomon wants to put us at peace and to give us a quiet mind in the everyday affairs and business of this life, so that we live contentedly in the present without care and yearning about the future and are, as Paul says, without care and anxiety (Phil. 4:6). [...] The proper contempt of the world is not that of the man who lives in solitude away from human society, nor is the proper contempt of gold that of the man who throws it away or who abstains from money, as the Franciscans do, but that of the man who lives his life in the midst of these things and yet is not carried away by his affection for them." Notes on Ecclesiastes, Luther's Works 15:7, 9.
You'll notice that Luther takes the same concept of having contempt for the world but attempts to break it free from monastic practice. In his hands, the text becomes a guide to navigating the vicissitudes of life without becoming weighed down or consumed by them. He finds them useful for a prince, who will often find their best laid plans sundered by chance, or for the everyday person, who knows well enough that planning and worrying are guarantees of nothing. At once point, Luther talks about putting tons of work into a dinner party only for no one to enjoy themselves, whereas sometimes you don't try, and everyone has a smashing time. Not only events are unpredictable, but our emotions and affections are as well. Thus for Luther and other reformers, the texts become about our emotional state and dealing with the fickleness of the human psyche.
I hope these samples give you a hint at the creative ways Ecclesiastes has been read and how the elements of it which led you to question its place in religious canons were in fact their justification for its inclusion. If you'd like to read more and understand, below are some suggested sources.
J. Robert Wright, ed. Ancient Christian Commentaries on Scripture, Vol. IX: Proverbs, Ecclesiastes, Song of Solomon.
Eric S. Christianson. Ecclesiastes Through the Centuries. Malden, MA: Wiley-Blackwell, 2007.
Robert Rosin. Reformers, the Preacher and Skepticism: Luther, Brenz, Melanchthon and Ecclesiastes. Mainz: P. von Zabern, 1997.