When I was trying to deploy or retrieve a file directly for one of my project, it is not working and showing me the error:
Error deploying or retrieving source The file or directory that you tried to deploy or retrieve isn’t in a package directory that’s specified in your sfdx-project.json file. Add this location to your “packageDirectories” value, or deploy or retrieve a different file or directory. For details about sfdx-project.json, see: https://developer.salesforce.com/docs/atlas.en-us.sfdx_dev.meta/sfdx_dev/sfdx_dev_ws_config.htm
Error deploying or retrieving source: The file or directory that you tried to deploy or retrieve isn't in a package directory that's specified in your sfdx-project.json file. Add this location to your "packageDirectories" value, or deploy or retrieve a different file or directory. For details about sfdx-project.json, see: https://developer.salesforce.com/docs/atlas.en-us.sfdx_dev.meta/sfdx_dev/sfdx_dev_ws_config.htm
Why Error deploying or retrieving source The file or directory that you tried to deploy or retrieve isn’t in a package directory that’s specified in your sfdx-project.json file is seen?
This feature was operational till July 8, 2022. This is an issue in the new extension of “Salesforce CLI Integration.” I was only able to make it work by downgrading the “Salesforce CLI Integration” extension version. Newer release should be able to fix the issue.
As of now, downgrade the “Salesforce CLI Integration” extension version and it should solve the issue for you.
How to Fix Error deploying or retrieving source The file or directory that you tried to deploy or retrieve isn’t in a package directory that’s specified in your sfdx-project.json file?
Fix 1: Downgrade the “Salesforce CLI Integration” Extension Version
To fix the error, downgrade the “Salesforce CLI Integration” extension version and it should solve the issue for you.
This issue is basically seen with the newer “Salesforce CLI Integration” extension version only after 8th July 2022.
Fix 2: Refresh the Apex Extensions
For Visual Studio, refreshing the Apex Extensions should solve the Error deploying or retrieving source: The file or directory that you tried to deploy or retrieve isn’t in a package directory that’s specified in your sfdx-project.json file.
Conclusion
To fix the error, downgrade the “Salesforce CLI Integration” extension version and it should solve the issue for you. This issue is basically seen with the newer “Salesforce CLI Integration” extension version only after 8th July 2022.
For Visual Studio, refreshing the Apex Extensions should solve the Error deploying or retrieving source: The file or directory that you tried to deploy or retrieve isn’t in a package directory that’s specified in your sfdx-project.json file.