[SalesForce] Setup Salesforce(Sandbox) project in Visual Studio Code and integrate source control with Bitbucket

I am new to VS Code, things I was able to do separately are

  • Create a Salesforce Manifest project and authorize the org, Retrieve/Deploy are good.
  • Have a repository on Bitbucket, cloned it on the VS Code, push/pull operations are good.

Problem is I am not able to make it a continuous process, retrieve from Sandbox and push it to Bitbucket, vice versa. Below are the steps followed in setup. Please help where I am going wrong.

  1. SFDX: Create Project with Manifest on VS Code, named Demo-Project under ../Documents folder.
  2. SFDX: Authorize an Org, credentials entered, success. Package xml under manifest folder. Empty aura, lwc folders under ../Documents/Demo-Project/force-app/main/default
  3. Under Bitbucket, I have repository Remote-Project, this has source classes, pages, etc. I copied the clone URL.
  4. GIT: Clone on VS Code. Directory selected ../Documents/Demo-Project/force-app. A folder named remote-project is created with the source content from bitbucket. Got a pop up asking for Open Repository and Add to Workspace. I clicked on Add to Workspace.
  5. Opened a file from ../Documents/Demo-Project/force-app/remote-project/aura/democmp, made changes, right click, Deploy to Source org, it got deployed. Source Control tracks the changes.
  6. Now, when I Retrieve from Source Org, a new file named democmp is created under ../Documents/Demo-Project/force-app/main/default/aura, and the Source Control does not track this changes.

I understand I am doing wrong at some setup step, please help me here to make this a continuous process.

Best Answer

GIT: Clone on VS Code. Directory selected ../Documents/Demo-Project/force-app. A folder named remote-project is created with the source content from bitbucket. Got a pop up asking for Open Repository and Add to Workspace. I clicked on Add to Workspace.

This is your issue. You're creating an SFDX project, and then cloning a different SFDX project inside it. When you complete this step, your directory tree probably looks like this:

Demo-Project/
    force-app/
        main/
            default/
                aura/
        remote-project/
            .git/
            aura/
                democmp/

Only the folder enclosing .git and its descendants are part of the Git repository, because that's where you chose to clone it. Git cannot see anything outside the repo boundary, so anything that is saved in main/default/aura/ isn't part of the repo and isn't visible to Git.

Conversely, you've told Visual Studio Code, by creating an SFDX project, that you want to be working in an outer directory tree rooted at force-app. If you look in your sfdx-project.json file, you should see something like

"packageDirectories": [
  {
    "path": "force-app",
    "default": true
  }
],

This tells VSC that your project's primary source directory is under force-app (not remote-project), and that's where your SFDX commands are going to save and push source from by default.

If you're starting from an existing SFDX project that's stored in a source code repo, which sounds like it's the case although I suspect something's a little off about the way you're discussing the folder tree, you probably want to just clone that project and then open its root folder in Visual Studio Code, rather than creating a new project locally first.