简体   繁体   English

VS Code 多根工作区,带有一个顶级 git 存储库

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

I am working on a project that requires multiple Google Cloud Functions .我正在从事一个需要多个 Google Cloud Functions的项目。 Each Cloud Function has its own virtualenv because each has its own Python dependencies.每个 Cloud Function 都有自己的 virtualenv,因为每个都有自己的 Python 依赖项。 All the Cloud Functions are related so I've added each folder containing the Cloud Function code to a single (multi-root) VSCode workspace.所有 Cloud Functions 都是相关的,因此我将包含 Cloud Function 代码的每个文件夹添加到单个(多根)VSCode 工作区。 I want to put the code of all the cloud functions in the same Git repo since they are related and work together.我想将所有云功能的代码放在同一个 Git 存储库中,因为它们是相关的并且可以协同工作。 So, my.git folder is at the top of my multi-root workspace.因此,my.git 文件夹位于我的多根工作区的顶部。 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).我的问题是我无法在 VS Code 中看到(因此无法编辑).gitignore 文件,因为 VS Code 不允许您将单个文件添加到工作区(仅限文件夹)。 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.相反,如果我尝试将整个折叠(包含 func-a 和 func-b)添加到工作区,那么我会遇到无法为每个 Cloud Function 设置不同解释器(virtualenvs)的问题,而 VS Code 似乎迫使我对整个工作区使用相同的 Python 解释器。

Is there a better way to do what I'm trying to do?有没有更好的方法来做我想做的事情?

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 ):您可以简单地定义两个工作区,每个项目一个,每个工作区使用git sparse-checkout (或更经典的.git/info/sparse-checkout )在自己的 Git 工作树中签出:

The idea is to sparsely checkout the repo with:这个想法是通过以下方式稀疏地签出回购协议:

  • the .gitignore and any other file you need .gitignore和您需要的任何其他文件
  • only the func-x folder you want只有你想要的func-x文件夹

You can then define a VSCode workspace for that first sparse checkout.然后,您可以为第一次稀疏结帐定义一个 VSCode 工作区。

Repeat the process for the second project.对第二个项目重复该过程。

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.通常这会使事情变得混乱,因为您会在 repo 根目录中看到func-a/func-b/文件夹,使它们看起来像是重复的。 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.您可以在工作区设置中通过调整文件来缓解这种情况:排除模式以忽略单个文件夹,因此您可以将func-a/func-b/的行添加到该列表中。 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.但是当您在 VS Code 中将它们作为单独的工作区根目录打开时,您仍然会看到这些文件夹内容。

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.这些项目根中的每一个都能够使用自己的 Python 解释器设置,因为 VS Code 会分别为每个根处理这些设置。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM