cake-build / cake-build/cake

Incorrect translation of literal constants used as optional parameter values (when constants are defined in a separate file)

Open
#2,646 1 comment 0 reactions 1 assignee Claimed by @bjorkstromm View on GitHub
Dominant language
C#
Stars
4.2k
Forks
778
Avg merge
3h 37m
Merged PRs (30d)
21

Description

### What You Are Seeing?
When executing a Cake script that includes a custom add-in, the following error is shown:

Compiling build script...
Error: Cake.Core.CakeException: Error(s) occurred when compiling build script:
(2955,122): error CS1009: Unrecognized escape sequence
at Cake.Scripting.Roslyn.RoslynScriptSession.Execute(Script script)
at Cake.Core.Scripting.ScriptRunner.Run(IScriptHost host, FilePath scriptPath, IDictionary`2 arguments)
at Cake.Commands.BuildCommand.Execute(CakeOptions options)
at Cake.CakeApplication.Run(CakeOptions options)
at Cake.Program.Main()

### What is Expected?
The compilation and execution of the build script.

### What version of Cake are you using?
I have seen this error with versions 0.29.0, 0.33.0 and 0.35.0

### Are you running on a 32 or 64 bit system?
I am running a 64-bit system.

### What environment are you running on? Windows? Linux? Mac?
The environment we are running on, is a Windows environment.

### Are you running on a CI Server? If so, which one?
The build is both failing on a local machine, as well as on our TeamCity build server.

### Additional troubleshooting information

After cloning the Cake github repository, I was able to take a look at the build script that was executed. (Having this in a file for diagnostic purposes would be extremely helpful ;))

This pointed me to a method in our add-in that contained the following signature:

##### Class A:
[CakeMethodAlias]
public static void DoSomething(this ICakeContext context, string value = Constants.DefaultValue) { ... }

###### Class B ('Constants'):
public const string DefaultValue = @".\Output";

In the script this got translated to:
[CakeMethodAlias]
public static void DoSomething(this ICakeContext context, string value = ".\Output") { ... }

Note the absence of the literal character '@'.

I managed to work-around this problem by using either of the following implementations in the add-in:

#### a)
##### Class A:
[CakeMethodAlias]
public static void DoSomething(this ICakeContext context, string value = default(string))
{
value = value ?? Constants.DefaultValue;
...
}

##### Class B ('Constants'):
public const string DefaultValue = @".\Output";

#### b)
##### Class A:
private const string DefaultValue = @".\Output";

[CakeMethodAlias]
public static void DoSomething(this ICakeContext context, string value = DefaultValue) { ... }

Contributor guide

Open the contributing guide

Assessment

This issue has not been assessed yet.

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.