Skip to content

Commit

Permalink
feat: Nice errors for shim flag parsing (#6737)
Browse files Browse the repository at this point in the history
### Description

This adds nice errors for shim parsing:

<img width="272" alt="Screenshot 2023-12-07 at 5 31 42 PM"
src="https://github.com/vercel/turbo/assets/7357863/a225b365-32b2-45c7-8a06-b921530537d9">
<img width="582" alt="Screenshot 2023-12-07 at 5 38 17 PM"
src="https://github.com/vercel/turbo/assets/7357863/a9e125b1-5ac2-4889-822c-50b681d62299">


### Testing Instructions

Added test for error output

Closes TURBO-1874

---------

Co-authored-by: nicholaslyang <Nicholas Yang>
Co-authored-by: Chris Olszewski <chris.olszewski@vercel.com>
  • Loading branch information
NicholasLYang and chris-olszewski committed Dec 14, 2023
1 parent ed6130b commit 7d458ce
Show file tree
Hide file tree
Showing 12 changed files with 273 additions and 59 deletions.
70 changes: 59 additions & 11 deletions Cargo.lock

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

2 changes: 1 addition & 1 deletion Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -82,7 +82,7 @@ lto = "off"
async-recursion = "1.0.2"
# Keep consistent with preset_env_base through swc_core
browserslist-rs = { version = "0.13.0" }

miette = { version = "5.10.0", features = ["fancy"] }
mdxjs = "0.1.20"
modularize_imports = { version = "0.62.0" }
styled_components = { version = "0.90.0" }
Expand Down
2 changes: 0 additions & 2 deletions crates/turborepo-api-client/src/error.rs
Original file line number Diff line number Diff line change
Expand Up @@ -31,8 +31,6 @@ pub enum Error {
status: CachingStatus,
message: String,
},
#[error("cache miss")]
CacheMiss,
}

pub type Result<T> = std::result::Result<T, Error>;
2 changes: 2 additions & 0 deletions crates/turborepo-lib/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -85,6 +85,7 @@ tonic-reflection = { version = "0.6.0", optional = true }
tower = "0.4.13"
turborepo-analytics = { path = "../turborepo-analytics" }
turborepo-auth = { path = "../turborepo-auth" }

turborepo-fs = { path = "../turborepo-fs" }
turborepo-graph-utils = { path = "../turborepo-graph-utils" }
turborepo-repository = { path = "../turborepo-repository" }
Expand All @@ -99,6 +100,7 @@ globwalk = { version = "0.1.0", path = "../turborepo-globwalk" }
go-parse-duration = "0.1.1"
is-terminal = "0.4.7"
lazy-regex = "2.5.0"
miette = { workspace = true, features = ["fancy"] }
node-semver = "2.1.0"
num_cpus = "1.15.0"
owo-colors.workspace = true
Expand Down
4 changes: 3 additions & 1 deletion crates/turborepo-lib/src/cli/error.rs
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
use std::backtrace;

use miette::Diagnostic;
use thiserror::Error;
use turborepo_repository::package_graph;

Expand All @@ -10,7 +11,7 @@ use crate::{
run,
};

#[derive(Debug, Error)]
#[derive(Debug, Error, Diagnostic)]
pub enum Error {
#[error("No command specified")]
NoCommand(#[backtrace] backtrace::Backtrace),
Expand Down Expand Up @@ -41,6 +42,7 @@ pub enum Error {
#[error(transparent)]
PackageManager(#[from] turborepo_repository::package_manager::Error),
#[error(transparent)]
#[diagnostic(transparent)]
Run(#[from] run::Error),
#[error(transparent)]
SerdeJson(#[from] serde_json::Error),
Expand Down
4 changes: 3 additions & 1 deletion crates/turborepo-lib/src/engine/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,9 @@ use std::{

pub use builder::{EngineBuilder, Error as BuilderError};
pub use execute::{ExecuteError, ExecutionOptions, Message, StopExecution};
use miette::Diagnostic;
use petgraph::Graph;
use thiserror::Error;
use turborepo_repository::package_graph::{PackageGraph, WorkspaceName};

use crate::{run::task_id::TaskId, task_graph::TaskDefinition};
Expand Down Expand Up @@ -226,7 +228,7 @@ impl Engine<Built> {
}
}

#[derive(Debug, thiserror::Error)]
#[derive(Debug, Error, Diagnostic)]
pub enum ValidateError {
#[error("Cannot find task definition for {task_id} in package {package_name}")]
MissingTask {
Expand Down
13 changes: 12 additions & 1 deletion crates/turborepo-lib/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -30,9 +30,10 @@ mod task_hash;
mod tracing;

pub use child::spawn_child;
use miette::Report;

use crate::commands::CommandBase;
pub use crate::{cli::Args, execution_state::ExecutionState};
use crate::{commands::CommandBase, shim::Error};

/// The payload from running main, if the program can complete without using Go
/// the Rust variant will be returned. If Go is needed then the execution state
Expand All @@ -54,12 +55,22 @@ pub fn get_version() -> &'static str {
pub fn main() -> Payload {
match shim::run() {
Ok(payload) => payload,
// We only print using miette for some errors because we want to keep
// compatibility with Go. When we've deleted the Go code we can
// move all errors to miette since it provides slightly nicer
// printing out of the box.
Err(err @ (Error::MultipleCwd(..) | Error::EmptyCwd { .. })) => {
println!("{:?}", Report::new(err));

Payload::Rust(Ok(1))
}
// We don't need to print "Turbo error" for Run errors
Err(err @ shim::Error::Cli(cli::Error::Run(_))) => Payload::Rust(Err(err)),
Err(err) => {
// This raw print matches the Go behavior, once we no longer care
// about matching formatting we should remove this.
println!("Turbo error: {err}");

Payload::Rust(Err(err))
}
}
Expand Down
7 changes: 4 additions & 3 deletions crates/turborepo-lib/src/run/error.rs
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
use miette::Diagnostic;
use thiserror::Error;
use turborepo_repository::package_graph;

Expand All @@ -8,13 +9,13 @@ use crate::{
task_graph, task_hash,
};

#[derive(Debug, Error)]
#[derive(Debug, Error, Diagnostic)]
pub enum Error {
#[error(transparent)]
Graph(#[from] graph_visualizer::Error),
#[error("error preparing engine: Invalid persistent task configuration:\n{0}")]
EngineValidation(String),
#[error(transparent)]
Graph(#[from] graph_visualizer::Error),
#[error(transparent)]
Builder(#[from] engine::BuilderError),
#[error(transparent)]
Env(#[from] turborepo_env::Error),
Expand Down
Loading

0 comments on commit 7d458ce

Please sign in to comment.