Skip to content

[ISSUE]: Formatting does not support format specifier "D" #4788

@BCSharp

Description

@BCSharp

Prerequisites

  • I have written a descriptive issue title
  • I have searched issues to ensure it has not already been reported

GitVersion package

GitVersion.Tool

GitVersion version

6.5.1+Branch.main.Sha.01c42647ce3d6db32caf7a0e0ed615a645de7b9a

Operating system

Windows

What are you seeing?

One of the commandline options is /format, which allows to output formatted variables. From the output of dotnet-gitversion /h:

    /format    Used in conjunction with /output json, will output a format
               containing version variables.
               Supports C# format strings - see [Format Strings](/docs/reference/custom-formatting) for details.
               E.g. /output json /format {SemVer} - will output `1.2.3+beta.4`
                    /output json /format {Major}.{Minor} - will output `1.2`

There is no /docs/reference/custom-formatting describing Format Strings for v6.x, but for v5.12.0 is seems to be a subset of Standard .NET Format Strings.

Unfortunately, this does not work on v6 with format the decimal format string "D".

What is expected?

If variable Major is 1 and Minor is 2, running

dotnet-gitversion /output json /format "{Major}.{Minor:D2}"

should produce

102

Steps to Reproduce

C:\my-project> dotnet-gitversion /output json /format "{Major}.{Minor:D2}"
INFO [25-12-14 19:50:22:54] Applicable build agent found: 'LocalBuild'.
INFO [25-12-14 19:50:22:60] Working directory: C:\my-project
INFO [25-12-14 19:50:22:63] Project root is: C:\my-project\
INFO [25-12-14 19:50:22:63] DotGit directory is: C:\my-project\.git
INFO [25-12-14 19:50:22:63] Branch from build environment:
INFO [25-12-14 19:50:22:65] Found configuration file at 'C:\my-project\GitVersion.yml'
INFO [25-12-14 19:50:22:69] -< Begin: Loading version variables from disk cache file C:\my-project\.git\gitversion_cache\CC1EF9659E7B62C8EEB864D6FAE080794DC06751 >-
INFO [25-12-14 19:50:22:73] -< End: Loading version variables from disk cache file C:\my-project\.git\gitversion_cache\CC1EF9659E7B62C8EEB864D6FAE080794DC06751 (Took: 34.08ms) >-
INFO [25-12-14 19:50:22:73] Found configuration file at 'C:\my-project\GitVersion.yml'
INFO [25-12-14 19:50:22:73] Using configuration file 'C:\my-project\GitVersion.yml'
ERROR [25-12-14 19:50:23:00] An unexpected error occurred:
System.FormatException: Format specifier was invalid.
   at System.Number.NumberToString[TChar](ValueListBuilder`1& vlb, NumberBuffer& number, Char format, Int32 nMaxDigits, NumberFormatInfo info)
   at System.Number.FormatDecimal(Decimal value, ReadOnlySpan`1 format, NumberFormatInfo info)
   at System.Decimal.ToString(String format, IFormatProvider provider)
   at GitVersion.Formatting.NumericFormatter.TryFormat(Object value, String format, CultureInfo cultureInfo, String& result) in /_/src/GitVersion.Core/Formatting/NumericFormatter.cs:line 40
   at GitVersion.Formatting.InvariantFormatter.TryFormat(Object value, String format, String& result) in /_/src/GitVersion.Core/Formatting/InvariantFormatter.cs:line 8
   at GitVersion.Formatting.ValueFormatter.TryFormat(Object value, String format, CultureInfo cultureInfo, String& result) in /_/src/GitVersion.Core/Formatting/ValueFormatter.cs:line 30
   at GitVersion.Formatting.InvariantFormatter.TryFormat(Object value, String format, String& result) in /_/src/GitVersion.Core/Formatting/InvariantFormatter.cs:line 8
   at GitVersion.Formatting.StringFormatWithExtension.EvaluateMember[T](T source, String member, String format, String fallback) in /_/src/GitVersion.Core/Formatting/StringFormatWithExtension.cs:line 95
   at GitVersion.Formatting.StringFormatWithExtension.EvaluateMatch[T](Match match, T source, IEnvironment environment) in /_/src/GitVersion.Core/Formatting/StringFormatWithExtension.cs:line 71
   at GitVersion.Formatting.StringFormatWithExtension.FormatWith[T](String template, T source, IEnvironment environment) in /_/src/GitVersion.Core/Formatting/StringFormatWithExtension.cs:line 50
   at GitVersion.Output.OutputGenerator.OutputGenerator.Execute(GitVersionVariables variables, OutputContext context) in /_/src/GitVersion.Output/OutputGenerator/OutputGenerator.cs:line 96
   at GitVersion.GitVersionOutputTool.OutputVariables(GitVersionVariables variables, Boolean updateBuildNumber) in /_/src/GitVersion.Output/GitVersionOutputTool.cs:line 31
   at GitVersion.GitVersionExecutor.RunGitVersionTool(GitVersionOptions gitVersionOptions) in /_/src/GitVersion.App/GitVersionExecutor.cs:line 73
INFO [25-12-14 19:50:23:00] Please run `git log --graph --format="%h %cr %d" --decorate --date=relative --all --remotes=*` to see the git graph. This can help you troubleshoot any issues.

RepositoryFixture Test

No response

Output log or link to your CI build (if appropriate).

Metadata

Metadata

Assignees

No one assigned

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions