Skip to content

fix(srp): count sibling modules as one concern - #866

Merged
DaisukeYoda merged 1 commit into
ludo-technologies:mainfrom
dodoshell:fix/srp-sibling-modules
Oct 28, 2026
Merged

DaisukeYoda merged 1 commit into
ludo-technologies:mainfrom
dodoshell:fix/srp-sibling-modules

Conversation

@dodoshell

Copy link
Copy Markdown
Contributor

Summary

The single-responsibility rule counted each sibling module in a module's own package as a separate concern. A facade like mypkg._actor that imports _errors, _ref, _registry and _messages was flagged as mixing 4 concerns, while the same imports raised the package's cohesion. With this PR the siblings count as one concern, named after the package.

Type of Change

  • Bug fix (non-breaking change)
  • New feature (non-breaking change)
  • Breaking change
  • Documentation update
  • Performance improvement
  • Refactoring

Related Issues

Closes #846

Changes

  • service/responsibility_analysis.go: in concernLabel, a neighbor with the same depth as the module and the same parent package (a direct sibling) returns the parent package's last segment. This is the "map all siblings to one own-package label" option from the issue, not the looser shared-prefix test it warns against. Sibling subpackages (a.b.mod → a.b.sub.x) and modules elsewhere keep their current label. Top-level modules (no parent package) are unchanged.
  • service/responsibility_analysis_test.go:
    • TestConcernLabelGroupsSiblingModules: the issue repro (mypkg._actor → mypkg) and the Masendor siblings (unit), the aiomisc ones to dns, siblings in a generic package such as app.utils to no concern (the generic-name filter still applies), and concernLabel("myapp.core", "myapp.routers.user_router") is still routers
    • TestAnalyzeResponsibilityCountsSiblingModulesAsOneConcern: the issue's _actor repro has one concern and no violation
  • website/docs/rules/single-responsibility.md and the fr, ja, zh translations: one sentence in the paragraph that explains how concerns are inferred.

Testing

  • make test passes locally (all responsibility and cohesion tests pass; see below for the rest)
  • make lint passes locally (go vet, golangci-lint run; gofmt is clean on the changed files)
  • Added tests for new functionality (if applicable)
  • Manual testing completed (if applicable)

Other checks:

  • TestAnalyzeResponsibilityDetectsHubModule and the other existing responsibility tests pass unchanged.
  • go test ./... on Windows: three tests in service fail on this machine (TestFileReader_ErrorTypes, TestFileReader_PermissionHandling, TestProjectSnapshotResolvesRelativeModulePathsAtCaptureTime), and they fail the same way on main without this change.
  • The issue repro with pyscn analyze --json --no-open .:
    • before: mypkg._actor concerns ["_errors", "_messages", "_ref", "_registry"], one SRP violation, compliance 0.857, health 88 (B)
    • after: concerns ["mypkg"], no violation, compliance 1.0, health 90 (A)

Documentation

  • Updated godoc comments
  • Updated README or other docs (if needed)

Additional Context

A sibling subpackage's __init__ (a.b.sub seen from a.b.mod) has the same depth as a sibling module, so it is grouped with them too. Telling them apart would need ModuleNode.IsPackage, which concernLabel doesn't get today. I left that out to keep the change small.

concernLabel returned the first segment that differs from the module's
own path, so for a module importing a sibling in its own package that
segment was the sibling's file name. A facade composing a few private
helpers (_actor importing _errors, _ref, _registry, _messages) was
reported as mixing one concern per file, while the same edges raised
the package's cohesion.

A direct sibling module now maps to one label named after the shared
package, so siblings count as at most one concern and flat-layout
packages are still checked instead of having the rule switched off. A
generic package name such as utils is skipped, as it is elsewhere.
Sibling subpackages and modules elsewhere in the project are labelled
as before.

Closes ludo-technologies#846

@DaisukeYoda DaisukeYoda left a comment

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

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

LGTM! Thanks

@DaisukeYoda
DaisukeYoda merged commit c3f9281 into ludo-technologies:main Oct 28, 2026
6 checks passed
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.

[ENH][auto] srp-sibling-modules-as-concerns: single-responsibility rule counts each sibling module as a separate concern

2 participants