Skip to content

Restore an id overridden as symbol and name in any removal order - #453

Closed
vorpalvorpal wants to merge 1 commit into
r-quantities:fix-install-shadowingfrom
vorpalvorpal:fix-override-restore-order
Closed

vorpalvorpal wants to merge 1 commit into
r-quantities:fix-install-shadowingfrom
vorpalvorpal:fix-override-restore-order

Conversation

@vorpalvorpal

Copy link
Copy Markdown
Contributor

A fix for a restore bug found while trying #452 (see #451), against fix-install-shadowing.

When the same id is overridden as both a symbol and a name, removing the symbol first (or both in one call) leaves the database unit unreadable for the rest of the session:

library(units)
install_unit("gal", "1 gallon", name = "gal", override = TRUE)
remove_unit("gal", name = "gal")
ud_convert(1, "gal", "cm/s^2")
#> Error: ut_get_converter(): NULL unit argument

Removing the name first, or overriding only the symbol or only the name, restores correctly.

Cause. set_aside() sets aside both the database symbol and the database name for the id and files both under the symbol's entry, so the later name call finds nothing left to record. Removing the symbol first then tries to put the database name back while the new name still holds it (ut_map_name_to_unit() fails and the return value is ignored). Removing the name afterwards unmaps the new name, and the database name is lost.

Fix. After the set_aside() calls in ud_install(), a small reown() hands each set-aside mapping to the installed id of its own kind when there is one. The name entry then restores the name whatever the removal order. A symbol-only override keeps the name aside under the symbol, as before.

Test. A new test in test_user_conversion.R covers both removal orders, both ids in one call, the vector case (c("gal", "galn") with c("gal", "galname")), and the symbol-only and name-only cases. It fails on 80ad1b2 with the error above; with the fix the suite gives 858 passed, 0 failed, 6 skipped here (macOS arm64, R 4.4, udunits from Homebrew).

🤖 Generated with Claude Code

https://claude.ai/code/session_01RzNVuCMZyYuDk4M1NMkB5Z

set_aside() files the database symbol and the database name of an id
under the symbol's entry. When the id is also installed as a name,
removing the symbol first tried to put the database name back while
the new name still held it, and removing the name afterwards left the
database name unmapped for the rest of the session.

Hand each set-aside mapping to the installed id of its own kind when
there is one, so the name entry restores the name whatever the order.

Co-Authored-By: Claude Fable 5.2 <noreply@anthropic.com>
Claude-Session: https://claude.ai/code/session_01RzNVuCMZyYuDk4M1NMkB5Z
Enchufa2 added a commit that referenced this pull request Nov 16, 2026
A database mapping set aside by an override was filed under whichever
installed id set it aside first. When an id was overridden as both a
symbol and a name, removing the symbol first tried to put the database
name back while the new name still held it; the failure went unnoticed,
and the database name was lost once the new name was removed (#451).

Any installed id, of either kind, that would set a mapping aside needs it
out of the way: symbols as they are, names whatever their case. So when
an id is removed, a mapping it set aside is handed to such an id if one
is left, and put back only when none is. This works in any removal order,
for ids installed in one call or in separate ones, and keeps the other
kind's override working when only one kind is removed.

- A mapping that cannot be put back is reported instead of ignored.
- remove_unit() no longer warns "left unchanged" about an id that the
  same call put back, e.g. remove_unit("gal", "gal") after overriding
  only the symbol.
- The override message and the docs say that overriding a symbol sets
  aside the name spelled the same in any letter case.

Supersedes #453, whose test is included.

Co-Authored-By: Robin Shannon <32329438+vorpalvorpal@users.noreply.github.com>
Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
@Enchufa2

Copy link
Copy Markdown
Member

Thanks a lot, @vorpalvorpal, both for testing #452 so thoroughly and for tracking down the cause! Your analysis was spot on.

I've fixed it directly in #452 (40dfac3), with you as co-author and your test included as is. The fix moves the decision to removal time instead of reassigning at install time: any installed id, of either kind, that would set a mapping aside needs it out of the way. So when an id is removed, what it set aside is handed to such an id if one is left, and put back only when none is. That also covers two cases that reassigning within one install_unit() call misses:

  • removing only the name after overriding both kinds in one call: the database gal came back and silently shadowed the symbol override that was still installed;
  • overriding both kinds in separate calls, then removing the symbol first: same loss of the database gal as in your report.

A restore that fails is now reported instead of ignored. Your two smaller observations are addressed too: the override message and the docs now say that overriding a symbol sets aside the name in any letter case, and remove_unit("gal", "gal") no longer warns about the name it has just put back.

Closing in favor of #452. Further testing on the updated branch is very welcome.

@Enchufa2 Enchufa2 closed this Nov 16, 2026
Enchufa2 added a commit that referenced this pull request Nov 18, 2026
…database ids (#452)

* Refuse ids that udunits already reads as a different unit

install_unit() only checked the map of the same kind, but udunits reads an
identifier by name (whatever its case), then by symbol, then the same after
stripping a prefix. So a new name "m" or symbol "km" silently redefined a
database unit, and a symbol "gal" was accepted but never reached, since the
database name "gal" wins over it.

- Refuse an id that udunits currently reads as a different unit.
- Refuse a name with a case variant that is read as a different unit (e.g.
  "KG" would shadow "kg"). Names with at most 12 letters try every variant;
  no database symbol, prefixed or not, is longer than 8 characters, so
  longer names are checked against installed symbols only.
- After mapping, every id must be read back as the new unit (e.g. "zz1" is
  read as zz^1); otherwise roll back and refuse.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>

* Warn in remove_unit() about any id udunits reads as a unit

remove_unit() only warned about ids it had not installed when they were
in the map of their own kind, so removing "km" (read by prefix), a name
"m" (a database symbol) or a symbol "gal" (a database name) did nothing
silently. Decide by what udunits reads the id as instead.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>

* Add override argument to install_unit() to redefine database ids

Before #449, remove_unit() on a database id followed by install_unit() was
the only way to give such an id another meaning, and it is no longer
possible (#451). install_unit(..., override = TRUE) does it in one step
and reversibly:

- The database mappings that give an id another meaning are set aside:
  the id in both the symbol and name maps, and the unit's own symbol or
  name when it is that id. They are recorded under the installed id, and
  remove_unit() puts them back after undoing the install.
- Setting aside, mapping and the read-back check run as one transaction,
  rolled back on any failure.
- What each id meant before is reported with a message, by whatever route
  (e.g. "km" by prefix).
- Ids installed by install_unit() must be removed first, and other ids a
  new name would shadow are still refused; case variants of the name
  itself (e.g. "microN" for "micron") are not.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>

* NEWS: merge the install_unit() entries, reference #452 and #451

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>

* Share what an override set aside between both kinds of the same id

A database mapping set aside by an override was filed under whichever
installed id set it aside first. When an id was overridden as both a
symbol and a name, removing the symbol first tried to put the database
name back while the new name still held it; the failure went unnoticed,
and the database name was lost once the new name was removed (#451).

Any installed id, of either kind, that would set a mapping aside needs it
out of the way: symbols as they are, names whatever their case. So when
an id is removed, a mapping it set aside is handed to such an id if one
is left, and put back only when none is. This works in any removal order,
for ids installed in one call or in separate ones, and keeps the other
kind's override working when only one kind is removed.

- A mapping that cannot be put back is reported instead of ignored.
- remove_unit() no longer warns "left unchanged" about an id that the
  same call put back, e.g. remove_unit("gal", "gal") after overriding
  only the symbol.
- The override message and the docs say that overriding a symbol sets
  aside the name spelled the same in any letter case.

Supersedes #453, whose test is included.

Co-Authored-By: Robin Shannon <32329438+vorpalvorpal@users.noreply.github.com>
Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>

---------

Co-authored-by: Claude Opus 5.5 <noreply@anthropic.com>
Co-authored-by: Robin Shannon <32329438+vorpalvorpal@users.noreply.github.com>
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.

2 participants