Stop same-named models in different packs from annotating each others related files by matching on owning root_dir. - #374
Open
OdenTakashi wants to merge 1 commit into
Open
Conversation
…s related files by matching on owning root_dir. ## Summary Prevent related-file collisions between models with the same basename in different roots. ## Problem In a Packwerk application with: * `app/models/address.rb` → `Address` (table: `addresses`) * `packs/contacts/app/public/models/address.rb` → `Contacts::Address` (table: `contact_addresses`) * `packs/contacts/test/models/address_test.rb` → test for `Contacts::Address` both models claim the same test file as a related file. Each model writes its own schema annotation to it—`addresses` or `contact_addresses`—and the last writer wins. As a result, a normal run reports the same file twice: ```sh Annotated (2): packs/contacts/test/models/address_test.rb, packs/contacts/test/models/address_test.rb ``` `--frozen` then fails for whichever model lost the last-write race, even though the annotations are not actually stale. ## Root cause With `root_dir: ["", "packs/*"]`, `PatternGetter` expands related-file patterns for every configured root, rather than only for the concrete root containing the current model. For both models, `%MODEL_NAME%` and `%MODEL_NAME_WITHOUT_NS%` resolve to `address`. This causes both models to generate and search the same glob, such as: ```text packs/*/test/models/address_test.rb ``` Consequently, both models match `packs/contacts/test/models/address_test.rb`. ## Solution After finding related files through the existing name-based globbing, filter them so that their concrete root matches the model's root: * `app/models/address.rb` belongs to the project root (`nil`) * It may match project-root related files such as `test/models/address_test.rb` * It does not match files under `packs/contacts` * `packs/contacts/app/public/models/address.rb` belongs to `packs/contacts` * It matches `packs/contacts/test/models/address_test.rb` Files in the project root continue to match each other because both have a root of `nil`. The default configuration, `root_dir: [""]`, is unchanged because all model and related files belong to the project root. Fixes #367
OdenTakashi
marked this pull request as ready for review
July 31, 2026 00:21
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Summary
Prevent related-file collisions between models with the same basename in different roots.
Problem
In a Packwerk application with:
app/models/address.rb→Address(table:addresses)packs/contacts/app/public/models/address.rb→Contacts::Address(table:contact_addresses)packs/contacts/test/models/address_test.rb→ test forContacts::Addressboth models claim the same test file as a related file. Each model writes its own schema annotation to it—
addressesorcontact_addresses—and the last writer wins.As a result, a normal run reports the same file twice:
--frozenthen fails for whichever model lost the last-write race, even though the annotations are not actually stale.Root cause
With
root_dir: ["", "packs/*"],PatternGetterexpands related-file patterns for every configured root, rather than only for the concrete root containing the current model.For both models,
%MODEL_NAME%and%MODEL_NAME_WITHOUT_NS%resolve toaddress. This causes both models to generate and search the same glob, such as:Consequently, both models match
packs/contacts/test/models/address_test.rb.Solution
After finding related files through the existing name-based globbing, filter them so that their concrete root matches the model's root:
app/models/address.rbbelongs to the project root (nil)test/models/address_test.rbpacks/contactspacks/contacts/app/public/models/address.rbbelongs topacks/contactspacks/contacts/test/models/address_test.rbFiles in the project root continue to match each other because both have a root of
nil.The default configuration,
root_dir: [""], is unchanged because all model and related files belong to the project root.Fixes #367