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

Use CWD to resolve settings from ruff.configuration #14352

Merged
merged 1 commit into from
Nov 15, 2024

Conversation

dhruvmanila
Copy link
Member

@dhruvmanila dhruvmanila commented Nov 15, 2024

Summary

This PR fixes a bug in the Ruff language server where the editor-specified configuration was resolved relative to the configuration directory and not the current working directory.

The existing behavior is confusing given that this config file is specified by the user and is not discovered by Ruff itself. The behavior of resolving this configuration file should be similar to that of the --config flag on the command-line which uses the current working directory:

// Second priority: the user specified a `pyproject.toml` file. Use that
// `pyproject.toml` for _all_ configuration, and resolve paths relative to the
// current working directory. (This matches ESLint's behavior.)
if let Some(pyproject) = config_arguments.config_file() {
let settings = resolve_root_settings(pyproject, Relativity::Cwd, config_arguments)?;
debug!(
"Using user-specified configuration file at: {}",
pyproject.display()
);
return Ok(PyprojectConfig::new(
PyprojectDiscoveryStrategy::Fixed,
settings,
Some(pyproject.to_path_buf()),
));
}

This creates problems where certain configuration options doesn't work because the paths resolved in that case are relative to the configuration directory and not the current working directory in which the editor is expected to be in. For example, the lint.per-file-ignores doesn't work as mentioned in the linked issue along with exclude, extend-exclude, etc.

fixes: #14282

Test Plan

Using the following directory tree structure:

.
├── .config
│   └── ruff.toml
└── src
    └── migrations
        └── versions
            └── a.py

where, the ruff.toml is:

# 1. Comment this out to test `per-file-ignores`
extend-exclude = ["**/versions/*.py"]

[lint]
select = ["D"]

# 2. Comment this out to test `extend-exclude`
[lint.per-file-ignores]
"**/versions/*.py" = ["D"]

# 3. Comment both `per-file-ignores` and `extend-exclude` to test selection works

And, the content of a.py:

"""Test"""

And, the VS Code settings:

{
  "ruff.nativeServer": "on",
  "ruff.path": ["/Users/dhruv/work/astral/ruff/target/debug/ruff"],
  // For single-file mode where current working directory is `/`
  // "ruff.configuration": "/tmp/ruff-repro/.config/ruff.toml",
  // When a workspace is opened containing this path
  "ruff.configuration": "./.config/ruff.toml",
  "ruff.trace.server": "messages",
  "ruff.logLevel": "trace"
}

I also tested out just opening the file in single-file mode where the current working directory is / in VS Code. Here, the ruff.configuration needs to be updated to use absolute path as shown in the above VS Code settings.

@dhruvmanila dhruvmanila added bug Something isn't working server Related to the LSP server labels Nov 15, 2024
Copy link
Contributor

ruff-ecosystem results

Linter (stable)

✅ ecosystem check detected no linter changes.

Linter (preview)

✅ ecosystem check detected no linter changes.

@dhruvmanila dhruvmanila merged commit 1f82731 into main Nov 15, 2024
20 checks passed
@dhruvmanila dhruvmanila deleted the dhruv/server-user-config branch November 15, 2024 08:15
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
bug Something isn't working server Related to the LSP server
Projects
None yet
Development

Successfully merging this pull request may close these issues.

lint.per-file-ignores option is ignored by native server in ruff.toml
2 participants