Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Support relative path imports from config file #7690

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
1 change: 1 addition & 0 deletions CHANGELOG.md
Expand Up @@ -20,6 +20,7 @@ The format is based on [Keep a Changelog](https://keepachangelog.com/en/1.0.0/).
- No unused-key warnings will be emitted for OS-specific config keys
- Use built-in font for sextant symbols from `U+1FB00` to `U+1FB3B`
- Kitty encoding is not used anymore for uncommon keys unless the protocol enabled
- Support relative path imports from config files

## 0.13.1

Expand Down
18 changes: 15 additions & 3 deletions alacritty/src/config/mod.rs
Expand Up @@ -210,7 +210,7 @@ fn parse_config(
let config = deserialize_config(path, false)?;

// Merge config with imports.
let imports = load_imports(&config, config_paths, recursion_limit);
let imports = load_imports(&config, path, config_paths, recursion_limit);
Ok(serde_utils::merge(imports, config))
}

Expand Down Expand Up @@ -242,9 +242,14 @@ pub fn deserialize_config(path: &Path, warn_pruned: bool) -> Result<Value> {
}

/// Load all referenced configuration files.
fn load_imports(config: &Value, config_paths: &mut Vec<PathBuf>, recursion_limit: usize) -> Value {
fn load_imports(
config: &Value,
base_path: &Path,
config_paths: &mut Vec<PathBuf>,
recursion_limit: usize,
) -> Value {
// Get paths for all imports.
let import_paths = match imports(config, recursion_limit) {
let import_paths = match imports(config, base_path, recursion_limit) {
Ok(import_paths) => import_paths,
Err(err) => {
error!(target: LOG_TARGET_CONFIG, "{err}");
Expand Down Expand Up @@ -284,6 +289,7 @@ fn load_imports(config: &Value, config_paths: &mut Vec<PathBuf>, recursion_limit
/// Get all import paths for a configuration.
pub fn imports(
config: &Value,
base_path: &Path,
recursion_limit: usize,
) -> StdResult<Vec<StdResult<PathBuf, String>>, String> {
let imports = match config.get("import") {
Expand Down Expand Up @@ -313,6 +319,12 @@ pub fn imports(
path = home_dir.join(stripped);
}

if path.is_relative() {
if let Some(config_path) = base_path.parent() {
path = config_path.join(path)
Comment on lines +323 to +324
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
if let Some(config_path) = base_path.parent() {
path = config_path.join(path)
if let Some(base_path) = base_path.parent() {
path = base_path.join(path)

Better to keep consistent naming to avoid confusion.

}
}

import_paths.push(Ok(path));
}

Expand Down
5 changes: 3 additions & 2 deletions alacritty/src/migrate.rs
Expand Up @@ -81,7 +81,7 @@ fn migrate_config(

// Migrate config imports.
if !options.skip_imports {
migrate_imports(options, &mut config, recursion_limit)?;
migrate_imports(options, &mut config, path, recursion_limit)?;
}

// Migrate deprecated field names to their new location.
Expand Down Expand Up @@ -110,9 +110,10 @@ fn migrate_config(
fn migrate_imports(
options: &MigrateOptions,
config: &mut Value,
base_path: &Path,
recursion_limit: usize,
) -> Result<(), String> {
let imports = match config::imports(config, recursion_limit) {
let imports = match config::imports(config, base_path, recursion_limit) {
Ok(imports) => imports,
Err(err) => return Err(format!("import error: {err}")),
};
Expand Down