{"record":{"id":"57db7d0e88df7619","repo":"apple/pkl","slug":"interpolationinconstant-57db7d","errorCode":"interpolationInConstant","errorMessage":"String constant cannot have interpolated values.","messagePattern":"String constant cannot have interpolated values\\.","errorType":"exception","errorClass":"ParserError","httpStatus":null,"severity":"error","filePath":"pkl-parser/src/main/java/org/pkl/parser/ParserImpl.java","lineNumber":1634,"sourceCode":"        }\n        case STRING_ESCAPE_QUOTE -> {\n          next();\n          builder.append('\"');\n        }\n        case STRING_ESCAPE_BACKSLASH -> {\n          next();\n          builder.append('\\\\');\n        }\n        case STRING_ESCAPE_RETURN -> {\n          next();\n          builder.append('\\r');\n        }\n        case STRING_ESCAPE_UNICODE -> builder.append(parseUnicodeEscape(next()));\n        case EOF -> {\n          var delimiter = new StringBuilder(startTk.text(lexer)).reverse().toString();\n          throw parserError(\"missingDelimiter\", delimiter);\n        }\n        case INTERPOLATION_START -> throw parserError(\"interpolationInConstant\");\n        // the lexer makes sure we only get the above tokens inside a string\n        default -> throw new RuntimeException(\"Unreacheable code\");\n      }\n    }\n    var end = next().span;\n    return new StringConstant(builder.toString(), start.endWith(end));\n  }\n\n  private String getEscapeText(FullToken tk) {\n    return switch (tk.token) {\n      case STRING_ESCAPE_NEWLINE -> \"\\n\";\n      case STRING_ESCAPE_QUOTE -> \"\\\"\";\n      case STRING_ESCAPE_BACKSLASH -> \"\\\\\";\n      case STRING_ESCAPE_TAB -> \"\\t\";\n      case STRING_ESCAPE_RETURN -> \"\\r\";\n      case STRING_ESCAPE_CONTINUATION -> \"\";\n      case STRING_ESCAPE_UNICODE -> parseUnicodeEscape(tk);\n      default -> throw new RuntimeException(\"Unreacheable code\");","sourceCodeStart":1616,"sourceCodeEnd":1652,"githubUrl":"https://github.com/apple/pkl/blob/f3efcbfc9b60d30053b0536d664948d7aa1b8673/pkl-parser/src/main/java/org/pkl/parser/ParserImpl.java#L1616-L1652","documentation":"The Pkl parser throws this when a string constant (e.g. a module/property name such as `foo` in backticks-less plain constant strings) contains an interpolation sequence (`\\(...)`) which is only allowed in string literals, not in string constants used as identifiers. The lexer delivers an INTERPOLATION_START token inside the constant string and the parser rejects it.","triggerScenarios":"Writing an interpolated expression like `\"value \\(1+1)\"`-style `\\(...)` inside a string constant that the grammar requires to be constant, e.g. a module name, property name, or other identifier-position string.","commonSituations":"Dynamically constructing a property/module name or AMQP-like constant string with a template expression; copy-pasting a normal string literal into an identifier position; refactoring code that moved a computed name into a constant position.","solutions":["Remove the `\\(...)` interpolation and write the fully expanded string constant literally","Compute the name in Pkl code instead (e.g. build the string as a value and use a computed property / dynamic object key if the grammar allows)","If the target API requires an identifier, generate the source text with a script rather than interpolating in the constant"],"exampleFix":"// before\nfoo\\(bar) = 1   // string constant with interpolation\n// after\nfoobar = 1      // or compute the name in a dynamic object","handlingStrategy":"validation","validationCode":"// before invoking the parser, reject interpolations in constant strings\nconst INTERP = /\\\\\\(/;\nfunction assertConstantString(s) {\n  if (INTERP.test(s)) throw new Error(`String constant cannot have interpolated values: ${s}`);\n}","typeGuard":"function isConstantString(tok) {\n  return typeof tok === 'string' && !tok.includes('\\\\(');\n}","tryCatchPattern":"try {\n  parser.parseModule(source);\n} catch (e) {\n  if (e.code === 'interpolationInConstant') {\n    console.error(`Replace \\\\(...) in the string constant at ${e.span}`);\n  } else throw e;\n}","preventionTips":["Never use \\\\(...) inside identifier-position strings (module/property names)","Generate dynamic names via computed properties or dynamic objects instead","Run `pkl format`/lint in CI to catch constant-string mistakes early"],"tags":["parser","pkl","string-constant","interpolation"],"backgroundTag":"invalid-argument-format","analyzedSha":"f3efcbfc9b60d30053b0536d664948d7aa1b8673","analyzedAt":"2026-09-08T13:10:45.570Z","contentChangedAt":"2026-09-08T13:10:45.570Z","schemaVersion":2},"datasetVersion":"2026-09-17T15:17:12.973Z"}