4

I have an issue with a C# project. When built from Visual Studio (or by using MSBuild from command-line), the project is compiled with no issue.

However, we are also working with TeamCity and the compilation fails:

[16:41:51][Step 1/1] (CoreCompile target) -> 
[16:41:51][Step 1/1]   AssemblyInfo.cs(4,12): error CS0246: The type or namespace name 'AssemblyVersionAttribute' could not be found (are you missing a using directive or an assembly reference?) [C:\BuildAgent\work\d567fa3cb4955788\src\PowerSaver.IO.Channel\PowerSaver.IO.Channel.csproj]
[16:41:51][Step 1/1]   AssemblyInfo.cs(4,12): error CS0246: The type or namespace name 'AssemblyVersion' could not be found (are you missing a using directive or an assembly reference?) [C:\BuildAgent\work\d567fa3cb4955788\src\PowerSaver.IO.Channel\PowerSaver.IO.Channel.csproj]
[16:41:51][Step 1/1]   AssemblyInfo.cs(5,12): error CS0246: The type or namespace name 'AssemblyInformationalVersionAttribute' could not be found (are you missing a using directive or an assembly reference?) [C:\BuildAgent\work\d567fa3cb4955788\src\PowerSaver.IO.Channel\PowerSaver.IO.Channel.csproj]
[16:41:51][Step 1/1]   AssemblyInfo.cs(5,12): error CS0246: The type or namespace name 'AssemblyInformationalVersion' could not be found (are you missing a using directive or an assembly reference?) [C:\BuildAgent\work\d567fa3cb4955788\src\PowerSaver.IO.Channel\PowerSaver.IO.Channel.csproj]
[16:41:51][Step 1/1]   AssemblyInfo.cs(6,12): error CS0246: The type or namespace name 'AssemblyFileVersionAttribute' could not be found (are you missing a using directive or an assembly reference?) [C:\BuildAgent\work\d567fa3cb4955788\src\PowerSaver.IO.Channel\PowerSaver.IO.Channel.csproj]
[16:41:51][Step 1/1]   AssemblyInfo.cs(6,12): error CS0246: The type or namespace name 'AssemblyFileVersion' could not be found (are you missing a using directive or an assembly reference?) [C:\BuildAgent\work\d567fa3cb4955788\src\PowerSaver.IO.Channel\PowerSaver.IO.Channel.csproj]
[16:41:51][Step 1/1] 
[16:41:51][Step 1/1]     0 Warning(s)
[16:41:51][Step 1/1]     6 Error(s)

I have found links to similar problems, but cannot solve my problem:

Thanks in advance for your help.

EDIT (additional information):

  • .NET Framework 4.6.1
  • Visual Studio Community 2017 (15.5.6) (using .csproj new format, compiled with MSBuild)
  • TeamCity Professional 2017.1.4 (build 47070) (no plugin used)
  • The attributes mentioned in the error message are already defined in an AssemblyInfo.cs file

Cake script we use to build the project:

#tool "nuget:?package=xunit.runner.console"
#tool nuget:?package=GitVersion.CommandLine
#addin Cake.Incubator

// Build script arguments

var target = Argument("target", "Default");
var configuration = Argument("configuration", "Debug");

// Build script parameters

var rootDir = Directory(".");

var distDir = Directory("./dist");
var outputDir = Directory("./output");

var solutionFile = "./PowerSaver.sln";

GitVersion versionInfo;

// Tasks

Task("Clean")
    .Does(() =>
{
    CleanDirectories ("./src/**/" + configuration + "/bin");
    CleanDirectories ("./src/**/" + configuration + "/obj");
    CleanDirectories ("./test/**/" + configuration + "/bin");
    CleanDirectories ("./test/**/" + configuration + "/obj");

    CleanDirectory(distDir);
    CleanDirectory(outputDir);
});

Task("Restore-NuGet-Packages")
    .IsDependentOn("Clean")
    .Does(() =>
{
    NuGetRestore(solutionFile);
});

Task("Set-Version")
    .Does(() => 
{
    Information("Working directory: {0}", rootDir);

    var isRunningOnTeamCity = BuildSystem.TeamCity.IsRunningOnTeamCity;

    var gitVersionLog = rootDir + File("gitversion.log");

    versionInfo = GitVersion(new GitVersionSettings {
        UpdateAssemblyInfo = isRunningOnTeamCity,
        RepositoryPath = rootDir,
        LogFilePath = gitVersionLog
    });

    Information("Building PowerSaver WebApp version {0}", versionInfo.FullSemVer);

    Information("Assembly semver {0}", versionInfo.AssemblySemVer);
    Information("Full build meta {0}", versionInfo.FullBuildMetaData);
    Information("Build meta padded {0}", versionInfo.BuildMetaDataPadded);
    Information("Build meta {0}", versionInfo.BuildMetaData);
    Information("Building version {0}", versionInfo.FullSemVer);
    Information("Semver {0}", versionInfo.SemVer);
    Information("LegacySemver {0}", versionInfo.LegacySemVer);
    Information("NuGet version {0}", versionInfo.NuGetVersion);
    Information("NuGet version v2 {0}", versionInfo.NuGetVersionV2);

    if(isRunningOnTeamCity)
    {
        Information(
            @"Environment:
            PullRequest: {0}
            Build Configuration Name: {1}
            TeamCity Project Name: {2}",
            BuildSystem.TeamCity.Environment.PullRequest.IsPullRequest,
            BuildSystem.TeamCity.Environment.Build.BuildConfName,
            BuildSystem.TeamCity.Environment.Project.Name
        );

        BuildSystem.TeamCity.SetBuildNumber(versionInfo.FullSemVer);
    }
    else
    {
        Information("Not running on TeamCity");
    }
});


Task("Build")
    .IsDependentOn("Restore-NuGet-Packages")
    .IsDependentOn("Set-Version")
    .Does(() =>
{
    var settings = new MSBuildSettings {

        Verbosity = Verbosity.Normal,
        ToolVersion = MSBuildToolVersion.VS2017,
        Configuration = configuration,
        PlatformTarget = PlatformTarget.MSIL
    }
    .WithProperty("RunOctoPack", "true")
    .WithProperty("OctoPackPackageVersion", versionInfo.NuGetVersionV2)
    .WithProperty("OctoPackEnforceAddingFiles", "true")
    .WithProperty("OctoPackNuGetExePath", "C:/Apps/nuget.exe");

    MSBuild(solutionFile, settings);
});

Task("Copy-Packages")
    .Does(() => 
{

    var packageVersion = versionInfo.NuGetVersionV2;

    var modules = new [] 
    {
        "PowerSaverCore"
    };

    foreach (var module in modules)
    {
        var directory = Directory("./src/" + module + "/obj/octopacked");
        var packageFile = directory + File(module + "." + packageVersion + ".nupkg");

        CopyFileToDirectory(packageFile, distDir);
    }
});

Task("Package")
    .IsDependentOn("Build")
    .IsDependentOn("Copy-Packages");

Task("Run-Tests")
    .Description("Executes xUnit tests")
    .Does(() =>
{
    // See https://github.com/cake-build/cake/issues/1577
    var testProjects = GetFiles("./test/**/*UnitTests.csproj");

    foreach(var testProject in testProjects)
    {
        DotNetCoreTool(testProject, "xunit", "-xml report.xml");
    }
});


// Targets

Task("Default")
    .IsDependentOn("Package")
    .IsDependentOn("Run-Tests");

RunTarget(target);

Solution

The building script was creating an AssemblyInfo.cs file by itself. We just had to delete the existing AssemblyInfo.cs file.

mcastron
  • 41
  • 3
  • 2
    Are you using the new `.csproj` format or the old? Is the generation of these attributes disabled? Is this .NET Framework, .NET Core, or .NET Standard, or other? What version of Visual Studio? Of TeamCity? Are you using assembly version patching in TeamCity? Are you using `dotnet build`, `dotnet msbuild`, `msbuild`, or some TeamCity plugin? This question barely has *any* of the information needed to provide an answer. – NightOwl888 Mar 07 '18 at 16:16
  • .csproj is new format, the attributes mentioned in the error message are already defined in an AssemblyInfo.cs file, it is .NET Framework 4.6.1 project, and I use Visual Studio Community 2017 (15.5.6) + TeamCity Professional 2017.1.4 (build 47070) (no plugin used), and the project is compiled with MSBuild. – mcastron Mar 08 '18 at 08:35
  • 2
    Thanks. Please edit your question to include that important info. Best guess with this info is you have some sort of build script overwriting the `AssemblyInfo.cs` file, but it is leaving the `using` directives out of the file. Please also include any scripts TeamCity might be executing in your question. – NightOwl888 Mar 08 '18 at 10:19
  • Thanks. I updated the main post and also added the Cake script we use to build it. I think you are right, it seems that there is a section called "Set-Version", it's probably a good start to investigate. – mcastron Mar 08 '18 at 10:36
  • Did you ever got a fix on this? I am facing the same problem, but can't find any valid fixes. – Lucas Araujo Jan 15 '19 at 11:51

0 Answers0