Skip to content

Commit a69accb

Browse files
authored
feat: Add support to the vscode-web module for loading workspaces at startup (#349)
1 parent 29e5307 commit a69accb

File tree

3 files changed

+58
-16
lines changed

3 files changed

+58
-16
lines changed

registry/coder/modules/vscode-web/README.md

Lines changed: 20 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,7 @@ Automatically install [Visual Studio Code Server](https://code.visualstudio.com/
1414
module "vscode-web" {
1515
count = data.coder_workspace.me.start_count
1616
source = "registry.coder.com/coder/vscode-web/coder"
17-
version = "1.3.1"
17+
version = "1.4.0"
1818
agent_id = coder_agent.example.id
1919
accept_license = true
2020
}
@@ -30,7 +30,7 @@ module "vscode-web" {
3030
module "vscode-web" {
3131
count = data.coder_workspace.me.start_count
3232
source = "registry.coder.com/coder/vscode-web/coder"
33-
version = "1.3.1"
33+
version = "1.4.0"
3434
agent_id = coder_agent.example.id
3535
install_prefix = "/home/coder/.vscode-web"
3636
folder = "/home/coder"
@@ -44,7 +44,7 @@ module "vscode-web" {
4444
module "vscode-web" {
4545
count = data.coder_workspace.me.start_count
4646
source = "registry.coder.com/coder/vscode-web/coder"
47-
version = "1.3.1"
47+
version = "1.4.0"
4848
agent_id = coder_agent.example.id
4949
extensions = ["github.copilot", "ms-python.python", "ms-toolsai.jupyter"]
5050
accept_license = true
@@ -59,7 +59,7 @@ Configure VS Code's [settings.json](https://code.visualstudio.com/docs/getstarte
5959
module "vscode-web" {
6060
count = data.coder_workspace.me.start_count
6161
source = "registry.coder.com/coder/vscode-web/coder"
62-
version = "1.3.1"
62+
version = "1.4.0"
6363
agent_id = coder_agent.example.id
6464
extensions = ["dracula-theme.theme-dracula"]
6565
settings = {
@@ -77,9 +77,24 @@ By default, this module installs the latest. To pin a specific version, retrieve
7777
module "vscode-web" {
7878
count = data.coder_workspace.me.start_count
7979
source = "registry.coder.com/coder/vscode-web/coder"
80-
version = "1.3.1"
80+
version = "1.4.0"
8181
agent_id = coder_agent.example.id
8282
commit_id = "e54c774e0add60467559eb0d1e229c6452cf8447"
8383
accept_license = true
8484
}
8585
```
86+
87+
### Open an existing workspace on startup
88+
89+
To open an existing workspace on startup the `workspace` parameter can be used to represent a path on disk to a `code-workspace` file.
90+
Note: Either `workspace` or `folder` can be used, but not both simultaneously. The `code-workspace` file must already be present on disk.
91+
92+
```tf
93+
module "vscode-web" {
94+
count = data.coder_workspace.me.start_count
95+
source = "registry.coder.com/coder/vscode-web/coder"
96+
version = "1.4.0"
97+
agent_id = coder_agent.example.id
98+
workspace = "/home/coder/coder.code-workspace"
99+
}
100+
```

registry/coder/modules/vscode-web/main.tf

Lines changed: 20 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -158,6 +158,12 @@ variable "platform" {
158158
}
159159
}
160160

161+
variable "workspace" {
162+
type = string
163+
description = "Path to a .code-workspace file to open in vscode-web."
164+
default = null
165+
}
166+
161167
data "coder_workspace_owner" "me" {}
162168
data "coder_workspace" "me" {}
163169

@@ -178,6 +184,7 @@ resource "coder_script" "vscode-web" {
178184
DISABLE_TRUST : var.disable_trust,
179185
EXTENSIONS_DIR : var.extensions_dir,
180186
FOLDER : var.folder,
187+
WORKSPACE : var.workspace,
181188
AUTO_INSTALL_EXTENSIONS : var.auto_install_extensions,
182189
SERVER_BASE_PATH : local.server_base_path,
183190
COMMIT_ID : var.commit_id,
@@ -195,6 +202,11 @@ resource "coder_script" "vscode-web" {
195202
condition = !var.offline || !var.use_cached
196203
error_message = "Offline and Use Cached can not be used together"
197204
}
205+
206+
precondition {
207+
condition = (var.workspace == "" || var.folder == "")
208+
error_message = "Set only one of `workspace` or `folder`."
209+
}
198210
}
199211
}
200212

@@ -218,6 +230,12 @@ resource "coder_app" "vscode-web" {
218230

219231
locals {
220232
server_base_path = var.subdomain ? "" : format("/@%s/%s/apps/%s/", data.coder_workspace_owner.me.name, data.coder_workspace.me.name, var.slug)
221-
url = var.folder == "" ? "http://localhost:${var.port}${local.server_base_path}" : "http://localhost:${var.port}${local.server_base_path}?folder=${var.folder}"
222-
healthcheck_url = var.subdomain ? "http://localhost:${var.port}/healthz" : "http://localhost:${var.port}${local.server_base_path}/healthz"
233+
url = (
234+
var.workspace != "" ?
235+
"http://localhost:${var.port}${local.server_base_path}?workspace=${urlencode(var.workspace)}" :
236+
var.folder != "" ?
237+
"http://localhost:${var.port}${local.server_base_path}?folder=${urlencode(var.folder)}" :
238+
"http://localhost:${var.port}${local.server_base_path}"
239+
)
240+
healthcheck_url = var.subdomain ? "http://localhost:${var.port}/healthz" : "http://localhost:${var.port}${local.server_base_path}/healthz"
223241
}

registry/coder/modules/vscode-web/run.sh

Lines changed: 18 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -109,18 +109,27 @@ if [ "${AUTO_INSTALL_EXTENSIONS}" = true ]; then
109109
if ! command -v jq > /dev/null; then
110110
echo "jq is required to install extensions from a workspace file."
111111
else
112-
WORKSPACE_DIR="$HOME"
113-
if [ -n "${FOLDER}" ]; then
114-
WORKSPACE_DIR="${FOLDER}"
115-
fi
116-
117-
if [ -f "$WORKSPACE_DIR/.vscode/extensions.json" ]; then
118-
printf "🧩 Installing extensions from %s/.vscode/extensions.json...\n" "$WORKSPACE_DIR"
119-
# Use sed to remove single-line comments before parsing with jq
120-
extensions=$(sed 's|//.*||g' "$WORKSPACE_DIR"/.vscode/extensions.json | jq -r '.recommendations[]')
112+
# Prefer WORKSPACE if set and points to a file
113+
if [ -n "${WORKSPACE}" ] && [ -f "${WORKSPACE}" ]; then
114+
printf "🧩 Installing extensions from %s...\n" "${WORKSPACE}"
115+
# Strip single-line comments then parse .extensions.recommendations[]
116+
extensions=$(sed 's|//.*||g' "${WORKSPACE}" | jq -r '(.extensions.recommendations // [])[]')
121117
for extension in $extensions; do
122118
$VSCODE_WEB "$EXTENSION_ARG" --install-extension "$extension" --force
123119
done
120+
else
121+
# Fallback to folder-based .vscode/extensions.json (existing behavior)
122+
WORKSPACE_DIR="$HOME"
123+
if [ -n "${FOLDER}" ]; then
124+
WORKSPACE_DIR="${FOLDER}"
125+
fi
126+
if [ -f "$WORKSPACE_DIR/.vscode/extensions.json" ]; then
127+
printf "🧩 Installing extensions from %s/.vscode/extensions.json...\n" "$WORKSPACE_DIR"
128+
extensions=$(sed 's|//.*||g' "$WORKSPACE_DIR/.vscode/extensions.json" | jq -r '.recommendations[]')
129+
for extension in $extensions; do
130+
$VSCODE_WEB "$EXTENSION_ARG" --install-extension "$extension" --force
131+
done
132+
fi
124133
fi
125134
fi
126135
fi

0 commit comments

Comments
 (0)