fix: Respect the user's XDG Document folder settings on Linux. (#1884)

This is related to issue #899.

Co-authored-by: Dennis Bartlett <bartlett.dc.1@gmail.com>
This commit is contained in:
Jon Atkinson 2025-02-28 02:23:29 +00:00 committed by GitHub
parent ea2f4080f8
commit 602d25075b
No known key found for this signature in database
GPG key ID: B5690EEEBB952194
2 changed files with 27 additions and 5 deletions

View file

@ -0,0 +1,5 @@
---
"claude-dev": patch
---
Change how Cline finds the path to the user's Documents folder by querying xdg-user-dir on Linux systems.

View file

@ -1141,21 +1141,38 @@ export class ClineProvider implements vscode.WebviewViewProvider {
async getDocumentsPath(): Promise<string> {
if (process.platform === "win32") {
// If the user is running Win 7/Win Server 2008 r2+, we want to get the correct path to their Documents directory.
try {
const { stdout: docsPath } = await execa("powershell", [
"-NoProfile", // Ignore user's PowerShell profile(s)
"-Command",
"[System.Environment]::GetFolderPath([System.Environment+SpecialFolder]::MyDocuments)",
])
return docsPath.trim()
const trimmedPath = docsPath.trim()
if (trimmedPath) {
return trimmedPath
}
} catch (err) {
console.error("Failed to retrieve Windows Documents path. Falling back to homedir/Documents.")
return path.join(os.homedir(), "Documents")
}
} else {
return path.join(os.homedir(), "Documents") // On POSIX (macOS, Linux, etc.), assume ~/Documents by default (existing behavior, but may want to implement similar logic here)
} else if (process.platform === "linux") {
try {
// First check if xdg-user-dir exists
await execa("which", ["xdg-user-dir"])
// If it exists, try to get XDG documents path
const { stdout } = await execa("xdg-user-dir", ["DOCUMENTS"])
const trimmedPath = stdout.trim()
if (trimmedPath) {
return trimmedPath
}
} catch {
// Log error but continue to fallback
console.error("Failed to retrieve XDG Documents path. Falling back to homedir/Documents.")
}
}
// Default fallback for all platforms
return path.join(os.homedir(), "Documents")
}
async ensureMcpServersDirectoryExists(): Promise<string> {