{"record":{"id":"fb6f3af24dd30792","repo":"apple/pkl","slug":"failed-to-convert-pkl-base-string-to-java-util","errorCode":null,"errorMessage":"Failed to convert `pkl.base#String` to `java.util.regex.Pattern`.","messagePattern":"Failed to convert `pkl\\.base#String` to `java\\.util\\.regex\\.Pattern`\\.","errorType":"exception","errorClass":"ConversionException","httpStatus":null,"severity":"error","filePath":"pkl-config-java/src/main/java/org/pkl/config/java/mapper/Conversions.java","lineNumber":196,"sourceCode":"          (value, mapper) -> {\n            try {\n              return Path.of(value);\n            } catch (InvalidPathException e) {\n              throw new ConversionException(\n                  \"Failed to convert `pkl.base#String` to `java.nio.file.Path`.\", e);\n            }\n          });\n\n  /** Conversion from {@code pkl.base#String} to {@link Pattern}. */\n  public static final Conversion<String, Pattern> pStringToPattern =\n      Conversion.of(\n          PClassInfo.String,\n          Pattern.class,\n          (value, mapper) -> {\n            try {\n              return Pattern.compile(value);\n            } catch (PatternSyntaxException e) {\n              throw new ConversionException(\n                  \"Failed to convert `pkl.base#String` to `java.util.regex.Pattern`.\", e);\n            }\n          });\n\n  /** Conversion from {@code pkl.base#Regex} to {@link String}. */\n  public static final Conversion<Pattern, String> pRegexToString =\n      Conversion.of(PClassInfo.Regex, String.class, (value, mapper) -> value.pattern());\n\n  /** Conversion from {@code pkl.base#Duration} to {@link java.time.Duration}. */\n  public static final Conversion<Duration, java.time.Duration> pDurationToDuration =\n      Conversion.of(\n          PClassInfo.Duration, java.time.Duration.class, (value, mapper) -> value.toJavaDuration());\n\n  /** Conversion from {@code pkl.semver#Version} to {@link Version}. */\n  // Cannot leave this to `ConverterFactories.pObjectToDataObject`\n  // because `Version` is part of pkl-core and thus cannot be annotated with `@Named`.\n  public static final Conversion<PObject, Version> pVersionToVersion =\n      Conversion.of(","sourceCodeStart":178,"sourceCodeEnd":214,"githubUrl":"https://github.com/apple/pkl/blob/f3efcbfc9b60d30053b0536d664948d7aa1b8673/pkl-config-java/src/main/java/org/pkl/config/java/mapper/Conversions.java#L178-L214","documentation":"Thrown by Conversions.pStringToPattern when mapping a Pkl `pkl.base#String` to a `java.util.regex.Pattern`. The string is not a syntactically valid Java regular expression, so `Pattern.compile(value)` throws PatternSyntaxException, wrapped in a ConversionException.","triggerScenarios":"Mapping to a target type with a `java.util.regex.Pattern` field (or calling Conversions.pStringToPattern directly) where the Pkl string has unbalanced groups/brackets, a dangling escape (`\"\\\\\"`), a bad quantifier (`\"*abc\"`, `\"a{2,1}\"`), or uses syntax from another regex flavor unsupported by java.util.regex (e.g. lookbehind of unbounded length on old JDKs, `\\p{...}` sets not supported).","commonSituations":"Patterns copied from PCRE/JS/Python flavors (e.g. `\\d` is fine but `(?'name'...)` is not); hand-edited patterns missing a closing `)` or `]`; shell glob patterns (`*.log`) mistakenly used where a regex is required.","solutions":["Fix the regex string in the Pkl config: balance parentheses/brackets, remove dangling backslashes, and ensure quantifiers follow a valid element.","Translate non-Java regex flavor constructs to java.util.regex equivalents (e.g. `(?:...)` named groups `(?<name>...)`).","Pre-validate with `Pattern.compile(value)` in a try/catch before mapping to get the exact error index and description.","If a glob is intended, convert it to a regex (e.g. quote literals and replace `*` with `.*`) before mapping."],"exampleFix":"// before (pkl)\nincludePattern = \"*.(txt|log\"\n// after (pkl)\nincludePattern = \".*\\\\.(txt|log)\"","handlingStrategy":"validation","validationCode":"try { java.util.regex.Pattern.compile(pklStringValue); } catch (java.util.regex.PatternSyntaxException e) { throw new IllegalStateException(\"Invalid regex in config: \" + pklStringValue + \" (\" + e.getDescription() + \" near index \" + e.getIndex() + \")\"); }","typeGuard":"static boolean isValidRegex(String s) { try { java.util.regex.Pattern.compile(s); return true; } catch (java.util.regex.PatternSyntaxException e) { return false; } }","tryCatchPattern":"try { MyConfig cfg = mapper.map(module, MyConfig.class); } catch (ConversionException e) { log.error(\"Config regex does not compile: {}\", e.getCause() != null ? e.getCause().getMessage() : e.getMessage()); }","preventionTips":["Test regex strings in a Java regex tool before adding them to config","Balance all groups and character classes; escape literal special chars with \\\\","Convert globs (*.log) to regex explicitly","Beware of regex flavors: java.util.regex is not full PCRE"],"tags":["pkl","java","regex","conversion","value-format"],"backgroundTag":"invalid-regex-pattern","analyzedSha":"f3efcbfc9b60d30053b0536d664948d7aa1b8673","analyzedAt":"2026-09-08T13:10:45.570Z","contentChangedAt":"2026-09-08T13:10:45.570Z","schemaVersion":2},"datasetVersion":"2026-09-14T16:17:12.679Z"}