Use XDG base directories and Known Folders on Windows (#1809)
This changes logic that previously read/wrote from `~/.pkl` to use XDG base directories (all OSes), and Known Folders locations on Windows. For example, Pkl will look for `settings.pkl` in: 1. `$XDG_CONFIG_HOME/pkl/settings.pkl` 2. `%APPDATA/pkl/settings.pkl` 3. `~/.pkl/settings.pkl` 4. Path pkl/settings/pkl within `$XDG_CONFIG_DIRS` 5. `/etc/xdg/pkl/settings.pkl` --------- Co-authored-by: Florin Ungur <florin@florinungur.com>
This commit is contained in:
1 parent
4dd37219c0
commit
6551a59f9e
24 files changed
+799
-40
No files matched your search
@@ -58,7 +58,7 @@ public final class EvaluatorBuilder {
|
||||
|
||||
private java.time.@Nullable Duration timeout;
|
||||
|
||||
private @Nullable Path moduleCacheDir = IoUtils.getDefaultModuleCacheDir();
|
||||
private @Nullable Path moduleCacheDir = IoUtils.getSystemModuleCacheDir();
|
||||
|
||||
private @Nullable String outputFormat;
|
||||
|
||||
|
||||
@@ -113,7 +113,7 @@ public final class ModuleCache {
|
||||
case "semver":
|
||||
return SemVerModule.getModule();
|
||||
case "settings":
|
||||
// always needed if ~/.pkl/settings.pkl is present
|
||||
// always needed if ~/.config/pkl/settings.pkl is present
|
||||
return SettingsModule.getModule();
|
||||
case "test":
|
||||
return TestModule.getModule();
|
||||
|
||||
@@ -26,6 +26,7 @@ import org.pkl.core.module.ModuleKeyFactories;
|
||||
import org.pkl.core.resource.ResourceReaders;
|
||||
import org.pkl.core.runtime.VmEvalException;
|
||||
import org.pkl.core.runtime.VmExceptionBuilder;
|
||||
import org.pkl.core.util.DebugLogger;
|
||||
import org.pkl.core.util.IoUtils;
|
||||
|
||||
/**
|
||||
@@ -33,8 +34,10 @@ import org.pkl.core.util.IoUtils;
|
||||
* {@literal pkl.settings} standard library module. To load a settings file, use one of the static
|
||||
* {@code load} methods.
|
||||
*/
|
||||
// keep in sync with stdlib/settings.pkl
|
||||
public record PklSettings(Editor editor, PklEvaluatorSettings.@Nullable Http http) {
|
||||
// keep in sync with stdlib/settings.pkl
|
||||
public static final PklSettings defaultInstance = new PklSettings(Editor.SYSTEM, null);
|
||||
|
||||
private static final List<Pattern> ALLOWED_MODULES =
|
||||
List.of(Pattern.compile("pkl:"), Pattern.compile("file:"));
|
||||
|
||||
@@ -42,16 +45,43 @@ public record PklSettings(Editor editor, PklEvaluatorSettings.@Nullable Http htt
|
||||
List.of(Pattern.compile("env:"), Pattern.compile("file:"));
|
||||
|
||||
/**
|
||||
* Loads the user settings file ({@literal ~/.pkl/settings.pkl}). If this file does not exist,
|
||||
* returns default settings defined by module {@literal pkl.settings}.
|
||||
* Loads the user settings file.
|
||||
*
|
||||
* <p>Prefers XDG_CONFIG_HOME (e.g. {@code ~/.config/pkl/settings.pkl}), falling back to the
|
||||
* legacy {@code ~/.pkl/settings.pkl}.
|
||||
*
|
||||
* <p>If neither file exists, returns default settings defined by module {@code pkl.settings}.
|
||||
*/
|
||||
public static PklSettings loadFromSystem() throws VmEvalException {
|
||||
var file = IoUtils.getSystemSettingsFile();
|
||||
if (Files.exists(file)) {
|
||||
DebugLogger.log("Loading settings file from " + file.normalize().toAbsolutePath());
|
||||
return load(ModuleSource.path(file));
|
||||
}
|
||||
return defaultInstance;
|
||||
}
|
||||
|
||||
/**
|
||||
* Loads the user settings file.
|
||||
*
|
||||
* @deprecated As of 0.33.0, use {@link #loadFromSystem()}, which now prefers {@code
|
||||
* ~/.config/pkl/settings.pkl} over the legacy {@code ~/.pkl/settings.pkl}.
|
||||
*/
|
||||
@Deprecated(since = "0.33.0", forRemoval = true)
|
||||
public static PklSettings loadFromPklHomeDir() throws VmEvalException {
|
||||
return loadFromPklHomeDir(IoUtils.getPklHomeDir());
|
||||
var candidate = IoUtils.getLegacyPklHomeDir().resolve("settings.pkl");
|
||||
if (Files.exists(candidate)) {
|
||||
return load(ModuleSource.path(candidate));
|
||||
}
|
||||
return defaultInstance;
|
||||
}
|
||||
|
||||
/** For testing only. */
|
||||
static PklSettings loadFromPklHomeDir(Path pklHomeDir) throws VmEvalException {
|
||||
var path = pklHomeDir.resolve("settings.pkl");
|
||||
static PklSettings loadFromSettingsDir(Path settingsDir) throws VmEvalException {
|
||||
return loadFromSettingsFile(settingsDir.resolve("settings.pkl"));
|
||||
}
|
||||
|
||||
private static PklSettings loadFromSettingsFile(Path path) throws VmEvalException {
|
||||
return Files.exists(path)
|
||||
? load(ModuleSource.path(path))
|
||||
: new PklSettings(Editor.SYSTEM, null);
|
||||
|
||||
@@ -0,0 +1,33 @@
|
||||
/*
|
||||
* Copyright © 2026 Apple Inc. and the Pkl project authors. All rights reserved.
|
||||
*
|
||||
* Licensed under the Apache License, Version 2.0 (the "License");
|
||||
* you may not use this file except in compliance with the License.
|
||||
* You may obtain a copy of the License at
|
||||
*
|
||||
* https://www.apache.org/licenses/LICENSE-2.0
|
||||
*
|
||||
* Unless required by applicable law or agreed to in writing, software
|
||||
* distributed under the License is distributed on an "AS IS" BASIS,
|
||||
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
||||
* See the License for the specific language governing permissions and
|
||||
* limitations under the License.
|
||||
*/
|
||||
package org.pkl.core.util;
|
||||
|
||||
final class BaseDirectories {
|
||||
public static final BaseDirectory config =
|
||||
new BaseDirectory(
|
||||
"XDG_CONFIG_HOME",
|
||||
"XDG_CONFIG_DIRS",
|
||||
"APPDATA",
|
||||
null,
|
||||
".config",
|
||||
new String[] {"/etc/xdg"});
|
||||
|
||||
public static final BaseDirectory cache =
|
||||
new BaseDirectory("XDG_CACHE_HOME", null, "LOCALAPPDATA", "Cache", ".cache", null);
|
||||
|
||||
public static final BaseDirectory state =
|
||||
new BaseDirectory("XDG_STATE_HOME", null, "LOCALAPPDATA", null, ".local/state", null);
|
||||
}
|
||||
@@ -0,0 +1,205 @@
|
||||
/*
|
||||
* Copyright © 2026 Apple Inc. and the Pkl project authors. All rights reserved.
|
||||
*
|
||||
* Licensed under the Apache License, Version 2.0 (the "License");
|
||||
* you may not use this file except in compliance with the License.
|
||||
* You may obtain a copy of the License at
|
||||
*
|
||||
* https://www.apache.org/licenses/LICENSE-2.0
|
||||
*
|
||||
* Unless required by applicable law or agreed to in writing, software
|
||||
* distributed under the License is distributed on an "AS IS" BASIS,
|
||||
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
||||
* See the License for the specific language governing permissions and
|
||||
* limitations under the License.
|
||||
*/
|
||||
package org.pkl.core.util;
|
||||
|
||||
import java.io.File;
|
||||
import java.nio.file.InvalidPathException;
|
||||
import java.nio.file.Path;
|
||||
import java.util.Arrays;
|
||||
import java.util.Map;
|
||||
import java.util.Objects;
|
||||
import java.util.function.Predicate;
|
||||
import org.jspecify.annotations.Nullable;
|
||||
|
||||
/**
|
||||
* Utility library for accessing files in base directories.
|
||||
*
|
||||
* <p>On macOS and Linux, follows the XDG base directory specification.
|
||||
*
|
||||
* <p>On Windows, follows {@code $APPDATA} and {@code $LOCALAPPDATA} conventions, but can be
|
||||
* overridden by {@code XDG} style env vars.
|
||||
*/
|
||||
class BaseDirectory {
|
||||
private final String xdgHomeEnvVar;
|
||||
private final @Nullable String xdgDirsEnvVar;
|
||||
private final String windowsEnvVar;
|
||||
private final @Nullable String windowsSubpath;
|
||||
private final String homeDefault;
|
||||
private final String @Nullable [] dirsDefault;
|
||||
|
||||
BaseDirectory(
|
||||
String xdgHomeEnvVar,
|
||||
@Nullable String xdgDirsEnvVar,
|
||||
String windowsEnvVar,
|
||||
@Nullable String windowsSubpath,
|
||||
String homeDefault,
|
||||
String @Nullable [] dirsDefault) {
|
||||
this.xdgHomeEnvVar = xdgHomeEnvVar;
|
||||
this.xdgDirsEnvVar = xdgDirsEnvVar;
|
||||
this.windowsEnvVar = windowsEnvVar;
|
||||
this.windowsSubpath = windowsSubpath;
|
||||
this.homeDefault = homeDefault;
|
||||
this.dirsDefault = dirsDefault;
|
||||
}
|
||||
|
||||
@Nullable Path getHome() {
|
||||
return getHome(System.getenv(), IoUtils.isWindows());
|
||||
}
|
||||
|
||||
/** Returns the first file within the search hierarchy that exists. */
|
||||
@Nullable Path firstMatchingPath(String subpath, Predicate<Path> predicate) {
|
||||
return firstMatchingPath(subpath, System.getenv(), IoUtils.isWindows(), predicate);
|
||||
}
|
||||
|
||||
/** Returns the subpath within the {@code home} of this base directory type. */
|
||||
@Nullable Path resolveHome(String subpath) {
|
||||
var homeDir = getHome(System.getenv(), IoUtils.isWindows());
|
||||
if (homeDir != null) {
|
||||
return homeDir.resolve(subpath);
|
||||
}
|
||||
return null;
|
||||
}
|
||||
|
||||
// for testing only
|
||||
@Nullable Path firstMatchingPath(
|
||||
String subpath, Map<String, String> envVars, boolean isWindows, Predicate<Path> predicate) {
|
||||
var home = getHome(envVars, isWindows);
|
||||
Path candidate;
|
||||
if (home != null) {
|
||||
candidate = home.resolve(subpath);
|
||||
if (predicate.test(candidate)) {
|
||||
return candidate;
|
||||
}
|
||||
}
|
||||
var dirs = getDirs(envVars);
|
||||
if (dirs != null) {
|
||||
for (var dir : dirs) {
|
||||
candidate = dir.resolve(subpath);
|
||||
if (predicate.test(candidate)) {
|
||||
return candidate;
|
||||
}
|
||||
}
|
||||
}
|
||||
return null;
|
||||
}
|
||||
|
||||
// possibly null if $HOME is not set.
|
||||
private static final @Nullable Path homeDir;
|
||||
|
||||
static {
|
||||
var userHome = System.getProperty("user.home");
|
||||
if (userHome != null) {
|
||||
homeDir = Path.of(userHome);
|
||||
} else {
|
||||
homeDir = null;
|
||||
}
|
||||
}
|
||||
|
||||
private static Path @Nullable [] getConfiguredPaths(String envVar, Map<String, String> envVars) {
|
||||
try {
|
||||
var value = envVars.get(envVar);
|
||||
if (value == null || value.isEmpty()) {
|
||||
return null;
|
||||
}
|
||||
var strs = value.split(File.pathSeparator);
|
||||
var ret = new Path[strs.length];
|
||||
for (var i = 0; i < strs.length; i++) {
|
||||
var dir = strs[i];
|
||||
if (dir.isEmpty()) {
|
||||
continue;
|
||||
}
|
||||
ret[i] = Path.of(dir).resolve("pkl");
|
||||
}
|
||||
return Arrays.stream(ret).filter(Objects::nonNull).toArray(Path[]::new);
|
||||
} catch (InvalidPathException e) {
|
||||
// can't use org.pkl.core.Logger here; logger isn't yet available
|
||||
// (can't call `VmContext.get()`).
|
||||
// do the next best thing and just write to stderr.
|
||||
System.err.println(
|
||||
"[org.pkl.core.util.BaseDirectory] '"
|
||||
+ envVar
|
||||
+ "' env var contains an invalid path: "
|
||||
+ e.getMessage());
|
||||
return null;
|
||||
}
|
||||
}
|
||||
|
||||
private static @Nullable Path getConfiguredPath(String envVar, Map<String, String> envVars) {
|
||||
try {
|
||||
var value = envVars.get(envVar);
|
||||
if (value == null || value.isEmpty()) {
|
||||
return null;
|
||||
}
|
||||
return Path.of(value);
|
||||
} catch (InvalidPathException e) {
|
||||
// can't use org.pkl.core.Logger here; logger isn't yet available
|
||||
// (can't call `VmContext.get()`).
|
||||
// do the next best thing and just write to stderr.
|
||||
System.err.println(
|
||||
"[org.pkl.core.util.BaseDirectory] '"
|
||||
+ envVar
|
||||
+ "' env var is an invalid path: "
|
||||
+ e.getMessage());
|
||||
return null;
|
||||
}
|
||||
}
|
||||
|
||||
@Nullable Path getHome(Map<String, String> envVars, boolean isWindows) {
|
||||
var configuredHome = getConfiguredPath(xdgHomeEnvVar, envVars);
|
||||
if (configuredHome != null) {
|
||||
return configuredHome.resolve("pkl");
|
||||
}
|
||||
if (isWindows) {
|
||||
configuredHome = getConfiguredPath(windowsEnvVar, envVars);
|
||||
if (configuredHome != null) {
|
||||
configuredHome = configuredHome.resolve("pkl");
|
||||
if (windowsSubpath != null) {
|
||||
configuredHome = configuredHome.resolve(windowsSubpath);
|
||||
}
|
||||
return configuredHome;
|
||||
}
|
||||
}
|
||||
if (homeDir != null) {
|
||||
return homeDir.resolve(homeDefault).resolve("pkl");
|
||||
}
|
||||
return null;
|
||||
}
|
||||
|
||||
Path @Nullable [] getDirs(Map<String, String> envVars) {
|
||||
if (xdgDirsEnvVar != null) {
|
||||
var paths = getConfiguredPaths(xdgDirsEnvVar, envVars);
|
||||
if (paths != null) {
|
||||
return paths;
|
||||
}
|
||||
}
|
||||
if (dirsDefault == null) {
|
||||
return null;
|
||||
}
|
||||
var ret = new Path[dirsDefault.length];
|
||||
for (var i = 0; i < dirsDefault.length; i++) {
|
||||
var dir = dirsDefault[i];
|
||||
if (!dir.startsWith("/")) {
|
||||
if (homeDir == null) {
|
||||
return null;
|
||||
}
|
||||
ret[i] = homeDir.resolve(dir).resolve("pkl");
|
||||
} else {
|
||||
ret[i] = Path.of(dir).resolve("pkl");
|
||||
}
|
||||
}
|
||||
return ret;
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,35 @@
|
||||
/*
|
||||
* Copyright © 2026 Apple Inc. and the Pkl project authors. All rights reserved.
|
||||
*
|
||||
* Licensed under the Apache License, Version 2.0 (the "License");
|
||||
* you may not use this file except in compliance with the License.
|
||||
* You may obtain a copy of the License at
|
||||
*
|
||||
* https://www.apache.org/licenses/LICENSE-2.0
|
||||
*
|
||||
* Unless required by applicable law or agreed to in writing, software
|
||||
* distributed under the License is distributed on an "AS IS" BASIS,
|
||||
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
||||
* See the License for the specific language governing permissions and
|
||||
* limitations under the License.
|
||||
*/
|
||||
package org.pkl.core.util;
|
||||
|
||||
import java.util.Objects;
|
||||
|
||||
public final class DebugLogger {
|
||||
private DebugLogger() {}
|
||||
|
||||
private static final boolean isEnabled;
|
||||
|
||||
static {
|
||||
isEnabled = Objects.equals(System.getenv("PKL_DEBUG"), "1");
|
||||
}
|
||||
|
||||
public static void log(String message) {
|
||||
if (!isEnabled) {
|
||||
return;
|
||||
}
|
||||
System.err.println("[pkl] " + message);
|
||||
}
|
||||
}
|
||||
@@ -49,7 +49,6 @@ import org.pkl.core.runtime.VmExceptionBuilder;
|
||||
import org.pkl.core.util.GlobResolver.InvalidGlobPatternException;
|
||||
|
||||
public final class IoUtils {
|
||||
|
||||
// Don't match paths like `C:\`, which are drive letters on Windows.
|
||||
private static final Pattern uriLike = Pattern.compile("[\\w+.-]+:[^\\\\].*");
|
||||
|
||||
@@ -228,13 +227,33 @@ public final class IoUtils {
|
||||
}
|
||||
|
||||
// not stored to avoid build-time initialization by native-image
|
||||
public static Path getPklHomeDir() {
|
||||
public static Path getLegacyPklHomeDir() {
|
||||
return Path.of(System.getProperty("user.home"), ".pkl");
|
||||
}
|
||||
|
||||
public static @Nullable Path getSystemModuleCacheDir() {
|
||||
return BaseDirectories.cache.getHome();
|
||||
}
|
||||
|
||||
public static Path getSystemSettingsFile() {
|
||||
var path = BaseDirectories.config.firstMatchingPath("settings.pkl", Files::exists);
|
||||
if (path == null) {
|
||||
return getLegacyPklHomeDir().resolve("settings.pkl");
|
||||
}
|
||||
return path;
|
||||
}
|
||||
|
||||
public static Path getSystemCaCertsDir() {
|
||||
var path = BaseDirectories.config.firstMatchingPath("cacerts", Files::isDirectory);
|
||||
if (path == null) {
|
||||
return getLegacyPklHomeDir().resolve("cacerts");
|
||||
}
|
||||
return path;
|
||||
}
|
||||
|
||||
// not stored to avoid build-time initialization by native-image
|
||||
public static Path getDefaultModuleCacheDir() {
|
||||
return getPklHomeDir().resolve("cache");
|
||||
public static @Nullable Path getReplHistoryFile() {
|
||||
return BaseDirectories.state.resolveHome("repl-history");
|
||||
}
|
||||
|
||||
// not stored to avoid build-time initialization by native-image
|
||||
|
||||
Reference in new issue
Block a user