Fix incorrect diagnostic message when reserved keyword is used in module import#44541
Conversation
When a reserved keyword (e.g. 'client') is used in a module import path without the '^' escape prefix, BLangIdentifier.value is empty string while originalValue holds the raw source text. getQualifiedPackageName() was using id.value for all identifiers, causing the keyword to be silently dropped and producing misleading diagnostics like 'cannot resolve module ballerinax/.config' instead of the correct 'cannot resolve module ballerinax/client.config'. Fall back to originalValue when value is empty to preserve the keyword in the diagnostic message. Fixes: ballerina-platform#44509, ballerina-platform#44519
|
No actionable comments were generated in the recent review. 🎉 ℹ️ Recent review info⚙️ Run configurationConfiguration used: Organization UI Review profile: CHILL Plan: Pro Run ID: 📒 Files selected for processing (2)
📝 WalkthroughWalkthroughUpdated import name construction and qualified-name rendering to preserve original token text for parser-synthesized/missing identifiers (reserved-keyword cases); added a negative test project and a unit test validating diagnostic ordering and messages for such imports. ChangesReserved-keyword import parsing and rendering
Sequence Diagram(s)sequenceDiagram
participant Parser as Parser (Syntax)
participant NodeBuilder as BLangNodeBuilder
participant ImportPkg as BLangImportPackage
participant Diagnostics as Diagnostics Engine
participant TestRunner as Unit Test
Parser-->>NodeBuilder: produce ImportDeclarationNode (with possible missing name token)
NodeBuilder-->>ImportPkg: construct identifier components (use leadingInvalidTokens when missing)
ImportPkg-->>Diagnostics: compute qualified package name (use originalValue if value empty)
TestRunner->>Diagnostics: compile test project and collect diagnostics
Diagnostics-->>TestRunner: return ordered diagnostics (module-resolve -> parser tokens -> missing identifier)
Estimated code review effort🎯 3 (Moderate) | ⏱️ ~20 minutes Possibly related issues
Poem
🚥 Pre-merge checks | ✅ 4 | ❌ 1❌ Failed checks (1 warning)
✅ Passed checks (4 passed)
✏️ Tip: You can configure your own custom pre-merge checks in the settings. ✨ Finishing Touches🧪 Generate unit tests (beta)
Warning There were issues while running some tools. Please review the errors and either fix the tool's configuration or disable the tool if it's a critical failure. 🔧 ast-grep (0.42.1)compiler/ballerina-lang/src/main/java/org/wso2/ballerinalang/compiler/parser/BLangNodeBuilder.javaThanks for using CodeRabbit! It's free for OSS, and your support helps us grow. If you like it, consider giving us a shout-out. Review rate limit: 7/8 reviews remaining, refill in 7 minutes and 30 seconds.Comment |
Adds a test case verifying that when a reserved keyword (e.g. 'client') is used unescaped in a module import path, the diagnostic message correctly includes the keyword in the module path string. Regression test for ballerina-platform#44509 and ballerina-platform#44519.
aefade6 to
f346fd5
Compare
|
can I get a review for this 👀 @MaryamZi @gimantha @sameerajayasoma @hasithaa |
|
|
When a reserved keyword (e.g. 'client') is used unescaped in a module import path, the new-parser synthesises a MISSING identifier and attaches the keyword as leading invalid-token minutiae on the next token. BLangNodeBuilder was calling createIdentifier with the empty text of the MISSING token, leaving originalValue unset. Without originalValue the fallback in getQualifiedPackageName() had nothing to recover, so the keyword was silently dropped from the module name. Extract the keyword text from leadingInvalidTokens() when the name token is missing, and pass it as originalValue so the module name is preserved correctly in diagnostics. Also fix the regression-test assertions: diagnostics are emitted in source-position order (col 1 before col 19/25), and the parser places 'invalid token' at col 19 while 'missing identifier' appears at col 25 (the following dot token's position). Co-Authored-By: Claude Sonnet 4.6 <noreply@anthropic.com>
Purpose
When a reserved keyword (e.g.,
client) is used in a module import path without the^escape prefix, the compiler produces misleading diagnostics. For instance,import ballerinax/client.config;results in the errorcannot resolve module 'ballerinax/.config as config'. The reserved keyword is dropped from the module path in the diagnostic message becauseBLangIdentifier.valueis empty for unescaped reserved keywords.Fixes #44509
Fixes #44519
Approach
In
BLangImportPackage.getQualifiedPackageName(), updated the package name construction to fall back tooriginalValuewhen.valueis empty. This ensures that the original source text of the reserved keyword is preserved when formatting the qualified package name for diagnostic messages.Added a negative test case in ImportsNegativeTests.java to verify that the generated diagnostic includes the reserved keyword correctly stringified in the module path.
Samples
N/A
Remarks
N/A
Check List
Overview
This pull request fixes misleading compiler diagnostics that omitted unescaped reserved keywords (for example
client) from module import paths shown in error messages. The change ensures the original source text for such identifiers is preserved when the compiler formats qualified package names for diagnostics.Changes Made
ballerinax/client.config as config). Regression test assertions were adjusted to reflect parser-emitted diagnostic ordering and token positions.Impact
Improves the accuracy and clarity of compiler diagnostics for import errors involving unescaped reserved keywords, making troubleshooting more straightforward for developers.
Related issues: #44509, #44519