简体   繁体   English

在 Visual Studio 2019 中编辑 .csproj 文件是否安全?

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

We have a C# solution with multiple projects.我们有一个包含多个项目的 C# 解决方案。 We are managing version control for the solution using Github desktop, ie, outside Visual Studio.我们正在使用 Github 桌面管理解决方案的版本控制,即在 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.在全新安装 Visual Studio 2019 社区版的新机器上将新代码推送到 GitHub 后,当我向解决方案中的 API 项目添加新文件时,我发现该文件被添加到 .csproj 文件中,如下所示,并且 IDE 永远不会在构建、调试、代码完成等过程中包含该文件。

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

I also see a + sign next to the TestController file in Solution Explorer.我还在解决方案资源管理器中的 TestController 文件旁边看到一个 + 号。 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?删除 .csproj 文件中的这个项目组块是否安全? 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.包含 5 个项目的 C# 解决方案 - (1) WebAPI、(2) 身份、(3) 数据访问、(4) 服务、(5) 共享库。 Editing using Visual Studio 2019 Community Edition.使用 Visual Studio 2019 社区版进行编辑。

Full timeline:完整时间表:

  1. Solution pulled from GitHub.com using GitHub desktop使用 GitHub 桌面从 GitHub.com 中提取的解决方案
  2. Files added to DataAccess and Services project using Visual Studio.使用 Visual Studio 添加到 DataAccess 和 Services 项目的文件。 Everything was fine一切都很好
  3. New branch created in GitHub desktop.在 GitHub 桌面中创建的新分支。 New files added to the branch using GitHub desktop and pushed to GitHub.com using GitHub desktop使用 GitHub 桌面将新文件添加到分支并使用 GitHub 桌面推送到 GitHub.com
  4. Code merged with parent branch by project administrator代码由项目管理员与父分支合并
  5. Local solution synced to remote repository using GitHub desktop本地解决方案使用 GitHub 桌面同步到远程存储库

--- alert: problem seen now --- --- 警报:现在发现问题---

  1. New file (TestController) added to the API project using Visual Studio.使用 Visual Studio 添加到 API 项目的新文件 (TestController)。 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." IDE 不会突出显示 ApiControllers、ControllerBase 等,也不会指示拼写错误的服务的错误,在 ApiControllers 上按 F11 会给出错误消息“无法导航到插入符号下的符号”。
  2. Google search suggests checking on .csproj file.谷歌搜索建议检查 .csproj 文件。 Here I find that the new file is within the 'compile remove' block.在这里,我发现新文件位于 'compile remove' 块内。

Is it safe to edit .csproj file in Visual Studio 2019?在 Visual Studio 2019 中编辑 .csproj 文件是否安全?

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?删除 .csproj 文件中的这个项目组块是否安全? 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.之所以发生这种情况,是因为 Visual Studio 没有作为该项目的一部分出现在项目子目录中的文件。 So it adds the <Compile Remove... /> element to exclude it from being compiled as part of the project.因此,它添加了<Compile Remove... />元素以将其作为项目的一部分从编译中排除。 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.作为一般规则,您不应将文件添加到由 Visual Studio 管理的项目目录,除非使用 Visual Studio UI 本身。 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 .您可能有XY 问题 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".如果您可以发布一个新问题来解释您添加不属于的文件的场景,并寻求指导以完成您拥有的任何更广泛的目标,但又不违反 Visual Studio 的规则,你可能会得到比“不要那样做”更好的答案。

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.我相信发生的事情是,一旦将本地分支推送到远程,即使我没有在 Visual Studio 中正式跟踪项目,使用 Visual Studio 对项目所做的进一步更改也不会集成到项目中。

git status showed that I was still on my initial local branch. git status 显示我仍然在我最初的本地分支上。 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.这将我切换到一个新分支,现在我可以像往常一样编辑文件。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM