Manish
Manish

Reputation: 1786

Is it safe to edit .csproj file in Visual Studio 2019?

We have a C# solution with multiple projects. We are managing version control for the solution using Github desktop, i.e., outside Visual Studio. After pushing new code to GitHub on a new machine with a fresh install of Visual Studio 2019 community edition, when I add a new file to the API project in the solution, I find that the file gets added to the .csproj file as follows, and the IDE never includes the file in the build, debugging, code completion etc.

<ItemGroup>
    <Compile Remove="Controllers\TestController.cs" />
</ItemGroup>

I also see a + sign next to the TestController file in Solution Explorer. Hovering over the + sign brings up the message "pending add."

Why is this happening? And is it safe to just remove this itemgroup block in the .csproj file? Is there a way to prevent this behavior for new files added to the project?

Details

Context:

C# solution with 5 projects - (1) WebAPI, (2) Identity, (3) DataAccess, (4) Services, (5) Shared libraries. Editing using Visual Studio 2019 Community Edition.

Full timeline:

  1. Solution pulled from GitHub.com using GitHub desktop
  2. Files added to DataAccess and Services project using Visual Studio. Everything was fine
  3. New branch created in GitHub desktop. New files added to the branch using GitHub desktop and pushed to GitHub.com using GitHub desktop
  4. Code merged with parent branch by project administrator
  5. Local solution synced to remote repository using GitHub desktop

--- alert: problem seen now ---

  1. New file (TestController) added to the API project using Visual Studio. This file does not become part of the build. IDE does not highlight ApiControllers, ControllerBase etc, does not indicate errors for mis-spelt services, pressing F11 on ApiControllers gives error message "cannot navigate to the symbol under the caret."
  2. Google search suggests checking on .csproj file. Here I find that the new file is within the 'compile remove' block.

Upvotes: 1

Views: 2189

Answers (2)

Manish
Manish

Reputation: 1786

I think I have identified the problem. I believe what happened is that once the local branch was pushed to remote, further changes to the project made using Visual Studio were not being integrated into the project, even though I was not formally tracking the project in Visual Studio.

git status showed that I was still on my initial local branch. I did the following to sync with the remote parent branch and then start a new local branch:

git stash
git checkout <parent_branch>
git pull
git checkout -b <new_branch>

This switched me to a new branch, and now I am able to edit the file as usual.

Upvotes: 2

Peter Duniho
Peter Duniho

Reputation: 70701

Is it safe to edit .csproj file in Visual Studio 2019?

If you know what you're doing and you do it correctly, sure. Otherwise, not so much.

Why is this happening? And is it safe to just remove this itemgroup block in the .csproj file? Is there a way to prevent this behavior for new files added to the project?

It's happening because a file showed up in the project subdirectory that Visual Studio doesn't have as part of that project. So it adds the <Compile Remove... /> element to exclude it from being compiled as part of the project. It is safe to remove the element, but in my experience it just shows up again.

As a general rule, you should not be adding files to project directories managed by Visual Studio, except by using the Visual Studio UI itself. It's not clear from your post why you are doing this. The directory you apparently are adding files to is your project directory. It should only have files that do in fact belong to the project.

The best fix is to not try to add files to a project directory when they shouldn't be part of that project.

You may have an XY Problem. If you can post a new question in which you explain the scenario in which you've added the files that don't belong, and ask for guidance to accomplish whatever broader goal it is you have, but without running afoul of Visual Studio's rules, you may get a better answer than "don't do that".

Upvotes: 2

Related Questions