4

While updating our build incrementer program that runs during the pre-build event, I noticed a potential problem that can cause quite a bit of issues. Building the application the first time successfully updates BuildInfo.cs and calculates all of the const values. Each subsequent build and successful execution of the pre-build event updates the proper file (as provided below) but each computed const value is out of date from the last build.

// In externally modified file BuildInfo.cs which is updated by our pre-build
// tool to update the version information and produce new consts.
namespace ConstProblem.Properties {
  static class BuildInfo {
    internal const string AssemblyVersionString = "1.1.0.0";
    internal const string BuildDate = "2012-11-07T08:52:32.5480259-07:00";
    internal const string FileVersionString = "1.1.12312.852";
    internal const string Full = "v1.1 (Build: 2012-11-07 08:52)";
    internal const string Short = "1.1";
  }
}

// Program.cs. Reproduces the problem for this question.
namespace ConstProblem {
  class Program {
    const string UserAgent = "ConstProblem/" + Properties.BuildInfo.Short;

    static void Main() {
      System.Console.WriteLine(UserAgent);
    }
  }
}

As an example, the application was originally built with the AssemblyVersionString at 1.0.0.0. The above program ran and compiled as expected. Increasing this to 1.1 and building/running the application a second time produced ConstProblem/1.0 as it's output and has this as it's values

// From the Immediate Window
Properties.BuildInfo
ConstProblem.Properties.BuildInfo
    base {object}: object
    AssemblyVersionString: "1.1.0.0"
    BuildDate: "2012-11-07T08:51:46.8404556-07:00"
    FileVersionString: "1.0.12312.851"
    Full: "v1.0 (Build: 2012-11-07 08:51)"
    Short: "1.0"

As you can see, the AssemblyVersionString was updated 1.1.0.0 properly but the rest of the computed values did not. If I were to build and execute a third time (and increase to 1.2) they would update to the information provided above.

I have confirmed that the output file by the pre-build event outputs all of the information correctly and exits with a status of 0 to allow the build to continue. I am at a loss as to why the const's are constantly one build behind. The build utility is also something I wrote and it just uses a template and replaces the contents of the BuildInfo.cs if the file is checked-out.

My environment is running Visual Studio 2010 Ultimate and compiling in .Net 4. I've reproduced this in both Console and Web applications. I got the idea for using const values from the comments in How to get the assembly version and file version of your own assembly?

Joshua
  • 8,112
  • 3
  • 35
  • 40
  • Is this happening if you try to build from the command line? Or just within Visual Studio? If it's just from Visual Studio, there's a build option that may be screwing up... – sybkar Nov 07 '12 at 17:59

1 Answers1

0

It's reading the successful build increment from AssemblyInfo.cs But that only increments by 1 after a successful build, hence why your always 1 behind.

add [assembly: AssemblyVersion("1.star.star.star")] to AssemblyInfo.cs (in Solution/Properties)

^ star = * (editor limitation)
Fabio Antunes
  • 22,251
  • 15
  • 81
  • 96
FlemGrem
  • 814
  • 4
  • 9