Skip to content

Read and set a package's top-level owner - #65

Merged
dduugg merged 2 commits into
mainfrom
package-owner-accessor
Sep 27, 2026
Merged

dduugg merged 2 commits into
mainfrom
package-owner-accessor

Conversation

@dduugg

@dduugg dduugg commented Sep 27, 2026 •

Copy link
Copy Markdown
Contributor

Fixes #49. Thank you to @oskarpearson for the clear report.

Summary

ParsePackwerk::Package had no way to read or change the top-level owner key, even though write_package_yml! already orders it among the canonical keys. package.owner raised NoMethodError, and package.with(owner: "x") raised ArgumentError, because T::Struct#with only accepts declared props. The only way in was editing package.config['owner'] by hand.

  • package.owner returns the top-level owner as a String, or nil.
  • package.with(owner: "Team") sets it, and package.with(owner: nil) removes it. write_package_yml! writes it with no other changes. Any other value raises TypeError.
  • metadata.owner is a separate, older convention and isn't read into owner. So owner can be nil for a pack that CodeOwnership.for_package still resolves through metadata.owner.

This bumps the version to 0.28.0, so merging publishes it to RubyGems through cd.yml.

Why owner reads from config instead of being a new prop

My first version added a const :owner prop. Review caught that a package loaded from disk would then hold its owner in two places, and on write the prop would silently override any edit made through config. That's the workaround in use today (package.config['owner'] = ...), and main honours it. So owner is a reader over config['owner'], and with(owner:) edits that key. With a single store, the two can't disagree, and every path that works on main behaves exactly as before.

A value that isn't a String (e.g. owner: 123) reads as nil and is written back unchanged. A typed prop would have raised TypeError inside ParsePackwerk.all on such a file.

with(owner:) edits the copy of config that T::Struct#with returns, rather than passing config: to it. T::Struct#with stringifies every key it's given, so passing config: would have rewritten a nested key such as 1: as '1':.

Package.new has no owner: argument. To build a package with an owner, pass it in config, as callers such as packs already do, or use .with(owner:).

Test plan

  • bundle exec rspec: 68 examples, 0 failures. srb tc and rubocop are clean.
  • Every new spec that calls owner or with(owner:) fails against main's lib with NoMethodError or ArgumentError.
  • Ran the same scripted scenarios against main's lib and this branch's lib, with preserve_key_order both on and off, and with a string owner, owner: 123, and a metadata-only owner. The scenarios were an unchanged round trip, config['owner'] = ..., with(config:) with the owner changed or removed, and with(dependencies: []). The written files are byte-identical in all 30 runs. A broader harness (14 fixtures, 17 existing-usage scenarios, preserve_key_order on and off: 476 runs) is also byte-identical to main, and with(owner:) writes the same bytes as main's config['owner'] = ... workaround in all 140 runs.
  • The test suites of downstream rubyatscale gems (packs, visualize_packs, query_packwerk, chatwerk, danger-packwerk, pack_stats, rubocop-packs) pass the same against main's lib and the first version of this change. That version stored owner as a separate prop, and none of these gems passes owner: to Package.new or with, so the reworked version doesn't change what they exercise.
  • CI passes.

`ParsePackwerk::Package` had no way to read or change the top-level `owner`
key, even though `write_package_yml!` already orders it among the canonical
keys: `package.owner` raised NoMethodError, and `package.with(owner: "x")`
raised ArgumentError, since T::Struct#with only accepts declared props. The
only way in was editing `package.config['owner']` by hand.

`owner` is a reader over `config['owner']` rather than a new prop, and
`with(owner:)` sets that key, or removes it when given nil. With a separate
prop, a package loaded from disk would hold its owner in two places, and the
prop would silently override any edit made through `config`, which is the
workaround in use today. Keeping `config` as the only store leaves every
path that works on main byte-identical. A value that isn't a String reads as
nil and is still written back unchanged. `metadata.owner` is a separate,
older convention and isn't read into `owner`.

Fixes #49. Also bumps the version to 0.28.0.
@dduugg
dduugg requested a review from a team as a code owner September 27, 2026 17:38
@github-project-automation github-project-automation Bot moved this to Triage in Modularity Sep 27, 2026
`with(owner:)` passed the whole config back through `T::Struct#with`, which
stringifies every hash key it is given, so a nested key such as `1:` or
`true:` was written back as `'1':` or `'true':`. It now edits the copy that
`super` returns, which is already a deep copy of the receiver's config, so
only the owner line changes and the file matches what the
`config['owner'] = ...` workaround writes. This also stops
`with(owner:, 'config' => {...})` from discarding the given config.

`with(owner:)` now raises TypeError for a value that isn't a String or nil,
rather than writing one that `owner` would then read as nil.

The comment on `owner` notes that it reads only the top-level key, unlike
`CodeOwnership.for_package`, which falls back to `metadata.owner`.
@dduugg
dduugg merged commit 5dc0a6b into main Sep 27, 2026
9 checks passed
@dduugg
dduugg deleted the package-owner-accessor branch September 27, 2026 18:19
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

Status: Done

Development

Successfully merging this pull request may close these issues.

Top level 'owner' packwerk key can't be retrieved via method call or set via with

1 participant