Osmium merge throws out-of-order error even after sort

I want to merge two files, which have significant overlap. As I understand it I just have to do the following commands

% osmium sort file1.pbf -o sorted1.pbf
% osmium sort file2.pbf -o sorted2.pbf
% osmium merge sorted1.pbf sorted2.pbf -o merged.pbf

The first two exit fine, while the last throws the error "Objects in input file ‘merged2.pbf’ out of order (smaller ids must come first).

This happens whether I reverse the order of the input files, use .pbf, use .osm. A quick eye test does look like the file is in order, but that would be hard to check item by item. file 1 is country-sized and directly from geofabrik, whereas file 2 is city-sized, fits entirely within file 1 and was edited through JOSM.
I believe the problem may stem from having either “split” or “duplicated” elements in file 2.

How can I ensure the files are in order or alternatively is there a way to merge without ordering the file.

Cheers

Edit: Actual, verbose output

% osmium sort file1.osm -o sorted.osm [======================================================================] 100% 
    
 % rm sorted2.osm                      
 % osmium sort file2.osm -o sorted2.osm -v
[ 0:00] Started osmium sort
[ 0:00]   osmium version 1.18.0
[ 0:00]   libosmium version 2.21.0
[ 0:00] Command line options and default settings:
[ 0:00]   input options:
[ 0:00]     file names: 
[ 0:00]       file2.osm
[ 0:00]     file format: 
[ 0:00]   output options:
[ 0:00]     file name: sorted2.osm
[ 0:00]     file format: 
[ 0:00]     generator: osmium/1.18.0
[ 0:00]     overwrite: no
[ 0:00]     fsync: no
[ 0:00]   other options:
[ 0:00]     strategy: simple
[ 0:00] Reading contents of input files...
[======================================================================] 100% 
[ 0:00] Number of buffers: 4
[ 0:00] Sum of buffer sizes: 3546824 (0.003 GB)
[ 0:00] Sum of buffer capacities: 4194304 (0.004 GB, 85% full)
[ 0:00] Opening output file...
[ 0:00] Sorting data...
[ 0:00] Writing out sorted data...
[ 0:00] Closing output file...
[ 0:00] Done.
 % osmium merge sorted.osm sorted2.osm -o merged.osm -v   
[ 0:00] Started osmium merge
[ 0:00]   osmium version 1.18.0
[ 0:00]   libosmium version 2.21.0
[ 0:00] Command line options and default settings:
[ 0:00]   input options:
[ 0:00]     file names: 
[ 0:00]       sorted.osm
[ 0:00]       sorted2.osm
[ 0:00]     file format: 
[ 0:00]   output options:
[ 0:00]     file name: merged.osm
[ 0:00]     file format: 
[ 0:00]     generator: osmium/1.18.0
[ 0:00]     overwrite: no
[ 0:00]     fsync: no
[ 0:00] Opening output file...
[ 0:00] Merging 2 input files to output file...
[======================================================================] 100% 
Objects in input file 'sorted2.osm' out of order (smaller ids must come first).

Note, this is a duplicate from openstreetmap - Osmium merge throws out of order error even after sort - Geographic Information Systems Stack Exchange

When I accept an answer on one, I will share it on the other platform.

You can not merge files that have different versions of the same object in them. You say you have overlapping files and have edited one of them. That means you have different versions of the same object. Also, you probably have objects with negative IDs in them, because that’s what JOSM will produce if you edit objects. And that will not work either.

All of this is not due to some problem of Osmium, but due to the OSM data model with objects referring to other objects, you can not easily just merge such OSM files.

1 Like