MSBuild 编译失败但通过 Visual Studios 2017

MSBuild fails to compile but passes on Visual Studios 2017

我的项目在 VS2017 中成功构建,但在 Jenkins 上使用旧的 MSBuild (v14.0 C:\Program Files (x86)\MSBuild.0\Bin\MSBuild.exe) 失败。错误日志显示缺少字符,例如分号和大括号。我查看了失败的文件,大多数 "errors" 都指的是下面的这个方法调用。

if (cache.TryGetProxyById<ResultTagTypeProxy, ResultTagType>(this.ResultTagTypeId, out var existingTagType))
                this.ResultTagType = existingTagType;
public TProxy GetOrCreateProxy<TProxy, TModel>(TModel navModel, Func<TModel, ProxyCache, TProxy> proxyConstructor)
            where TProxy : ModelProxyBase<TModel>
            where TModel : IDatabaseEntity
        {
            TProxy navProxy = null;

            // Set the navigation property if it already exists in the cache by model
            if (this.TryGetProxyByModel<TProxy, TModel>(navModel, out var existingProxy))
                navProxy = existingProxy;
            else if (navModel != null)
            {
                // Else try loading by id
                if (this.TryGetProxyById<TProxy, TModel>(navModel.Id, out existingProxy))
                    navProxy = existingProxy;
                // Else create a
                else
                    navProxy = proxyConstructor(navModel, this);
            }

            return navProxy;
        }

我认为问题是因为此 MSBuild 不支持 C# 7 新功能,我需要下载新的 MSBuild。我一直在使用 chocolatey 来安装我的包,我安装了 'microsoft-build-tools'.

在构建工具路径(C:\Program Files (x86)\Microsoft Visual Studio17\BuildTools\MSBuild.0\Bin\MSbuild.exe)中使用新的MSBuild,我发现有不同的编译错误.第一个错误是我的一个项目需要引用 netstandard 2.0。修复该引用后,在我的功能测试项目中找不到更多引用。

我想知道这种变化(从 MSBuild 14.0 迁移到 MSBuild 15.0)是否符合预期。功能测试项目之前的所有内容都编译成功...

您应该使用 msbuild.exe 的 VS 2017(构建工具)版本,它位于 VS 2017 安装文件夹中,而不是 C:\Program Files (x86)\MSBuild。我建议使用 vswhere 来定位 msbuild.exe.

的路径

或者,您可以在您的项目中使用 Microsoft.Net.Compilers NuGet 包来获取最新的 C# 编译器来替换 MSBuild/Visual Studio 使用的默认编译器,但它也会在开发时覆盖工具集,稍微减慢编译速度并导致不同的诊断和构建行为。

I was wondering if this change (moving from MSBuild 14.0 to MSBuild 15.0) is expected. Everything up to the functional test project compiles successfully...

1.We始终推荐使用vs版本对应的msbuild工具

由于您的项目来自 VS2017 并且在其中构建良好。当你需要在 VS 之外构建它时 IDE,Build tools for VS2017(Msbuild 15.0) 包应该是不错的选择。

ps: VS2015 <=> MSBuild14.0, VS2017 <=> MSBuild15.0

build tools for vs2017 包中 msbuild.exe 的默认路径:C:\Program Files (x86)\Microsoft Visual Studio17\BuildTools\MSBuild.0\Bin

2.Also,如 Jonathon 评论中所述,要支持 C# 7 功能,我们需要使用 Msbuild15.0 版本。

假设您有两个项目(A 和 B,A 使用 C#7 而 B 不)。在您的 CI 服务器中,如果您有 msbuild15,则可以使用 msbuild14.0 构建 B 但不能构建 A .0,你可以用它来构建A和B等等

所以我认为从 14.0 升级到 15.0 是一个意料之中的选择。

另外:

在vs中新建一个C#工程,Right-clickProject=>Properties=>Build=>Advanced可以找到语言版本列表。

第一张 vs2015 和 msbuild14.0 的图片,第二张 vs2017 和 msbuild15.0 的图片:

你的 msbuild14 是 visual studio 2015 您必须使用 msbuild15.