Skip to content

Commit

Permalink
fix: support Windows line-endings in TOML files (#936)
Browse files Browse the repository at this point in the history
The TOML parser that Wrangler uses crashes if there is a Windows line-ending in a comment.
See iarna/iarna-toml#33.

According to the TOML spec, we should be able to normalize line-endings as we see fit.
See https://toml.io/en/v1.0.0#:~:text=normalize%20newline%20to%20whatever%20makes%20sense.

This change normalizes line-endings of TOML strings before parsing to avoid hitting this bug.

Fixes #915
  • Loading branch information
petebacondarwin authored May 9, 2022
1 parent 1b5f1bd commit a0e0b26
Show file tree
Hide file tree
Showing 3 changed files with 22 additions and 1 deletion.
15 changes: 15 additions & 0 deletions .changeset/wise-lies-visit.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
---
"wrangler": patch
---

fix: support Windows line-endings in TOML files

The TOML parser that Wrangler uses crashes if there is a Windows line-ending in a comment.
See https://github.com/iarna/iarna-toml/issues/33.

According to the TOML spec, we should be able to normalize line-endings as we see fit.
See https://toml.io/en/v1.0.0#:~:text=normalize%20newline%20to%20whatever%20makes%20sense.

This change normalizes line-endings of TOML strings before parsing to avoid hitting this bug.

Fixes https://github.com/cloudflare/wrangler2/issues/915
4 changes: 4 additions & 0 deletions packages/wrangler/src/__tests__/parse.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -165,6 +165,10 @@ describe("parseTOML", () => {
});
}
});

it("should cope with Windows line-endings", () => {
expect(parseTOML("# A comment with a Windows line-ending\r\n")).toEqual({});
});
});

describe("parseJSON", () => {
Expand Down
4 changes: 3 additions & 1 deletion packages/wrangler/src/parse.ts
Original file line number Diff line number Diff line change
Expand Up @@ -77,7 +77,9 @@ type TomlError = Error & {
*/
export function parseTOML(input: string, file?: string): TOML.JsonMap | never {
try {
return TOML.parse(input);
// Normalize CRLF to LF to avoid hitting https://github.com/iarna/iarna-toml/issues/33.
const normalizedInput = input.replace(/\r\n$/g, "\n");
return TOML.parse(normalizedInput);
} catch (err) {
const { name, message, line, col } = err as TomlError;
if (name !== TOML_ERROR_NAME) {
Expand Down

0 comments on commit a0e0b26

Please sign in to comment.