SCANPY-249 Default sonar.sources to current directory when not provided#319
Conversation
SummaryThis PR adds a sensible default for The changes are backward-compatible: projects that already set What reviewers should knowWhere to start: Look at the main logic in Key reviewer points:
Test coverage: Existing assertions were updated to expect the new default (e.g., expecting
|
There was a problem hiding this comment.
Solid PR overall — the ordering is correct (test auto-detection runs before the sources defaulting, so auto-detected test paths are visible to the exclusion logic), the appending logic handles existing exclusions cleanly, and the test suite covers the key scenarios well. One edge case in the exclusion-building block needs a guard before merge.
| if SONAR_TESTS in resolved_properties: | ||
| test_dirs = [d.strip() for d in resolved_properties[SONAR_TESTS].split(",") if d.strip()] | ||
| test_exclusion_patterns = ",".join(f"{d}/**" for d in test_dirs) | ||
| existing = resolved_properties.get(SONAR_EXCLUSIONS, "") | ||
| resolved_properties[SONAR_EXCLUSIONS] = f"{existing},{test_exclusion_patterns}" if existing else test_exclusion_patterns | ||
| logging.info( | ||
| "sonar.sources defaults to '.'; adding test directories to sonar.exclusions to avoid overlap: '%s'", | ||
| test_exclusion_patterns, | ||
| ) |
There was a problem hiding this comment.
If sonar.tests is present but blank (e.g. sonar.tests= in sonar-project.properties, or SONAR_TESTS="" as an env var), test_dirs ends up empty after filtering. The code then:
- Computes
test_exclusion_patterns = ""(empty join) - If
SONAR_EXCLUSIONSalready has a value: writes"existing,"— a trailing comma into the scanner's exclusion glob list - If
SONAR_EXCLUSIONSis absent: silently sets it to"", creating a property that wasn't there before
Both are wrong. Guard the inner block with if test_dirs: so the exclusion mutation only fires when there are actual patterns to add.
| if SONAR_TESTS in resolved_properties: | |
| test_dirs = [d.strip() for d in resolved_properties[SONAR_TESTS].split(",") if d.strip()] | |
| test_exclusion_patterns = ",".join(f"{d}/**" for d in test_dirs) | |
| existing = resolved_properties.get(SONAR_EXCLUSIONS, "") | |
| resolved_properties[SONAR_EXCLUSIONS] = f"{existing},{test_exclusion_patterns}" if existing else test_exclusion_patterns | |
| logging.info( | |
| "sonar.sources defaults to '.'; adding test directories to sonar.exclusions to avoid overlap: '%s'", | |
| test_exclusion_patterns, | |
| ) | |
| if SONAR_TESTS in resolved_properties: | |
| test_dirs = [d.strip() for d in resolved_properties[SONAR_TESTS].split(",") if d.strip()] | |
| if test_dirs: | |
| test_exclusion_patterns = ",".join(f"{d}/**" for d in test_dirs) | |
| existing = resolved_properties.get(SONAR_EXCLUSIONS, "") | |
| resolved_properties[SONAR_EXCLUSIONS] = f"{existing},{test_exclusion_patterns}" if existing else test_exclusion_patterns | |
| logging.info( | |
| "sonar.sources defaults to '.'; adding test directories to sonar.exclusions to avoid overlap: '%s'", | |
| test_exclusion_patterns, | |
| ) |
290380d to
66a5895
Compare
66a5895 to
5e7f0d6
Compare
| if (sources_defaulted or tests_auto_detected) and SONAR_TESTS in resolved_properties: | ||
| test_dirs = [d.strip() for d in resolved_properties[SONAR_TESTS].split(",") if d.strip()] | ||
| test_exclusion_patterns = ",".join(f"{d}/**" for d in test_dirs) | ||
| existing = resolved_properties.get(SONAR_EXCLUSIONS, "") | ||
| resolved_properties[SONAR_EXCLUSIONS] = ( | ||
| f"{existing},{test_exclusion_patterns}" if existing else test_exclusion_patterns | ||
| ) | ||
| logging.info( | ||
| "Adding test directories to sonar.exclusions to avoid overlap with sonar.sources: '%s'", | ||
| test_exclusion_patterns, | ||
| ) |
There was a problem hiding this comment.
💡 Edge Case: Exclusions added for auto-detected tests even when sources is narrow
When tests_auto_detected is True but the user explicitly set sonar.sources to a narrow path like "src", the condition on line 98 still evaluates to True (via the tests_auto_detected branch), and test directory exclusion patterns are appended to sonar.exclusions. These exclusions are harmless but unnecessary when the source path doesn't overlap with the test directories.
For example: --sonar-sources src with auto-detected tests directory → sonar.exclusions gets tests/** even though src never included tests.
This is a minor concern since extra exclusions don't cause incorrect behavior, but the log message "Adding test directories to sonar.exclusions to avoid overlap with sonar.sources" would be misleading when there's no actual overlap.
Only add test exclusions when sources actually includes '.' (the root), which is the only case where overlap is likely:
if SONAR_TESTS in resolved_properties and (
sources_defaulted or resolved_properties.get(SONAR_SOURCES) == "."
):
- Apply fix
Check the box to apply the fix or reply for a change | Was this helpful? React with 👍 / 👎
Code Review 👍 Approved with suggestions 0 resolved / 1 findingsDefaults sonar.sources to the current directory when not provided, with auto-detected test directories added to sonar.exclusions to prevent overlap. Consider refining the exclusion logic to account for cases where users explicitly set a narrow sonar.sources configuration. 💡 Edge Case: Exclusions added for auto-detected tests even when sources is narrow📄 src/pysonar_scanner/configuration/configuration_loader.py:98-108 When For example: This is a minor concern since extra exclusions don't cause incorrect behavior, but the log message "Adding test directories to sonar.exclusions to avoid overlap with sonar.sources" would be misleading when there's no actual overlap. Only add test exclusions when sources actually includes '.' (the root), which is the only case where overlap is likely🤖 Prompt for agentsOptionsAuto-apply is off → Gitar will not commit updates to this branch. Comment with these commands to change:
Was this helpful? React with 👍 / 👎 | Gitar |
Seppli11
left a comment
There was a problem hiding this comment.
I think the change looks good.
I was trying to think if the situation where the user didn't set the sources or tests, but did set the test exclusion would cause problems. But an easy workaround is to just set the two properties
| f"{existing},{test_exclusion_patterns}" if existing else test_exclusion_patterns | ||
| ) | ||
| logging.info( | ||
| "Adding test directories to sonar.exclusions to avoid overlap with sonar.sources: '%s'", |
There was a problem hiding this comment.
nitpick: It would probably make sense to tell the user what they should do if they want to disable this behavior
|




Summary by Gitar
sonar.sourcesis not provided, defaults to current directory'.'sonar.exclusionsto prevent overlap withsonar.sourcesThis will update automatically on new commits.