Skip to content

sln-add: --include-references when adding a project #48815

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

Merged
merged 12 commits into from
May 9, 2025

Conversation

edvilme
Copy link
Member

@edvilme edvilme commented May 5, 2025

No description provided.

@edvilme edvilme force-pushed the edvilme-sln-add-recursive branch from 2f429ad to 471d3f2 Compare May 5, 2025 18:37
@edvilme edvilme marked this pull request as ready for review May 5, 2025 21:41
@Copilot Copilot AI review requested due to automatic review settings May 5, 2025 21:41
Copy link
Contributor

@Copilot Copilot AI left a comment

Choose a reason for hiding this comment

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

Pull Request Overview

This PR introduces a new command option (--include-references) to enable adding referenced projects along with a project in a solution.

  • Added IncludeReferencesOption to the command parser with a default value of true
  • Modified the command implementation to extract and recursively add project references when the flag is enabled

Reviewed Changes

Copilot reviewed 3 out of 21 changed files in this pull request and generated 2 comments.

File Description
src/Cli/dotnet/Commands/Solution/Add/SolutionAddCommandParser.cs Added a new option for including referenced projects
src/Cli/dotnet/Commands/Solution/Add/SolutionAddCommand.cs Captured the new flag in the command and extended AddProject to add referenced projects conditionally
Files not reviewed (18)
  • src/Cli/dotnet/Commands/CliCommandStrings.resx: Language not supported
  • src/Cli/dotnet/Commands/xlf/CliCommandStrings.cs.xlf: Language not supported
  • src/Cli/dotnet/Commands/xlf/CliCommandStrings.de.xlf: Language not supported
  • src/Cli/dotnet/Commands/xlf/CliCommandStrings.es.xlf: Language not supported
  • src/Cli/dotnet/Commands/xlf/CliCommandStrings.fr.xlf: Language not supported
  • src/Cli/dotnet/Commands/xlf/CliCommandStrings.it.xlf: Language not supported
  • src/Cli/dotnet/Commands/xlf/CliCommandStrings.ja.xlf: Language not supported
  • src/Cli/dotnet/Commands/xlf/CliCommandStrings.ko.xlf: Language not supported
  • src/Cli/dotnet/Commands/xlf/CliCommandStrings.pl.xlf: Language not supported
  • src/Cli/dotnet/Commands/xlf/CliCommandStrings.pt-BR.xlf: Language not supported
  • src/Cli/dotnet/Commands/xlf/CliCommandStrings.ru.xlf: Language not supported
  • src/Cli/dotnet/Commands/xlf/CliCommandStrings.tr.xlf: Language not supported
  • src/Cli/dotnet/Commands/xlf/CliCommandStrings.zh-Hans.xlf: Language not supported
  • src/Cli/dotnet/Commands/xlf/CliCommandStrings.zh-Hant.xlf: Language not supported
  • test/TestAssets/TestProjects/SlnFileWithReferencedProjects/A/A.csproj: Language not supported
  • test/TestAssets/TestProjects/SlnFileWithReferencedProjects/App.sln: Language not supported
  • test/TestAssets/TestProjects/SlnFileWithReferencedProjects/App.slnx: Language not supported
  • test/TestAssets/TestProjects/SlnFileWithReferencedProjects/B/B.csproj: Language not supported

@@ -202,6 +204,21 @@ private void AddProject(SolutionModel solution, string fullProjectPath, ISolutio
project.AddProjectConfigurationRule(new ConfigurationRule(BuildDimension.BuildType, solutionBuildType, "*", projectBuildType));
}

// Get referencedprojects from the project instance
Copy link
Preview

Copilot AI May 5, 2025

Choose a reason for hiding this comment

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

[nitpick] Consider updating the comment to 'Get referenced projects from the project instance' for improved clarity.

Suggested change
// Get referencedprojects from the project instance
// Get referenced projects from the project instance

Copilot uses AI. Check for mistakes.

Reporter.Output.WriteLine(CliStrings.ProjectAddedToTheSolution, solutionRelativeProjectPath);

if (_includeReferences)
Copy link
Preview

Copilot AI May 5, 2025

Choose a reason for hiding this comment

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

Consider adding cycle detection or duplicate addition checks when recursively adding referenced projects to prevent potential infinite recursion in cases of cyclic project references.

Copilot uses AI. Check for mistakes.

@edvilme edvilme requested a review from a team May 5, 2025 21:42
@@ -2483,4 +2483,7 @@ To display a value, specify the corresponding command-line option without provid
<data name="ZeroTestsRan" xml:space="preserve">
<value>Zero tests ran</value>
</data>
</root>
<data name="SolutionAddReferencedProjectsOptionDescription" xml:space="preserve">
<value>Recursively add projects' ReferencedProjects to solution</value>
Copy link
Member

Choose a reason for hiding this comment

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

Referring to ReferencedProjects is clear to you but probably opaque to most people

Copy link
Member Author

Choose a reason for hiding this comment

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

"Recursively add project references to the solution"?

Copy link
Member

Choose a reason for hiding this comment

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

How about "Set to false to disable recursively adding all ProjectReferences"? My initial thought was that ReferencedProjects is an internal implementation detail, but looking now, it'd also be nice to include that the default is 'true'.

var referencedProjectsFullPaths = projectInstance.EvaluatedItemElements
.Where(item => item.ItemType == "ProjectReference")
.Select(item => item.Include)
.Select(item => Path.GetFullPath(item, Path.GetDirectoryName(fullProjectPath)))
Copy link
Member

Choose a reason for hiding this comment

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

You don't need to pay attention to Exclude/Remove?

@Forgind
Copy link
Member

Forgind commented May 6, 2025

Currently looking exclude/remove handling and skipping already-added projects (per copilot's comment)

@edvilme
Copy link
Member Author

edvilme commented May 6, 2025

Currently looking exclude/remove handling and skipping already-added projects (per copilot's comment)

The current behavior for adding existing projects is to just print a message saying it's been added already. It doesn't fail and in this case I think it should absolutely not fail (i.e., as a user I wouldn't expect it to fail if I'm being a good citizen and doing things "correctly" -- adding all needed projects). However, we could make the output message conditional. I would love to also hear @baronfel 's opinion here.

I will look into how to handle the exclude/remove and see if ProjectInstance::EvaluatedItemElements resolves that, or if it considers globbing.

@Forgind
Copy link
Member

Forgind commented May 6, 2025

I agree that it absolutely should not fail; I just hadn't realized it was covered properly. Looks like it is, but it's halfway down the method, so I missed it. Perhaps check earlier so we can skip some extra work? I also would prefer that it not print a message when it's a recursive call, as that would add confusion when users see things that weren't added that they'd never intended to add or that they'd already added. Like:
Successfully added foo to my.sln.
Failed to add foo to my.sln because it is already there.
???

@edvilme
Copy link
Member Author

edvilme commented May 6, 2025

I agree that it absolutely should not fail; I just hadn't realized it was covered properly. Looks like it is, but it's halfway down the method, so I missed it. Perhaps check earlier so we can skip some extra work? I also would prefer that it not print a message when it's a recursive call, as that would add confusion when users see things that weren't added that they'd never intended to add or that they'd already added. Like: Successfully added foo to my.sln. Failed to add foo to my.sln because it is already there. ???

I like the idea of not adding the message if in a recursive call, and how it can be very confusing to users. I will also check for a good way of identifying this beforehand to avoid unnecessary steps

@edvilme
Copy link
Member Author

edvilme commented May 6, 2025

Just noticed this kindof addresses #28781

@edvilme
Copy link
Member Author

edvilme commented May 8, 2025

Currently looking exclude/remove handling and skipping already-added projects (per copilot's comment)

As per offline discussion, by the time you're enumerating individual items with projectInstance.GetItems("ProjectReference") all include/exclude/remove operations are done

@edvilme edvilme requested review from Copilot, Forgind and a team May 8, 2025 21:12
Copy link
Contributor

@Copilot Copilot AI left a comment

Choose a reason for hiding this comment

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

Pull Request Overview

This pull request adds the --include-references option when adding a project to a solution. The key changes include new project files and solution files for test assets, updated resource strings for localized messages, and modifications to the solution add command parser and implementation to recursively add referenced projects.

Reviewed Changes

Copilot reviewed 24 out of 24 changed files in this pull request and generated 1 comment.

File Description
test/TestAssets/TestProjects/SlnFileWithReferencedProjects/* New test projects and solution files to support referenced projects
src/Cli/dotnet/Commands/Solution/Add/SolutionAddCommandParser.cs Added a new option for including referenced projects
src/Cli/dotnet/Commands/Solution/Add/SolutionAddCommand.cs Updated command implementation to process the new option and recursively add project references
src/Cli/dotnet/Commands/xlf/*, CliCommandStrings.resx Added localized strings for the new option

Copy link
Member

@Forgind Forgind left a comment

Choose a reason for hiding this comment

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

Just the one tweak for when vs. if, and I think I'm happy 🙂 Thanks for working through this!

@edvilme edvilme enabled auto-merge (squash) May 9, 2025 04:14
@edvilme edvilme merged commit 509db15 into dotnet:main May 9, 2025
30 checks passed
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Projects
None yet
Development

Successfully merging this pull request may close these issues.

2 participants