|
| 1 | +# Ensures host directories used as devcontainer bind mounts exist before Docker tries to mount them. |
| 2 | +# If "Code - Insiders" is not installed, we create an empty directory so the mount succeeds; |
| 3 | +# the extension then falls back to the always-present "Code" (stable) data automatically. |
| 4 | + |
| 5 | +# ── Detect which VS Code edition launched this devcontainer ────────────────── |
| 6 | +$launchedFrom = "Unknown" |
| 7 | + |
| 8 | +# $env:VSCODE_PID is set by VS Code in its integrated terminal / task runner. |
| 9 | +# Use it to look up the executable path of the launching process. |
| 10 | +if ($env:VSCODE_PID) { |
| 11 | + try { |
| 12 | + $vsProc = Get-Process -Id $env:VSCODE_PID -ErrorAction Stop |
| 13 | + $exePath = $vsProc.MainModule.FileName |
| 14 | + if ($exePath -match 'Insiders') { |
| 15 | + $launchedFrom = "Code - Insiders" |
| 16 | + } else { |
| 17 | + $launchedFrom = "Code" |
| 18 | + } |
| 19 | + Write-Host "Detected VS Code edition from PID $($env:VSCODE_PID): $launchedFrom ($exePath)" |
| 20 | + } catch { |
| 21 | + Write-Host "Could not resolve VSCODE_PID $($env:VSCODE_PID): $_" |
| 22 | + } |
| 23 | +} |
| 24 | + |
| 25 | +# Fallback: scan running processes for Code/Code-Insiders executables |
| 26 | +if ($launchedFrom -eq "Unknown") { |
| 27 | + $insidersRunning = Get-Process -ErrorAction SilentlyContinue | |
| 28 | + Where-Object { try { $_.MainModule.FileName -match 'Code.*Insiders' } catch { $false } } | |
| 29 | + Select-Object -First 1 |
| 30 | + |
| 31 | + $stableRunning = Get-Process -ErrorAction SilentlyContinue | |
| 32 | + Where-Object { try { $_.MainModule.FileName -match '\\Code\\' } catch { $false } } | |
| 33 | + Select-Object -First 1 |
| 34 | + |
| 35 | + if ($insidersRunning) { |
| 36 | + $launchedFrom = "Code - Insiders (detected from running process)" |
| 37 | + } elseif ($stableRunning) { |
| 38 | + $launchedFrom = "Code (detected from running process)" |
| 39 | + } else { |
| 40 | + $launchedFrom = "Could not detect (no Code process found; defaulting to stable)" |
| 41 | + } |
| 42 | + Write-Host "VS Code edition (fallback detection): $launchedFrom" |
| 43 | +} |
| 44 | + |
| 45 | +$activeSubPath = if ($launchedFrom -match 'Insiders') { "Code - Insiders\User" } else { "Code\User" } |
| 46 | +$activePath = Join-Path $env:APPDATA $activeSubPath |
| 47 | +Write-Host "Active VS Code session data: $activePath" |
| 48 | + |
| 49 | +# ── Ensure both mount source directories exist ─────────────────────────────── |
| 50 | +$stablePath = Join-Path $env:APPDATA "Code\User" |
| 51 | +$insidersPath = Join-Path $env:APPDATA "Code - Insiders\User" |
| 52 | + |
| 53 | +foreach ($entry in @( |
| 54 | + @{ Path = $stablePath; Label = "Code (stable)" }, |
| 55 | + @{ Path = $insidersPath; Label = "Code - Insiders" } |
| 56 | +)) { |
| 57 | + if (-not (Test-Path $entry.Path)) { |
| 58 | + Write-Host "$($entry.Label) not found at '$($entry.Path)'. Creating empty directory so the bind mount does not fail." |
| 59 | + New-Item -ItemType Directory -Force $entry.Path | Out-Null |
| 60 | + } else { |
| 61 | + $marker = if ($entry.Path -eq $activePath) { " ← active" } else { "" } |
| 62 | + Write-Host "$($entry.Label) found at '$($entry.Path)'.$marker" |
| 63 | + } |
| 64 | +} |
0 commit comments