-
Notifications
You must be signed in to change notification settings - Fork 182
xtask: Convert workspacing checks to new lint structure #3172
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
Open
smalis-msft
wants to merge
1
commit into
microsoft:main
Choose a base branch
from
smalis-msft:workspaced-lint
base: main
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
+193
−253
Open
Changes from all commits
Commits
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,189 @@ | ||
| // Copyright (c) Microsoft Corporation. | ||
| // Licensed under the MIT License. | ||
|
|
||
| //! Checks that every crate's Cargo.toml is properly workspaced. | ||
|
|
||
| use super::Lint; | ||
| use super::LintCtx; | ||
| use super::Lintable; | ||
| use std::path::Path; | ||
| use std::path::PathBuf; | ||
| use toml_edit::DocumentMut; | ||
| use toml_edit::Item; | ||
| use toml_edit::TableLike; | ||
| use toml_edit::Value; | ||
|
|
||
| /// List of exceptions to using workspace package declarations. | ||
| static WORKSPACE_EXCEPTIONS: &[(&str, &[&str])] = &[ | ||
| // Allow disk_blob to use tokio for now, but no one else. | ||
| // | ||
| // disk_blob eventually will remove its tokio dependency. | ||
| ("disk_blob", &["tokio"]), | ||
| // Allow mesh_rpc to use tokio, since h2 depends on it for the tokio IO | ||
| // trait definitions. Hopefully this can be resolved upstream once async IO | ||
| // trait "vocabulary types" move to a common crate. | ||
| ("mesh_rpc", &["tokio"]), | ||
| ]; | ||
|
|
||
| pub struct WorkspacedManifest { | ||
| members: Vec<PathBuf>, | ||
| excluded: Vec<PathBuf>, | ||
| dependencies: Vec<PathBuf>, | ||
| } | ||
|
|
||
| impl Lint for WorkspacedManifest { | ||
| fn new(_ctx: &LintCtx) -> Self { | ||
| WorkspacedManifest { | ||
| members: Vec::new(), | ||
| excluded: Vec::new(), | ||
| dependencies: Vec::new(), | ||
| } | ||
| } | ||
|
|
||
| fn enter_workspace(&mut self, content: &Lintable<DocumentMut>) { | ||
| // Gather the set of crates we expect to see: all members, dependencies, and exclusions | ||
| self.members = content["workspace"] | ||
| .get("members") | ||
| .and_then(|m| m.as_array()) | ||
| .into_iter() | ||
| .flat_map(|a| a.into_iter()) | ||
| .map(|m| Path::new(m.as_str().unwrap()).join("Cargo.toml")) | ||
| .collect(); | ||
smalis-msft marked this conversation as resolved.
Show resolved
Hide resolved
|
||
| self.excluded = content["workspace"] | ||
| .get("exclude") | ||
| .and_then(|e| e.as_array()) | ||
| .into_iter() | ||
| .flat_map(|a| a.into_iter()) | ||
| .map(|e| Path::new(e.as_str().unwrap()).join("Cargo.toml")) | ||
smalis-msft marked this conversation as resolved.
Show resolved
Hide resolved
|
||
| .collect(); | ||
| self.dependencies = content["workspace"] | ||
| .get("dependencies") | ||
| .and_then(|d| d.as_table()) | ||
| .into_iter() | ||
| .flat_map(|t| t.into_iter()) | ||
| // We only need to keep local dependencies, external dependencies don't get visited | ||
| .filter_map(|(_k, v)| { | ||
| v.get("path") | ||
| .map(|p| Path::new(p.as_str().unwrap()).join("Cargo.toml")) | ||
smalis-msft marked this conversation as resolved.
Show resolved
Hide resolved
|
||
| }) | ||
| .collect(); | ||
| } | ||
|
|
||
| fn enter_crate(&mut self, content: &Lintable<DocumentMut>) { | ||
| // Remove this crate from whichever set it appears in, but ensure it only appears in one | ||
| let mut count = 0; | ||
| if let Some(member) = self.members.iter().position(|m| content.path() == m) { | ||
| self.members.remove(member); | ||
| count += 1; | ||
| } | ||
| if let Some(excluded) = self.excluded.iter().position(|e| content.path() == e) { | ||
| self.excluded.remove(excluded); | ||
| count += 1; | ||
| } | ||
| if let Some(dependency) = self.dependencies.iter().position(|d| content.path() == d) { | ||
| self.dependencies.remove(dependency); | ||
| count += 1; | ||
smalis-msft marked this conversation as resolved.
Show resolved
Hide resolved
|
||
| } | ||
|
|
||
| if count == 0 { | ||
| content.unfixable("crate is not a workspace member, dependency, or exclusion"); | ||
| } else if count > 1 { | ||
| content.unfixable("crate appears in multiple workspace sections"); | ||
| } | ||
| } | ||
|
|
||
| fn visit_file(&mut self, _content: &mut Lintable<String>) {} | ||
|
|
||
| fn exit_crate(&mut self, content: &mut Lintable<DocumentMut>) { | ||
| // Verify that all dependencies of this crate are workspaced | ||
| let mut dep_tables = Vec::new(); | ||
| for (name, v) in content.iter() { | ||
| match name { | ||
| "dependencies" | "build-dependencies" | "dev-dependencies" => { | ||
| dep_tables.push(v.as_table_like().unwrap()) | ||
| } | ||
| "target" => { | ||
| let flattened = v | ||
| .as_table_like() | ||
| .unwrap() | ||
| .iter() | ||
| .flat_map(|(_, v)| v.as_table_like().unwrap().iter()); | ||
|
|
||
| for (k, v) in flattened { | ||
| match k { | ||
| "dependencies" | "build-dependencies" | "dev-dependencies" => { | ||
| dep_tables.push(v.as_table_like().unwrap()) | ||
| } | ||
| _ => {} | ||
| } | ||
| } | ||
| } | ||
| _ => {} | ||
| } | ||
| } | ||
|
|
||
| let crate_name = content["package"]["name"].as_str().unwrap(); | ||
smalis-msft marked this conversation as resolved.
Show resolved
Hide resolved
|
||
| let handle_bad_dep = |dep_name| { | ||
| let allowed = WORKSPACE_EXCEPTIONS | ||
| .iter() | ||
| .find_map(|&(p, crates)| (p == crate_name).then_some(crates)) | ||
| .unwrap_or(&[]); | ||
|
|
||
| if allowed.contains(&dep_name) { | ||
| log::debug!( | ||
| "{} contains non-workspaced dependency {}. Allowed by exception.", | ||
| content.path().display(), | ||
| dep_name | ||
| ); | ||
| } else { | ||
| content.unfixable(&format!("non-workspaced dependency {} found", dep_name)); | ||
| } | ||
| }; | ||
| let check_table_like = |t: &dyn TableLike, dep_name| { | ||
| if t.get("workspace").and_then(|x| x.as_bool()) != Some(true) { | ||
| handle_bad_dep(dep_name); | ||
| } | ||
| }; | ||
|
|
||
| for table in dep_tables { | ||
| for (dep_name, value) in table.iter() { | ||
| match value { | ||
| Item::Value(Value::String(_)) => handle_bad_dep(dep_name), | ||
| Item::Value(Value::InlineTable(t)) => { | ||
| check_table_like(t, dep_name); | ||
|
|
||
| if t.len() == 1 { | ||
| content.unfixable(&format!( | ||
| "inline table syntax used for dependency on {} but only one table entry is present, change to the dotted form", | ||
| dep_name | ||
| )); | ||
| } | ||
| } | ||
| Item::Table(t) => check_table_like(t, dep_name), | ||
| _ => unreachable!(), | ||
| } | ||
| } | ||
| } | ||
| } | ||
|
|
||
| fn exit_workspace(&mut self, content: &mut Lintable<DocumentMut>) { | ||
| // Any members or dependencies that we expected to see but didn't are errors | ||
| for member in self.members.iter() { | ||
| content.unfixable(&format!( | ||
| "workspace member {} does not exist", | ||
| member.display() | ||
| )); | ||
| } | ||
| for dependency in self.dependencies.iter() { | ||
| // TODO: Remove this exception once xsync no longer depends on ci_logger | ||
| if dependency == Path::new("../support/ci_logger/Cargo.toml") { | ||
| continue; | ||
| } | ||
| content.unfixable(&format!( | ||
| "workspace dependency {} does not exist", | ||
| dependency.display() | ||
| )); | ||
| } | ||
smalis-msft marked this conversation as resolved.
Show resolved
Hide resolved
|
||
| // Exclusions that we didn't see may be nested workspaces, which don't get visited, so they're allowed | ||
| } | ||
| } | ||
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.