skip to Main Content

I have the following folder structure..where _app, and _infra are two different projects. At the root of the workspace however are two files, the workspace project file itself and a .gitignore file.
Each project has it’s own .vscode folder and own .env files.
The entire workspace is a single repository in 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

the code-workspace file looks like this:

{
    "folders": [
        {
            "path": "./proj1_app"
        },
        {
            "path": "./proj1_infra"
        }
    ],
}

This is all good, but i want to include the .git_ignore and my_app_workspace.code-workspace files also into the vscode editor so that i can easy make modifications to them.
I know i can add another folder with ‘"path": "."’, but this will add a folder with the project folders again – which seems redundant and not efficient.

Is there a way to add individual files to the workspace? Is the problem here i should simply split these up into two different repository in git? this way each has it’s own .gitignore file as opposed to what im doing now is the entire workspace is a git repository

3

Answers


  1. Since you are working on two entirely different projects, it is always preferred to have separate .gitignore files. The reasons for the same are:-

    • The presence of different dependency and config files for both projects, and putting all of them into a single .gitignore file will only make it unnecessarily bulky.
    • If you are planning to host the projects on a remote platform such as Github, Heroku, etc., then you would be able to better manage the ignored files for both, and it would also be better from the contributors’ perspective.
    • The VS Code workspace should always be separate. I have recently encountered some IDE errors due to which I had to clean up the workspace file, in that case, all cached changes(and unstaged) were lost. So it’s better to maintain different workspace files.

    In my opinion, you should go for separate repositories approach.

    Also, VS Code workspace currently doesn’t provide any feature of including separate files in a workspace, as it maintains an array of folders only.

    Hope it helps!

    Login or Signup to reply.
  2. This is unfortunately not yet supported in the Visual Studio Code even though the community has made a request almost 5 years ago. By community has made a request I mean that the issue was actually opened by a Microsoft employee, but was hugely backed by the community.

    However, it is possible to include a directory in which the files you want to see in the Explorer view are located, but then it is also needed to exclude all the files and directories located in the included directory that you don’t want to see in the Explorer view.

    The drawback of this solution is that I haven’t found a way to position the files in the VSC workspace root, but you can provide a name for the directory in which the files will appear in the VSC Explorer (in the example below it is my_custom_name). If the name attribute is not defined, then the files will be positioned in the directory that is named like the one on the disk in which they are located.

    To achieve that for your example, my_app_workspace.code-workspace should have the following content:

    {
    "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
        },
    }
    }
    

    Filesystem view:
    enter image description here

    Visual Studio Code Explorer view:
    enter image description here

    Login or Signup to reply.
  3. Yes, you can add individual files to the VS Code workspace by specifying the file path in the "path" field of the "folders" array in the workspace file. For example, you can add the .gitignore and my_app_workspace.code-workspace files by adding the following to the "folders" array:

    {
        "path": ".gitignore"
    },
    {
        "path": "my_app_workspace.code-workspace"
    }
    

    This will allow you to easily make modifications to these files within the VS Code editor.

    As for whether you should split these up into two different repositories in Git, it depends on your use case and workflow. If you want to version control each project separately and have separate .gitignore files for each, then it would make sense to split them into two different repositories. However, if you want to version control the entire workspace as a single unit and have a single .gitignore file for the entire workspace, then keeping them in a single repository would be appropriate.

    Login or Signup to reply.
Please signup or login to give your own answer.
Back To Top
Search