The Short Answer
The word "evil" in context is best translated "injustice," although "evil" is a fine translation.
Here are some variations in versions:
- ESV,NASB + others - "evil"
- NET - "injustice"
- ISV - "troublesome"
- HCSB - "wrong"
Contextual Analysis
Some features to notice:
- The passage describes more than one outcome,
- the opinion of the author develops over the course of the passage.
- The condition in verse 26 is the reverse of that in verse 21
v 18,19 (ESV)
I hated all my toil in which I toil under the sun, seeing that I must
leave it to the man who will come after me, and who knows whether he
will be wise or a fool? Yet he will be master of all for which I
toiled and used my wisdom under the sun. This also is vanity.
Note the phrase "who knows whether he will be wise or a fool?" Because the speaker does not know if his work will be inherited by someone wise or foolish, he hated his toil. In the worst case, a fool would gain the toil accomplished by the wise. This is an injustice. Apparently, this does not extend to the case in which someone wise inherited the work of the wise (no injustice in this case-- an outcome to be hoped for).
v 20,21 (ESV)
So I turned about and gave my heart up to despair over all the toil of
my labors under the sun, because sometimes a person who has toiled
with wisdom and knowledge and skill must leave everything to be
enjoyed by someone who did not toil for it. This also is vanity and a
great evil.
This vanity is expounded upon: The toil in wisdom would be inherited by someone who did not work for it (the fool mentioned in verse 19). The contrast is between wise and foolish. This is in keeping with the preceding verses (12-17) in which the same things can happen to the wise or foolish, which itself is vanity. The flavor so far, is something like "it's all a waste! there is no justice!" (or as your children might say, "life is so unfair!")
22,23 (ESV)
What has a man from all the toil and striving of heart with which he
toils beneath the sun? For all his days are full of sorrow, and his
work is a vexation. Even in the night his heart does not rest. This
also is vanity.
The argument continues: So what point is then, since work is hard? If fools can inherit what you do in wisdom, what is the point of doing all that work? It's a waste!
24,25 (ESV)
There is nothing better for a person than that he should eat and drink
and find enjoyment in his toil. This also, I saw, is from the hand of
God, for apart from him who can eat or who can have enjoyment?
Here now, is an answer to the question, "what is the point?" Namely, to enjoy our work and eat and drink as a result of it. This is "from the hand of God." There is nothing beyond this we can expect. This is the only scenario which is not pronounced a vanity. It is not a waste to work wisely and enjoy it. We see that in verse 18 the speaker "hated all his toil," but now he enjoys it. He has found the proper motivation for his work. It's not about what will happen later, but about what is happening now.
26 (ESV)
For to the one who pleases him God has given wisdom and knowledge and
joy, but to the sinner he has given the business of gathering and
collecting, only to give to one who pleases God. This also is vanity
and a striving after wind.
And the conclusion is driven further. The wise worker has joy in his work because wisdom and joy are from God, toward those who please him.
This is in contrast to the fool. Here we have a second case in which someone works and another inherits, but the roles are reversed. In this case, the wise inherits from the fool! This is done because the wise one pleases God (and therefore this is not a great evil-- in fact, it is fitting that it should happen this way).
Why is it a vanity then? It is vanity (futile, pointless) for the fool. At least in wisdom, one's work is joyful and it provides. But for the fool, all is a waste! He has no joy in his work, and ultimately everything he works for will go to the wise.
In Conclusion
The two end conditions are actually in contrast with each other, so there is no conflict calling one condition evil. It is exact opposite of the one that God brings to pass:
wise toil / inherited by fool --> great injustice
foolish toil / given to wise --> given by God
Also, one is before the "revelation" in 24,25 ("This also, I saw, is from God..."), the second is after this revelation, and therefore change in viewpoint.
Final Note:
The logic of the passage is much like Pascal's wager, which is fitting considering "folly" is associated with unbelief (Psalm 14:1).