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

[red-knot] User-level configuration #16021

Merged
merged 7 commits into from
Feb 10, 2025
Merged

Conversation

MichaReiser
Copy link
Member

Summary

This PR adds support for user-level configurations (~/.config/knot/knot.toml) to Red Knot.

Red Knot will watch the user-level configuration file for changes but only if it exists
when the process start. It doesn't watch for new configurations,
mainly to simplify things for now (it would require watching the entire .config directory because the knot subfolder might not exist either).

The new ConfigurationFile struct seems a bit overkill for now but I plan to use it for
hierarchical configurations as well.

Red Knot uses the same strategy as uv and Ruff by using the etcetera crate.

Test Plan

Added CLI and file watching test

@MichaReiser MichaReiser added the red-knot Multi-file analysis & type inference label Feb 7, 2025
@@ -99,10 +99,12 @@ fn run_check(args: CheckCommand) -> anyhow::Result<ExitStatus> {
let exit_zero = args.exit_zero;

let cli_options = args.into_options();
let mut workspace_metadata = ProjectMetadata::discover(system.current_directory(), &system)?;
workspace_metadata.apply_cli_options(cli_options.clone());
let mut project_metadata = ProjectMetadata::discover(system.current_directory(), &system)?;
Copy link
Member Author

Choose a reason for hiding this comment

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

We may want nicer error messages here that use proper diagnostics but I decided to not tackle that for now and continue doing what we do for other errors at this stage.

@@ -47,7 +48,7 @@ impl ProjectDatabase {
if let Some(path) = change.system_path() {
if matches!(
path.file_name(),
Some(".gitignore" | ".ignore" | "ruff.toml" | ".ruff.toml" | "pyproject.toml")
Some(".gitignore" | ".ignore" | "knot.toml" | "pyproject.toml")
Copy link
Member Author

Choose a reason for hiding this comment

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

Whoops

Comment on lines +148 to +152
if let Err(error) = metadata.apply_configuration_files(self.system()) {
tracing::error!(
"Failed to apply configuration files, continuing without applying them: {error}"
);
}
Copy link
Member Author

Choose a reason for hiding this comment

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

We may want to use proper diagnostics here so that LSP users get notified about this error but there are other errors in this file where we're using tracing -> A problem for another day

Comment on lines +211 to +220
if entry
.path()
.extension()
.and_then(PySourceType::try_from_extension)
.is_some()
{
let mut paths = added_paths.lock().unwrap();

paths.push(entry.into_path());
paths.push(entry.into_path());
}
Copy link
Member Author

Choose a reason for hiding this comment

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

I got really confused when red knot started parsing my knot.toml file as a python file. For now, I did the simplest fix by copying this condition from Project::discover_files. I have to rework this code next month anyway to properly support file inclusion and exclusions.

@MichaReiser MichaReiser force-pushed the micha/user-level-configuration branch 2 times, most recently from 39bef15 to a1ab60d Compare February 7, 2025 14:45
#![allow(dead_code)]

/// Sets an environment variable for the duration this struct is in scope.
pub(crate) struct EnvVarGuard {
Copy link
Member Author

Choose a reason for hiding this comment

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

This is actually not sufficient because cargo runs tests concurrently. So we also need some form of a lock to avoid that multiple tests hold on to the same env variable. Ugh, this is getting out of hand.

@MichaReiser MichaReiser force-pushed the micha/user-level-configuration branch 2 times, most recently from b2aeb58 to dd75dc2 Compare February 8, 2025 16:17
Copy link
Contributor

github-actions bot commented Feb 8, 2025

ruff-ecosystem results

Linter (stable)

✅ ecosystem check detected no linter changes.

Linter (preview)

✅ ecosystem check detected no linter changes.

@MichaReiser

This comment was marked as resolved.

Copy link
Contributor

@sharkdp sharkdp left a comment

Choose a reason for hiding this comment

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

Thank you!

@MichaReiser MichaReiser force-pushed the micha/system-user-configurationd-directory branch from 6b78710 to 902cdb4 Compare February 10, 2025 14:41
Base automatically changed from micha/system-user-configurationd-directory to main February 10, 2025 14:50
@MichaReiser MichaReiser force-pushed the micha/user-level-configuration branch from 4355d5d to 521cc67 Compare February 10, 2025 14:53
@MichaReiser MichaReiser force-pushed the micha/user-level-configuration branch from 118ae5b to 1dbf7de Compare February 10, 2025 15:36
@MichaReiser MichaReiser merged commit af83256 into main Feb 10, 2025
21 checks passed
@MichaReiser MichaReiser deleted the micha/user-level-configuration branch February 10, 2025 15:44
dcreager added a commit that referenced this pull request Feb 11, 2025
* main:
  add diagnostic `Span` (couples `File` and `TextRange`) (#16101)
  Remove `Hash` and `Eq` from `AstNodeRef` for types not implementing `Eq` or `Hash` (#16100)
  Fix release build warning about unused todo type message (#16102)
  [`pydocstyle`] Handle arguments with the same names as sections (`D417`) (#16011)
  [red-knot] Reduce usage of `From<Type>` implementations when working with `Symbol`s (#16076)
  Transition to salsa coarse-grained tracked structs (#15763)
  [`pyupgrade`] Handle micro version numbers correctly (`UP036`) (#16091)
  [red-knot] `T | object == object` (#16088)
  [`ruff`] Skip singleton starred expressions for `incorrectly-parenthesized-tuple-in-subscript` (`RUF031`) (#16083)
  Delete left-over `verbosity.rs (#16081)
  [red-knot] User-level configuration (#16021)
  Add `user_configuration_directory` to `System` (#16020)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
red-knot Multi-file analysis & type inference
Projects
None yet
Development

Successfully merging this pull request may close these issues.

2 participants