这是indexloc提供的服务,不要输入任何密码
Skip to content

feat: improve parsing error #10682

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 8 commits into from
Jul 22, 2025
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
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
1 change: 1 addition & 0 deletions Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

1 change: 1 addition & 0 deletions crates/turborepo-repository/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -41,6 +41,7 @@ wax = { workspace = true }
which = { workspace = true }

[dev-dependencies]
insta = { workspace = true }
pretty_assertions = { workspace = true }
tempfile = { workspace = true }
test-case = { workspace = true }
51 changes: 48 additions & 3 deletions crates/turborepo-repository/src/package_graph/builder.rs
Original file line number Diff line number Diff line change
Expand Up @@ -59,6 +59,21 @@ pub enum Error {
Discovery(#[from] crate::discovery::Error),
}

/// Attempts to extract the file path that caused the error from the error chain
/// Falls back to the lockfile path if no specific file can be determined
fn extract_file_path_from_error(
error: &Error,
package_manager: &crate::package_manager::PackageManager,
repo_root: &AbsoluteSystemPath,
) -> AbsoluteSystemPathBuf {
match error {
Error::PackageJsonMissingName(path) => path.clone(),
// TODO: We're handling every other error here. We could handle situations where the
// lockfile isn't the issue better.
_ => package_manager.lockfile_path(repo_root),
}
}

impl<'a> PackageGraphBuilder<'a, LocalPackageDiscoveryBuilder> {
pub fn new(repo_root: &'a AbsoluteSystemPath, root_package_json: PackageJson) -> Self {
Self {
Expand Down Expand Up @@ -444,9 +459,13 @@ impl<'a, T: PackageDiscovery> BuildState<'a, ResolvedWorkspaces, T> {
let lockfile = match self.populate_lockfile().await {
Ok(lockfile) => Some(lockfile),
Err(e) => {
let problematic_file_path =
extract_file_path_from_error(&e, &package_manager, self.repo_root);

warn!(
"Issues occurred when constructing package graph. Turbo will function, but \
some features may not be available:\n {:?}",
"An issue occurred while attempting to parse {}. Turborepo will still \
function, but some features may not be available:\n {:?}",
problematic_file_path,
Report::new(e)
);
None
Expand Down Expand Up @@ -595,7 +614,7 @@ impl PackageInfo {

#[cfg(test)]
mod test {
use std::assert_matches::assert_matches;
use std::{assert_matches::assert_matches, collections::HashMap};

use turborepo_errors::Spanned;

Expand Down Expand Up @@ -651,4 +670,30 @@ mod test {
}));
assert_matches!(builder.build().await, Err(Error::DuplicateWorkspace { .. }));
}

#[test]
#[cfg(unix)]
fn test_missing_name_field_warning_message() {
let package_json_path =
AbsoluteSystemPathBuf::new("/my-project/packages/app/package.json").unwrap();
let missing_name_error = Error::PackageJsonMissingName(package_json_path.clone());

let fake_repo_root = AbsoluteSystemPathBuf::new("/my-project").unwrap();
let fake_package_manager = crate::package_manager::PackageManager::Npm;
let extracted_path = extract_file_path_from_error(
&missing_name_error,
&fake_package_manager,
&fake_repo_root,
);
assert_eq!(extracted_path, package_json_path);

let warning_message = format!(
"An issue occurred while attempting to parse {}. Turborepo will still function, but \
some features may not be available:\n {:?}",
package_json_path,
miette::Report::new(missing_name_error)
);

insta::assert_snapshot!("missing_name_field_warning_message", warning_message);
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
---
source: crates/turborepo-repository/src/package_graph/builder.rs
expression: warning_message
---
An issue occurred while attempting to parse /my-project/packages/app/package.json. Turborepo will still function, but some features may not be available:
× package.json must have a name field:
│ /my-project/packages/app/package.json
Loading