aws-cdk icon indicating copy to clipboard operation
aws-cdk copied to clipboard

CloudFront.Function: Uploading from file prefixes strange character

Open peterghu opened this issue 2 years ago • 2 comments

Describe the bug

This is in C#/.NET. When creating CloudFront function from a file, the Javascript uploaded is prefixed with a strange character that prevents the function from running correctly.

Expected Behavior

Code uploaded to the CloudFront function should be exactly the same and be able to test/execute successfully.

test-mapper.js:

function handler(event) {
  var request = event.request;
  return request;
}

Current Behavior

Code is uploaded with a strange space character in front and I cannot run the function.

Function code on CloudFront Functions page:

 function handler(event) {
  var request = event.request;
  return request;
}

image

If I click on 'Test' and try to run the function, I get the following error: Test execution failed: InternalError

To fix it, I can manually go back to the code, delete that space character at the start, Publish and then run it successfully.

Reproduction Steps

var testFunction = new Function(this, $"{ props.StackObjectPrefix }-test-mapper", new FunctionProps
            {
                Code = FunctionCode.FromFile(new FileCodeOptions() { FilePath = "./src/MyProject/Functions/test-mapper.js" }),
                Comment = "test-mapper",
            });

test-mapper.js:

function handler(event) {
  var request = event.request;
  return request;
}

Possible Solution

File parsing in the .NET CDK must be doing something strange. If I use FromInline() and read in Javascript code using the System.IO.File class, it works as expected and does not prefix with a space character.

FunctionCode.FromInline(File.ReadAllText("./src/MyProject/Functions/test-mapper.js", System.Text.Encoding.UTF8))

Additional Information/Context

No response

CDK CLI Version

2.37.1 (build f15dee0)

Framework Version

No response

Node.js Version

16.14.2

OS

Windows 10

Language

.NET

Language Version

.Net Core 3.1

Other information

No response

peterghu avatar Aug 17 '22 16:08 peterghu

This appears to be a UTF-8 BOM marker.

Is the original file encoded as UTF8 without BOM?

indrora avatar Sep 09 '22 19:09 indrora

This issue has not received a response in a while. If you want to keep this issue open, please leave a comment below and auto-close will be canceled.

github-actions[bot] avatar Sep 21 '22 20:09 github-actions[bot]