-
Notifications
You must be signed in to change notification settings - Fork 0
feat: add required entrypoint reachability rule #629
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
Merged
Merged
Changes from 7 commits
Commits
Show all changes
12 commits
Select commit
Hold shift + click to select a range
8266c14
feat: add required entrypoint reachability rule
jonathanong b4fb2d4
fix: satisfy repository rule layout guards
jonathanong 32c7155
fix: preserve aggregate rule complexity threshold
jonathanong d4911c1
fix: isolate aggregate graph rule execution
jonathanong 8190d6a
test: cover reachability configuration edges
jonathanong fd34128
test: cover graph rule error propagation
jonathanong 0e01c55
test: reject out-of-root reachability entrypoints
jonathanong 16c6c74
fix: preserve graph edge semantics
jonathanong 4226e93
test: satisfy AST module guards
jonathanong 542b965
fix: preserve standalone graph scope
jonathanong 24acef8
test: cover workspace edge-kind branches
jonathanong e718a3a
test: import vitest fixture API
jonathanong 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
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -1,5 +1,5 @@ | ||
| pub(crate) mod enabled; | ||
| mod forbidden_plan; | ||
| mod graph_plan; | ||
| pub(crate) mod prepared; | ||
| mod results; | ||
| mod run_all; | ||
|
|
||
File renamed without changes.
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
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
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
201 changes: 201 additions & 0 deletions
201
crates/no-mistakes/src/codebase/rules/required_entrypoint_reachability.rs
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,201 @@ | ||
| use super::RuleFinding; | ||
| use crate::codebase::dependencies::graph::{DepGraph, EdgeKind, GraphBuildPlan, NodeId}; | ||
| use crate::codebase::ts_source::relative_slash_path; | ||
| use crate::config::v2::NoMistakesConfig; | ||
| use anyhow::Result; | ||
| use serde::Deserialize; | ||
| use std::collections::HashSet; | ||
| use std::path::{Path, PathBuf}; | ||
|
|
||
| pub const RULE_ID: &str = "required-entrypoint-reachability"; | ||
|
|
||
| #[derive(Debug, Deserialize, Default)] | ||
| #[serde(default, rename_all = "camelCase")] | ||
| pub(crate) struct Options { | ||
| pub(crate) source_globs: Vec<String>, | ||
| pub(crate) entrypoints: Vec<String>, | ||
| pub(crate) max_depth: Option<usize>, | ||
| } | ||
|
|
||
| pub(crate) fn graph_plan(config: &NoMistakesConfig) -> Option<GraphBuildPlan> { | ||
| config | ||
| .rule_configured(RULE_ID) | ||
| .then(GraphBuildPlan::imports_and_workspace) | ||
|
jonathanong marked this conversation as resolved.
Outdated
|
||
| } | ||
|
|
||
| pub(crate) fn check_with_graph_and_inferred( | ||
| root: &Path, | ||
| config: &NoMistakesConfig, | ||
| files: &[PathBuf], | ||
| graph: &DepGraph, | ||
| inferred_roots: Option<&crate::codebase::config::InferredRoots>, | ||
| ) -> Result<Vec<RuleFinding>> { | ||
| let file_universe = files | ||
| .iter() | ||
| .map(|path| crate::codebase::ts_resolver::normalize_path(path)) | ||
| .collect::<HashSet<_>>(); | ||
| let mut findings = Vec::new(); | ||
| for rule in config.rule_applications(RULE_ID) { | ||
| let options: Options = rule.rule_options(); | ||
| let mut inferred_roots = inferred_roots.cloned().unwrap_or_default(); | ||
| let source_filter = super::path_filter::RulePathFilter::new_with_inferred( | ||
| root, | ||
| config, | ||
| rule, | ||
| &mut inferred_roots, | ||
| )?; | ||
| let scoped_files = files | ||
| .iter() | ||
| .filter(|path| source_filter.is_match(path)) | ||
| .cloned() | ||
| .collect::<Vec<_>>(); | ||
| let target_roots = | ||
| super::target_roots_with_inferred(root, config, rule, &mut inferred_roots); | ||
| findings.extend(check_rule_application( | ||
| root, | ||
| &options, | ||
| &scoped_files, | ||
| &target_roots, | ||
| graph, | ||
| &file_universe, | ||
| )); | ||
| } | ||
| super::sort_findings(&mut findings); | ||
| Ok(findings) | ||
| } | ||
|
|
||
| fn check_rule_application( | ||
| root: &Path, | ||
| options: &Options, | ||
| scoped_files: &[PathBuf], | ||
| target_roots: &[PathBuf], | ||
| graph: &DepGraph, | ||
| file_universe: &HashSet<PathBuf>, | ||
| ) -> Vec<RuleFinding> { | ||
| let mut findings = Vec::new(); | ||
| if options.source_globs.is_empty() { | ||
| findings.push(config_finding( | ||
| "each rule entry requires at least one sourceGlobs pattern", | ||
| None, | ||
| )); | ||
| } | ||
| if options.entrypoints.is_empty() { | ||
| findings.push(config_finding( | ||
| "each rule entry requires at least one entrypoint", | ||
| None, | ||
| )); | ||
| } | ||
|
|
||
| let mut source_files = HashSet::new(); | ||
| for pattern in &options.source_globs { | ||
| match super::matching_files( | ||
| root, | ||
| std::slice::from_ref(pattern), | ||
| scoped_files, | ||
| target_roots, | ||
| ) { | ||
| Ok(matches) if matches.is_empty() => findings.push(config_finding( | ||
| &format!("sourceGlobs pattern `{pattern}` matched no files"), | ||
| Some(pattern.clone()), | ||
| )), | ||
| Ok(matches) => source_files.extend(matches), | ||
| Err(error) => findings.push(config_finding( | ||
| &format!("invalid sourceGlobs pattern `{pattern}`: {error}"), | ||
| Some(pattern.clone()), | ||
| )), | ||
| } | ||
| } | ||
|
|
||
| let mut entrypoint_paths = Vec::new(); | ||
| let mut entrypoint_labels = Vec::new(); | ||
| for configured in &options.entrypoints { | ||
| let path = resolve_entrypoint(root, configured); | ||
| if path | ||
| .as_ref() | ||
| .is_none_or(|path| !file_universe.contains(path) || !graph.contains_file(path)) | ||
| { | ||
| findings.push(config_finding( | ||
| &format!("entrypoint `{configured}` does not exist"), | ||
| Some(configured.clone()), | ||
| )); | ||
| continue; | ||
| } | ||
| let path = path.expect("validated entrypoint path"); | ||
| entrypoint_labels.push(relative_slash_path(root, &path)); | ||
| entrypoint_paths.push(path); | ||
| } | ||
| entrypoint_labels.sort(); | ||
| entrypoint_labels.dedup(); | ||
| entrypoint_paths.sort(); | ||
| entrypoint_paths.dedup(); | ||
|
|
||
| if !entrypoint_paths.is_empty() { | ||
| let allowed = runtime_edge_kinds(); | ||
| let roots = entrypoint_paths | ||
| .iter() | ||
| .cloned() | ||
| .map(NodeId::File) | ||
| .collect::<Vec<_>>(); | ||
| let mut reachable = graph | ||
| .deps_of_in_file_universe(&roots, options.max_depth, Some(&allowed), file_universe) | ||
| .into_iter() | ||
| .filter_map(|entry| entry.node.as_file().map(Path::to_path_buf)) | ||
| .collect::<HashSet<_>>(); | ||
| reachable.extend(entrypoint_paths); | ||
| let target = entrypoint_labels.join(","); | ||
| for source in source_files { | ||
| let source = crate::codebase::ts_resolver::normalize_path(&source); | ||
| if reachable.contains(&source) { | ||
| continue; | ||
| } | ||
| let file = relative_slash_path(root, &source); | ||
| findings.push(RuleFinding { | ||
| rule: RULE_ID.to_string(), | ||
| file: file.clone(), | ||
| line: 1, | ||
| message: format!( | ||
| "{file} is not runtime-reachable from configured entrypoints: {target}" | ||
| ), | ||
| import: None, | ||
| target: Some(target.clone()), | ||
| }); | ||
| } | ||
| } | ||
| findings | ||
| } | ||
|
|
||
| fn resolve_entrypoint(root: &Path, configured: &str) -> Option<PathBuf> { | ||
| let configured = Path::new(configured.trim_start_matches("./")); | ||
| let path = if configured.is_absolute() { | ||
| configured.to_path_buf() | ||
| } else { | ||
| root.join(configured) | ||
| }; | ||
| let path = crate::codebase::ts_resolver::normalize_path(&path); | ||
| path.starts_with(root).then_some(path) | ||
| } | ||
|
|
||
| fn runtime_edge_kinds() -> HashSet<EdgeKind> { | ||
| [ | ||
| EdgeKind::Import, | ||
| EdgeKind::DynamicImport, | ||
| EdgeKind::Require, | ||
|
jonathanong marked this conversation as resolved.
|
||
| EdgeKind::WorkspaceImport, | ||
|
jonathanong marked this conversation as resolved.
|
||
| ] | ||
| .into_iter() | ||
| .collect() | ||
| } | ||
|
|
||
| fn config_finding(message: &str, target: Option<String>) -> RuleFinding { | ||
| RuleFinding { | ||
| rule: RULE_ID.to_string(), | ||
| file: ".no-mistakes.yml".to_string(), | ||
| line: 1, | ||
| message: format!("{RULE_ID}: {message}"), | ||
| import: None, | ||
| target, | ||
| } | ||
| } | ||
|
|
||
| #[cfg(test)] | ||
| mod tests; | ||
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.