`duplicate_keys: mb_albumid` never matches on autotagged imports and matches albums without an MBID instead
Problem
With import.duplicate_keys.album containing mb_albumid (the setting the config docs now suggest for MusicBrainz-tagged libraries, #7028), duplicate detection fails for autotagged imports:
- Importing a release that is already in the library is not reported as a duplicate.
- An unrelated album with an empty
mb_albumidis reported as the duplicate instead.
duplicate_keys.item: mb_trackid fails the same way for singleton imports.
Cause: for an applied match, ImportTask.chosen_info() returns self.match.info.copy(). Its keys are the AlbumInfo/TrackInfo names (album_id, track_id), not the library field names. find_duplicates() builds its temporary Album/Item from that dict, so mb_albumid stays unset and the query becomes mb_albumid = ''. The album path already maps artist to albumartist by hand; album_id → mb_albumid and the other MEDIA_FIELD_MAP entries are missing.
As-is imports (-A) are not affected, because there chosen_info() returns the files' own tags. That is why the setting looked correct in #7026.
A command-line reproduction needs a live MusicBrainz match, so this script drives ImportTask directly, with no plugins loaded:
import os, tempfile
import beets
from beets import config, library
from beets.autotag.hooks import AlbumInfo, TrackInfo
from beets.autotag.match import AlbumMatch
from beets.importer import Action
from beets.importer.tasks import ImportTask
config.clear()
config.read(user=False, defaults=True)
config["plugins"] = []
config["import"]["duplicate_keys"]["album"] = ["mb_albumid"]
tmp = tempfile.mkdtemp()
lib = library.Library(os.path.join(tmp, "library.db"), tmp)
def add_album(name, mbid, path):
album = library.Album(lib, album=name, albumartist="Artist", mb_albumid=mbid)
album.add(lib)
item = library.Item(path=path, title="Track", album=name, albumartist="Artist", mb_albumid=mbid)
lib.add(item)
item.album_id = album.id
item.store()
add_album("Same Release", "11111111-1111-1111-1111-111111111111", b"/music/same/01.flac")
add_album("No MBID", "", b"/music/nombid/01.flac")
new_item = library.Item(path=b"/incoming/01.flac", title="Track", artist="Artist")
info = AlbumInfo(tracks=[TrackInfo(title="Track")], album="Same Release", artist="Artist",
album_id="11111111-1111-1111-1111-111111111111")
task = ImportTask(None, [b"/incoming"], [new_item])
task.match = AlbumMatch(distance=None, info=info, mapping={new_item: info.tracks[0]})
task.choice_flag = Action.APPLY
print("beets", beets.__version__)
print("found: ", [(a.album, a.mb_albumid) for a in task.find_duplicates(lib)])
print("expected:", [("Same Release", "11111111-1111-1111-1111-111111111111")])Output on master (f6dc7ab):
beets 2.14.1
found: [('No MBID', '')]
expected: [('Same Release', '11111111-1111-1111-1111-111111111111')]2.11.0 gives the same result. The equivalent singleton test (SingletonImportTask, duplicate_keys.item: mb_trackid, TrackInfo(track_id=...)) also returns the item with an empty mb_trackid and not the matching one.
With the temporary album built from self.match.info.item_data (which applies MEDIA_FIELD_MAP), plus the artist key that find_duplicates() still reads, the script returns the expected album.
Setup
- OS: Linux
- Python version: 3.12
- beets version: 2.14.1 (master f6dc7ab) and 2.11.0
- Turning off plugins made problem go away (yes/no): no. The script loads no plugins.
My configuration: defaults, plus import.duplicate_keys.album: mb_albumid as set in the script.
Source: beetbox/beets