Skip to content

3.6. VS Code Workspace

What is a VS Code Workspace?

A VS Code Workspace is a feature that allows you to group multiple project folders into a single, unified environment. This configuration is stored in a .code-workspace file, which acts as a central hub for your project's settings, recommended extensions, and UI layout, separate from your global user settings.

Workspaces are ideal for complex projects, such as monorepos containing both a backend API and a frontend application, or MLOps projects with separate folders for data processing, model training, and deployment scripts.

Why are VS Code Workspaces beneficial for MLOps projects?

Using a VS Code Workspace provides several key advantages, especially for collaborative MLOps environments:

  • Standardized Environments: By defining project-specific settings in the .code-workspace file, you ensure that every team member uses the same linter, formatter, and testing configurations. This consistency is crucial for maintaining code quality and reducing "it works on my machine" issues.
  • Enhanced Productivity: Workspaces allow you to define task configurations and debugging setups that are specific to the project. This saves time and streamlines common development workflows, such as running tests or launching applications.
  • Simplified Onboarding: New contributors can get up to speed quickly. When they open the .code-workspace file, VS Code can automatically prompt them to install the recommended extensions, ensuring they have the right tools from the start.
  • Multi-Root Management: Easily manage multiple related but separate folders within one window. For an MLOps project, this could mean having your main source code, infrastructure-as-code (IaC) definitions, and documentation all accessible in one workspace.

How do you create and use a VS Code Workspace?

Setting up a workspace is straightforward:

  1. Open a Project Folder: Start by opening your main project folder in VS Code (File > Open Folder...).
  2. Save the Workspace: Go to File > Save Workspace As.... This creates a .code-workspace file. It's best practice to save this file in the root of your project directory.
  3. Add More Folders (Optional): If your project spans multiple directories, you can add them by navigating to File > Add Folder to Workspace....
  4. Open the Workspace: From now on, you should open the project by using File > Open Workspace from File... and selecting your .code-workspace file.

How do you configure a VS Code Workspace?

Workspace settings are defined within the .code-workspace JSON file. These settings override your global user settings, allowing for project-specific customization.

Here is a practical example for a typical Python-based MLOps project:

{
    "folders": [
        {
            "path": "." // Represents the root folder where the .code-workspace file is located
        }
    ],
    "settings": {
        // General editor settings
        "editor.formatOnSave": true, // Automatically format code on each save

        // Python-specific settings
        "python.defaultInterpreterPath": ".venv/bin/python", // Points to the project's virtual environment
        "python.testing.pytestEnabled": true, // Enables Pytest for test discovery
        "python.testing.pytestArgs": [ "tests" ], // Specifies the directory for tests

        // Language-specific editor settings for Python
        "[python]": {
            "editor.codeActionsOnSave": {
                "source.organizeImports": "explicit" // Automatically organize imports on save
            },
            "editor.defaultFormatter": "charliermarsh.ruff" // Sets Ruff as the default formatter
        }
    },
    "extensions": {
        "recommendations": [
            // Essential Python tooling
            "ms-python.python",
            "ms-python.vscode-pylance",

            // Linting, formatting, and type checking
            "charliermarsh.ruff",
            "ms-python.mypy-type-checker",

            // Other useful tools
            "redhat.vscode-yaml" // For editing YAML files (e.g., CI/CD pipelines)
        ]
    }
}

This configuration ensures a consistent and efficient development setup by: - folders: Defining which folders are part of the workspace. - settings: Enforcing coding standards like formatting on save and pointing to the correct Python interpreter and test suite. - extensions: Recommending essential extensions to the team, which VS Code will prompt users to install if they are missing.

What is the difference between User, Workspace, and Folder settings?

VS Code applies settings with the following order of precedence:

  1. User Settings (Global): These apply to all your VS Code windows and projects. You configure them once, and they serve as your default setup.
  2. Workspace Settings: These are defined in your .code-workspace file and apply to all folders within that workspace. They override User Settings.
  3. Folder Settings (Local): These are stored in a .vscode/settings.json file within a specific folder. They are useful for folder-specific tweaks within a multi-root workspace. They override both Workspace and User Settings.

For team projects, it's best to use Workspace Settings to ensure everyone shares the same core configuration. Use User Settings for personal preferences like themes or keybindings.

How can you optimize your VS Code User Settings for productivity?

While workspace settings standardize team environments, your personal user settings can significantly boost your individual productivity. This article from the MLOps Community provides an in-depth guide: How to configure VS Code for AI, ML and MLOps development in Python 🛠️️.

Key takeaways include: - Essential Extensions: Beyond project recommendations, consider tools like GitLens (for Git history), Docker, and Thunder Client (for API testing). - Productivity Settings: Configure files.autoSave to afterDelay to prevent data loss, and customize workbench.colorTheme and editor.fontFamily for visual comfort. - Custom Keybindings: Create shortcuts for frequent actions, such as running tests, opening a terminal, or toggling the side panel, to minimize mouse usage and speed up your workflow.

Additional Resources