1

Hello I ran into a XY problem using Visual Studio 2017 and dotnet 6.0.100

My original problem is: "How to add project dependencies in a MSBuild solution programmatically with C#"

This is needed to force a modular application to compile unreferenced projects in a .sln during the build process to also compile all other projects in the solution.

This .sln file is built programmatically and should also do this step automatically during creation of the .sln to reduce possible problems for the user working with this .sln.

The .csproj of the projects however should not be touched since they are part of a git submodule, that should not include these changes to the .csproj but only the changes to the .csproj made by the user.

Solution

There is a 10 year old solution described here and here using the DTE Interface.

Derived problem: I failed to include the correct references for the EnvDTE.DTE or EnvDTE80.DTE2 objects to be available in code. I tried adding the Microsoft.VisualStudio.Setup.Configuration.Interop NuGet package and searched in the references for EnvDTE or Microsoft.VisualStudio.Shell as seen e.g. in the example here without luck.

Original problem

Manually the project dependencies can be added as described here

I tried adding the project dependencies via the dotnet sln command but didn't find a corresponding command to add the dependencies.

dotnet sln -h only points to add, list and remove. The add command didn't show an option for dependencies.

Since I already use dotnet commands to create the solution and add projects this would be a good solution for me if there is a way.

The other thing I tried was using Microsoft.Build.Construction:

SolutionFile solutionFile = SolutionFile.Parse(PROJECT_SOLUTION_FILE.FullName);
var projectInSolution = solutionFile.ProjectsInOrder;

which does enable me to read the projects in the sln but seemingly not to manipulate it since its all readonly.

Summary

I need a way to tell the solution that all projects have to be compiled on run/compilation of the designated one.

  • To accomplish this I either need a way to add project dependencies to the .sln file via
    • dotnet shell commands
    • something like Microsoft.Build.Construction
  • or a good source explaining to me how I can get the EnvDTE code running in my MS Visual Studio 2017
  • or there is some awesome way to do this that I didn't stumble across yet

I really appreciate any help to solving my problem.

Edit:

To clarify project dependencies are different from project references in .csproj files! The project dependencies I refer to are this section in the .sln:

Project("{GUID}") = "PROJECTNAME", "CSPROJECT_LOCATION", "{GUID}"
    ProjectSection(ProjectDependencies) = postProject
        {GUID} = {GUID}
        {GUID} = {GUID}
        ...
    EndProjectSection
EndProject
negatic
  • 33
  • 8
  • 1
    Solution does not have dependencies... Projects have. So dotnet sln command does not have any options to add dependencies. If you have one project dependent on another project then you can add that project to solution via dotnet sln command. – Chetan Dec 24 '21 at 11:19
  • 1
    @sommmen The solution file itself does not seem to be an xml file, did you mean to use the SolutionFile.Parse to get to the xml file? Theoretically i could try to edit the file manually but this is somewhat my last resort, and im convinced this is some problem with a rather easy solution im just not finding. – negatic Dec 24 '21 at 11:19
  • @Chetan You can actually add projects as dependencies to other projects on the solution level only as described at https://learn.microsoft.com/en-us/visualstudio/ide/how-to-create-and-remove-project-dependencies?view=vs-2022 – negatic Dec 24 '21 at 11:21
  • @negatic lol you're right. Its friday i suppose. – sommmen Dec 24 '21 at 11:37
  • The project file is XML, cant you just parse it and add the Dependencies in the `.csproj` file before you build? – Charles Dec 24 '21 at 20:06
  • @Charles Thinking about this a bit more, since i already have every project look conditional for a .target file containing the output path maybe i could add the project dependencies there to achieve this goal, even tho it might not be as clean. I will try that Monday. – negatic Dec 24 '21 at 20:32
  • @Charles I tested it out and it behaves different than using project dependencies. It results in undesired behavior. – negatic Dec 27 '21 at 12:14

1 Answers1

1

I found the solution to my Y problem, adding the dependencies using EnvDTE as described in the answer.

The references for EnvDTE and Microsoft.VisualStudio.Setup.Configuration.Interop did not show up because I was using a SDK-Style project to test them, since we do use them by default currently.
Installing the Microsoft.VisualStudio.Setup.Configuration.Interop NuGet did not remedy this either!

According to this question SDK-Style projects do not support working with the DTE Interface yet.
That is the reason, why I was unable to find the references described in this example.
The example also mentions creating a .NET Framework Console App, which I did not see as a requirement at the time.

Summary:
To work with the DTE Interface in 2021 better use the old project format and avoid the SDK-Style project format!

negatic
  • 33
  • 8