Repository navigation
fix(lib): escape backslashes in HCL string literals - #593
Open
Ramnath0521 wants to merge 1 commit into
Open
Ramnath0521 wants to merge 1 commit into
Ramnath0521 wants to merge 1 commit into
Conversation
escapeQuotes only escaped quotes, so a Windows path such as the default
local backend path was written as "C:\Users\..." and Terraform rejected it
at init. A value ending in a backslash left the string unterminated.
Escape backslashes and quotes, and drop the lookbehind that skipped a quote
after a backslash. A string with ${...} interpolations, including locals and
outputs, keeps each interpolation as written and escapes the text around it.
Closes: open-constructs#562
Co-Authored-By: Claude Sonnet 5.5 <noreply@anthropic.com>
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Closes: #562
Summary
HCL synth copied backslashes in string values through unchanged, so a Windows path rendered as
"C:\Users\me\project\terraform.tfstate". Terraform and OpenTofu reject that atinit(The \U escape sequence must be followed by eight hexadecimal digits). The defaultLocalBackendpath ispath.join(process.cwd(), ...), so every HCL synth on Windows with the default backend produced acdk.tfthat cannot initialise. A value ending in a backslash left the string unterminated.Change
In
packages/cdktn/src/hcl/render.ts,escapeQuotesnow escapes both\and", so Terraform reads the value back as written (the same result asJSON.stringify). The old(?<!\\)lookbehind is gone: once backslashes are escaped, a backslash before a quote no longer means the quote is already escaped.A string that contains
${...}used to skip escaping entirely, and locals and outputs also wrote such strings without any escaping. Both now go through the same function, which keeps each${...}interpolation as written and escapes only the text around it. The scanner follows nested braces and quoted strings inside the interpolation, so${lookup({a = "}"}, "a")}is not cut short, and leaves$${alone.Tests
Seven tests in
test/json-to-hcl.test.ts: a Windows path, a trailing backslash, a quote after a backslash, backslashes around an interpolation, a quoted string inside an interpolation, a brace inside a quoted string inside an interpolation, and the defaultLocalBackendpath on Windows. Six fail onmainand pass with the change; the one with a quoted string inside an interpolation passes either way and pins that handling. All 47 tests injson-to-hcl.test.tspass, snapshots included (40/40).I ran the whole
cdktnpackage on Windows 11. 13 tests fail in 4 suites, with and without this change: noterraformbinary on PATH (matchers.test.ts) and no builtlibdirectory (theasset-*suites). Prettier and eslint are clean on both files. I did not runterraform initon the output; the tests compare the generated HCL text.Notes
\nmeant as a newline) is now written as a literal backslash andn. That follows the behaviour proposed in the issue, and a multi-line string still goes into a heredoc, which does not interpret escapes.🤖 Generated with Claude Code