Skip to content

Commit 604a606

Browse files
committed
refactor(storage): add non-creating path resolution for settings dir; use no-create in migrations and instructions to avoid side effects
1 parent a44c30a commit 604a606

File tree

3 files changed

+21
-12
lines changed

3 files changed

+21
-12
lines changed

src/core/prompts/instructions/create-mode.ts

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -2,13 +2,14 @@ import * as path from "path"
22
import * as vscode from "vscode"
33

44
import { GlobalFileNames } from "../../../shared/globalFileNames"
5-
import { getSettingsDirectoryPath } from "../../../utils/storage"
5+
import { getStorageBasePath } from "../../../utils/storage"
66

77
export async function createModeInstructions(context: vscode.ExtensionContext | undefined): Promise<string> {
88
if (!context) throw new Error("Missing VSCode Extension Context")
99

10-
// Use getSettingsDirectoryPath to respect custom storage path setting
11-
const settingsDir = await getSettingsDirectoryPath(context.globalStorageUri.fsPath)
10+
// Resolve settings directory without creating it (avoid side effects for help text)
11+
const basePath = await getStorageBasePath(context.globalStorageUri.fsPath, false)
12+
const settingsDir = path.join(basePath, "settings")
1213
const customModesPath = path.join(settingsDir, GlobalFileNames.customModes)
1314

1415
return `

src/utils/migrateSettings.ts

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@ import * as path from "path"
33
import * as fs from "fs/promises"
44
import { fileExistsAtPath } from "./fs"
55
import { GlobalFileNames } from "../shared/globalFileNames"
6-
import { getSettingsDirectoryPath } from "./storage"
6+
import { getStorageBasePath } from "./storage"
77
import * as yaml from "yaml"
88

99
const deprecatedCustomModesJSONFilename = "custom_modes.json"
@@ -27,8 +27,9 @@ export async function migrateSettings(
2727
]
2828

2929
try {
30-
// Use getSettingsDirectoryPath to respect custom storage path
31-
const settingsDir = await getSettingsDirectoryPath(context.globalStorageUri.fsPath)
30+
// Resolve settings directory without creating it to preserve early-return behavior
31+
const basePath = await getStorageBasePath(context.globalStorageUri.fsPath, false)
32+
const settingsDir = path.join(basePath, "settings")
3233

3334
// Check if settings directory exists first
3435
if (!(await fileExistsAtPath(settingsDir))) {

src/utils/storage.ts

Lines changed: 13 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,13 @@ import { t } from "../i18n"
1111
* If a custom path is configured, uses that path
1212
* Otherwise uses the default VSCode extension global storage path
1313
*/
14-
export async function getStorageBasePath(defaultPath: string): Promise<string> {
14+
/**
15+
* Gets the base storage path for conversations
16+
* If a custom path is configured, uses that path
17+
* Otherwise uses the default VSCode extension global storage path
18+
* Optionally avoid creating the directory (create = false) for pure path resolution.
19+
*/
20+
export async function getStorageBasePath(defaultPath: string, create = true): Promise<string> {
1521
// Get user-configured custom storage path
1622
let customStoragePath = ""
1723

@@ -30,11 +36,12 @@ export async function getStorageBasePath(defaultPath: string): Promise<string> {
3036
}
3137

3238
try {
33-
// Ensure custom path exists
34-
await fs.mkdir(customStoragePath, { recursive: true })
35-
36-
// Check directory write permission without creating temp files
37-
await fs.access(customStoragePath, fsConstants.R_OK | fsConstants.W_OK | fsConstants.X_OK)
39+
// When create is requested, ensure the custom path exists and is accessible
40+
if (create) {
41+
await fs.mkdir(customStoragePath, { recursive: true })
42+
// Check directory write permission without creating temp files
43+
await fs.access(customStoragePath, fsConstants.R_OK | fsConstants.W_OK | fsConstants.X_OK)
44+
}
3845

3946
return customStoragePath
4047
} catch (error) {

0 commit comments

Comments
 (0)