VS2019报MSB4018错误Nerdbank.GitVersioning任务意外失败如何解决
MSB4018 Nerdbank.GitVersioning任务异常报错解决方案
报错信息
错误 MSB4018 “Nerdbank.GitVersioning.Tasks.GetBuildVersion”任务意外失败。 System.ArgumentNullException: 反序列化筛选路径时基目录不能为空。请确认已向GetJsonSettings传递了对应参数,且version.json文件存放在Git仓库中。 参数名: repoRelativeBaseDirectory 在 Nerdbank.GitVersioning.FilterPathJsonConverter.ReadJson(JsonReader reader, Type objectType, Object existingValue, JsonSerializer serializer) 在 Newtonsoft.Json.Serialization.JsonSerializerInternalReader.DeserializeConvertable(JsonConverter converter, JsonReader reader, Type objectType, Object existingValue) 在 Newtonsoft.Json.Serialization.JsonSerializerInternalReader.PopulateList(IList list, JsonReader reader, JsonArrayContract contract, JsonProperty containerProperty, String id) 在 Newtonsoft.Json.Serialization.JsonSerializerInternalReader.CreateList(JsonReader reader, Type objectType, JsonContract contract, JsonProperty member, Object existingValue, String id) 在 Newtonsoft.Json.Serialization.JsonSerializerInternalReader.CreateValueInternal(JsonReader reader, Type objectType, JsonContract contract, JsonProperty member, JsonContainerContract containerContract, JsonProperty containerMember, Object existingValue) 在 Newtonsoft.Json.Serialization.JsonSerializerInternalReader.SetPropertyValue(JsonProperty property, JsonConverter propertyConverter, JsonContainerContract containerContract, JsonProperty containerProperty, JsonReader reader, Object target) 在 Newtonsoft.Json.Serialization.JsonSerializerInternalReader.PopulateObject(Object newObject, JsonReader reader, JsonObjectContract contract, JsonProperty member, String id) 在 Newtonsoft.Json.Serialization.JsonSerializerInternalReader.CreateObject(JsonReader reader, Type objectType, JsonContract contract, JsonProperty member, JsonContainerContract containerContract, JsonProperty containerMember, Object existingValue) 在 Newtonsoft.Json.Serialization.JsonSerializerInternalReader.CreateValueInternal(JsonReader reader, Type objectType, JsonContract contract, JsonProperty member, JsonContainerContract containerContract, JsonProperty containerMember, Object existingValue) 在 Newtonsoft.Json.Serialization.JsonSerializerInternalReader.Deserialize(JsonReader reader, Type objectType, Boolean checkAdditionalContent) 在 Newtonsoft.Json.JsonSerializer.DeserializeInternal(JsonReader reader, Type objectType) 在 Newtonsoft.Json.JsonConvert.DeserializeObject(String value, Type type, JsonSerializerSettings settings) 在 Newtonsoft.Json.JsonConvert.DeserializeObject[T](String value, JsonSerializerSettings settings) 在 Nerdbank.GitVersioning.VersionFile.TryReadVersionJsonContent(String jsonContent, String repoRelativeBaseDirectory) 在 Nerdbank.GitVersioning.VersionFile.GetVersion(String projectDirectory, String& actualDirectory) 在 Nerdbank.GitVersioning.VersionFile.GetVersion(String projectDirectory) 在 Nerdbank.GitVersioning.VersionOracle..ctor(String projectDirectory, Repository repo, Commit head, ICloudBuild cloudBuild, Nullable`1 overrideVersionHeightOffset, String projectPathRelativeToGitRepoRoot) 在 Nerdbank.GitVersioning.VersionOracle.Create(String projectDirectory, String gitRepoDirectory, ICloudBuild cloudBuild, Nullable`1 overrideBuildNumberOffset, String projectPathRelativeToGitRepoRoot) 在 Nerdbank.GitVersioning.Tasks.GetBuildVersion.ExecuteInner() 在 MSBuildExtensionTask.ContextAwareTask.Execute() 在 Microsoft.Build.BackEnd.TaskExecutionHost.Microsoft.Build.BackEnd.ITaskExecutionHost.Execute()
核心原因
该报错的根因是Nerdbank.GitVersioning自动版本生成工具无法识别到当前项目对应的Git仓库根目录,导致解析version.json配置文件时缺少必要的相对路径参数,抛出空参数异常。
解决方案
- 检查项目所在目录的Git仓库状态
确认项目文件夹属于已初始化的Git仓库范围内,不存在仓库外放置项目、嵌套Git仓库(子目录单独执行git init)的问题。可在项目目录下执行git status命令验证Git仓库是否可正常识别。 - 校验version.json配置文件
确认Git仓库根目录或项目根目录下存在合法的version.json文件,无JSON语法错误。如果配置了pathFilters字段,需确保所有路径为仓库相对路径,不要使用本地绝对路径。 - 清理本地缓存和编译产物
关闭VS2019后删除解决方案目录下的.vs隐藏文件夹,以及所有项目的bin、obj文件夹。重新打开VS后先执行清理解决方案操作,再尝试重新生成项目。 - 调整依赖包版本
部分旧版本的Nerdbank.GitVersioning存在该场景的已知Bug,可将该NuGet包升级到最新稳定版,或降级到经过验证的兼容版本。 - 临时关闭功能规避
如果暂时不需要自动版本生成能力,可直接在项目文件(.csproj/.vbproj)的PropertyGroup节点中添加<DisableNerdbankGitVersioning>true</DisableNerdbankGitVersioning>配置,关闭该组件的执行逻辑。
内容的提问来源于stack exchange,提问作者Jayaprakash
相关产品推荐
相关产品推荐

