From 90e6b9587ac0bf46d2a7837ff0fa69f634872699 Mon Sep 17 00:00:00 2001 From: dpny518 Date: Thu, 20 Nov 2025 11:17:21 -0800 Subject: [PATCH 1/2] Improve parser error messages for templates This commit enhances parse error diagnostics to provide more helpful, context-specific error messages for common Daml template issues. Fixes #22354: When 'choice' is badly indented, the error now includes a hint about proper indentation with an example. Addresses #22361: For generic parse errors that commonly occur with missing key type signatures, the error now includes hints about common template parsing issues, including the requirement that 'key' needs an explicit type signature. The implementation adds a diagnostic post-processing layer in sendFileDiagnostics that pattern-matches specific error messages and enhances them with targeted hints, similar to how LF conversion errors are handled. --- .gitignore | 3 +- .../src/Development/IDE/Core/Rules/Daml.hs | 43 ++++++++++++++++++- 2 files changed, 44 insertions(+), 2 deletions(-) diff --git a/.gitignore b/.gitignore index 8751c9815ea2..aa953430f0ba 100644 --- a/.gitignore +++ b/.gitignore @@ -1,3 +1,4 @@ .envrc.private /.idea/ -.DS_Store \ No newline at end of file +.DS_Store +.github_token \ No newline at end of file diff --git a/sdk/compiler/damlc/daml-ide-core/src/Development/IDE/Core/Rules/Daml.hs b/sdk/compiler/damlc/daml-ide-core/src/Development/IDE/Core/Rules/Daml.hs index 46b599ed6e4e..a0c1e9602fc1 100644 --- a/sdk/compiler/damlc/daml-ide-core/src/Development/IDE/Core/Rules/Daml.hs +++ b/sdk/compiler/damlc/daml-ide-core/src/Development/IDE/Core/Rules/Daml.hs @@ -171,9 +171,50 @@ uriToVirtualResource uri = do $ BS.fromString $ URI.unEscapeString u +-- | Enhance parse error messages with more helpful, context-specific information +enhanceParseErrorDiagnostics :: [FileDiagnostic] -> [FileDiagnostic] +enhanceParseErrorDiagnostics = map enhanceSingleDiagnostic + where + enhanceSingleDiagnostic :: FileDiagnostic -> FileDiagnostic + enhanceSingleDiagnostic fd@(file, showDiag, diag@Diagnostic{..}) = + case _severity of + Just DsError | isParseError _message -> + let enhancedMessage = enhanceParseErrorMessage _message + in (file, showDiag, diag { _message = enhancedMessage }) + _ -> fd + + isParseError :: T.Text -> Bool + isParseError msg = "parse error" `T.isInfixOf` msg + + enhanceParseErrorMessage :: T.Text -> T.Text + enhanceParseErrorMessage msg + -- Issue #22354: Improve error for badly indented 'choice' + | "parse error on input 'choice'" `T.isInfixOf` msg = + msg <> "\n\nHint: This often occurs when 'choice' is not properly indented.\n" <> + "Make sure 'choice' is indented at the same level as other template clauses like 'signatory' and 'observer'.\n" <> + "Example:\n" <> + " template MyTemplate\n" <> + " with ...\n" <> + " where\n" <> + " signatory ...\n" <> + " choice MyChoice : ..." + + -- Issue #22361: Improve generic parse error with template-related hints + -- The missing key type signature produces a very generic error, so we provide + -- general hints that cover common template parsing issues + | "parse error (possibly incorrect indentation or mismatched brackets)" `T.isInfixOf` msg = + msg <> "\n\nHint: Common causes of this error in templates include:\n" <> + " - Missing type signature on 'key' (unlike 'signatory', 'key' requires a type)\n" <> + " Example: key owner : Party\n" <> + " - Incorrect indentation of template clauses\n" <> + " - Mismatched brackets or parentheses" + + | otherwise = msg + sendFileDiagnostics :: [FileDiagnostic] -> Action () sendFileDiagnostics diags = - mapM_ (uncurry sendDiagnostics) (groupSort $ map (\(file, _showDiag, diag) -> (file, diag)) diags) + let enhancedDiags = enhanceParseErrorDiagnostics diags + in mapM_ (uncurry sendDiagnostics) (groupSort $ map (\(file, _showDiag, diag) -> (file, diag)) enhancedDiags) sendDiagnostics :: NormalizedFilePath -> [Diagnostic] -> Action () sendDiagnostics fp diags = do From 84d7fd1db66db23da045c81eb02869ce12dabb1f Mon Sep 17 00:00:00 2001 From: Dhonam Pemba <5721671+dpny518@users.noreply.github.com> Date: Fri, 21 Nov 2025 04:04:27 +0700 Subject: [PATCH 2/2] Update .gitignore --- .gitignore | 1 - 1 file changed, 1 deletion(-) diff --git a/.gitignore b/.gitignore index aa953430f0ba..b98c2f34ff40 100644 --- a/.gitignore +++ b/.gitignore @@ -1,4 +1,3 @@ .envrc.private /.idea/ .DS_Store -.github_token \ No newline at end of file