From 604a606c743918efe2fd8688788ef0484cbbe00c Mon Sep 17 00:00:00 2001 From: daniel-lxs Date: Mon, 22 Sep 2025 19:16:48 -0500 Subject: [PATCH] refactor(storage): add non-creating path resolution for settings dir; use no-create in migrations and instructions to avoid side effects --- src/core/prompts/instructions/create-mode.ts | 7 ++++--- src/utils/migrateSettings.ts | 7 ++++--- src/utils/storage.ts | 19 +++++++++++++------ 3 files changed, 21 insertions(+), 12 deletions(-) diff --git a/src/core/prompts/instructions/create-mode.ts b/src/core/prompts/instructions/create-mode.ts index 39721319f6..8ece23e001 100644 --- a/src/core/prompts/instructions/create-mode.ts +++ b/src/core/prompts/instructions/create-mode.ts @@ -2,13 +2,14 @@ import * as path from "path" import * as vscode from "vscode" import { GlobalFileNames } from "../../../shared/globalFileNames" -import { getSettingsDirectoryPath } from "../../../utils/storage" +import { getStorageBasePath } from "../../../utils/storage" export async function createModeInstructions(context: vscode.ExtensionContext | undefined): Promise { if (!context) throw new Error("Missing VSCode Extension Context") - // Use getSettingsDirectoryPath to respect custom storage path setting - const settingsDir = await getSettingsDirectoryPath(context.globalStorageUri.fsPath) + // Resolve settings directory without creating it (avoid side effects for help text) + const basePath = await getStorageBasePath(context.globalStorageUri.fsPath, false) + const settingsDir = path.join(basePath, "settings") const customModesPath = path.join(settingsDir, GlobalFileNames.customModes) return ` diff --git a/src/utils/migrateSettings.ts b/src/utils/migrateSettings.ts index ec8f518c31..65feb9bef3 100644 --- a/src/utils/migrateSettings.ts +++ b/src/utils/migrateSettings.ts @@ -3,7 +3,7 @@ import * as path from "path" import * as fs from "fs/promises" import { fileExistsAtPath } from "./fs" import { GlobalFileNames } from "../shared/globalFileNames" -import { getSettingsDirectoryPath } from "./storage" +import { getStorageBasePath } from "./storage" import * as yaml from "yaml" const deprecatedCustomModesJSONFilename = "custom_modes.json" @@ -27,8 +27,9 @@ export async function migrateSettings( ] try { - // Use getSettingsDirectoryPath to respect custom storage path - const settingsDir = await getSettingsDirectoryPath(context.globalStorageUri.fsPath) + // Resolve settings directory without creating it to preserve early-return behavior + const basePath = await getStorageBasePath(context.globalStorageUri.fsPath, false) + const settingsDir = path.join(basePath, "settings") // Check if settings directory exists first if (!(await fileExistsAtPath(settingsDir))) { diff --git a/src/utils/storage.ts b/src/utils/storage.ts index 5125d6cf44..848f05cba2 100644 --- a/src/utils/storage.ts +++ b/src/utils/storage.ts @@ -11,7 +11,13 @@ import { t } from "../i18n" * If a custom path is configured, uses that path * Otherwise uses the default VSCode extension global storage path */ -export async function getStorageBasePath(defaultPath: string): Promise { +/** + * Gets the base storage path for conversations + * If a custom path is configured, uses that path + * Otherwise uses the default VSCode extension global storage path + * Optionally avoid creating the directory (create = false) for pure path resolution. + */ +export async function getStorageBasePath(defaultPath: string, create = true): Promise { // Get user-configured custom storage path let customStoragePath = "" @@ -30,11 +36,12 @@ export async function getStorageBasePath(defaultPath: string): Promise { } try { - // Ensure custom path exists - await fs.mkdir(customStoragePath, { recursive: true }) - - // Check directory write permission without creating temp files - await fs.access(customStoragePath, fsConstants.R_OK | fsConstants.W_OK | fsConstants.X_OK) + // When create is requested, ensure the custom path exists and is accessible + if (create) { + await fs.mkdir(customStoragePath, { recursive: true }) + // Check directory write permission without creating temp files + await fs.access(customStoragePath, fsConstants.R_OK | fsConstants.W_OK | fsConstants.X_OK) + } return customStoragePath } catch (error) {