【问题标题】:Assembly references broken when compiled with msbuild (CLI)使用 msbuild (CLI) 编译时程序集引用损坏
【发布时间】:2022-01-05 06:14:36
【问题描述】:

背景

我的总体目标是让 Sonarqube 在 CI 中为 Unity 项目运行。 据我了解,要让声纳扫描仪工作,我必须在统一之外编译我想用 msbuild 分析的项目。 我构建了一个基于 ubuntu 的 docker 镜像,其中包含 Unity、monodevel 和 sonarscanner。 ci 运行一个包含以下行的 shell 脚本:

mono /opt/sonar-scanner-msbuild/SonarScanner.MSBuild.exe begin \ #...more arguments
msbuild Assembly-CSharp-Editor.csproj
mono /opt/sonar-scanner-msbuild/SonarScanner.MSBuild.exe end -d:sonar.login="$SONAR_TOKEN"

虽然解决方案在 Unity 中完美编译,但 msbuild 步骤失败并出现 cs0246cs0234 等编译器错误。看一眼这些错误,它们似乎都源自跨程序集的引用。

小例子:

我在 Unity 中创建了 2 个程序集 ABA 不依赖于任何东西,B 只依赖于 A。 Unity 创建 .csproj 文件 A.csprojB.csproj。 两个程序集都包含一个静态类:

public static class A {
    public static void TestA() => Console.WriteLine("Testing A");
}
public static class B {
    public static void TestB() => A.TestA(); 
}

当我跑步时

msbuild B.csproj

很明显,依赖项被拾取,因为在日志的开头附近有

项目“/builds/root/logoleon-app/B.csproj”(1)正在建设中 节点 1 上的“/builds/root/logoleon-app/A.csproj”(2:2)(默认 目标)。

成功构建A 程序集后

完成构建项目“/builds/root/logoleon-app/A.csproj”(默认 目标)。

它移动到B,但失败了

Assets/test/B/B.cs(3,9):错误 CS0103:名称“A”不存在于 当前上下文 [/builds/root/logoleon-app/B.csproj]

msbuild B.csproj 命令开头的日志文件的完整结尾:https://pastebin.com/mdBdPTrJ,包括A.csprojB.csproj 的内容

可能是什么问题?

【问题讨论】:

    标签: unity3d msbuild mono


    【解决方案1】:

    问题似乎在于,虽然 Unity 2020.3.1f1 在 .csproj 中生成项目引用,但它们包含

    <ReferenceOutputAssembly>false</ReferenceOutputAssembly>
    

    有一个 fix by user Kichang-Kim on the Unity Forum 构建错误可以帮助。 (使用c#6表达式体修改)

    using UnityEditor;
    public class SolutionFileFixer : AssetPostprocessor
    {
        private static string OnGeneratedCSProject(string path, string content) => content.Replace(
            "<ReferenceOutputAssembly>false</ReferenceOutputAssembly>",
            "<ReferenceOutputAssembly>true</ReferenceOutputAssembly>");
    }
    

    我还不知道在哪个统一版本中 - 或者根本不知道 - 这是固定的。

    【讨论】:

      猜你喜欢
      • 2015-10-03
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多