 Congratulations, you have finally created your very own custom.NET template. But when you try to get it to work properly, turns out it's not working. So what do you do? So on this final episode of our mini-series on.NET templates, we're going to learn how you can best troubleshoot and diagnose those issues. So come on down for this episode of Visual Studio Toolbox. Hey everyone, welcome back to another episode of Visual Studio Toolbox. I'm your host, Leslie Richardson, and I'm joined again by senior PM, Saed Hashimi, long time no see. Yeah, hello. I'm proud of you since I've seen you. I know it's been a whole like two minutes. So last time we discussed creating the template and then adding parameters to it. So now that we have that up and running, chances are if you did it, you being the audience member probably didn't work great the first time. So we're going to talk about ways to troubleshoot those issues, right? Yeah, that's right. So we have made the process pretty straightforward for creating templates, but there's still a lot of work that we need to do, especially on the logging front when things went bad. So what I'll do is I'll go through some things that template authors can use and some pointers and what to avoid and what to check for certain cases and those sorts of things, and then go from there. Yeah. Great. I'm actually very excited about this one because who doesn't like troubleshoot tips? All right. Let's go ahead and dive right into it. So everything that we're going to cover here is captured in the readme for my samples repository and another pointer to get to it is aka.ms-netcore-templates. All right. So what I've done is I've created a template analyzer tool here and I've got the command for users for how to go ahead and get that one installed and I've already got that installed and this tool is actually open source too. So if anybody's interested in taking a look at it, they can do that. So we'll talk about this tool, how to use it to let me open up my demo project here. That's got the template that we created in the last one. Then what we'll do is we'll make some changes to this. Let's just get rid of the framework symbol there. Let's go ahead and I'm going to rebuild this package here to show people what might happen here. So I've got my reset templates command here and also it's a reminder there. If you're interested in that reset template command, I've got a PowerShell file here that's got the definition of that. You can just drop that in your PowerShell prompt and then call that function or even put it in your PowerShell profile that's up to you. So we're going to reset the templates, create the Nougat package again and then install it again. I'll restart Visual Studio at that same time too. So what I'll do is what I'm expecting here is I should see my template, but when I get to the additional information page, my author name parameter is likely to not show up. I actually does show up here because we forgot the parameter here for framework. But in either case, it's still very important to add that framework parameter here so that way users know what framework they're getting here. All right. Let me go and let's take a look at where we're at. Let me go into the content folder here. Now let's take a look at the templates command here. So it's templates analyzed.h. So this will give us the help for this. So if you have a Nougat package, you can pass the path to the Nougat package or if you have a folder, you can just pass the path to the folder and that's what we'll use here. So I'll say, you know, I want you to analyze my console 01. Okay. So now we're getting some errors and warnings or essentially just warnings here. So we're saying, hey, there's no description. And then you're also missing the framework symbol. But let me go back in the Visual Studio and we will introduce a couple of additional errors here to just double check this guy as well. Let me go into my template.json file and let's just say I kind of misspelled one of these guys here or two of these. So let's go back there and on our little template analyzer once again. All right. So we get the error that says, you know, source name is not found and we talked about the source name last time. And that's a very important one because that's how all the replacements happen. So I'll fix that. And then for my default name, you know, that's a warning saying, hey, default name wasn't found. You might want to consider adding that. Let's go ahead and add that. And then let's go ahead and add our description here as well. So description, demo, console template. And then it's telling us about framework again. So let me go ahead and re-add the framework property there, the framework object there. Let's go ahead and save that. Let me run it one more time. All right. So no issues found. So now we're good to go. But let me show you some other things that this will look for. So let's say if I didn't have my IDE.host.json file, it should warn me about that saying there's no host file found. Go ahead and do that. So those are some kind of things that it picks up on. And if there are any errors in the host file, it should pick up on those as well, I believe. Let's rename that to something else. Try to run it one more time. Okay. So yeah, I guess I didn't pick up that one. So yeah, I still got some kind of work to do in this little validator here, but it should have checked for that. And maybe by the time that users watch this video, I might have actually gone through and fixed that. All right. So now we've pretty much fixed everything up here and we're good to go. So what we can do is just kind of go through that process one more time of resetting the templates, creating the NuGet package, and then installing it again. All right. So that analyzer seems very powerful. And the fact that it even exists at all, that can save a lot of people a lot of time in trial and error. Yeah, no, it is pretty cool. And even I use it for my own kind of templates because sometimes I'll forget something here or there. So yeah, it's definitely pretty helpful. And I've also even discovered issues with kind of built-in templates here. So that's really great. And in the read me, it does kind of cover what are all the different things that it looks for here. I forget where they're at. Oh yeah, it's over here somewhere, like required properties and stuff. So yeah, there's some info here in the read me as well for what is it that this tool is looking for but does some additional things on top of this as well. All right, so we've got that going. So now what I wanted to do was, why don't we talk about, what could possibly go wrong here during this process, right? So let's get rid of this. Let me reset the templates. So now let me go ahead and explain, what is reset templates doing here? So let me, I will actually just go ahead and reinstall the Nougat package. I'm gonna reinstall this template here. And I'm gonna show you where these things are going into. So with the template engine, there's a cache here. So if I go to local app data, there's a cache that the template engine uses or actually, no, it's in the users. It's just right in users folder here actually. Yeah, so it's in the user directory dot template engine. So here we can see it's empty because I just reset my, I called that reset templates and it just deletes everything from here. We go ahead and install this. So now that I've installed it with dot net new, I can see I've got this dot net new CLI folder here. So whenever you install something with dot net new, they will appear as a sub folder here. So it'll be under packages. So I can see I've got my sayedemo.com 01 Nougat package there. All right, so now let me go ahead and fire up Visual Studio and we'll keep an eye on this folder as I go into create new project. So now we see a new folder appearing here that's named VS. And then here, there'll be some long string of the name of Visual Studio or whatever. And then it's essentially a copy of that folder structure there. So I can see packages here. So one thing that could happen is, let's say if you're developing your, let's say if you're developing your project templates locally and you're trying it in Visual Studio. So let's say during that process, if let's say you made a change but you're not seeing it in Visual Studio, odds are this file has not been updated. And this can happen for a number of different reasons, right? Like for example, if the name and version of the Nougat package are the same, in a lot of cases it will believe that that file is up to date because the Nougat package is like, it's supposed to be unmodifiable, right? So when I publish Nougat package to nougat.org, I shouldn't be able to modify that anymore. But that's an unfortunate artifact of what we're seeing here because here, I'm developing this locally. So obviously I'm gonna be changing the contents of it. So we're kind of breaking that rule, right? So that's why I have reset templates, right? So now let me close out of Visual Studio here and go back up. So now when I do reset templates and we can actually just look at the, is it, we can just look at the, let's go take a look at what reset templates does because it does a number of things here. Sorry. So it does a number of things here. So basically what I do is I'll take a look at that template engine directory and just delete all the folders that are in it. That's one thing. And then after that, it calls .NET new dash dash debug re-init. So that's another way to send a command to .NET new that says, hey, get rid of all customizations and take me back to what the experience was when I installed it. So that's what it does. So yeah, if you run into a problem where, you're not seeing your changes in Visual Studio, odds are you didn't call reset templates and maybe that new get package is kind of sitting there somewhere that's been cached. So that's definitely a problem. So we've got that. And then another problem here is, and I think this is really kind of an artifact of how .NET new works, but every kind of new version of the SDK, so let's say with .NET Core 3.1 versus .NET 5, right? So with those two different versions, .NET new maintains a separate list of templates there. So let's say, for example, I've got .NET new 3.1 installed and I don't have anything else installed. When I do .NET new install and I install a template, that's gonna install it into the 3.1 hive as we call it in the template engine world, right? So then let's say after some time passes, you install .NET 5. Now the .NET 5 version is not gonna have those templates that you have installed with 3.1, so you have to reinstall those. So now one thing that could happen, let's say you're working on a template and you create your template and you call .NET new dash dash install and then you go to use it and you're just not seeing it. So what could happen is, it could be controlled by global.json. So with global.json is a file that you can put into a folder. Maybe we can actually just demonstrate this real quick, actually. So let me show you how this works. I'm gonna go into a different, see what folder am I in. Okay, that's fine. So I will just go into a different, let me go up one. All right, if I do .NET version, I'm gonna get this 5.0 preview, right? Let me see what versions of the SDK I have installed here. Okay, so I just have these two versions, but I can demo with this. All right, so now with .NET new, I can actually create a global.json file. So I can say .NET new global.json and I can do .h to figure out what are the parameters there. So I can say .sdk-version and I'll put this guy here. So now we saw when I ran the version command initially, I got this preview version of it, right? So now if I add this, now I can see I've got that global.json. If I re-execute that same exact command, I'm gonna get the 5.0.0. So let me install .NET new install. All right, let me install this package here. Yeah, so now I can see I've got my say at console one, right? So now if I go back, if I go back up to .NET version, I'm back in the preview. So if I do .NET new dash L, you know, I'd never reset the templates. Let me just reset the templates one more time and then we'll do it again. Go back into the demo two and install it one more time. All right, so now I can see my say at console one. This is with 5.0.0. Let's do it again. I do .NET new dash L. Here, I'm expecting not to see that. So I don't see my say at console, right? And then if I was to do version here, we'll see that it's in the preview version. So this is a real big kind of gotcha here, basically. So when you install a package, and then when you use it, it has to be in the same kind of SDK version of .NET new. So let's say you install a package and it's just not showing up. You know, make sure that wherever you install that package that there is not a global.json file that's restricting the SDK here. So that's kind of a big problem there. And you know, this is something that I believe that the .NET new team will be addressing in the future. But yeah, this can definitely happen to template users. And also, you know, one thing that could happen is, you know, let's say you've been using .NET Core 3.1 for a while and you've got your templates installed and you update Visual Studio and all of a sudden you don't see those templates. This is the reason why. So you have to reinstall those templates. So yeah, those are some kind of gotchas there, but you know, I would definitely love for template authors to use that tool that I've created there. And the way all the information for that tool is in that template sample repository. So you just install it with .NET with this command here and then you just use it kind of how we should it. So yeah, I'd love people to try that out and then give me, you know, additional pointers for how we can further test these templates and additional kind of best practices there. Yeah. Thanks. Yeah, that definitely seems like some extremely useful stuff that would be absolute time savers for so many people. Yeah, the global JSON one, man, that one can be really frustrating, you know. Yeah. If you don't know it, it's super hard to figure out. And then, you know, you could end up in a situation where let's say you clone it to a different computer and then it just works on the other computer and not on this one, that could be very frustrating. And you know, I know a few people who have kind of fallen into that pit, yeah. Yeah, it doesn't sound fun. Cool. So what's up next? So next I think, you know, I'm not really sure at this point what we can do next here. I think we've got a pretty good set of videos here. So what I would actually be interested in is, you know, we already kind of showed how to use .NET new. We showed how to create a basic template. We showed how to use that in Visual Studio itself and we talked about some gotchas. You know, I'd love to hear from the community, you know, what are some additional videos that we can create around the templates and then we can kind of add to this video series in the future if that's all right, you know? Yeah, this whole mini series I think has been really cool and I hope anyone who's ever been interested in creating a template or just using a custom template takes advantage of the many easy to use tools and resources that are available for it. So thank you so much, Sayed, for sharing all that information and definitely come back if you have more developments in the template space. Oh, for sure, thank you very much. Sweet. So until next time, happy coding.