VS Code multi-root workspace with one top level git repo

Question:

I am working on a project that requires multiple Google Cloud Functions. Each Cloud Function has its own virtualenv because each has its own Python dependencies. All the Cloud Functions are related so I’ve added each folder containing the Cloud Function code to a single (multi-root) VSCode workspace. I want to put the code of all the cloud functions in the same Git repo since they are related and work together. So, my .git folder is at the top of my multi-root workspace. My folder structure looks like this:

.
├── .git
├── .gitignore
├── .pylintrc
├── .pytest_cache
├── .vscode
│   └── settings.json
├── func-a
│   ├── .vscode
│   │   ├── launch.json
│   │   └── settings.json
│   ├── main.py
│   ├── requirements.txt
│   ├── schema.json
│   └── service-account.json
├── project.code-workspace
└── func-b
    ├── .pytest_cache
    ├── .vscode
    │   ├── launch.json
    │   └── settings.json
    ├── main.py
    ├── requirements.txt
    ├── service-account.json
    └── test_local.py

My issue is that I am unable to see (and therefore edit) the .gitignore file in VS Code because VS Code doesn’t let you add individual files to a workspace (only folders). If instead I try to add the entire fold (that contains func-a and func-b) to the workspace, then I run into the issue of not being able to set different interpreters (virtualenvs) for each Cloud Function and instead VS Code seems to force me to use the same Python interpreter for the entire workspace.

Is there a better way to do what I’m trying to do?

Asked By: faridghar

||

Answers:

You could simply define two workspaces, one for each projects, each one checked out in their own Git working tree using git sparse-checkout (or the more classic .git/info/sparse-checkout):

The idea is to sparsely checkout the repo with:

  • the .gitignore and any other file you need
  • only the func-x folder you want

You can then define a VSCode workspace for that first sparse checkout.

Repeat the process for the second project.

Answered By: VonC

You can add both the repository root and its subfolders as separate roots in the same workspace, without needing to checkout the repo in any special way:

# project.code-workspace:
/my-repo
├── .git
├── .gitignore
├── .pylintrc
├── .pytest_cache
└── .vscode
    └── settings.json
/func-a
├── .vscode
│   ├── launch.json
│   └── settings.json
├── main.py
├── requirements.txt
├── schema.json
└── service-account.json
/func-b
├── .pytest_cache
├── .vscode
│   ├── launch.json
│   └── settings.json
├── main.py
├── requirements.txt
├── service-account.json
└── test_local.py

Normally this makes things confusing since you would see func-a/ and func-b/ folders in the repo root, making them seem like duplicates. You can alleviate this in Workspace Settings by adjusting the Files: Exclude patterns to ignore the individual folders, so you would add lines for func-a/ and func-b/ to that list. That way, the individual folders don’t show up in the File Explorer within the repo root; but as you have them opened as separate Workspace roots in VS Code, you still see those folder contents.

This allows you to view all files in the whole repo, while still being able to separate different components into their own "projects" within the workspace. And each of those project roots is able to use its own Python interpreter settings, as VS Code handles that separately for each root.

Answered By: Galen Rice