VSCode 多项目工作区:如何在工作区根目录添加单个文件,例如 .gitignore?

mik*_*010 5 python git visual-studio-code

我有以下文件夹结构..其中 _app 和 _infra 是两个不同的项目。然而,工作区的根目录有两个文件,工作区项目文件本身和 .gitignore 文件。每个项目都有自己的 .vscode 文件夹和自己的 .env 文件。整个工作区是 git 中的单个存储库。

my_app_workspace
   - proj1_app/
     - .venv/ (virtual environment)
     - vscode/
       - settings.json
       - launch.json
       - task.json
     - src/
       - config.py
     - .env
     - .env_linux
   - proj1_infra/
     - vscode/
       - settings.json
       - launch.json
       - task.json
     - src/
       - config.py
     - .env
     - .env_linux
  - .git_ignore
  -  my_app_workspace.code-workspace
Run Code Online (Sandbox Code Playgroud)

代码工作区文件如下所示:

{
    "folders": [
        {
            "path": "./proj1_app"
        },
        {
            "path": "./proj1_infra"
        }
    ],
}
Run Code Online (Sandbox Code Playgroud)

这一切都很好,但我想将 .git_ignore 和 my_app_workspace.code-workspace 文件也包含到 vscode 编辑器中,以便我可以轻松地对它们进行修改。我知道我可以使用 '"path": "."' 添加另一个文件夹,但这将再次添加一个包含项目文件夹的文件夹 - 这似乎多余且效率不高。

有没有办法将单个文件添加到工作区?这里的问题是我应该简单地将它们分成 git 中的两个不同的存储库吗?这样每个人都有自己的 .gitignore 文件,而不是我现在所做的是整个工作区是一个 git 存储库

Jur*_*rza 4

不幸的是,尽管社区在大约 5 年前Visual Studio Code就提出了请求,但目前尚未得到支持。我所说的社区提出了请求,是指该问题实际上是由 Microsoft 员工提出的,但得到了社区的大力支持。

但是,可以包含您想要在资源管理器视图中看到的文件所在的目录,但随后还需要排除位于包含目录中您不想看到的所有文件和目录视图中Explorer

此解决方案的缺点是,我还没有找到将文件放置在 VSC 工作区根目录中的方法,但您可以为文件将出现在 VSC Explorer 中的目录提供名称(在下面的示例中为my_custom_name)。如果name未定义该属性,则文件将定位在其名称与其所在磁盘上的目录相同的目录中。

要在您的示例中实现这一目标,my_app_workspace.code-workspace应具有以下内容:

{
"folders": [
    {
        "path": "proj1_app",
    },
    {
        "path": "proj1_infra",
    },
    {
        "path": ".", // points to the directory in which your .git_ignore is located
        "name": "my_custom_name"
    },
],
"settings": {
    "files.exclude": {
        // these below hide proj1_app and proj1_infra from the "my_custom_name" directory in the explorer view
        "proj1_app": true,
        "proj1_infra": true,
        // in this section it is needed to add all of the files and directories that are 
        // located in the workspace root but are not needed in the explorer view
        "ignore_me.txt": true
    },
}
}
Run Code Online (Sandbox Code Playgroud)

文件系统视图: 在此输入图像描述

Visual Studio 代码资源管理器视图: 在此输入图像描述