Skip to content

Commit f39cc5c

Browse files
authored
fix(global-config): respect XDG_CONFIG_HOME on all platforms (#378)
Prioritize XDG_CONFIG_HOME on Windows to fix test environment overrides. Previously, Windows would always use APPDATA regardless of XDG_CONFIG_HOME, causing tests to fail. Now XDG_CONFIG_HOME is checked first on all platforms before falling back to platform-specific defaults. Also update the Windows APPDATA test to explicitly clear XDG_CONFIG_HOME when testing the fallback behavior.
1 parent 5129a8c commit f39cc5c

File tree

2 files changed

+12
-9
lines changed

2 files changed

+12
-9
lines changed

src/core/global-config.ts

Lines changed: 10 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -18,10 +18,17 @@ const DEFAULT_CONFIG: GlobalConfig = {
1818
/**
1919
* Gets the global configuration directory path following XDG Base Directory Specification.
2020
*
21-
* - Unix/macOS: $XDG_CONFIG_HOME/openspec/ or ~/.config/openspec/
22-
* - Windows: %APPDATA%/openspec/
21+
* - All platforms: $XDG_CONFIG_HOME/openspec/ if XDG_CONFIG_HOME is set
22+
* - Unix/macOS fallback: ~/.config/openspec/
23+
* - Windows fallback: %APPDATA%/openspec/
2324
*/
2425
export function getGlobalConfigDir(): string {
26+
// XDG_CONFIG_HOME takes precedence on all platforms when explicitly set
27+
const xdgConfigHome = process.env.XDG_CONFIG_HOME;
28+
if (xdgConfigHome) {
29+
return path.join(xdgConfigHome, GLOBAL_CONFIG_DIR_NAME);
30+
}
31+
2532
const platform = os.platform();
2633

2734
if (platform === 'win32') {
@@ -34,12 +41,7 @@ export function getGlobalConfigDir(): string {
3441
return path.join(os.homedir(), 'AppData', 'Roaming', GLOBAL_CONFIG_DIR_NAME);
3542
}
3643

37-
// Unix/macOS: use XDG_CONFIG_HOME or fallback to ~/.config
38-
const xdgConfigHome = process.env.XDG_CONFIG_HOME;
39-
if (xdgConfigHome) {
40-
return path.join(xdgConfigHome, GLOBAL_CONFIG_DIR_NAME);
41-
}
42-
44+
// Unix/macOS fallback: ~/.config
4345
return path.join(os.homedir(), '.config', GLOBAL_CONFIG_DIR_NAME);
4446
}
4547

test/core/global-config.test.ts

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -70,10 +70,11 @@ describe('global-config', () => {
7070
}
7171
});
7272

73-
it('should use APPDATA on Windows', () => {
73+
it('should use APPDATA on Windows when XDG_CONFIG_HOME is not set', () => {
7474
// This test only makes sense conceptually - we can't change os.platform()
7575
// But we can verify the APPDATA logic by checking the code path
7676
if (os.platform() === 'win32') {
77+
delete process.env.XDG_CONFIG_HOME;
7778
const appData = process.env.APPDATA;
7879
if (appData) {
7980
const result = getGlobalConfigDir();

0 commit comments

Comments
 (0)