If it is a small fixture with a standard canopy, I use a surface-mounted pancake box with toggle bolts. The screw holes in the back of the box are far enough apart to span a 1-1/2" pipe which is most likely to be all that you will encounter.
If it is a strip or bar light with a solid back, I just use a Romex connector. If there's not a KO near where it needs to be, I just punch one.
I do it about the same, except I always put a backer between the studs to mount the box on, with the top of the backer at the centre of the light. There is always room for something, even plywood, to cover about half the box. Worst case would be to get behind the sides of the box. Then, you can cut the pan box in after and have something to screw to.
I got burned by a vanity that was a different size than drawn and a mirror that made it obvious that the light wasn't centred.