Skip to content

Feature Request: Add MsBuildArgument supports for MSBuild special characters #2719

Open
@filzrev

Description

@filzrev

Currently MsBuildArgument constructor accept single string argument.
And this string value is passed to MSBuild command line parameter as is.

So when specified string contains MSBuild special characters
It needs manually escape these characters.
(e.g. When passing list values. It need to escape semicolon(;) char to %3B)

Is it able to handle these special characters inside MsBuildArgument class?
or add [MsBuildArgument constructor] that accept string params to handle multiple values?

Test Code

    internal class Program
    {
        static void Main(string[] args)
        {
            BenchmarkRunner.Run<Benchmarks>();
        }
    }

    [Config(typeof(CustomConfig))]
    public class Benchmarks
    {
        [Benchmark]
        public void Benchmark01()
        {
#if TEST1 && TEST2
            // DefineConstants symbols are defined as expected.
#else
            throw new Exception("DefineConstants value is unexpected value!");
#endif
        }

        public class CustomConfig : ManualConfig
        {
            public CustomConfig()
            {
                AddLogger(ConsoleLogger.Default);

                AddColumnProvider(DefaultConfig.Instance.GetColumnProviders().ToArray());
              
                AddJob(Job.ShortRun
                    .WithStrategy(RunStrategy.Monitoring)
                    .WithArguments(
                    [
                       // new MsBuildArgument("/p:DefineConstants=TEST1;TEST2"),     // Error: MSBUILD : error MSB1006: Property is not valid.
                       // new MsBuildArgument("/p:DefineConstants=\"TEST1;TEST2\""), // Double quote does not works.
                       new MsBuildArgument("/p:DefineConstants=TEST1%3BTEST2"),      // It works when using MSBuild special character
                    ])
                    .WithId("CustomJob"));

                WithOption(ConfigOptions.KeepBenchmarkFiles, true); // Use this settings for output to JobId folder
            }
        }
    }

Activity

timcassell

timcassell commented on Apr 23, 2025

@timcassell
Collaborator

I think it's reasonable to escape those characters in the MsBuildArgument ctor.

filzrev

filzrev commented on May 3, 2025

@filzrev
ContributorAuthor

Double quote does not works.

It seems double quote is stripped on .bat execution timing.
So, it need to escape " char also on Windows environment.
(e.g. new MsBuildArgument("/p:DefineConstants=\\\"TEST1;TEST2\\\""))

I'll try to create PR later to resolve this issue.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

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

      Feature Request: Add MsBuildArgument supports for MSBuild special characters · Issue #2719 · dotnet/BenchmarkDotNet