Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix: support Windows line-endings in TOML files #936

Merged
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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