Visual Studio 中的预构建事件将 $(SolutionDir) 替换为 *Undefined*
我相信该问题已记录在此处 移至此处 看起来这可能是 Visual Studio 中的一个错误,但我想知道是否有人知道解决方法。
基本上我有以下两行(除其他外),在预构建事件中,一行紧接着另一行。
"C:\WINDOWS\Microsoft.NET\Framework\v3.5\MSBuild.exe" /p:configuration=Release;platform=x86 /t:rebuild "$(SolutionDir)Folder1\Project1.csproj"
"C:\WINDOWS\Microsoft.NET\Framework\v3.5\MSBuild.exe" /p:configuration=Release;platform=x86 /t:rebuild "$(SolutionDir)Folder2\Folder3\Project2.csproj"
第一个成功,另一个失败,说 The command ""C:\WINDOWS\Microsoft.NET\Framework\v3.5\MSBuild.exe" /p:configuration=Release;platform=x86 /t:rebuild "*Undefined*Folder2\Folder3\Project2.csproj"" 退出,代码为 1。
。
编辑:
发现问题是与预构建具有相同行的其他项目之一失败了,因为 MSBuild 本身不知道宏。
I believe the problem is documented here moved here and looks like it might be a bug in visual studio, but I'm wondering if anyone knows of a workaround.
Basically I have the following two lines (among other things) one right after the other in the prebuild event.
"C:\WINDOWS\Microsoft.NET\Framework\v3.5\MSBuild.exe" /p:configuration=Release;platform=x86 /t:rebuild "$(SolutionDir)Folder1\Project1.csproj"
"C:\WINDOWS\Microsoft.NET\Framework\v3.5\MSBuild.exe" /p:configuration=Release;platform=x86 /t:rebuild "$(SolutionDir)Folder2\Folder3\Project2.csproj"
The first one succeeds and the other fails saying that The command ""C:\WINDOWS\Microsoft.NET\Framework\v3.5\MSBuild.exe" /p:configuration=Release;platform=x86 /t:rebuild "*Undefined*Folder2\Folder3\Project2.csproj"" exited with code 1.
.
Edit:
Figured out the problem was that one of the other projects with the same line as it's prebuild failed, since MSBuild itself doesn't know about the macros.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(8)
我通过将所有
$(SolutionDir)
替换为$(ProjectDir)..\
修复了此问题。这是由于 MSBuild 独立运行每个项目,因此不是解决方案。 它在 VS2010 中运行良好,但在构建服务器上运行不佳。
I fixed this by replacing all
$(SolutionDir)
with$(ProjectDir)..\
.It was due to MSBuild running each project independently, and thus not the solution. It worked fine in VS2010, but not on the build server.
浪费了很多时间来找到这个问题的完美解决方案。 使用 Directory.Build.props。
在您的
sln
位置文件夹中创建一个名为Directory.Build.props
的文件,并将此代码放入其中:该文件将被所有
csproj< /code> 文件并将定义
(SolutionDir)
属性。Wasted a lot of time to find perfect solution for this problem. Use Directory.Build.props.
In your
sln
location folder create a file with nameDirectory.Build.props
and put this code inside:This file will be automagically picked up by all your
csproj
files and will define(SolutionDir)
property.艾伦的答案完全正确,但是如果您想移动到
ProjectDir
的父目录,或者$(ProjectDir)..\
输出将是一个字符串
,需要在msbuild中执行“转到上级目录”操作。以下是实现此目的的方法:
此代码将
ProjectDir
的父目录路径设置为TrimmedDir
变量。[System.IO.Path]::GetDirectoryName()
执行此操作。Allan's answer is exactly on the right path, but if you want to move to parent directory of the
ProjectDir
, or the$(ProjectDir)..\
output is going to be astring
, you need to perform the 'go to parent directory' operation in msbuild.Here is how you can achieve this:
This code will set the parent directory path of the
ProjectDir
toTrimmedDir
variable.[System.IO.Path]::GetDirectoryName()
performs this operation.Allan 的答案有正确的想法,但是用
$(ProjectDir)..\
替换所有$(SolutionDir)
对我来说在 VS 2019 16.7.2 中不起作用。$(ProjectDir)..\
计算为实际的ProjectDir
并附加了“..\”,而不是向上一级目录。相反,我将所有
$(SolutionDir)
替换为$(ProjectDir)\..
,这正确地上升到了一个目录。Allan's answer had the right idea, but replacing all
$(SolutionDir)
with$(ProjectDir)..\
did not work for me in VS 2019 16.7.2.$(ProjectDir)..\
evaluated to the actualProjectDir
with "..\" appended to it, instead of going up one directory.Instead, I replaced all
$(SolutionDir)
with$(ProjectDir)\..
and this properly went up one directory.我遇到了同样的问题,在我的例子中,Azure DevOps 构建管道指向project.csproj 文件,而不是project.sln 文件。
这导致$(SolutionDir)成为未定义
工具需要一段时间才能弄清楚; 我想我会分享以防其他人遇到同样的问题。
I experienced the same problem, in my case the Azure DevOps build pipeline was pointing to the project.csproj file, not the project.sln file.
This resulted in $(SolutionDir) being undefined
Tool a while to figure out; thought I'd share in case others have the same problem.
您没有指定这种情况是在许多项目中发生还是仅在一两个项目中发生。
如果仅在一两个项目中,临时解决方法可能是将 $(SolutionDir) 替换为 Folder2 所在的显式路径。例如,类似:
但使用项目的正确路径。
You don't specify if this happens for many projects or just one or two.
If it is only in one or two projects, a temporary workaround might be to replace $(SolutionDir) with the explicit path that Folder2 is located in. For example, something like:
but with the correct path for your project.
如果您仅使用 MSBuild,而 SolutionDir 适用于某些项目,但不适用于其他项目,则 SolutionDir 可能已在工作项目的 .csproj 文件中的某处定义。
If you are using only MSBuild and SolutionDir works for some projects but for others not, it's possible that SolutionDir is already defined somewhere in .csproj file of the working projects.
可能为时已晚,但我遇到了同样的问题并在这里找到了官方推荐:
https://github.com/dotnet/msbuild/issues/2743
为了您的方便.. 也将文本复制到此处。
MSBuild 可以更改其解决方案处理代码,以在构建元项目时将这些属性设置为全局属性。 这消除了每个 sln 扩展手动传播它们的需要。 另一方面,这更有可能破坏某处的东西。
3.中间解决方案,MSBuild 可以在 metaproj 文件中发出一个额外的属性,sln 扩展可以使用该属性向下传播其 msbuild 任务调用。 消除了向后兼容问题的潜在风险,并使其能够适应潜在的全球属性变化。
May be too late to but I got the same issue and found the official recommandation here:
https://github.com/dotnet/msbuild/issues/2743
For your conveninece.. Copying the text here as well.
MSBuild could change its solution handling code to set those properties as global properties when building the metaproj. This eliminates the need for every sln extension to manually propagate them. On the other hand, this has a higher chance to break something somewhere.
3.Intermediate solution, MSBuild can emit an extra property in the metaproj file which sln extensions can use to propagate down their msbuild task invocations. Eliminates potential risks of back compat issues, and makes it resilient to potential global properties changes.