refactor(storage): add non-creating path resolution for settings dir; use no-create in migrations and instructions to avoid side effects

This commit is contained in:
daniel-lxs 2025-09-22 19:16:48 -05:00
parent a44c30ab86
commit 604a606c74
No known key found for this signature in database
GPG key ID: 21C74479048B3AA6
3 changed files with 21 additions and 12 deletions

View file

@ -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<string> {
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 `

View file

@ -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))) {

View file

@ -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<string> {
/**
* 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<string> {
// Get user-configured custom storage path
let customStoragePath = ""
@ -30,11 +36,12 @@ export async function getStorageBasePath(defaultPath: string): Promise<string> {
}
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) {