The bug
Villagers, zombie villagers and passive mobs inside villages are spawned into the world one block too high. This causes them to occasionally get stuck, especially since they also are shifted into the wall occasionally.
All types of villagers are affected (unemployed, nitwits, babies, and zombie villagers). Also, all biome variants of villagers are affected.
How to reproduce
Create a world with the seed
5322602735514886292
Run
/setblock ~ ~ ~ minecraft:repeating_command_block{auto:1b,Command:"execute as @e[type=villager,nbt=!{NoAI:1b}] run data merge entity @s {NoAI:1b}"}
Run
/tp @s -5967 68 2598
Look around
→ ❌ You will notice that all villagers are floating inside of the air, and the villager near/tp -5986 68 2555
even is stuck inside of the wall of its house
If you reproduce this without the command block, most villagers will be able to roam around freely, but some still are stuck in walls.
[media][media]
See another example in this comment.
The cause
All villagers in the villager structure files are spawned on top of the jigsaw block instead of inside of it. Also, some villagers (especially baby villagers) spawn with their hitbox partially outside of the block they're standing in.
[media][media]
[media]
Linked issues
is duplicated by
Attachments
Comments


Affects even their animals

confirmed for 1.15.2

Seeing this in 20w18a.
Can confirm in 1.17.1 Release Candidate 2.

Can confirm in 21w40a.

Confirmed in 21w43a. I reproduced this by generating a large area of terrain using a redstone mechanism. Occasionally the log gets messages about villagers suffocating and includes coordinates. The coordinates turned out to be villager houses.

This also affects zombie villages. Can confirm in 21w44a.

Can confirm in 1.18 Pre-release 1.

Can confirm in 1.18 Pre-release 4.

I have confirmed this and have a solid repro case. I think it is OK to just keep adding the affected versions instead of leaving comments.
3984153921072542614
/execute in minecraft:overworld run tp @s 2778.34 74.16 12476.52 289.83 27.30

This issue isn't exclusive to villagers, I just witnessed two cows spawn floating on generation and one of them got stuck in a wall and died.

In 1.19 Pre-2.

Can confirm in 23w03a

i confirm in 23w04a. the seed is -8347609665039207485, and the location is /execute in minecraft:overworld run tp @s -3647.30 65.00 6011.57 685.17 79.95

With the new /tick command's functionality, I think this bug could finally be fixed relatively easily. I believe the issues at the source of this problem are that previously, having mobs with AI inside jigsaw blocks wouldn't have been possible, and the mobs would have needed to be contained by barrier blocks while their structures are being saved. This would have allowed some mobs to move towards the edge of their 1x2x1 barrier enclosure, which causes slight overlap between their hitboxes and the adjacent blocks.
Now, however, someone can run /tick freeze and then recreate these jigsaws pieces containing entities. You can summon mobs with AI sitting at the same height as the jigsaw block, since they won't be forced out while ticks are frozen. Likewise, mobs can't move to the edge of their barrier enclosures while ticks are frozen (barriers wouldn't even be needed), thus preventing any overlap with adjacent blocks. If entities are summoned at the bottom of the jigsaw block and at the block's horizontal center, and then those jigsaw pieces are saved to replace the old ones, this should fix both villagers getting stuck in the ceiling and villagers getting stuck in walls. I believe this would also fix issues with village-generated zombie villagers, pigs, cows, and sheep. It might not fix issues with any village-generated iron golems, horses, or camels, as those entities all have hitboxes that pass the bounds of a 1x2x1 volume.
I have tested a tiny bit in creative: structure blocks can still be saved and loaded while ticks are frozen, and after unfreezing ticks these mobs loaded from structure blocks have AI and act normally. (I have not confirmed that my proposed fix would actually fix the issue, but if I should do that I can try.)

In 1.20.5

Confirmed in 1.21.4
1.14
1.14.1
1.14.2
1.14.3
1.14.4 Confirmed