This is the primary Talmudic source for Mashiach ben Yosef: the Gemara states that one of the four craftsmen of the end of days is Mashiach ben Yosef, and that he will be killed in the final war — an event mourned as described in Zechariah 12:10. The Shelah's discussion draws on this sugya.
הָא הֶסְפֵּידָא מַאי עֲבִידְתֵּיהּ? פְּלִיגִי בַּהּ רַבִּי דּוֹסָא וְרַבָּנַן. חַד אָמַר: עַל מָשִׁיחַ בֶּן יוֹסֵף שֶׁנֶּהֱרַג, וְחַד אָמַר: עַל יֵצֶר הָרַע שֶׁנֶּהֱרַג. בִּשְׁלָמָא לְמַאן דְּאָמַר עַל מָשִׁיחַ בֶּן יוֹסֵף שֶׁנֶּהֱרַג, הַיְינוּ דִּכְתִיב: ״וְהִבִּיטוּ אֵלַי אֵת אֲשֶׁר דָּקָרוּ וְסָפְדוּ עָלָיו כְּמִסְפֵּד עַל הַיָּחִיד״. אֶלָּא לְמַאן דְּאָמַר עַל יֵצֶר הָרַע שֶׁנֶּהֱרַג — הַאי הֶסְפֵּידָא בָּעֵי לְמֶעְבַּד? שִׂמְחָה בָּעֵי לְמֶעְבַּד! אַמַּאי בָּכוּ? תָּנוּ רַבָּנַן: מָשִׁיחַ בֶּן דָּוִד שֶׁעָתִיד לְהִגָּלוֹת בִּמְהֵרָה בְּיָמֵינוּ, אוֹמֵר לוֹ הַקָּדוֹשׁ בָּרוּךְ הוּא: שְׁאַל מִמֶּנִּי דָּבָר וְאֶתֵּן לָךְ, שֶׁנֶּאֱמַר: ״אֲסַפְּרָה אֶל חוֹק וְגוֹ׳ אֲנִי הַיּוֹם יְלִדְתִּיךָ. שְׁאַל מִמֶּנִּי וְאֶתְּנָה גוֹיִם נַחֲלָתֶךָ״. וְכֵיוָן שֶׁרָאָה מָשִׁיחַ בֶּן יוֹסֵף שֶׁנֶּהֱרַג, אוֹמֵר לְפָנָיו: רִבּוֹנוֹ שֶׁל עוֹלָם! אֵינִי מְבַקֵּשׁ מִמְּךָ אֶלָּא חַיִּים. אוֹמֵר לוֹ: חַיִּים, עַד שֶׁלֹּא אָמַרְתָּ כְּבָר הִתְנַבֵּא עָלֶיךָ דָּוִד אָבִיךָ, שֶׁנֶּאֱמַר: ״חַיִּים שָׁאַל מִמְּךָ נָתַתָּה לוֹ וְגוֹ׳״.
Apropos the eulogy at the end of days, the Gemara asks: For what is the nature of this eulogy? The Gemara answers: Rabbi Dosa and the Rabbis disagree concerning this matter. One said that this eulogy is for Messiah ben Yosef who was killed in the war of Gog from the land of Magog prior to the ultimate redemption with the coming of Messiah ben David. And one said that this eulogy is for the evil inclination that was killed. The Gemara asks: Granted, according to the one who said that the lament is for Messiah ben Yosef who was killed, this would be the meaning of that which is written in that context: “And they shall look unto Me because they have thrust him through; and they shall mourn for him, as one mourns for his only son” (Zechariah 12:10). However, according to the one who said that the eulogy is for the evil inclination that was killed, does one need to conduct a eulogy for this? On the contrary, one should conduct a celebration. Why, then, did they cry? The Sages taught: To Messiah ben David, who is destined to be revealed swiftly in our time, the Holy One, Blessed be He, says: Ask of Me anything and I will give you whatever you wish, as it is stated: “I will tell of the decree; the Lord said unto me: You are My son, this day have I begotten you, ask of Me, and I will give the nations for your inheritance, and the ends of the earth for your possession” (Psalms 2:7–8). Once the Messiah ben David saw Messiah ben Yosef, who was killed, he says to the Holy One, Blessed be He: Master of the Universe, I ask of you only life; that I will not suffer the same fate. The Holy One, Blessed be He, says to him: Life? Even before you stated this request, your father, David, already prophesied about you with regard to this matter precisely, as it is stated: “He asked life of You, You gave it to him; even length of days for ever and ever” (Psalms 21:5).