Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Updated the Blazor Hybrid Solution template to work better with the dotnet cli or IDEs that rely on it. #25997

Open
wants to merge 3 commits into
base: main
Choose a base branch
from

Conversation

jbw716
Copy link

@jbw716 jbw716 commented Nov 20, 2024

Description of Change

Updated the Blazor Hybrid Solution template to better support using the dotnet cli or IDEs that rely on it, like VSCode and Rider.

Issues Fixed

Excluding the .sln file doesn't make sense, since other IDEs can use the .sln file as well. If it's not needed, it can be deleted, much like other templates do. Not having it in the first place makes life significantly more difficult for users of this template on non Visual Studio environments.

Fixes #

Updated the template to remove the conditionals that exclude the .sln file for dotnetcli.

@jbw716 jbw716 requested a review from a team as a code owner November 20, 2024 19:31
@dotnet-policy-service dotnet-policy-service bot added the community ✨ Community Contribution label Nov 20, 2024
@rmarinho
Copy link
Member

/azp run

Copy link

Azure Pipelines successfully started running 1 pipeline(s).

@rmarinho rmarinho added area-blazor Blazor Hybrid / Desktop, BlazorWebView area-templates Project templates, Item Templates for Blazor and MAUI labels Nov 20, 2024
@rmarinho rmarinho requested a review from Eilon November 20, 2024 23:47
Copy link
Member

@mattleibow mattleibow left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I am not sure that this is what you expect. The condition says to not create a sln in an IDE, because the IDE is already creating a sln - that is the only way for it to have multiple projects loaded at once.

Comment on lines 17 to 22
{
"condition": "(HostIdentifier != \"dotnetcli\" && HostIdentifier != \"dotnetcli-preview\")",
"path": "MauiApp.1.Shared/Pages/Home.razor"
},
{
"condition": "(HostIdentifier == \"dotnetcli\" || HostIdentifier == \"dotnetcli-preview\")",
"path": "MauiApp.1.sln"
},
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This is just telling VS that it should open these files. If you do this, the CLI will now have an error saying it can't open files.

Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Totally fair here. I am still relatively new to templates but trying to get this one to work for non-VS users.

Comment on lines 107 to 104
{
"condition": "(HostIdentifier != \"dotnetcli\" && HostIdentifier != \"dotnetcli-preview\")",
"exclude": [
"*.sln"
]
}
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

We want to exclude the sln creation in VS because the IDE will have issues as it creates its own sln already.

Here is the ASP.NET templates doing just this: https://github.com/dotnet/aspnetcore/blob/main/src/ProjectTemplates/Web.ProjectTemplates/content/BlazorWeb-CSharp/.template.config/template.json#L116-L119

VSCode uses the CLI, so they will get the sln. Rider should be relying on the sln in the template because they will have their own as well. I would strongly suggest opening an issue on Rider's side if they are not creating a sln for a multi-project template.

Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Should this not, then, check if VS and if so, exclude the sln? The template at the link you provided also does not provide a sln file, however, since it is only a single project template, it's not nearly as big of an issue.

Copy link
Member

@mattleibow mattleibow left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Please open an issue in the IDE that is not creating a sln as that is an IDE bug, not a behaviour that should be happening.

@jbw716
Copy link
Author

jbw716 commented Nov 21, 2024

I updated the PR with a new suggestion and better clarification around the issue.

@Eilon
Copy link
Member

Eilon commented Nov 22, 2024

Pinging @vijayrkn and @phenning to understand if this is logically the right change. @mattleibow is indeed correct with the intention of the logic: VS already creates SLNs automatically, so if we create one via the template then the world is less happy.

But then this same exact condition is copied in probably dozens of templates across multiple repos. I've never seen a check for vs before, so I want to make sure it's logically the right thing to do. (It seems logical, but I don't know the template infrastructure and behavior as well.)

Is there an example of a scenario that doesn't work?

@jbw716
Copy link
Author

jbw716 commented Nov 22, 2024

@Eilon I actually grabbed the idea for the straight vs check from the end of line 41 in this same file.

@jbw716
Copy link
Author

jbw716 commented Nov 22, 2024

Apologies in advance for the verbosity of this comment, but I just want to make sure it's understandable.

I did mention Rider in the PR notes, however, the bigger issue, in my opinion, is the lack of support with VSCode with the new C# Dev Kit extension. Without a sln file included in the template when generating from VSCode + C# Dev Kit, VSCode + C# Dev Kit will generate a new empty sln file upon opening the workspace folder containing the template (with the default settings). That sln file does not include any of the 4 project references required though, since it is not the sln file from the template, but rather, a brand new, empty one. In that case, the dev has to manually go and add each of the projects to the new sln file or may even just be confused and think that it didn't work, since the sln (and solution explorer) is just empty at that point.

@phenning
Copy link
Contributor

A few comments/observations.

  1. I think the change here is appropriate and is a valid approach, given current behaviors in Visual Studio and the DevKit for VSCode.

  2. I do find it interesting though that the VS Code behavior is what it is. What should be happening in my opinion here is the same as the Visual Studio behavior, which is:

  • For type: solution, if no SLN is in the primary outputs, Visual Studio will try to find any SLN in the project output folder which Visual Studio did not itself create, and if it finds it, it opens that solution with a flag to merge it to the existing solution. This causes any solution folders or other customizations to be merged into the VS created one. In the absence of a SLN file being generated, it will look in the primary outputs for .csproj files (or recursively look on disk if none specified) and manually add each project to the generated solution. This last bit not happening in DevKit seems like it is likely a bug or a gap in functionality.

  • For type: project, skip all the SLN specific stuff above and just go to the csproj in the outputs and on disk.

  1. Lastly, I'm not sure why this project actually needs to be a type:solution if it is not making the SLN file available to Visual Studio.

Note, there is one thing to be aware of, and it could be why the SLN is being excluded in Visual Studio, even though it is a type:solution. That is, the best practice to avoid stomping on the Visual Studio created .sln file is to follow the guidance here, noteably, the part about making sure that the generated sln file in the template does not conflict with the Visual Studio generated one.

https://github.com/[sayedihashimi/template-sample](https://github.com/sayedihashimi/template-sample?tab=readme-ov-file#how-to-create-a-multi-project-solution-template)?tab=readme-ov-file#how-to-create-a-multi-project-solution-template

@mattleibow
Copy link
Member

mattleibow commented Nov 23, 2024

If I do a new maui app with dev kit in vscode I am sure I get a sln... I'll check again.

Edit: I am more sure now that I think vscode uses the cli to create templates.....

@jbw716
Copy link
Author

jbw716 commented Nov 23, 2024

@mattleibow In my experience, you do get a new sln file with devkit when creating the project from the template through VSCode, however, it's not the template sln, and it's empty, without the 4 projects added to it.

@mattleibow
Copy link
Member

That may be a bug somewhere. I think it should be creating a sln, otherwise the solution explorer in vscode is not going to be able to work...

@jbw716
Copy link
Author

jbw716 commented Nov 23, 2024

Right. So, I'm not sure if this would be an issue with dev kit or with the template, but I was thinking it's an issue that can be solved on the template side, considering the issues with the solution view are present with both the C# Dev Kit as well and Jet Brains Rider.

But you are correct, in that the solution view is broken with dev kit, since the sln that dev kit generates is empty (no projects added).

If I generate the project from the template using the dotnet cli and then open that with VSCode, dev kit and the solution explorer, as well as Rider, work perfectly.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
area-blazor Blazor Hybrid / Desktop, BlazorWebView area-templates Project templates, Item Templates for Blazor and MAUI community ✨ Community Contribution
Projects
None yet
Development

Successfully merging this pull request may close these issues.

5 participants