Skip to content

Commit 6c0a95b

Browse files
committed
refactor: simplify synthetic task creation with UserCacheConfig helpers
1 parent 7586a26 commit 6c0a95b

File tree

7 files changed

+48
-36
lines changed

7 files changed

+48
-36
lines changed

crates/vite_task/src/session/mod.rs

Lines changed: 2 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -9,15 +9,13 @@ use std::{ffi::OsStr, fmt::Debug, sync::Arc};
99
use cache::ExecutionCache;
1010
pub use cache::{CacheMiss, FingerprintMismatch};
1111
pub use event::ExecutionEvent;
12-
use monostate::MustBe;
1312
use once_cell::sync::OnceCell;
1413
pub use reporter::ExitStatus;
1514
use reporter::LabeledReporter;
1615
use vite_path::{AbsolutePath, AbsolutePathBuf};
1716
use vite_str::Str;
1817
use vite_task_graph::{
19-
IndexedTaskGraph, TaskGraph, TaskGraphLoadError,
20-
config::user::{UserCacheConfig, UserTaskOptions},
18+
IndexedTaskGraph, TaskGraph, TaskGraphLoadError, config::user::UserCacheConfig,
2119
loader::UserConfigLoader,
2220
};
2321
use vite_task_plan::{
@@ -105,10 +103,7 @@ impl vite_task_plan::PlanRequestParser for PlanRequestParser<'_> {
105103
Command::Cache { .. } => Ok(Some(PlanRequest::Synthetic(SyntheticPlanRequest {
106104
program: Arc::from(OsStr::new(command.program.as_str())),
107105
args: Arc::clone(&command.args),
108-
task_options: UserTaskOptions {
109-
cache_config: UserCacheConfig::Disabled { cache: MustBe!(false) },
110-
..Default::default()
111-
},
106+
cache_config: UserCacheConfig::disabled(),
112107
envs: Arc::clone(&command.envs),
113108
}))),
114109
Command::Run(run_command) => Ok(Some(run_command.into_plan_request(&command.cwd)?)),

crates/vite_task_bin/src/lib.rs

Lines changed: 8 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,7 @@ use vite_path::AbsolutePath;
1212
use vite_str::Str;
1313
use vite_task::{
1414
Command, EnabledCacheConfig, HandledCommand, ScriptCommand, SessionCallbacks, UserCacheConfig,
15-
UserTaskOptions, get_path_env, plan_request::SyntheticPlanRequest,
15+
get_path_env, plan_request::SyntheticPlanRequest,
1616
};
1717

1818
#[derive(Debug, Default)]
@@ -48,7 +48,10 @@ fn synthesize_node_modules_bin_task(
4848
Ok(SyntheticPlanRequest {
4949
program: find_executable(get_path_env(envs), &*cwd, executable_name)?,
5050
args: args.into(),
51-
task_options: Default::default(),
51+
cache_config: UserCacheConfig::with_config(EnabledCacheConfig {
52+
envs: None,
53+
pass_through_envs: None,
54+
}),
5255
envs: Arc::clone(envs),
5356
})
5457
}
@@ -108,16 +111,9 @@ impl vite_task::CommandHandler for CommandHandler {
108111
Ok(HandledCommand::Synthesized(SyntheticPlanRequest {
109112
program: find_executable(get_path_env(&envs), &*command.cwd, "print-env")?,
110113
args: [name.clone()].into(),
111-
task_options: UserTaskOptions {
112-
cache_config: UserCacheConfig::Enabled {
113-
cache: None,
114-
enabled_cache_config: EnabledCacheConfig {
115-
envs: None,
116-
pass_through_envs: Some(vec![name]),
117-
},
118-
},
119-
..Default::default()
120-
},
114+
cache_config: UserCacheConfig::with_config({
115+
EnabledCacheConfig { envs: None, pass_through_envs: Some(vec![name]) }
116+
}),
121117
envs: Arc::new(envs),
122118
}))
123119
}

crates/vite_task_bin/src/main.rs

Lines changed: 7 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@ use std::{process::ExitCode, sync::Arc};
33
use clap::Parser;
44
use vite_str::Str;
55
use vite_task::{
6-
EnabledCacheConfig, ExitStatus, Session, UserCacheConfig, UserTaskOptions, get_path_env,
6+
EnabledCacheConfig, ExitStatus, Session, UserCacheConfig, get_path_env,
77
plan_request::SyntheticPlanRequest,
88
};
99
use vite_task_bin::{Args, OwnedSessionCallbacks, find_executable};
@@ -29,16 +29,12 @@ async fn run() -> anyhow::Result<ExitStatus> {
2929
let request = SyntheticPlanRequest {
3030
program,
3131
args: [Str::from("FOO")].into(),
32-
task_options: UserTaskOptions {
33-
cache_config: UserCacheConfig::Enabled {
34-
cache: None,
35-
enabled_cache_config: EnabledCacheConfig {
36-
envs: Some(Box::from([Str::from("FOO")])),
37-
pass_through_envs: None,
38-
},
39-
},
40-
..Default::default()
41-
},
32+
cache_config: UserCacheConfig::with_config({
33+
EnabledCacheConfig {
34+
envs: Some(Box::from([Str::from("FOO")])),
35+
pass_through_envs: None,
36+
}
37+
}),
4238
envs: Arc::clone(envs),
4339
};
4440
let cache_key: Arc<[Str]> = Arc::from([Str::from("print-env-foo")]);

crates/vite_task_graph/src/config/mod.rs

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -40,6 +40,8 @@ pub struct ResolvedTaskOptions {
4040

4141
impl ResolvedTaskOptions {
4242
/// Resolves from user-defined options and the directory path where the options are defined.
43+
/// For user-defined tasks or scripts in package.json, `dir` is the package directory
44+
/// For synthetic tasks, `dir` is the cwd of the original command (e.g. the cwd of `vp lint`).
4345
pub fn resolve(user_options: UserTaskOptions, dir: &Arc<AbsolutePath>) -> Self {
4446
let cwd: Arc<AbsolutePath> = match user_options.cwd_relative_to_package {
4547
Some(ref cwd) if !cwd.as_str().is_empty() => dir.join(cwd).into(),

crates/vite_task_graph/src/config/user.rs

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -31,6 +31,18 @@ pub enum UserCacheConfig {
3131
},
3232
}
3333

34+
impl UserCacheConfig {
35+
/// Create an enabled cache config with the given `EnabledCacheConfig`.
36+
pub fn with_config(config: EnabledCacheConfig) -> Self {
37+
UserCacheConfig::Enabled { cache: Some(MustBe!(true)), enabled_cache_config: config }
38+
}
39+
40+
/// Create a disabled cache config.
41+
pub fn disabled() -> Self {
42+
UserCacheConfig::Disabled { cache: MustBe!(false) }
43+
}
44+
}
45+
3446
/// Cache configuration fields when caching is enabled
3547
#[derive(Debug, Deserialize, PartialEq, Eq)]
3648
#[cfg_attr(test, derive(TS), ts(optional_fields))]

crates/vite_task_plan/src/plan.rs

Lines changed: 14 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,10 @@ use futures_util::FutureExt;
1111
use vite_path::{AbsolutePath, AbsolutePathBuf, RelativePathBuf, relative::InvalidPathDataError};
1212
use vite_shell::try_parse_as_and_list;
1313
use vite_str::Str;
14-
use vite_task_graph::{TaskNodeIndex, config::ResolvedTaskOptions};
14+
use vite_task_graph::{
15+
TaskNodeIndex,
16+
config::{ResolvedTaskOptions, user::UserTaskOptions},
17+
};
1518

1619
use crate::{
1720
ExecutionItem, ExecutionItemDisplay, ExecutionItemKind, LeafExecutionKind, PlanContext,
@@ -290,10 +293,18 @@ pub fn plan_synthetic_request(
290293
execution_cache_key: Option<ExecutionCacheKey>,
291294
cwd: &Arc<AbsolutePath>,
292295
) -> Result<SpawnExecution, TaskPlanErrorKind> {
293-
let SyntheticPlanRequest { program, args, task_options, envs } = synthetic_plan_request;
296+
let SyntheticPlanRequest { program, args, cache_config, envs } = synthetic_plan_request;
294297

295298
let program_path = which(&program, &envs, cwd).map_err(TaskPlanErrorKind::ProgramNotFound)?;
296-
let resolved_options = ResolvedTaskOptions::resolve(task_options, &cwd);
299+
let resolved_options = ResolvedTaskOptions::resolve(
300+
UserTaskOptions {
301+
cache_config,
302+
// cwd_relative_to_package and depends_on don't make sense for synthetic tasks.
303+
cwd_relative_to_package: None,
304+
depends_on: None,
305+
},
306+
&cwd,
307+
);
297308

298309
plan_spawn_execution(
299310
workspace_path,

crates/vite_task_plan/src/plan_request.rs

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@ use std::{collections::HashMap, ffi::OsStr, sync::Arc};
22

33
use vite_path::AbsolutePath;
44
use vite_str::Str;
5-
use vite_task_graph::{config::user::UserTaskOptions, query::TaskQuery};
5+
use vite_task_graph::{config::UserCacheConfig, query::TaskQuery};
66

77
/// A parsed command from a task script, passed to [`super::PlanRequestParser::get_plan_request`].
88
///
@@ -43,8 +43,8 @@ pub struct SyntheticPlanRequest {
4343
/// The arguments to pass to the program
4444
pub args: Arc<[Str]>,
4545

46-
/// The task options as if it's defined in `vite.config.*`
47-
pub task_options: UserTaskOptions,
46+
/// The cache config as if it's defined in `vite.config.*`
47+
pub cache_config: UserCacheConfig,
4848

4949
/// All environment variables to set for the synthetic task.
5050
///

0 commit comments

Comments
 (0)