Skip to content

SONARJAVA-7259 Introduce a compiler-neutral frontend backend with scoped resource management - #6432

Closed
aurelien-coet-sonarsource wants to merge 6 commits into
ac/java-psi-frontendfrom
ac/java-psi-frontend-1
Closed

aurelien-coet-sonarsource wants to merge 6 commits into
ac/java-psi-frontendfrom
ac/java-psi-frontend-1

Conversation

@aurelien-coet-sonarsource

@aurelien-coet-sonarsource aurelien-coet-sonarsource commented Oct 29, 2026 •

Copy link
Copy Markdown
Contributor

Summary by Gitar

  • Frontend backend abstraction:
    • Added FrontendBackend, EcjFrontendBackend, and FrontendSession to own parser configuration and parse lifecycle.
    • Introduced FrontendResources so cleanup actions run on success, failure, and cancellation.
    • Added FrontendParser, FrontendConfiguration, and FrontendBatch as entry points for parsing and batch inputs.
  • Batch and scanner integration:
    • JParserConfig, JavaFrontend, and JavaAstScanner now parse through sessions and owned resources.
    • JSema registers its environment cleaner on the owning resource set.
  • Test infrastructure:
    • Added TestResourceScope and SemanticFixture for scoped parser fixtures in tests.
    • Added FrontendContractSnapshot and FrontendLifecycleTest to check frontend output and lifecycle behavior.
  • Packaging:
    • Bundled apache.txt license text and redirected Apache license URLs in sonar-java-plugin/pom.xml.

This will update automatically on new commits.

@hashicorp-vault-sonar-prod hashicorp-vault-sonar-prod Bot changed the title Introduce a compiler-neutral frontend backend with scoped resource management SONARJAVA-7259 Introduce a compiler-neutral frontend backend with scoped resource management Oct 29, 2026
@hashicorp-vault-sonar-prod

hashicorp-vault-sonar-prod Bot commented Oct 29, 2026 •

Copy link
Copy Markdown
Contributor

SONARJAVA-7259

@aurelien-coet-sonarsource
aurelien-coet-sonarsource force-pushed the ac/java-psi-frontend-1 branch 3 times, most recently from c922a82 to 21c6673 Compare October 29, 2026 14:24
@aurelien-coet-sonarsource

Copy link
Copy Markdown
Contributor Author

/pvf

@hashicorp-vault-sonar-prod

Copy link
Copy Markdown
Contributor

📊 Performance Validation complete
🆕 0 new · 🗑️ 0 lost issues
View dashboard

Resolve Apache license URLs through the existing local license-resource mechanism so normal packaging can validate its generated licenses.
Add repeatable snapshots of trees, tokens, semantics, diagnostics and scanner issues, plus explicit generic symbol and type identity assertions.
Route ordinary parser helpers through the neutral frontend facade and retain sessions through lazy assertions. Keep compiler-specific probes and capture batch metadata before session construction to preserve failure behavior.
@gitar-bot

gitar-bot Bot commented Nov 2, 2026

Copy link
Copy Markdown
Code Review ✅ Approved 2 closed / 2 findings

🟡 Medium risk · Refactoring parsing into backend-owned sessions changes AST and semantic resource lifecycles, which could leak or prematurely close resources during analysis.

Introduces a compiler-neutral frontend backend with scoped resource management, routing parsing through owned sessions so cleanup runs on success, failure, and cancellation. Resolved the cleaner Error handling that skipped remaining cleaners and the session leak when parsing throws an Error. No open issues found.

✅ 2 closed
✅ Bug: Cleaner Error now skips and discards all remaining cleaners

📄 java-frontend/src/main/java/org/sonar/java/model/FrontendResources.java:39-53 📄 java-checks-test-sources/test-classpath-reader/src/main/java/org/sonar/java/test/TestResourceScope.java:88-102 📄 java-frontend/src/test/java/org/sonar/java/model/FrontendLifecycleTest.java:67-79
Before this commit, FrontendResources.close() caught RuntimeException | Error, ran every cleaner, and then rethrew. Now it catches only RuntimeException. If a cleaner throws an Error (StackOverflowError, OutOfMemoryError, AssertionError, LinkageError), the loop exits right away, and the finally { cleaners.clear(); } together with closed = true drops the cleaners that haven't run yet. They can never be retried. Any RuntimeException already collected is also lost, as the new test's hasNoSuppressedExceptions() shows. In production this resource set holds every ECJ environment cleaner for a whole batch (JavaFrontend.scanBatch's environmentsCleaners, plus the session's own cleaners in Batch.parse), so one failing cleaner leaks the LookupEnvironments of every later file in the batch. That goes against the class's stated job of owning cleanup handles, and against the earlier commit's "attempts every resource" guarantee. TestResourceScope.Scope.close() got the same change: an Error from one fixture stops the remaining fixtures from closing, and resources.clear() then discards them. The commit swaps this guarantee for a quality-gate fix (S1181). A better fix is to keep catch (RuntimeException | Error e) with a justified // NOSONAR, or to wrap the loop body in try/finally so an Error still lets the remaining cleaners run.

✅ Bug: FrontendParser.parse leaks the session when parsing throws an Error

📄 java-frontend/src/main/java/org/sonar/java/model/FrontendParser.java:33-47 📄 java-frontend/src/test/java/org/sonar/java/model/FrontendLifecycleTest.java:107-118
FrontendParser.parse now catches only RuntimeException. When session.parse throws an Error, for example a StackOverflowError while converting deeply nested code, the session that backend.open opened is never closed. The caller gets no ParsedFile back, so it has no handle to close it either. For the ECJ backend, JParser.parse registers the environment cleaner before conversion runs (see ecj_registers_environment_before_syntax_conversion_failure), so that LookupEnvironment leaks. The new test parse_errors_propagate_directly asserts session.closed is zero, which writes the leak into the contract. A fix is a success flag with finally { if (!success) session.close(); } for the Error path, keeping the RuntimeException catch so cleanup failures are still added as suppressed.

Verification Repos 1 inspected · PRs 1 inspected

Functional validation No results

Rules 1 not applicable

Broader impact Repos 1 inspected PRs 1 inspected
Cross-repo 1 repository inspected 0 open impacts

SonarSource/sonar-java-symbolic-execution Inspected

Cross-PR 1 PR inspected 0 open impacts

PR 6416: SONARJAVA-7252 Decouple the Java frontend from Eclipse JDT core Inspected

Auto-approval Not enabled

Set up

For more verification details, comment gitar display:verbose.

Options

Auto-apply is off → Gitar will not commit updates to this branch.
Display: compact → Showing compact verification results.

Comment with these commands to change the behavior for this request:

Auto-apply Compact
gitar auto-apply:on         
gitar display:verbose         

Was this helpful? React with 👍 / 👎 | Gitar

@sonarqube-next

sonarqube-next Bot commented Nov 2, 2026

Copy link
Copy Markdown
Contributor

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant