]> git.proxmox.com Git - rustc.git/blob - src/tools/rust-analyzer/crates/rust-analyzer/src/reload.rs
fcfe4be0b8cec9aa5578286e8e34e483e6341bb8
[rustc.git] / src / tools / rust-analyzer / crates / rust-analyzer / src / reload.rs
1 //! Project loading & configuration updates.
2 //!
3 //! This is quite tricky. The main problem is time and changes -- there's no
4 //! fixed "project" rust-analyzer is working with, "current project" is itself
5 //! mutable state. For example, when the user edits `Cargo.toml` by adding a new
6 //! dependency, project model changes. What's more, switching project model is
7 //! not instantaneous -- it takes time to run `cargo metadata` and (for proc
8 //! macros) `cargo check`.
9 //!
10 //! The main guiding principle here is, as elsewhere in rust-analyzer,
11 //! robustness. We try not to assume that the project model exists or is
12 //! correct. Instead, we try to provide a best-effort service. Even if the
13 //! project is currently loading and we don't have a full project model, we
14 //! still want to respond to various requests.
15 use std::{mem, sync::Arc};
16
17 use flycheck::{FlycheckConfig, FlycheckHandle};
18 use hir::db::DefDatabase;
19 use ide::Change;
20 use ide_db::base_db::{
21 CrateGraph, Env, ProcMacro, ProcMacroExpander, ProcMacroExpansionError, ProcMacroKind,
22 ProcMacroLoadResult, SourceRoot, VfsPath,
23 };
24 use proc_macro_api::{MacroDylib, ProcMacroServer};
25 use project_model::{ProjectWorkspace, WorkspaceBuildScripts};
26 use syntax::SmolStr;
27 use vfs::{file_set::FileSetConfig, AbsPath, AbsPathBuf, ChangeKind};
28
29 use crate::{
30 config::{Config, FilesWatcher, LinkedProject},
31 global_state::GlobalState,
32 lsp_ext,
33 main_loop::Task,
34 op_queue::Cause,
35 };
36
37 #[derive(Debug)]
38 pub(crate) enum ProjectWorkspaceProgress {
39 Begin,
40 Report(String),
41 End(Vec<anyhow::Result<ProjectWorkspace>>),
42 }
43
44 #[derive(Debug)]
45 pub(crate) enum BuildDataProgress {
46 Begin,
47 Report(String),
48 End((Arc<Vec<ProjectWorkspace>>, Vec<anyhow::Result<WorkspaceBuildScripts>>)),
49 }
50
51 impl GlobalState {
52 pub(crate) fn is_quiescent(&self) -> bool {
53 !(self.fetch_workspaces_queue.op_in_progress()
54 || self.fetch_build_data_queue.op_in_progress()
55 || self.vfs_progress_config_version < self.vfs_config_version
56 || self.vfs_progress_n_done < self.vfs_progress_n_total)
57 }
58
59 pub(crate) fn update_configuration(&mut self, config: Config) {
60 let _p = profile::span("GlobalState::update_configuration");
61 let old_config = mem::replace(&mut self.config, Arc::new(config));
62 if self.config.lru_capacity() != old_config.lru_capacity() {
63 self.analysis_host.update_lru_capacity(self.config.lru_capacity());
64 }
65 if self.config.linked_projects() != old_config.linked_projects() {
66 self.fetch_workspaces_queue.request_op("linked projects changed".to_string())
67 } else if self.config.flycheck() != old_config.flycheck() {
68 self.reload_flycheck();
69 }
70
71 if self.analysis_host.raw_database().enable_proc_attr_macros()
72 != self.config.expand_proc_attr_macros()
73 {
74 self.analysis_host
75 .raw_database_mut()
76 .set_enable_proc_attr_macros(self.config.expand_proc_attr_macros());
77 }
78 }
79
80 pub(crate) fn current_status(&self) -> lsp_ext::ServerStatusParams {
81 let mut status = lsp_ext::ServerStatusParams {
82 health: lsp_ext::Health::Ok,
83 quiescent: self.is_quiescent(),
84 message: None,
85 };
86
87 if self.proc_macro_changed {
88 status.health = lsp_ext::Health::Warning;
89 status.message =
90 Some("Reload required due to source changes of a procedural macro.".into())
91 }
92 if let Err(_) = self.fetch_build_data_error() {
93 status.health = lsp_ext::Health::Warning;
94 status.message =
95 Some("Failed to run build scripts of some packages, check the logs.".to_string());
96 }
97 if !self.config.cargo_autoreload()
98 && self.is_quiescent()
99 && self.fetch_workspaces_queue.op_requested()
100 {
101 status.health = lsp_ext::Health::Warning;
102 status.message = Some("Workspace reload required".to_string())
103 }
104
105 if let Err(error) = self.fetch_workspace_error() {
106 status.health = lsp_ext::Health::Error;
107 status.message = Some(error)
108 }
109
110 if self.config.linked_projects().is_empty()
111 && self.config.detached_files().is_empty()
112 && self.config.notifications().cargo_toml_not_found
113 {
114 status.health = lsp_ext::Health::Warning;
115 status.message = Some("Workspace reload required".to_string())
116 }
117 status
118 }
119
120 pub(crate) fn fetch_workspaces(&mut self, cause: Cause) {
121 tracing::info!(%cause, "will fetch workspaces");
122
123 self.task_pool.handle.spawn_with_sender({
124 let linked_projects = self.config.linked_projects();
125 let detached_files = self.config.detached_files().to_vec();
126 let cargo_config = self.config.cargo();
127
128 move |sender| {
129 let progress = {
130 let sender = sender.clone();
131 move |msg| {
132 sender
133 .send(Task::FetchWorkspace(ProjectWorkspaceProgress::Report(msg)))
134 .unwrap()
135 }
136 };
137
138 sender.send(Task::FetchWorkspace(ProjectWorkspaceProgress::Begin)).unwrap();
139
140 let mut workspaces = linked_projects
141 .iter()
142 .map(|project| match project {
143 LinkedProject::ProjectManifest(manifest) => {
144 project_model::ProjectWorkspace::load(
145 manifest.clone(),
146 &cargo_config,
147 &progress,
148 )
149 }
150 LinkedProject::InlineJsonProject(it) => {
151 project_model::ProjectWorkspace::load_inline(
152 it.clone(),
153 cargo_config.target.as_deref(),
154 &cargo_config.extra_env,
155 )
156 }
157 })
158 .collect::<Vec<_>>();
159
160 if !detached_files.is_empty() {
161 workspaces
162 .push(project_model::ProjectWorkspace::load_detached_files(detached_files));
163 }
164
165 tracing::info!("did fetch workspaces {:?}", workspaces);
166 sender
167 .send(Task::FetchWorkspace(ProjectWorkspaceProgress::End(workspaces)))
168 .unwrap();
169 }
170 });
171 }
172
173 pub(crate) fn fetch_build_data(&mut self, cause: Cause) {
174 tracing::info!(%cause, "will fetch build data");
175 let workspaces = Arc::clone(&self.workspaces);
176 let config = self.config.cargo();
177 self.task_pool.handle.spawn_with_sender(move |sender| {
178 sender.send(Task::FetchBuildData(BuildDataProgress::Begin)).unwrap();
179
180 let progress = {
181 let sender = sender.clone();
182 move |msg| {
183 sender.send(Task::FetchBuildData(BuildDataProgress::Report(msg))).unwrap()
184 }
185 };
186 let res = ProjectWorkspace::run_all_build_scripts(&workspaces, &config, &progress);
187
188 sender.send(Task::FetchBuildData(BuildDataProgress::End((workspaces, res)))).unwrap();
189 });
190 }
191
192 pub(crate) fn switch_workspaces(&mut self, cause: Cause) {
193 let _p = profile::span("GlobalState::switch_workspaces");
194 tracing::info!(%cause, "will switch workspaces");
195
196 if let Err(error_message) = self.fetch_workspace_error() {
197 self.show_and_log_error(error_message, None);
198 if !self.workspaces.is_empty() {
199 // It only makes sense to switch to a partially broken workspace
200 // if we don't have any workspace at all yet.
201 return;
202 }
203 }
204
205 if let Err(error) = self.fetch_build_data_error() {
206 self.show_and_log_error("failed to run build scripts".to_string(), Some(error));
207 }
208
209 let Some(workspaces) = self.fetch_workspaces_queue.last_op_result() else { return; };
210 let workspaces =
211 workspaces.iter().filter_map(|res| res.as_ref().ok().cloned()).collect::<Vec<_>>();
212
213 fn eq_ignore_build_data<'a>(
214 left: &'a ProjectWorkspace,
215 right: &'a ProjectWorkspace,
216 ) -> bool {
217 let key = |p: &'a ProjectWorkspace| match p {
218 ProjectWorkspace::Cargo {
219 cargo,
220 sysroot,
221 rustc,
222 rustc_cfg,
223 cfg_overrides,
224
225 build_scripts: _,
226 toolchain: _,
227 } => Some((cargo, sysroot, rustc, rustc_cfg, cfg_overrides)),
228 _ => None,
229 };
230 match (key(left), key(right)) {
231 (Some(lk), Some(rk)) => lk == rk,
232 _ => left == right,
233 }
234 }
235
236 let same_workspaces = workspaces.len() == self.workspaces.len()
237 && workspaces
238 .iter()
239 .zip(self.workspaces.iter())
240 .all(|(l, r)| eq_ignore_build_data(l, r));
241
242 if same_workspaces {
243 let (workspaces, build_scripts) = self.fetch_build_data_queue.last_op_result();
244 if Arc::ptr_eq(workspaces, &self.workspaces) {
245 tracing::debug!("set build scripts to workspaces");
246
247 let workspaces = workspaces
248 .iter()
249 .cloned()
250 .zip(build_scripts)
251 .map(|(mut ws, bs)| {
252 ws.set_build_scripts(bs.as_ref().ok().cloned().unwrap_or_default());
253 ws
254 })
255 .collect::<Vec<_>>();
256
257 // Workspaces are the same, but we've updated build data.
258 self.workspaces = Arc::new(workspaces);
259 } else {
260 tracing::info!("build scripts do not match the version of the active workspace");
261 // Current build scripts do not match the version of the active
262 // workspace, so there's nothing for us to update.
263 return;
264 }
265 } else {
266 tracing::debug!("abandon build scripts for workspaces");
267
268 // Here, we completely changed the workspace (Cargo.toml edit), so
269 // we don't care about build-script results, they are stale.
270 self.workspaces = Arc::new(workspaces)
271 }
272
273 if let FilesWatcher::Client = self.config.files().watcher {
274 let registration_options = lsp_types::DidChangeWatchedFilesRegistrationOptions {
275 watchers: self
276 .workspaces
277 .iter()
278 .flat_map(|ws| ws.to_roots())
279 .filter(|it| it.is_local)
280 .flat_map(|root| {
281 root.include.into_iter().flat_map(|it| {
282 [
283 format!("{}/**/*.rs", it.display()),
284 format!("{}/**/Cargo.toml", it.display()),
285 format!("{}/**/Cargo.lock", it.display()),
286 ]
287 })
288 })
289 .map(|glob_pattern| lsp_types::FileSystemWatcher { glob_pattern, kind: None })
290 .collect(),
291 };
292 let registration = lsp_types::Registration {
293 id: "workspace/didChangeWatchedFiles".to_string(),
294 method: "workspace/didChangeWatchedFiles".to_string(),
295 register_options: Some(serde_json::to_value(registration_options).unwrap()),
296 };
297 self.send_request::<lsp_types::request::RegisterCapability>(
298 lsp_types::RegistrationParams { registrations: vec![registration] },
299 |_, _| (),
300 );
301 }
302
303 let mut change = Change::new();
304
305 let files_config = self.config.files();
306 let project_folders = ProjectFolders::new(&self.workspaces, &files_config.exclude);
307
308 if self.proc_macro_clients.is_empty() {
309 if let Some((path, path_manually_set)) = self.config.proc_macro_srv() {
310 tracing::info!("Spawning proc-macro servers");
311 self.proc_macro_clients = self
312 .workspaces
313 .iter()
314 .map(|ws| {
315 let (path, args): (_, &[_]) = if path_manually_set {
316 tracing::debug!(
317 "Pro-macro server path explicitly set: {}",
318 path.display()
319 );
320 (path.clone(), &[])
321 } else {
322 match ws.find_sysroot_proc_macro_srv() {
323 Some(server_path) => (server_path, &[]),
324 None => (path.clone(), &["proc-macro"]),
325 }
326 };
327
328 tracing::info!(?args, "Using proc-macro server at {}", path.display(),);
329 ProcMacroServer::spawn(path.clone(), args).map_err(|err| {
330 let error = format!(
331 "Failed to run proc-macro server from path {}, error: {:?}",
332 path.display(),
333 err
334 );
335 tracing::error!(error);
336 error
337 })
338 })
339 .collect()
340 };
341 }
342
343 let watch = match files_config.watcher {
344 FilesWatcher::Client => vec![],
345 FilesWatcher::Server => project_folders.watch,
346 };
347 self.vfs_config_version += 1;
348 self.loader.handle.set_config(vfs::loader::Config {
349 load: project_folders.load,
350 watch,
351 version: self.vfs_config_version,
352 });
353
354 // Create crate graph from all the workspaces
355 let crate_graph = {
356 let dummy_replacements = self.config.dummy_replacements();
357
358 let vfs = &mut self.vfs.write().0;
359 let loader = &mut self.loader;
360 let mem_docs = &self.mem_docs;
361 let mut load = move |path: &AbsPath| {
362 let _p = profile::span("GlobalState::load");
363 let vfs_path = vfs::VfsPath::from(path.to_path_buf());
364 if !mem_docs.contains(&vfs_path) {
365 let contents = loader.handle.load_sync(path);
366 vfs.set_file_contents(vfs_path.clone(), contents);
367 }
368 let res = vfs.file_id(&vfs_path);
369 if res.is_none() {
370 tracing::warn!("failed to load {}", path.display())
371 }
372 res
373 };
374
375 let mut crate_graph = CrateGraph::default();
376 for (idx, ws) in self.workspaces.iter().enumerate() {
377 let proc_macro_client = match self.proc_macro_clients.get(idx) {
378 Some(res) => res.as_ref().map_err(|e| &**e),
379 None => Err("Proc macros are disabled"),
380 };
381 let mut load_proc_macro = move |crate_name: &str, path: &AbsPath| {
382 load_proc_macro(
383 proc_macro_client,
384 path,
385 dummy_replacements.get(crate_name).map(|v| &**v).unwrap_or_default(),
386 )
387 };
388 crate_graph.extend(ws.to_crate_graph(
389 &mut load_proc_macro,
390 &mut load,
391 &self.config.cargo().extra_env,
392 ));
393 }
394 crate_graph
395 };
396 change.set_crate_graph(crate_graph);
397
398 self.source_root_config = project_folders.source_root_config;
399
400 self.analysis_host.apply_change(change);
401 self.process_changes();
402 self.reload_flycheck();
403 tracing::info!("did switch workspaces");
404 }
405
406 fn fetch_workspace_error(&self) -> Result<(), String> {
407 let mut buf = String::new();
408
409 let Some(last_op_result) = self.fetch_workspaces_queue.last_op_result() else { return Ok(()) };
410 if last_op_result.is_empty() {
411 stdx::format_to!(buf, "rust-analyzer failed to discover workspace");
412 } else {
413 for ws in last_op_result {
414 if let Err(err) = ws {
415 stdx::format_to!(buf, "rust-analyzer failed to load workspace: {:#}\n", err);
416 }
417 }
418 }
419
420 if buf.is_empty() {
421 return Ok(());
422 }
423
424 Err(buf)
425 }
426
427 fn fetch_build_data_error(&self) -> Result<(), String> {
428 let mut buf = String::new();
429
430 for ws in &self.fetch_build_data_queue.last_op_result().1 {
431 match ws {
432 Ok(data) => match data.error() {
433 Some(stderr) => stdx::format_to!(buf, "{:#}\n", stderr),
434 _ => (),
435 },
436 // io errors
437 Err(err) => stdx::format_to!(buf, "{:#}\n", err),
438 }
439 }
440
441 if buf.is_empty() {
442 Ok(())
443 } else {
444 Err(buf)
445 }
446 }
447
448 fn reload_flycheck(&mut self) {
449 let _p = profile::span("GlobalState::reload_flycheck");
450 let config = match self.config.flycheck() {
451 Some(it) => it,
452 None => {
453 self.flycheck = Arc::new([]);
454 self.diagnostics.clear_check_all();
455 return;
456 }
457 };
458
459 let sender = self.flycheck_sender.clone();
460 let invocation_strategy = match config {
461 FlycheckConfig::CargoCommand { .. } => flycheck::InvocationStrategy::PerWorkspace,
462 FlycheckConfig::CustomCommand { invocation_strategy, .. } => invocation_strategy,
463 };
464
465 self.flycheck = match invocation_strategy {
466 flycheck::InvocationStrategy::Once => vec![FlycheckHandle::spawn(
467 0,
468 Box::new(move |msg| sender.send(msg).unwrap()),
469 config.clone(),
470 self.config.root_path().clone(),
471 )],
472 flycheck::InvocationStrategy::PerWorkspace => {
473 self.workspaces
474 .iter()
475 .enumerate()
476 .filter_map(|(id, w)| match w {
477 ProjectWorkspace::Cargo { cargo, .. } => Some((id, cargo.workspace_root())),
478 ProjectWorkspace::Json { project, .. } => {
479 // Enable flychecks for json projects if a custom flycheck command was supplied
480 // in the workspace configuration.
481 match config {
482 FlycheckConfig::CustomCommand { .. } => Some((id, project.path())),
483 _ => None,
484 }
485 }
486 ProjectWorkspace::DetachedFiles { .. } => None,
487 })
488 .map(|(id, root)| {
489 let sender = sender.clone();
490 FlycheckHandle::spawn(
491 id,
492 Box::new(move |msg| sender.send(msg).unwrap()),
493 config.clone(),
494 root.to_path_buf(),
495 )
496 })
497 .collect()
498 }
499 }
500 .into();
501 }
502 }
503
504 #[derive(Default)]
505 pub(crate) struct ProjectFolders {
506 pub(crate) load: Vec<vfs::loader::Entry>,
507 pub(crate) watch: Vec<usize>,
508 pub(crate) source_root_config: SourceRootConfig,
509 }
510
511 impl ProjectFolders {
512 pub(crate) fn new(
513 workspaces: &[ProjectWorkspace],
514 global_excludes: &[AbsPathBuf],
515 ) -> ProjectFolders {
516 let mut res = ProjectFolders::default();
517 let mut fsc = FileSetConfig::builder();
518 let mut local_filesets = vec![];
519
520 for root in workspaces.iter().flat_map(|ws| ws.to_roots()) {
521 let file_set_roots: Vec<VfsPath> =
522 root.include.iter().cloned().map(VfsPath::from).collect();
523
524 let entry = {
525 let mut dirs = vfs::loader::Directories::default();
526 dirs.extensions.push("rs".into());
527 dirs.include.extend(root.include);
528 dirs.exclude.extend(root.exclude);
529 for excl in global_excludes {
530 if dirs
531 .include
532 .iter()
533 .any(|incl| incl.starts_with(excl) || excl.starts_with(incl))
534 {
535 dirs.exclude.push(excl.clone());
536 }
537 }
538
539 vfs::loader::Entry::Directories(dirs)
540 };
541
542 if root.is_local {
543 res.watch.push(res.load.len());
544 }
545 res.load.push(entry);
546
547 if root.is_local {
548 local_filesets.push(fsc.len());
549 }
550 fsc.add_file_set(file_set_roots)
551 }
552
553 let fsc = fsc.build();
554 res.source_root_config = SourceRootConfig { fsc, local_filesets };
555
556 res
557 }
558 }
559
560 #[derive(Default, Debug)]
561 pub(crate) struct SourceRootConfig {
562 pub(crate) fsc: FileSetConfig,
563 pub(crate) local_filesets: Vec<usize>,
564 }
565
566 impl SourceRootConfig {
567 pub(crate) fn partition(&self, vfs: &vfs::Vfs) -> Vec<SourceRoot> {
568 let _p = profile::span("SourceRootConfig::partition");
569 self.fsc
570 .partition(vfs)
571 .into_iter()
572 .enumerate()
573 .map(|(idx, file_set)| {
574 let is_local = self.local_filesets.contains(&idx);
575 if is_local {
576 SourceRoot::new_local(file_set)
577 } else {
578 SourceRoot::new_library(file_set)
579 }
580 })
581 .collect()
582 }
583 }
584
585 /// Load the proc-macros for the given lib path, replacing all expanders whose names are in `dummy_replace`
586 /// with an identity dummy expander.
587 pub(crate) fn load_proc_macro(
588 server: Result<&ProcMacroServer, &str>,
589 path: &AbsPath,
590 dummy_replace: &[Box<str>],
591 ) -> ProcMacroLoadResult {
592 let res: Result<Vec<_>, String> = (|| {
593 let dylib = MacroDylib::new(path.to_path_buf())
594 .map_err(|io| format!("Proc-macro dylib loading failed: {io}"))?;
595 let server = server.map_err(ToOwned::to_owned)?;
596 let vec = server.load_dylib(dylib).map_err(|e| format!("{e}"))?;
597 if vec.is_empty() {
598 return Err("proc macro library returned no proc macros".to_string());
599 }
600 Ok(vec
601 .into_iter()
602 .map(|expander| expander_to_proc_macro(expander, dummy_replace))
603 .collect())
604 })();
605 return match res {
606 Ok(proc_macros) => {
607 tracing::info!(
608 "Loaded proc-macros for {}: {:?}",
609 path.display(),
610 proc_macros.iter().map(|it| it.name.clone()).collect::<Vec<_>>()
611 );
612 Ok(proc_macros)
613 }
614 Err(e) => {
615 tracing::warn!("proc-macro loading for {} failed: {e}", path.display());
616 Err(e)
617 }
618 };
619
620 fn expander_to_proc_macro(
621 expander: proc_macro_api::ProcMacro,
622 dummy_replace: &[Box<str>],
623 ) -> ProcMacro {
624 let name = SmolStr::from(expander.name());
625 let kind = match expander.kind() {
626 proc_macro_api::ProcMacroKind::CustomDerive => ProcMacroKind::CustomDerive,
627 proc_macro_api::ProcMacroKind::FuncLike => ProcMacroKind::FuncLike,
628 proc_macro_api::ProcMacroKind::Attr => ProcMacroKind::Attr,
629 };
630 let expander: Arc<dyn ProcMacroExpander> =
631 if dummy_replace.iter().any(|replace| &**replace == name) {
632 match kind {
633 ProcMacroKind::Attr => Arc::new(IdentityExpander),
634 _ => Arc::new(EmptyExpander),
635 }
636 } else {
637 Arc::new(Expander(expander))
638 };
639 ProcMacro { name, kind, expander }
640 }
641
642 #[derive(Debug)]
643 struct Expander(proc_macro_api::ProcMacro);
644
645 impl ProcMacroExpander for Expander {
646 fn expand(
647 &self,
648 subtree: &tt::Subtree,
649 attrs: Option<&tt::Subtree>,
650 env: &Env,
651 ) -> Result<tt::Subtree, ProcMacroExpansionError> {
652 let env = env.iter().map(|(k, v)| (k.to_string(), v.to_string())).collect();
653 match self.0.expand(subtree, attrs, env) {
654 Ok(Ok(subtree)) => Ok(subtree),
655 Ok(Err(err)) => Err(ProcMacroExpansionError::Panic(err.0)),
656 Err(err) => Err(ProcMacroExpansionError::System(err.to_string())),
657 }
658 }
659 }
660
661 /// Dummy identity expander, used for attribute proc-macros that are deliberately ignored by the user.
662 #[derive(Debug)]
663 struct IdentityExpander;
664
665 impl ProcMacroExpander for IdentityExpander {
666 fn expand(
667 &self,
668 subtree: &tt::Subtree,
669 _: Option<&tt::Subtree>,
670 _: &Env,
671 ) -> Result<tt::Subtree, ProcMacroExpansionError> {
672 Ok(subtree.clone())
673 }
674 }
675
676 /// Empty expander, used for proc-macros that are deliberately ignored by the user.
677 #[derive(Debug)]
678 struct EmptyExpander;
679
680 impl ProcMacroExpander for EmptyExpander {
681 fn expand(
682 &self,
683 _: &tt::Subtree,
684 _: Option<&tt::Subtree>,
685 _: &Env,
686 ) -> Result<tt::Subtree, ProcMacroExpansionError> {
687 Ok(tt::Subtree::default())
688 }
689 }
690 }
691
692 pub(crate) fn should_refresh_for_change(path: &AbsPath, change_kind: ChangeKind) -> bool {
693 const IMPLICIT_TARGET_FILES: &[&str] = &["build.rs", "src/main.rs", "src/lib.rs"];
694 const IMPLICIT_TARGET_DIRS: &[&str] = &["src/bin", "examples", "tests", "benches"];
695
696 let file_name = match path.file_name().unwrap_or_default().to_str() {
697 Some(it) => it,
698 None => return false,
699 };
700
701 if let "Cargo.toml" | "Cargo.lock" = file_name {
702 return true;
703 }
704 if change_kind == ChangeKind::Modify {
705 return false;
706 }
707
708 // .cargo/config{.toml}
709 if path.extension().unwrap_or_default() != "rs" {
710 let is_cargo_config = matches!(file_name, "config.toml" | "config")
711 && path.parent().map(|parent| parent.as_ref().ends_with(".cargo")).unwrap_or(false);
712 return is_cargo_config;
713 }
714
715 if IMPLICIT_TARGET_FILES.iter().any(|it| path.as_ref().ends_with(it)) {
716 return true;
717 }
718 let parent = match path.parent() {
719 Some(it) => it,
720 None => return false,
721 };
722 if IMPLICIT_TARGET_DIRS.iter().any(|it| parent.as_ref().ends_with(it)) {
723 return true;
724 }
725 if file_name == "main.rs" {
726 let grand_parent = match parent.parent() {
727 Some(it) => it,
728 None => return false,
729 };
730 if IMPLICIT_TARGET_DIRS.iter().any(|it| grand_parent.as_ref().ends_with(it)) {
731 return true;
732 }
733 }
734 false
735 }