Maybe I’m just missing something but all the samples I’ve seen of taking bytecode and trying to reverse engineer it into solidity code looks quite imperfect, yet looking at Etherscan for a particular contract address is showing perfect solidity code, even with comments included. How is this possible? Where is Etherscan getting the code from? https://etherscan.io/address/0x93a796b1e846567fe3577af7b7bb89f71680173a#code
The developers who wrote the contract have to upload the code themselves. Etherscan then verifies that the code they upload compiles to the on-chain bytecode. https://etherscan.io/verifyContract
Basically all the contracts of the most popular dApps have been verified, which I assume you've been looking at, but not every contract has been verified. If you deploy a contract yourself, you'll see that by default it's not verified and Etherscan only shows bytecode.