Skip to content
Draft
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
20 changes: 19 additions & 1 deletion .agents/skills/new-rule/SKILL.md
Original file line number Diff line number Diff line change
Expand Up @@ -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:
Comment on lines +17 to +21

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

💡 Quality: AbsOnNegative examples break the skill's placement and marker rules

SKILL.md:19 holds up AbsOnNegativeCheckTest as the example of a rule that reports in both modes. AGENTS.md:25 points to java-checks/src/test/files/checks/AbsOnNegative.java as the example for "verifier assertions and issue markers". Both examples break guidance from this same PR:

  • The test loads its sample from the legacy src/test/files/checks directory instead of using TestUtils.mainCodeSourcesPath. rule-tests.md:19 says not to add new rule samples there.
  • The test repeats the path string instead of sharing a sample-path constant, which SKILL.md:17 asks for one sentence earlier.
  • Only 2 of the 12 // Noncompliant markers in the sample have carets. SKILL.md:21 says carets belong under each reported location.

An agent that copies these files will produce a test the docs reject. Pick a both-modes test that follows the documented layout, or say plainly that these files show only the verifier calls.

Was this helpful? React with 👍 / 👎


```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.
8 changes: 4 additions & 4 deletions AGENTS.md
Original file line number Diff line number Diff line change
Expand Up @@ -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

Expand Down
5 changes: 3 additions & 2 deletions CONTRIBUTING.md
Original file line number Diff line number Diff line change
Expand Up @@ -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.

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

ℹ️ Cross-PR: New PR-title rule asks for SONARJAVA keys after #6597 switches to JAVA

Breaks when this PR and #6597 both merge, and a contributor follows the new PR-title instruction in CONTRIBUTING.md while the Jira key is JAVA.

How

  1. CONTRIBUTING.md:30 (this PR) says PR titles must start with a SONARJAVA-XXXX issue key.
  2. #6597 changes the Jira link at CONTRIBUTING.md:7 to the JAVA project, so the same file then gives two different project keys.
  3. #6597 sets jira-project: JAVA at .github/workflows/PullRequestCreated.yml:29 and also changes automated-release.yml, so the PR automation stops using the SONARJAVA project.
  4. The two edits are in different hunks, so git reports no conflict and the stale key goes in unnoticed.

Fix

  • This PR: Write the PR-title instruction with the JAVA-XXXX key, or describe it without hard-coding the project key.
  • #6597: Rename the SONARJAVA-XXXX key in the new CONTRIBUTING.md PR-title sentence along with the other renamed sites.

Compared b939552 with #6597 at d16fca0

Was this helpful? React with 👍 / 👎


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.
8 changes: 6 additions & 2 deletions docs/systems/build.md
Original file line number Diff line number Diff line change
Expand Up @@ -18,15 +18,15 @@ 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
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.

Expand All @@ -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.
2 changes: 1 addition & 1 deletion docs/systems/licenses.md
Original file line number Diff line number Diff line change
Expand Up @@ -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`.
28 changes: 9 additions & 19 deletions docs/systems/rule-tests.md
Original file line number Diff line number Diff line change
Expand Up @@ -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.
Comment on lines +23 to +25

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

💡 Quality: Docs still send readers to rule-tests.md for content now in the skill

This PR moves the semantic/no-semantic guidance, the verifier example and the caret/marker rules out of docs/systems/rule-tests.md and into .agents/skills/new-rule/SKILL.md#rule-test-assertions. Several pages still say rule-tests.md itself has that content:

  • docs/systems/quickfix-tests.md:3 says the rule test guide "explains the underlying issue and caret markers".
  • docs/adrs/junit-5.md says "See Rule tests for a verifier example". rule-tests.md no longer has one.
  • docs/systems/index.md describes rule-tests.md as "choose semantic and no-semantic expectations".
  • docs/systems/rule-api.md says "Test that fallback explicitly; see Rule tests". The unresolved-type guidance has moved.
  • java-checks-test-sources/README.md:10 says that guide covers "verifier setup". This README is edited in this PR.

Rule-tests.md is now a one-line redirect, so each of these pages costs the reader an extra hop. Point these references straight at the skill anchor, or update their wording.

Example: update the quick-fix guide reference:

Declare expected fixes in the same sample as the issue. This fragment comes from `java-checks-test-sources/default/src/main/java/checks/DurationTimeUnitAgreementCheckSample.java`; the [rule test assertions](../../.agents/skills/new-rule/SKILL.md#rule-test-assertions) explain the underlying issue and caret markers.
  • Apply fix

Check the box to apply the fix or reply for a change | Was this helpful? React with 👍 / 👎


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.
2 changes: 2 additions & 0 deletions docs/systems/verification.md
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
2 changes: 1 addition & 1 deletion java-checks-test-sources/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -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`
Loading