Use xdg directories: Separate configuration, data, state, cache, and runtime storage #4437
Closedcolonelpanic8(GitHub profile, opens in a new tab)wants to merge 8 commits into
main fromrefs/pull/4437/headopened View on GitHubgithub-actions[bot](GitHub profile, opens in a new tab) added labelsvouch:unvouched
github-actions[bot](GitHub profile, opens in a new tab) added labelssize:XL
- apps/server/src/cli/config.ts lines 371 to 374Resolvedby macroscopeapp[bot](GitHub profile, opens in a new tab)@@ -200,0 +259,21 @@259+ {},260+) {261+ override get message(): string {262+ return "T3CODE_HOME/--base-dir cannot be combined with T3CODE_CONFIG_DIR, T3CODE_DATA_DIR, T3CODE_STATE_DIR, T3CODE_CACHE_DIR, or T3CODE_RUNTIME_DIR.";263+ }264+}265+266+const legacyStorageIsInitialized = Effect.fn(function* (roots: T3StorageRoots) {267+ const fs = yield* FileSystem.FileSystem;268+ const path = yield* Path.Path;269+ const artifacts = [270+ path.join(roots.stateDir, "state.sqlite"),271+ path.join(roots.configDir, "settings.json"),272+ path.join(roots.configDir, "keybindings.json"),273+ path.join(roots.stateDir, "environment-id"),274+ path.join(roots.stateDir, "connection-catalog.json"),275+ ];276+ const results = yield* Effect.all(277+ artifacts.map((artifact) => fs.exists(artifact).pipe(Effect.orElseSucceed(() => false))),278+ { concurrency: "unbounded" },279+ );
- apps/desktop/src/app/DesktopEnvironment.ts line 274Resolvedby cursor[bot](GitHub profile, opens in a new tab)@@ -151,0 +253,21 @@253+ path.join(legacyRoots.stateDir, "connection-catalog.json"),254+ path.join(appDataDirectory, legacyUserDataDirName, "Local State"),255+ path.join(appDataDirectory, legacyUserDataDirName, "Preferences"),256+ path.join(appDataDirectory, userDataDirName, "Local State"),257+ path.join(appDataDirectory, userDataDirName, "Preferences"),258+ ];259+ const legacyStorageInitialized = (yield* Effect.all(260+ legacyArtifacts.map((artifact) =>261+ fileSystem.exists(artifact).pipe(Effect.orElseSucceed(() => false)),262+ ),263+ { concurrency: "unbounded" },264+ )).some(Boolean);265+ const storageRoots = selectT3StorageRoots({266+ ...(Option.isNone(explicitLegacyRoots)267+ ? {}268+ : { explicitLegacyRoots: explicitLegacyRoots.value }),269+ ...(explicitSplitRoots === undefined ? {} : { explicitSplitRoots }),270+ defaultSplitRoots,271+ legacyRoots,272+ legacyStorageInitialized,273+ });@@ -272,0 +387,21 @@387+ homeDirectory,388+ temporaryDirectory,389+ ...(userId === undefined ? {} : { userId }),390+ isDevelopment: devUrl !== undefined,391+ environment: storageEnvironment,392+ path: pathOperations,393+ });394+ const explicitSplitRoots = hasT3StorageDirectoryOverrides(directoryOverrides)395+ ? applyT3StorageDirectoryOverrides(defaultSplitRoots, directoryOverrides)396+ : undefined;397+ const explicitLegacyRoots = Option.isSome(explicitBaseDir)398+ ? resolveLegacyT3StorageRoots({399+ baseDir: yield* resolveBaseDir(explicitBaseDir.value),400+ stateDirectoryName: "userdata",401+ path,402+ })403+ : undefined;404+ const legacyRoots = resolveLegacyT3StorageRoots({405+ baseDir: path.join(homeDirectory, ".t3"),406+ stateDirectoryName: devUrl === undefined ? "userdata" : "dev",407+ path,
- @@ -385,3 +584,4 @@385584 port: Option.none(),386585 host: Option.none(),387586 baseDir: flags.baseDir,587+ storageLayout: flags.storageLayout ?? Option.none(),
- apps/desktop/src/backend/DesktopBackendConfiguration.ts line 515Resolvedby cursor[bot](GitHub profile, opens in a new tab)@@ -470,12 +499,10 @@470499 }471500 }472501473- // Build an explicit copy of process.env minus T3CODE_HOME (dev-runner474- // exports the Windows-side base dir for the primary; if it leaks into475- // the WSL backend the Linux side ends up sharing C:\Users\...\.t3 via476- // /mnt/c, which means both backends read/write the same database and477- // their env-ids collide).478- const parentEnvWithoutT3Home: Record<string, string | undefined> = {};502+ // Do not leak Windows-side storage overrides into the Linux backend. Paths503+ // that are valid for the primary can resolve through /mnt/c in WSL, causing504+ // both backends to share a database and environment identity.505+ const parentEnvWithoutStorageOverrides: Record<string, string | undefined> = {};479506 for (const [key, value] of Object.entries(process.env)) {480- if (key === "T3CODE_HOME") continue;481- parentEnvWithoutT3Home[key] = value;507+ if (WINDOWS_STORAGE_ENV_NAMES.has(key)) continue;508+ parentEnvWithoutStorageOverrides[key] = value;apps/server/src/cli/config.ts line 469OutdatedResolvedby cursor[bot](GitHub profile, opens in a new tab)@@ -272,0 +449,21 @@449+ : resolveLegacyT3StorageRoots({450+ baseDir: yield* resolveBaseDir(bootstrap.t3Home),451+ stateDirectoryName: devUrl === undefined ? "userdata" : "dev",452+ path,453+ }));454+ const storageRoots = selectT3StorageRoots({455+ ...(forcedStorageLayout === "legacy"456+ ? { explicitLegacyRoots: explicitLegacyRoots ?? legacyRoots }457+ : explicitLegacyRoots === undefined458+ ? {}459+ : { explicitLegacyRoots }),460+ ...(forcedStorageLayout === "xdg"461+ ? { explicitSplitRoots: explicitSplitRoots ?? defaultSplitRoots }462+ : explicitSplitRoots === undefined463+ ? {}464+ : { explicitSplitRoots }),465+ ...(bootstrapRoots === undefined ? {} : { bootstrapRoots }),466+ defaultSplitRoots,467+ legacyRoots,468+ legacyStorageInitialized: yield* legacyStorageIsInitialized(legacyRoots),469+ });@@ -273,7 +461,18 @@461+ ? { explicitSplitRoots: explicitSplitRoots ?? defaultSplitRoots }462+ : explicitSplitRoots === undefined463+ ? {}464+ : { explicitSplitRoots }),465+ ...(bootstrapRoots === undefined ? {} : { bootstrapRoots }),466+ defaultSplitRoots,467+ legacyRoots,468+ legacyStorageInitialized: yield* legacyStorageIsInitialized(legacyRoots),469+ });273470 const rawCwd = Option.getOrElse(normalizedFlags.cwd, () => process.cwd());274471 const cwd = path.resolve(yield* expandHomePath(rawCwd.trim()));275472 yield* fs.makeDirectory(cwd, { recursive: true });276- const derivedPaths = yield* ServerConfig.deriveServerPaths(baseDir, devUrl, {277- baseDirIsExplicit: Option.isSome(explicitBaseDir),278- });473+ const derivedPaths = yield* ServerConfig.deriveServerPathsFromRoots(storageRoots);474+ const baseDir = derivedPaths.dataDir;279475 yield* ServerConfig.ensureServerDirectories(derivedPaths);476+ if (platform !== "win32") {477+ yield* fs.chmod(derivedPaths.runtimeDir, 0o700);478+ }
- apps/server/src/cli/config.ts line 492OutdatedResolvedby macroscopeapp[bot](GitHub profile, opens in a new tab)@@ -273,7 +475,18 @@475+ : { explicitLegacyRoots }),476+ ...(forcedStorageLayout === "xdg"477+ ? { explicitSplitRoots: explicitSplitRoots ?? defaultSplitRoots }478+ : explicitSplitRoots === undefined479+ ? {}480+ : { explicitSplitRoots }),481+ defaultSplitRoots,482+ legacyRoots,483+ legacyStorageInitialized: yield* legacyStorageIsInitialized(legacyRoots),484+ });273485 const rawCwd = Option.getOrElse(normalizedFlags.cwd, () => process.cwd());274486 const cwd = path.resolve(yield* expandHomePath(rawCwd.trim()));275487 yield* fs.makeDirectory(cwd, { recursive: true });276- const derivedPaths = yield* ServerConfig.deriveServerPaths(baseDir, devUrl, {277- baseDirIsExplicit: Option.isSome(explicitBaseDir),278- });488+ const derivedPaths = yield* ServerConfig.deriveServerPathsFromRoots(storageRoots);489+ const baseDir = derivedPaths.dataDir;279490 yield* ServerConfig.ensureServerDirectories(derivedPaths);491+ if (platform !== "win32" && storageRoots.layout === "split") {492+ yield* fs.chmod(derivedPaths.runtimeDir, 0o700);apps/server/src/cli/config.ts lines 360 to 370Resolvedby macroscopeapp[bot](GitHub profile, opens in a new tab)@@ -200,0 +279,21 @@279+export class StorageLayoutConfigurationConflictError extends Schema.TaggedErrorClass<StorageLayoutConfigurationConflictError>()(280+ "StorageLayoutConfigurationConflictError",281+ { storageLayout: CliStorageLayout },282+) {283+ override get message(): string {284+ return this.storageLayout === "xdg"285+ ? "--storage-layout xdg cannot be combined with T3CODE_HOME or --base-dir."286+ : "--storage-layout legacy cannot be combined with T3CODE_CONFIG_DIR, T3CODE_DATA_DIR, T3CODE_STATE_DIR, T3CODE_CACHE_DIR, or T3CODE_RUNTIME_DIR.";287+ }288+}289+290+const legacyStorageIsInitialized = Effect.fn(function* (roots: T3StorageRoots) {291+ const fs = yield* FileSystem.FileSystem;292+ const path = yield* Path.Path;293+ const artifacts = [294+ path.join(roots.stateDir, "state.sqlite"),295+ path.join(roots.configDir, "settings.json"),296+ path.join(roots.configDir, "keybindings.json"),297+ path.join(roots.stateDir, "environment-id"),298+ path.join(roots.stateDir, "connection-catalog.json"),299+ ];
colonelpanic8(GitHub profile, opens in a new tab) changed the title
github-actions[bot](GitHub profile, opens in a new tab) removed labelssize:XL
github-actions[bot](GitHub profile, opens in a new tab) added labelssize:XXL
- apps/server/src/cli/config.ts lines 297 to 304OutdatedResolvedby macroscopeapp[bot](GitHub profile, opens in a new tab)@@ -200,0 +284,21 @@284+ { storageLayout: CliStorageLayout },285+) {286+ override get message(): string {287+ return this.storageLayout === "xdg"288+ ? "--storage-layout xdg cannot be combined with T3CODE_HOME or --base-dir."289+ : "--storage-layout legacy cannot be combined with T3CODE_CONFIG_DIR, T3CODE_DATA_DIR, T3CODE_STATE_DIR, T3CODE_CACHE_DIR, or T3CODE_RUNTIME_DIR.";290+ }291+}292+293+export class UnsafeRuntimeDirectoryError extends Schema.TaggedErrorClass<UnsafeRuntimeDirectoryError>()(294+ "UnsafeRuntimeDirectoryError",295+ {296+ runtimeDir: Schema.String,297+ reason: Schema.Literals(["open", "not-directory", "wrong-owner", "chmod"]),298+ expectedUserId: Schema.optional(Schema.Number),299+ actualUserId: Schema.optional(Schema.Number),300+ cause: Schema.optional(Schema.Defect()),301+ },302+) {303+ override get message(): string {304+ switch (this.reason) {
- apps/desktop/src/app/DesktopEnvironment.ts line 261Resolvedby cursor[bot](GitHub profile, opens in a new tab)@@ -151,0 +237,21 @@237+ }238+ const legacyBaseDir = path.join(homeDirectory, ".t3");239+ const legacyRoots = resolveLegacyT3StorageRoots({240+ baseDir: legacyBaseDir,241+ stateDirectoryName: isDevelopment ? "dev" : "userdata",242+ path,243+ });244+ const explicitLegacyRoots = Option.map(configuredBaseDir, (baseDir) =>245+ resolveLegacyT3StorageRoots({246+ baseDir,247+ stateDirectoryName: isDevelopment ? "dev" : "userdata",248+ path,249+ }),250+ );251+ const legacyArtifacts = [252+ path.join(legacyRoots.stateDir, "state.sqlite"),253+ path.join(legacyRoots.configDir, "settings.json"),254+ path.join(legacyRoots.configDir, "keybindings.json"),255+ path.join(legacyRoots.stateDir, "environment-id"),256+ path.join(legacyRoots.stateDir, "connection-catalog.json"),257+ ];
- apps/desktop/src/app/DesktopEnvironment.ts line 244Resolvedby macroscopeapp[bot](GitHub profile, opens in a new tab)@@ -151,0 +224,21 @@224+ ? applyT3StorageDirectoryOverrides(defaultSplitRoots, directoryOverrides)225+ : undefined;226+ const configuredBaseDir = Option.map(config.t3Home, (value) => {227+ const expanded =228+ value === "~"229+ ? homeDirectory230+ : value.startsWith("~/") || value.startsWith("~\\")231+ ? path.join(homeDirectory, value.slice(2))232+ : value;233+ return path.resolve(expanded);234+ });235+ if (Option.isSome(configuredBaseDir) && hasT3StorageDirectoryOverrides(directoryOverrides)) {236+ return yield* new DesktopStorageDirectoryConfigurationConflictError();237+ }238+ const legacyBaseDir = path.join(homeDirectory, ".t3");239+ const legacyRoots = resolveLegacyT3StorageRoots({240+ baseDir: legacyBaseDir,241+ stateDirectoryName: isDevelopment ? "dev" : "userdata",242+ path,243+ });244+ const explicitLegacyRoots = Option.map(configuredBaseDir, (baseDir) =>apps/desktop/src/app/DesktopApp.ts line 232OutdatedResolvedby macroscopeapp[bot](GitHub profile, opens in a new tab)@@ -229,3 +229,4 @@229229 yield* shellEnvironment.installIntoProcess;230230 const userDataPath = yield* appIdentity.resolveUserDataPath;231231 yield* electronApp.setPath("userData", userDataPath);232+ if (environment.storageLayout === "split") {
- apps/desktop/src/app/DesktopEnvironment.ts lines 251 to 261Resolvedby macroscopeapp[bot](GitHub profile, opens in a new tab)@@ -151,0 +238,21 @@238+ const legacyBaseDir = path.join(homeDirectory, ".t3");239+ const legacyRoots = resolveLegacyT3StorageRoots({240+ baseDir: legacyBaseDir,241+ stateDirectoryName: isDevelopment ? "dev" : "userdata",242+ path,243+ });244+ const explicitLegacyRoots = Option.map(configuredBaseDir, (baseDir) =>245+ resolveLegacyT3StorageRoots({246+ baseDir,247+ stateDirectoryName: "userdata",248+ path,249+ }),250+ );251+ const legacyArtifacts = [252+ path.join(legacyRoots.stateDir, "state.sqlite"),253+ path.join(legacyRoots.configDir, "settings.json"),254+ path.join(legacyRoots.configDir, "keybindings.json"),255+ path.join(legacyRoots.stateDir, "environment-id"),256+ path.join(legacyRoots.stateDir, "connection-catalog.json"),257+ path.join(legacyRoots.stateDir, "secrets"),258+ ];
- apps/server/src/cli/config.ts lines 282 to 287OutdatedResolvedby macroscopeapp[bot](GitHub profile, opens in a new tab)@@ -198,3 +270,18 @@198270 return parsePersistedServerObservabilitySettings(raw);199271 });200272273+export class StorageDirectoryConfigurationConflictError extends Schema.TaggedErrorClass<StorageDirectoryConfigurationConflictError>()(274+ "StorageDirectoryConfigurationConflictError",275+ {},276+) {277+ override get message(): string {278+ return "T3CODE_HOME/--base-dir cannot be combined with T3CODE_CONFIG_DIR, T3CODE_DATA_DIR, T3CODE_STATE_DIR, T3CODE_CACHE_DIR, or T3CODE_RUNTIME_DIR.";279+ }280+}281+282+export class StorageLayoutConfigurationConflictError extends Schema.TaggedErrorClass<StorageLayoutConfigurationConflictError>()(283+ "StorageLayoutConfigurationConflictError",284+ { storageLayout: CliStorageLayout },285+) {286+ override get message(): string {287+ return this.storageLayout === "xdg"
- apps/desktop/src/app/DesktopEnvironment.ts line 260Resolvedby cursor[bot](GitHub profile, opens in a new tab)@@ -151,0 +240,21 @@240+ baseDir: legacyBaseDir,241+ stateDirectoryName: isDevelopment ? "dev" : "userdata",242+ path,243+ });244+ const explicitLegacyRoots = Option.map(configuredBaseDir, (baseDir) =>245+ resolveLegacyT3StorageRoots({246+ baseDir,247+ stateDirectoryName: "userdata",248+ path,249+ }),250+ );251+ const legacyArtifacts = [252+ path.join(legacyRoots.stateDir, "state.sqlite"),253+ path.join(legacyRoots.configDir, "settings.json"),254+ path.join(legacyRoots.configDir, "keybindings.json"),255+ path.join(legacyRoots.configDir, "desktop-settings.json"),256+ path.join(legacyRoots.configDir, "client-settings.json"),257+ path.join(legacyRoots.stateDir, "environment-id"),258+ path.join(legacyRoots.stateDir, "connection-catalog.json"),259+ path.join(legacyRoots.stateDir, "secrets"),260+ path.join(legacyRoots.stateDir, "saved-environments.json"),
- apps/desktop/src/app/DesktopEnvironment.ts line 249Resolvedby cursor[bot](GitHub profile, opens in a new tab)@@ -151,0 +229,21 @@229+ ? homeDirectory230+ : value.startsWith("~/") || value.startsWith("~\\")231+ ? path.join(homeDirectory, value.slice(2))232+ : value;233+ return path.resolve(expanded);234+ });235+ if (Option.isSome(configuredBaseDir) && hasT3StorageDirectoryOverrides(directoryOverrides)) {236+ return yield* new DesktopStorageDirectoryConfigurationConflictError();237+ }238+ const legacyBaseDir = path.join(homeDirectory, ".t3");239+ const legacyRoots = resolveLegacyT3StorageRoots({240+ baseDir: legacyBaseDir,241+ stateDirectoryName: isDevelopment ? "dev" : "userdata",242+ path,243+ });244+ const explicitLegacyRoots = Option.map(configuredBaseDir, (baseDir) =>245+ resolveLegacyT3StorageRoots({246+ baseDir,247+ stateDirectoryName: "userdata",248+ path,249+ }),
- packages/shared/src/storagePaths.ts lines 131 to 131OutdatedResolvedby macroscopeapp[bot](GitHub profile, opens in a new tab)@@ -0,0 +111,21 @@111+ trimmed(environment.APPDATA) ?? path.join(homeDirectory, "AppData", "Roaming");112+ const localApplicationData =113+ trimmed(environment.LOCALAPPDATA) ?? path.join(homeDirectory, "AppData", "Local");114+ const localRoot = path.join(localApplicationData, applicationDirectoryName);115+ return {116+ layout: "split",117+ configDir: path.join(roamingApplicationData, applicationDirectoryName),118+ dataDir: path.join(localRoot, "data"),119+ stateDir: path.join(localRoot, "state"),120+ cacheDir: path.join(localRoot, "cache"),121+ runtimeDir: path.join(localRoot, "runtime"),122+ };123+ }124+125+ const xdgConfigHome = absoluteEnvironmentDirectory(environment.XDG_CONFIG_HOME, path);126+ const xdgDataHome = absoluteEnvironmentDirectory(environment.XDG_DATA_HOME, path);127+ const xdgStateHome = absoluteEnvironmentDirectory(environment.XDG_STATE_HOME, path);128+ const xdgCacheHome = absoluteEnvironmentDirectory(environment.XDG_CACHE_HOME, path);129+ const xdgRuntimeHome = absoluteEnvironmentDirectory(environment.XDG_RUNTIME_DIR, path);130+131+ if (platform === "darwin" && xdgConfigHome === undefined && xdgDataHome === undefined) {
t3dotgg(GitHub profile, opens in a new tab) closed this
Sign in to join this conversation.


Important
Review skipped#
Auto reviews are disabled on this repository. Please check the settings in the CodeRabbit UI or the
.coderabbit.yamlfile in this repository. To trigger a single review, invoke the@coderabbitai reviewcommand.⚙️ Run configuration
Configuration used: Repository UI
Review profile: CHILL
Plan: Pro Plus
Run ID:
fed5be41-0b94-441c-8ef2-accbcb1b5945You can disable this status message by setting the
reviews.review_statustofalsein the CodeRabbit configuration file.Use the checkbox below for a quick retry:
✨ Finishing Touches
🧪 Generate unit tests (beta)
Thanks for using CodeRabbit! It's free for OSS, and your support helps us grow. If you like it, consider giving us a shout-out.
❤️ Share