It is strange that there is a system where you expect the building inspector to act in the best interest of society as a whole, but then when they fail to do this, there are functionally no consequences for this failure.
I can understand the argument that a building inspector must act with the benefit of society in mind, and that one person may find themselves on the short end of that stick, so there must be some level of protection there. I just really question if statutory immunity is the vehicle to get there.
The reality is that enforcement of the codes benefit society and the owner the overwhelming amount of time, so why would there need to be statutory immunity? Why not the Anns test or a similar legal test?
Codes benefit not only the current owner/tenant but all future owners and tenants. When a build-out or addition is done illegally or includes non-compliant new work, that work will need to be checked against the standards at the time it was built in order to know if it was code-compliant or not. If it wasn't code-compliant when installed, it should never be considered legal - and laws in Virginia agree with this.
I encounter tenants who were rented a space built out for the same use they have - however, the build-out was had no permit and no certificate of occupancy, so now they're signed to a space where they thought they could use it right away, but instead they have to tear out and redo parts of it - sometimes major parts, sometimes it can't be occupied for their use at all. But you still wind up with these and often a municipality will decide to only require remediation of egress and safety items. But future users of these space won't be expecting that this newish space has non-conforming work either.
A car dealership is accepted though they bought a house-load of residential doors for their offices and used them all - including the 30" bedroom doors and 24" restroom doors - for their offices. It was allowed that since they had some compliant sliders and 36" doors for the offices (mostly pre-existing this build-out) it would be OK to have some other non-ADA doors. But the accessibility of those spaces and limits on what can and cannot become part of a hallway - all of that will be imposed on future users as well.
Reframing and replacing all those doors would be expensive (found a human services space had framed for a "house load" of doors, but they hadn't gotten to the drywall yet, so they were able to fix it more readily) but leaving them there is a risk that can trap future users. Home inspections often turn up unpermitted/illegal work that has to be fixed, but commercial buildings don't get the same benefit - when the building is measured up for a new tenant/owner the facts will often emerge, but the idea that grandfathering applies to everything existing is difficult to erase from the real estate sales and leasing mindset.
When I took a course on laws on construction in Virginia, we were told that whoever controls the space when non-permitted work is discovered is the one on the hook for it. So it falls to the tenant or owner to fix it. That can be a massively expensive discovery for start-ups or people who are relatively new to owning commercial buildings. We make people have an asbestos inspection filed if the building was built in 1984 or earlier, but what do we do to help business people know if the space/building they are looking at is code compliant when an inspector or RDP isn't involved? When non-compliant work done without a permit is allowed to stand, the cost of fixing it is being passed along to the next owner, who typically knows nothing about it.