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

Deduplicate deprecation warnings for v0.2.0 release #9764

Merged
merged 3 commits into from
Feb 1, 2024
Merged
Show file tree
Hide file tree
Changes from 1 commit
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
8 changes: 2 additions & 6 deletions crates/ruff/tests/format.rs
Original file line number Diff line number Diff line change
Expand Up @@ -508,11 +508,9 @@ if __name__ == '__main__':
say_hy("dear Ruff contributor")

----- stderr -----
warning: The top-level linter settings are deprecated in favour of their counterparts in the `lint` section. Please update the following options in your configuration:
warning: The top-level linter settings are deprecated in favour of their counterparts in the `lint` section. Please update the following options in `ruff.toml`:
- 'extend-select' -> 'lint.extend-select'
- 'ignore' -> 'lint.ignore'


"###);
Ok(())
}
Expand Down Expand Up @@ -551,11 +549,9 @@ if __name__ == '__main__':
say_hy("dear Ruff contributor")

----- stderr -----
warning: The top-level linter settings are deprecated in favour of their counterparts in the `lint` section. Please update the following options in your configuration:
warning: The top-level linter settings are deprecated in favour of their counterparts in the `lint` section. Please update the following options in `ruff.toml`:
- 'extend-select' -> 'lint.extend-select'
- 'ignore' -> 'lint.ignore'


"###);
Ok(())
}
Expand Down
25 changes: 23 additions & 2 deletions crates/ruff_linter/src/logging.rs
Original file line number Diff line number Diff line change
Expand Up @@ -8,14 +8,15 @@ use fern;
use log::Level;
use once_cell::sync::Lazy;
use ruff_python_parser::{ParseError, ParseErrorType};
use rustc_hash::FxHashSet;

use ruff_source_file::{LineIndex, OneIndexed, SourceCode, SourceLocation};

use crate::fs;
use crate::source_kind::SourceKind;
use ruff_notebook::Notebook;

pub static WARNINGS: Lazy<Mutex<Vec<&'static str>>> = Lazy::new(Mutex::default);
pub static IDENTIFIERS: Lazy<Mutex<Vec<&'static str>>> = Lazy::new(Mutex::default);

/// Warn a user once, with uniqueness determined by the given ID.
#[macro_export]
Expand All @@ -24,7 +25,7 @@ macro_rules! warn_user_once_by_id {
use colored::Colorize;
use log::warn;

if let Ok(mut states) = $crate::logging::WARNINGS.lock() {
if let Ok(mut states) = $crate::logging::IDENTIFIERS.lock() {
if !states.contains(&$id) {
let message = format!("{}", format_args!($($arg)*));
warn!("{}", message.bold());
Expand All @@ -34,6 +35,26 @@ macro_rules! warn_user_once_by_id {
};
}

pub static MESSAGES: Lazy<Mutex<FxHashSet<String>>> = Lazy::new(Mutex::default);
Copy link
Member

Choose a reason for hiding this comment

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

A small nit for clarity.

Suggested change
pub static MESSAGES: Lazy<Mutex<FxHashSet<String>>> = Lazy::new(Mutex::default);
pub static PREVIOUS_MESSAGES: Lazy<Mutex<FxHashSet<String>>> = Lazy::new(Mutex::default);


/// Warn a user once, if warnings are enabled, with uniqueness determined by the content of the
/// message.
#[macro_export]
macro_rules! warn_user_once_by_message {
($($arg:tt)*) => {
use colored::Colorize;
use log::warn;

if let Ok(mut states) = $crate::logging::MESSAGES.lock() {
let message = format!("{}", format_args!($($arg)*));
if !states.contains(&message) {
warn!("{}", message.bold());
states.insert(message);
}
}
};
}

/// Warn a user once, with uniqueness determined by the calling location itself.
#[macro_export]
macro_rules! warn_user_once {
Expand Down
4 changes: 2 additions & 2 deletions crates/ruff_wasm/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -108,8 +108,8 @@ impl Workspace {
#[wasm_bindgen(constructor)]
pub fn new(options: JsValue) -> Result<Workspace, Error> {
let options: Options = serde_wasm_bindgen::from_value(options).map_err(into_error)?;
let configuration =
Configuration::from_options(options, Path::new(".")).map_err(into_error)?;
let configuration = Configuration::from_options(options, Path::new("."), Path::new("."))
.map_err(into_error)?;
let settings = configuration
.into_settings(Path::new("."))
.map_err(into_error)?;
Expand Down
26 changes: 17 additions & 9 deletions crates/ruff_workspace/src/configuration.rs
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,8 @@ use ruff_linter::settings::types::{
};
use ruff_linter::settings::{LinterSettings, DEFAULT_SELECTORS, DUMMY_VARIABLE_RGX, TASK_TAGS};
use ruff_linter::{
fs, warn_user, warn_user_once, warn_user_once_by_id, RuleSelector, RUFF_PKG_VERSION,
fs, warn_user_once, warn_user_once_by_id, warn_user_once_by_message, RuleSelector,
RUFF_PKG_VERSION,
};
use ruff_python_formatter::{
DocstringCode, DocstringCodeLineWidth, MagicTrailingComma, QuoteStyle,
Expand Down Expand Up @@ -395,8 +396,9 @@ impl Configuration {
})
}

pub fn from_options(options: Options, project_root: &Path) -> Result<Self> {
warn_about_deprecated_top_level_lint_options(&options.lint_top_level.0);
/// Convert the [`Options`] read from the given [`Path`] into a [`Configuration`].
pub fn from_options(options: Options, path: &Path, project_root: &Path) -> Result<Self> {
warn_about_deprecated_top_level_lint_options(&options.lint_top_level.0, path);
Copy link
Member

Choose a reason for hiding this comment

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

Do we know why we call this method twice for every configuration ?

Copy link
Member Author

Choose a reason for hiding this comment

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

Presumedly because we call it once at the very start of Ruff to find the "default configuration", and then once when we visited it as part of the directory of files we're analyzing. (We should fix that -- it's not "incorrect" but it is inefficient.)

Copy link
Member

Choose a reason for hiding this comment

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

...and we didn't catch this in the tests because we pass --config which bypasses the discovery


let lint = if let Some(mut lint) = options.lint {
lint.common = lint.common.combine(options.lint_top_level.0);
Expand Down Expand Up @@ -429,7 +431,7 @@ impl Configuration {
.output_format
.map(|format| match format {
SerializationFormat::Text => {
warn_user!(r#"Setting `output_format` to "text" is deprecated. Use "full" or "concise" instead. "text" will be treated as "{}"."#, SerializationFormat::default(options.preview.unwrap_or_default()));
warn_user_once!(r#"Setting `output_format` to "text" is deprecated. Use "full" or "concise" instead. "text" will be treated as "{}"."#, SerializationFormat::default(options.preview.unwrap_or_default()));
SerializationFormat::default(options.preview.unwrap_or_default())
},
other => other
Expand Down Expand Up @@ -1010,7 +1012,7 @@ impl LintConfiguration {
if preview.mode.is_disabled() {
for selection in deprecated_selectors.iter().sorted() {
let (prefix, code) = selection.prefix_and_code();
warn_user!(
warn_user_once_by_message!(
"Rule `{prefix}{code}` is deprecated and will be removed in a future release.",
);
}
Expand Down Expand Up @@ -1038,7 +1040,9 @@ impl LintConfiguration {

for selection in ignored_preview_selectors.iter().sorted() {
let (prefix, code) = selection.prefix_and_code();
warn_user!("Selection `{prefix}{code}` has no effect because preview is not enabled.",);
warn_user_once_by_message!(
"Selection `{prefix}{code}` has no effect because preview is not enabled.",
);
}

let mut rules = RuleTable::empty();
Expand Down Expand Up @@ -1257,7 +1261,10 @@ pub fn resolve_src(src: &[String], project_root: &Path) -> Result<Vec<PathBuf>>
Ok(paths)
}

fn warn_about_deprecated_top_level_lint_options(top_level_options: &LintCommonOptions) {
fn warn_about_deprecated_top_level_lint_options(
top_level_options: &LintCommonOptions,
path: &Path,
) {
let mut used_options = Vec::new();

if top_level_options.allowed_confusables.is_some() {
Expand Down Expand Up @@ -1447,8 +1454,9 @@ fn warn_about_deprecated_top_level_lint_options(top_level_options: &LintCommonOp
.map(|option| format!("- '{option}' -> 'lint.{option}'"))
.join("\n ");

warn_user!(
"The top-level linter settings are deprecated in favour of their counterparts in the `lint` section. Please update the following options in your configuration:\n {options_mapping}\n\n",
Copy link
Member Author

Choose a reason for hiding this comment

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

The two trailing newlines at the end here looked odd to me in practice.

warn_user_once_by_message!(
"The top-level linter settings are deprecated in favour of their counterparts in the `lint` section. Please update the following options in `{}`:\n {options_mapping}",
fs::relativize_path(path),
);
}

Expand Down
2 changes: 1 addition & 1 deletion crates/ruff_workspace/src/resolver.rs
Original file line number Diff line number Diff line change
Expand Up @@ -264,7 +264,7 @@ fn resolve_configuration(
let options = pyproject::load_options(&path)?;

let project_root = relativity.resolve(&path);
let configuration = Configuration::from_options(options, &project_root)?;
let configuration = Configuration::from_options(options, &path, &project_root)?;

// If extending, continue to collect.
next = configuration.extend.as_ref().map(|extend| {
Expand Down