From b939552714e772fe4041b22d82ecf114af4146be Mon Sep 17 00:00:00 2001 From: Erwan Le Forestier Date: Fri, 27 Nov 2026 16:12:55 +0100 Subject: [PATCH] AP-1542 Correct SonarJava contributor guidance --- .agents/skills/new-rule/SKILL.md | 20 +++++++++++++++++++- AGENTS.md | 8 ++++---- CONTRIBUTING.md | 5 +++-- docs/systems/build.md | 8 ++++++-- docs/systems/licenses.md | 2 +- docs/systems/rule-tests.md | 28 +++++++++------------------- docs/systems/verification.md | 2 ++ java-checks-test-sources/README.md | 2 +- 8 files changed, 45 insertions(+), 30 deletions(-) diff --git a/.agents/skills/new-rule/SKILL.md b/.agents/skills/new-rule/SKILL.md index d198df5238e..9a6f1f9fa03 100644 --- a/.agents/skills/new-rule/SKILL.md +++ b/.agents/skills/new-rule/SKILL.md @@ -8,6 +8,24 @@ metadata: # Implement a new SonarJava rule 1. **Generate rule inputs.** Confirm the rule key, title and RSPEC branch before starting. Follow `docs/systems/rule-metadata.md` to generate the descriptors and review profile membership. Do not invent metadata if the specification is unavailable. -2. **Write a reproducer.** Follow `docs/systems/rule-tests.md` for sample placement, semantic and nonsemantic cases, and issue locations. Add a minimal check with the rule key and confirm that the test fails because it expected a finding. Also include compliant lookalikes and cases where information is missing. +2. **Write a reproducer.** Follow [Rule tests and sample placement](../../../docs/systems/rule-tests.md) to choose a fixture module and [Rule test assertions](#rule-test-assertions) to write cases and mark issues. Add a minimal check with the rule key and confirm that the test fails because it expected a finding. Also include compliant lookalikes and cases where information is missing. 3. **Implement the check.** Follow `docs/systems/rule-api.md` to choose the visitor and match Java constructs. If the check needs cross-file evidence or a quick fix, read `docs/systems/project-level-rules.md` or `docs/systems/quickfix-tests.md` respectively. Run the targeted verifier test and compile changed sample sources. 4. **Validate on real projects.** When CI results are available, follow `docs/systems/ruling-tests.md` to review the generated baseline and evaluate precision. Add false positives as regression cases, refine the check, and review the next CI results rather than accepting unexpected findings. + +## Rule test assertions + +Use JUnit Jupiter and `java-checks-testkit/src/main/java/org/sonar/java/checks/verifier/CheckVerifier.java`. Cover triggering and similar-looking compliant cases. Every new rule needs a semantic test and a `withoutSemantic()` test. Call `verifyIssues()` when you expect findings and `verifyNoIssues()` when you expect none. In the nonsemantic test, assert any findings the rule can still report safely from syntax alone, but do not assume unresolved types. Share sample-path constants when tests load the same file. For rules gated on a library version, test absent or incompatible dependencies separately; `withoutSemantic()` does not simulate dependency version discovery. + +See `java-checks/src/test/java/org/sonar/java/checks/DurationTimeUnitAgreementCheckTest.java` for a rule that needs resolution, and `java-checks/src/test/java/org/sonar/java/checks/AbsOnNegativeCheckTest.java` for a rule that reports in both modes. + +Mark each expected issue with `// Noncompliant {{Expected message.}}`. On the line directly below each reported location, align carets under the exact span, for both primary and secondary locations. Add `<` after the secondary carets when the primary is above, or `>` when it is below: + +```java + new Thread(() -> { +// ^^^^^^> + Thread.sleep(1000); // Noncompliant {{Do not sleep here.}} +// ^^^^^ + }); +``` + +If carets cannot express the location (for example, a span at column 1 or across multiple lines), use explicit range attributes such as `sc`, `el`, and `ec`, or the `secondary` attribute to assert secondary lines. Use relative line offsets where possible. diff --git a/AGENTS.md b/AGENTS.md index e283b8205c7..57f970ae75d 100644 --- a/AGENTS.md +++ b/AGENTS.md @@ -12,18 +12,18 @@ All agent instructions start here; there are no component-level `AGENTS.md` file ## Commands -Read [Setup, build and test commands](docs/systems/build.md) before selecting a JDK or running Maven. +Read [Setup, build and test commands](docs/systems/build.md) before selecting a JDK or running Maven. From the root, run `mise install` and `mise run doctor` to check which tasks can run. Use mise to run Maven, not the system installation. ## Constraints - This repository is public. All agent files and docs are public. Do not publish internal URLs, customer data, credentials or private repository content. - Preserve public API compatibility and SonarQube for IDE support; read `docs/systems/public-api.md` before changing the API or plugin registration. - Use the supported build and runtime JDKs in `docs/systems/build.md`; CI exercises them for different purposes. -- The project uses SSALv1 (`LICENSE.txt`); read `docs/systems/licenses.md` before changing dependencies. +- The project uses SSALv1 (`LICENSE.txt`); read `docs/systems/licenses.md` before changing dependencies. Regenerate the bundled dependency licenses in `sonar-java-plugin/src/main/resources/licenses` instead of editing them by hand. - Do not edit generated rule metadata or the generated check list by hand. Read `docs/systems/rule-metadata.md` before registering rules or changing profiles. - New rules are stateless and must not introduce shared abstract check bases; read the decisions in `docs/adrs/index.md` before adding rule logic or unit tests. -- Use JUnit Jupiter for unit tests and cover rule behavior both with and without semantic resolution; read `docs/systems/rule-tests.md` before changing a rule or fixture. -- Let CI run ruling and generate baseline updates; read `docs/systems/ruling-tests.md` before reviewing SARIF changes. +- Use the [new-rule skill](.agents/skills/new-rule/SKILL.md#rule-test-assertions) for verifier assertions and issue markers. Read [rule tests and sample placement](docs/systems/rule-tests.md) before adding or moving a fixture. For an existing legacy example, see `java-checks/src/test/files/checks/AbsOnNegative.java`. +- Do not hand-edit ruling sources or change the pinned `its/sources` submodule revision as part of a rule change. Let CI run ruling and generate baseline updates; read `docs/systems/ruling-tests.md` before reviewing SARIF changes. ## Escalation diff --git a/CONTRIBUTING.md b/CONTRIBUTING.md index 877655452ba..a0db3a3e8f8 100644 --- a/CONTRIBUTING.md +++ b/CONTRIBUTING.md @@ -27,5 +27,6 @@ To run ITs locally, please follow the [README](https://github.com/SonarSource/so ### Clean commit history -To ease the review, please have a clean, minimal history of commits in your pull request. Your commits should have a single purpose. -This will help to make your contribution accepted as we like to keep a clean linear history and prefer rebase over merging commits. +Start the pull request title with the associated `SONARJAVA-XXXX` issue key and use the [pull request template](.github/PULL_REQUEST_TEMPLATE.md). If there is no Jira issue yet, follow the standalone PR instructions in the template rather than inventing a key. + +Keep commits focused and history linear. Rebase the branch onto the target branch instead of merging the target into it; integrate the PR by rebase only, without a merge or squash commit. diff --git a/docs/systems/build.md b/docs/systems/build.md index e1009e3d8ac..0ed34dd564e 100644 --- a/docs/systems/build.md +++ b/docs/systems/build.md @@ -18,7 +18,7 @@ mise run doctor mise run check ``` -`doctor` reports prerequisites for each task without installing tools or displaying credentials. Maven tasks require Repox access and Maven credentials. `check` runs the unit tests, dependency license check, custom-rules example and sanity test. Use `build` to install reactor artifacts without running tests. `test:unit` runs without JaCoCo unless you add `--coverage`. +`doctor` reports prerequisites for each task without installing tools or displaying credentials. It exits nonzero if any task is unavailable; check the status of the task you need. Maven tasks require Repox access and Maven credentials. Use a mise task when available. For ad hoc reactor Maven commands, select JDK 26 with mise as shown in [rule test verification](rule-tests.md#verify). `check` runs the unit tests, dependency license check, custom-rules example and sanity test. Use `build` to install reactor artifacts without running tests. `test:unit` runs without JaCoCo unless you add `--coverage`. ```bash mise run test:unit --module java-checks --test AbsOnNegativeCheckTest @@ -26,7 +26,7 @@ mise run check:changed mise run analyze:vortex ``` -The changed tasks include committed, staged, unstaged and untracked changes relative to the merge base with `origin/master`. Set `MISE_CHANGE_BASE` to the intended branch or commit if that ref is missing, stale or not your review base. A changed rule with a matching test runs that test; changes that cannot be mapped to a focused test use the relevant modules instead. Focused changed checks skip the reactor-wide license-header scan. Changed checks do not replace full CI. The sanity task uses a test scanner context, not SonarQube Server. +The changed tasks include committed, staged, unstaged and untracked changes relative to the merge base with the origin/master ref. Set `MISE_CHANGE_BASE` to the intended branch or commit if that ref is missing, stale or not your review base. A changed rule with a matching test runs that test; changes that cannot be mapped to a focused test use the relevant modules instead. Focused changed checks skip the reactor-wide license-header scan. Changed checks do not replace full CI. The sanity task uses a test scanner context, not SonarQube Server. `pom.xml` keeps the JAR manifest's `Build-Time` constant so identical inputs remain eligible for Develocity cache hits. Do not restore a time-varying value. @@ -41,3 +41,7 @@ mise run its:plugin --sq-version LATEST_RELEASE --test JavaTestSuite The full plugin and ruling suites need a GitHub token with read access to the QA licenses. A nonblank `GITHUB_TOKEN` overrides `github.token` from the Orchestrator file for these tests. `--community-only` skips the commercial JSP test but does not remove the license requirement for `JavaTestSuite`. Use Plugin QA in CI if those prerequisites are unavailable locally. `mise run ci` runs `check`, Plugin QA and both Linux ruling profiles. Ruling needs `git submodule update --init --recursive its/sources`. For authorized troubleshooting, use `mise run ruling without-sonarqube-project --rules S100,S101 --project guava`. Use CI for [ruling baselines and review](ruling-tests.md), not a local run. + +## What you cannot run locally + +`mise run ci` does not reproduce the Windows unit-test and Ruling QA jobs. It also does not deploy or promote candidate artifacts or upload failed ruling reports. Those steps need the operating system, CI credentials, or workflow context provided by the [build workflow](../../.github/workflows/build.yml); use the CI results for that evidence. diff --git a/docs/systems/licenses.md b/docs/systems/licenses.md index c623aaa8ee2..17ff1d43544 100644 --- a/docs/systems/licenses.md +++ b/docs/systems/licenses.md @@ -13,7 +13,7 @@ Run from the repository root after [toolchain setup](build.md): ```bash mise run lint:licenses -mise exec -- mvn clean package -PupdateLicenses +mise exec java@26 -- mvn clean package -PupdateLicenses ``` Review and commit the generated license changes. If a new dependency does not change the bundled licenses, explain why in the PR rather than editing generated files just to satisfy the check. The license goal runs in **Custom Rules and License Check** in `.github/workflows/build.yml`. diff --git a/docs/systems/rule-tests.md b/docs/systems/rule-tests.md index d645bef8231..4c91d3f08aa 100644 --- a/docs/systems/rule-tests.md +++ b/docs/systems/rule-tests.md @@ -20,32 +20,22 @@ For the default main-code case, name the sample after the check with a **Sample. Reuse the prepared test dependencies or use focused stubs. Do not add dependencies just to exercise an API when the existing test reactor can model it. -## Assert both analysis modes +## Write verifier assertions -Use JUnit Jupiter (see [JUnit 5](../adrs/junit-5.md)) and `java-checks-testkit/src/main/java/org/sonar/java/checks/verifier/CheckVerifier.java`. Every new rule needs a semantic test and a `withoutSemantic()` test. Resolution can fail when code is incomplete or the classpath is missing; check that the rule neither crashes nor speculates on unresolved types. Use `verifyNoIssues()` if the rule needs resolution, or `verifyIssues()` with the expected remaining findings if it can safely report from syntax alone. +Follow the [new-rule skill](../../.agents/skills/new-rule/SKILL.md#rule-test-assertions) for test cases, verifier methods, and primary and secondary location markers. See [Quick-fix tests](quickfix-tests.md) for fix declarations that accompany an issue marker. -For a concrete pair, see `java-checks/src/test/java/org/sonar/java/checks/DurationTimeUnitAgreementCheckTest.java`. Share sample-path constants when tests load the same sample. If behavior depends on a library version, add a case for an absent or incompatible dependency as well as the supported one; `withoutSemantic()` alone does not simulate a change to dependency version discovery. - -## Mark expected issues in the sample +## Verify -Cover both triggering and similar-looking compliant cases. Put `// Noncompliant {{Expected message.}}` on the line with an expected issue. Put carets directly below to assert the primary range; a bare marker cannot catch a regression that moves highlighting to the wrong expression. Mark secondary ranges with carets and a direction marker: `<` points back to a primary above and `>` points forward to a primary below. +From the repository root after [toolchain setup](build.md), run the targeted test and its prerequisite reactor modules. Substitute the test class for the example: -```java - new Thread(() -> { -// ^^^^^^> - Thread.sleep(1000); // Noncompliant {{Do not sleep here.}} -// ^^^^^ - }); +```bash +mise run test:unit --module java-checks --test DurationTimeUnitAgreementCheckTest ``` -If a caret cannot express the span (for example at column 1 or across multiple lines), use the explicit attributes such as `sc`, `el`, `ec` or `secondary`. Prefer relative lines to absolute lines so unrelated edits above the marker do not break the test. See [Quick-fix tests](quickfix-tests.md) for fix declarations that accompany an issue marker. - -## Verify - -From the repository root with the project dependencies and compiled samples available, run the targeted rule test; substitute the test class for the example: +To compile the shared samples as in CI, run from the root: ```bash -mvn test -pl java-checks -Dtest=DurationTimeUnitAgreementCheckTest +mise exec java@26 -- mvn -f java-checks-test-sources/pom.xml clean compile ``` -From `java-checks-test-sources`, `mvn clean compile` compiles the shared samples as in CI. It does not compile test-scope samples; use `mvn test-compile` from the same directory when changing those. +Use `test-compile` instead of `compile` when changing test-scope samples. diff --git a/docs/systems/verification.md b/docs/systems/verification.md index 60c7f197caf..c225bfb72d1 100644 --- a/docs/systems/verification.md +++ b/docs/systems/verification.md @@ -29,6 +29,8 @@ Run the relevant checks in [build and test commands](build.md). Run the focused ## Review and merge +Follow the [PR and commit conventions](../../CONTRIBUTING.md#clean-commit-history). + Use the actual required checks and review rules shown on the PR; `.github/CODEOWNERS` identifies the owning team. Public contributions and high-risk changes need human review. Treat public API, generated metadata, baselines, licensing, and changes in parser, cache or analysis behavior as requiring explicit review of their impact, not routine low-risk cleanup. Code review automation does not replace branch protections or maintainer approval. ## Performance validation diff --git a/java-checks-test-sources/README.md b/java-checks-test-sources/README.md index 7f3612397c0..80915dc3125 100644 --- a/java-checks-test-sources/README.md +++ b/java-checks-test-sources/README.md @@ -9,7 +9,7 @@ generated: Read [Rule tests and sample placement](../docs/systems/rule-tests.md) before adding or moving a sample. That guide covers the build JDK, module choice, and verifier setup. -To analyze the default samples on a local SonarQube instance, run `mvn clean install sonar:sonar -Panalyze-tests` from `java-checks-test-sources/default`. To compare scanner behavior with and without compiled classes, use the corresponding settings file in that module: +After [toolchain setup](../docs/systems/build.md), run `mise exec java@26 -- mvn -f java-checks-test-sources/default/pom.xml clean install sonar:sonar -Panalyze-tests` from the repository root to analyze the default samples on a local SonarQube instance. To compare scanner behavior with and without compiled classes, use the corresponding settings file in that module: - `java-checks-test-sources/default/sonar-project-with-binaries.properties` - `java-checks-test-sources/default/sonar-project-no-binaries.properties`