From 59fc0fe9dbad6a077eadf444234ce9a8c084da21 Mon Sep 17 00:00:00 2001 From: Xingyao Wang Date: Thu, 1 May 2025 12:00:43 +0800 Subject: [PATCH] Move VSCode settings.json setup from Dockerfile to VSCode plugin initialization (#8192) Co-authored-by: openhands --- openhands/runtime/plugins/vscode/__init__.py | 27 +++++++++++++++++++ .../utils/runtime_templates/Dockerfile.j2 | 6 ----- 2 files changed, 27 insertions(+), 6 deletions(-) diff --git a/openhands/runtime/plugins/vscode/__init__.py b/openhands/runtime/plugins/vscode/__init__.py index 852e0e3045..e583e2a107 100644 --- a/openhands/runtime/plugins/vscode/__init__.py +++ b/openhands/runtime/plugins/vscode/__init__.py @@ -1,7 +1,9 @@ import asyncio import os +import shutil import uuid from dataclasses import dataclass +from pathlib import Path from typing import Optional from openhands.core.logger import openhands_logger as logger @@ -33,6 +35,9 @@ class VSCodePlugin(Plugin): ) return + # Set up VSCode settings.json + self._setup_vscode_settings() + self.vscode_port = int(os.environ['VSCODE_PORT']) self.vscode_connection_token = str(uuid.uuid4()) assert check_port_available(self.vscode_port) @@ -67,6 +72,28 @@ class VSCodePlugin(Plugin): f'VSCode server started at port {self.vscode_port}. Output: {output}' ) + def _setup_vscode_settings(self) -> None: + """ + Set up VSCode settings by creating the .vscode directory in the workspace + and copying the settings.json file there. + """ + # Get the path to the settings.json file in the plugin directory + current_dir = Path(__file__).parent + settings_path = current_dir / 'settings.json' + + # Create the .vscode directory in the workspace if it doesn't exist + vscode_dir = Path('/workspace/.vscode') + vscode_dir.mkdir(parents=True, exist_ok=True) + + # Copy the settings.json file to the .vscode directory + target_path = vscode_dir / 'settings.json' + shutil.copy(settings_path, target_path) + + # Make sure the settings file is readable and writable by all users + os.chmod(target_path, 0o666) + + logger.debug(f'VSCode settings copied to {target_path}') + async def run(self, action: Action) -> Observation: """Run the plugin for a given action.""" raise NotImplementedError('VSCodePlugin does not support run method') diff --git a/openhands/runtime/utils/runtime_templates/Dockerfile.j2 b/openhands/runtime/utils/runtime_templates/Dockerfile.j2 index 610ce537ef..a381edc11a 100644 --- a/openhands/runtime/utils/runtime_templates/Dockerfile.j2 +++ b/openhands/runtime/utils/runtime_templates/Dockerfile.j2 @@ -155,9 +155,6 @@ RUN \ COPY ./code/pyproject.toml ./code/poetry.lock /openhands/code/ -COPY ./code/openhands/runtime/plugins/vscode/settings.json /workspace/.vscode/settings.json -RUN chmod -R a+rwx /workspace/.vscode/settings.json - {{ install_dependencies() }} # ================================================================ @@ -171,9 +168,6 @@ RUN chmod -R a+rwx /workspace/.vscode/settings.json RUN if [ -d /openhands/code/openhands ]; then rm -rf /openhands/code/openhands; fi COPY ./code/pyproject.toml ./code/poetry.lock /openhands/code/ -COPY ./code/openhands/runtime/plugins/vscode/settings.json /workspace/.vscode/settings.json -RUN chmod -R a+rwx /workspace/.vscode/settings.json - COPY ./code/openhands /openhands/code/openhands RUN chmod a+rwx /openhands/code/openhands/__init__.py