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

Refactor Error Types #24

Merged
merged 19 commits into from
Mar 26, 2024
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
Show all changes
19 commits
Select commit Hold shift + click to select a range
a169008
Remove superfluous error field relative_package_file
willbush Mar 22, 2024
d12e15a
Rename package_name field in InvalidPackageName error
willbush Mar 22, 2024
0bb6405
Derive relative_package_dir from the package name for PackageNonDir
willbush Mar 22, 2024
f8bdeab
Refactor ratchet problems into a struct with common fields
willbush Mar 22, 2024
5c502a2
Remove unnecessary braces
willbush Mar 23, 2024
9391e85
Group shard problems into a struct for shared context
willbush Mar 23, 2024
686cbbc
Fix clippy warnings
willbush Mar 23, 2024
94244b0
Refactor nix file problems into a struct with common fields
willbush Mar 23, 2024
7eaa754
Refactor ByNameOveride problems into a struct with common fields
willbush Mar 24, 2024
d32d4cf
Refactor check_nix_file to use to_problem closure
willbush Mar 24, 2024
797f201
Refactor ByName problems into a struct with common fields
willbush Mar 24, 2024
e5aa63d
Refactor Path related problems into a struct with common fields
willbush Mar 24, 2024
b0d270e
Refactor Package related problems into a struct with common fields
willbush Mar 24, 2024
81e45fa
Refactor some nixpkgs_problem imports
willbush Mar 24, 2024
25adca4
Fix clone of `node` by moving variables above usage of `cast`
willbush Mar 26, 2024
2ed5984
Change most to_problem closures into to_validation
willbush Mar 26, 2024
c57206d
Replace RatchetErrorKind with is_new and is_empty bools
willbush Mar 26, 2024
bcc472d
Rename RatchetError to TopLevelPackageError
willbush Mar 26, 2024
591be8e
Add comments to NixpkgsProblem struct sub-types
willbush Mar 26, 2024
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
93 changes: 48 additions & 45 deletions src/nixpkgs_problem.rs
Original file line number Diff line number Diff line change
Expand Up @@ -79,31 +79,7 @@ pub enum NixpkgsProblem {
subpath: RelativePathBuf,
io_error: String,
},
PathInterpolation {
relative_package_dir: RelativePathBuf,
subpath: RelativePathBuf,
line: usize,
text: String,
},
SearchPath {
relative_package_dir: RelativePathBuf,
subpath: RelativePathBuf,
line: usize,
text: String,
},
OutsidePathReference {
relative_package_dir: RelativePathBuf,
subpath: RelativePathBuf,
line: usize,
text: String,
},
UnresolvablePathReference {
relative_package_dir: RelativePathBuf,
subpath: RelativePathBuf,
line: usize,
text: String,
io_error: String,
},
NixFileProblem(NixFileError),
RatchetProblem(RatchetError),
InternalCallPackageUsed {
attr_name: String,
Expand All @@ -126,6 +102,23 @@ pub enum ShardErrorKind {
CaseSensitiveDuplicate { first: OsString, second: OsString },
}

#[derive(Clone)]
pub struct NixFileError {
pub relative_package_dir: RelativePathBuf,
pub subpath: RelativePathBuf,
pub line: usize,
pub text: String,
pub kind: NixFileErrorKind,
}

#[derive(Clone)]
pub enum NixFileErrorKind {
PathInterpolation,
SearchPath,
OutsidePathReference,
UnresolvablePathReference { io_error: String },
}

#[derive(Clone)]
pub struct RatchetError {
pub package_name: String,
Expand Down Expand Up @@ -306,26 +299,36 @@ impl fmt::Display for NixpkgsProblem {
f,
"{relative_package_dir}: Path {subpath} is a symlink which cannot be resolved: {io_error}.",
),
NixpkgsProblem::PathInterpolation { relative_package_dir, subpath, line, text } =>
write!(
f,
"{relative_package_dir}: File {subpath} at line {line} contains the path expression \"{text}\", which is not yet supported and may point outside the directory of that package.",
),
NixpkgsProblem::SearchPath { relative_package_dir, subpath, line, text } =>
write!(
f,
"{relative_package_dir}: File {subpath} at line {line} contains the nix search path expression \"{text}\" which may point outside the directory of that package.",
),
NixpkgsProblem::OutsidePathReference { relative_package_dir, subpath, line, text } =>
write!(
f,
"{relative_package_dir}: File {subpath} at line {line} contains the path expression \"{text}\" which may point outside the directory of that package.",
),
NixpkgsProblem::UnresolvablePathReference { relative_package_dir, subpath, line, text, io_error } =>
write!(
f,
"{relative_package_dir}: File {subpath} at line {line} contains the path expression \"{text}\" which cannot be resolved: {io_error}.",
),
NixpkgsProblem::NixFileProblem(NixFileError {
relative_package_dir,
subpath,
line,
text,
kind
}) => {
match kind {
NixFileErrorKind::PathInterpolation =>
write!(
f,
"{relative_package_dir}: File {subpath} at line {line} contains the path expression \"{text}\", which is not yet supported and may point outside the directory of that package.",
),
NixFileErrorKind::SearchPath =>
write!(
f,
"{relative_package_dir}: File {subpath} at line {line} contains the nix search path expression \"{text}\" which may point outside the directory of that package.",
),
NixFileErrorKind::OutsidePathReference =>
write!(
f,
"{relative_package_dir}: File {subpath} at line {line} contains the path expression \"{text}\" which may point outside the directory of that package.",
),
NixFileErrorKind::UnresolvablePathReference { io_error } =>
write!(
f,
"{relative_package_dir}: File {subpath} at line {line} contains the path expression \"{text}\" which cannot be resolved: {io_error}.",
),
}
},
NixpkgsProblem::RatchetProblem(RatchetError {
package_name,
call_package_path,
Expand Down
58 changes: 23 additions & 35 deletions src/references.rs
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
use crate::nixpkgs_problem::NixpkgsProblem;
use crate::nixpkgs_problem::{NixFileError, NixFileErrorKind, NixpkgsProblem};
use crate::utils;
use crate::validation::{self, ResultIteratorExt, Validation::Success};
use crate::NixFileStore;
Expand Down Expand Up @@ -125,51 +125,39 @@ fn check_nix_file(

Ok(validation::sequence_(
nix_file.syntax_root.syntax().descendants().map(|node| {
let text = node.text().to_string();
let line = nix_file.line_index.line(node.text_range().start().into());
willbush marked this conversation as resolved.
Show resolved Hide resolved

// We're only interested in Path expressions
let Some(path) = rnix::ast::Path::cast(node) else {
let Some(path) = rnix::ast::Path::cast(node.clone()) else {
willbush marked this conversation as resolved.
Show resolved Hide resolved
return Success(());
};

use crate::nix_file::ResolvedPath;

match nix_file.static_resolve_path(path, absolute_package_dir) {
ResolvedPath::Interpolated => NixpkgsProblem::PathInterpolation {
relative_package_dir: relative_package_dir.to_owned(),
subpath: subpath.to_owned(),
line,
text,
}
.into(),
ResolvedPath::SearchPath => NixpkgsProblem::SearchPath {
relative_package_dir: relative_package_dir.to_owned(),
subpath: subpath.to_owned(),
line,
text,
let error_or_none = match nix_file.static_resolve_path(path, absolute_package_dir) {
ResolvedPath::Interpolated => Some(NixFileErrorKind::PathInterpolation),
ResolvedPath::SearchPath => Some(NixFileErrorKind::SearchPath),
ResolvedPath::Outside => Some(NixFileErrorKind::OutsidePathReference),
ResolvedPath::Unresolvable(e) => {
Some(NixFileErrorKind::UnresolvablePathReference {
io_error: e.to_string(),
})
}
.into(),
ResolvedPath::Outside => NixpkgsProblem::OutsidePathReference {
relative_package_dir: relative_package_dir.to_owned(),
subpath: subpath.to_owned(),
line,
text,
}
.into(),
ResolvedPath::Unresolvable(e) => NixpkgsProblem::UnresolvablePathReference {
relative_package_dir: relative_package_dir.to_owned(),
subpath: subpath.to_owned(),
line,
text,
io_error: e.to_string(),
}
.into(),
ResolvedPath::Within(..) => {
// No need to handle the case of it being inside the directory, since we scan through the
// entire directory recursively anyways
Success(())
None
}
};
if let Some(kind) = error_or_none {
NixpkgsProblem::NixFileProblem(NixFileError {
relative_package_dir: relative_package_dir.to_owned(),
subpath: subpath.to_owned(),
line: nix_file.line_index.line(node.text_range().start().into()),
text: node.text().to_string(),
kind,
})
.into()
} else {
Success(())
}
}),
))
Expand Down