-
Notifications
You must be signed in to change notification settings - Fork 2.8k
Add validation for example target paths with clearer error messages #16329
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
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -445,13 +445,17 @@ fn to_example_targets( | |
| let mut result = Vec::new(); | ||
| for toml in targets { | ||
| let path = package_root.join(&toml.path.as_ref().expect("previously normalized").0); | ||
| let target_name = name_or_panic(&toml); | ||
|
|
||
| validate_target_path_as_source_file(&path, target_name, TARGET_KIND_HUMAN_EXAMPLE)?; | ||
|
|
||
| let crate_types = match toml.crate_types() { | ||
| Some(kinds) => kinds.iter().map(|s| s.into()).collect(), | ||
| None => Vec::new(), | ||
| }; | ||
|
|
||
| let mut target = Target::example_target( | ||
| name_or_panic(&toml), | ||
| target_name, | ||
| crate_types, | ||
| path, | ||
| toml.required_features.clone(), | ||
|
|
@@ -928,6 +932,54 @@ fn validate_unique_build_scripts(scripts: &[String]) -> CargoResult<()> { | |
| Ok(()) | ||
| } | ||
|
|
||
| // Will check if `path` specified for a target is a valid source file | ||
| fn validate_target_path_as_source_file( | ||
| path: &Path, | ||
| target_name: &str, | ||
| target_kind: &str, | ||
| ) -> CargoResult<()> { | ||
| if !path.exists() { | ||
| anyhow::bail!( | ||
| "can't find {} `{}` at path `{}`", | ||
| target_kind, | ||
| target_name, | ||
| path.display() | ||
| ); | ||
| } | ||
|
Comment on lines
+941
to
+948
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. We should have a test for this case as well. This might also represent a breaking change. |
||
|
|
||
| // If the path is likely a crate, then suggest setting the path to the entrypoint | ||
| if path.is_dir() { | ||
| anyhow::bail!( | ||
| "path `{}` for {} `{}` is a directory, but a source file was expected.\n\ | ||
| Consider setting the path to the intended entrypoint file instead: `{}/.../main.rs`", | ||
|
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. What if we talked the directory to report concrete
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. A message like this should start with
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. While |
||
| path.display(), | ||
| target_kind, | ||
| target_name, | ||
| path.display(), | ||
| ); | ||
| } | ||
|
|
||
| // Validate if the path has a .rs extension | ||
| if let Some(extension) = path.extension() { | ||
| if extension != "rs" { | ||
| anyhow::bail!( | ||
| "{} `{}` has path `{}` which does not have a `.rs` extension", | ||
| target_kind, | ||
| target_name, | ||
| path.display() | ||
| ); | ||
| } | ||
| } else { | ||
| anyhow::bail!( | ||
| "{} `{}` has path `{}` which has no file extension (expected `.rs`)", | ||
| target_kind, | ||
| target_name, | ||
| path.display() | ||
| ); | ||
| } | ||
|
Comment on lines
+962
to
+979
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. What happens today when you have an extension-less file or a non-
Contributor
Author
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. today it works if the example and its path is explicitly mentioned in |
||
| Ok(()) | ||
| } | ||
|
|
||
| fn configure( | ||
| toml: &TomlTarget, | ||
| target: &mut Target, | ||
|
|
||
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
As I mentioned on #10173, we shouldn't limit ourselves to examples