-
Notifications
You must be signed in to change notification settings - Fork 21
Expand file tree
/
Copy pathmod.rs
More file actions
205 lines (191 loc) · 6.96 KB
/
mod.rs
File metadata and controls
205 lines (191 loc) · 6.96 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
pub mod user;
use std::sync::Arc;
use monostate::MustBe;
use rustc_hash::FxHashSet;
use serde::Serialize;
pub use user::{
EnabledCacheConfig, ResolvedGlobalCacheConfig, UserCacheConfig, UserGlobalCacheConfig,
UserRunConfig, UserTaskConfig,
};
use vite_path::AbsolutePath;
use vite_str::Str;
use crate::config::user::UserTaskOptions;
/// Task configuration resolved from `package.json` scripts and/or `vite.config.ts` tasks,
/// without considering external factors like additional args from cli or environment variables.
///
/// It should resolve as much as possible to the final form to save duplicated work when it's further resolved into a spawnable command later.
/// but must be independent of external factors.
///
/// For example, `cwd` is resolved to absolute ones (no external factor can change it),
/// but `command` is not parsed into program and args yet because environment variables in it may need to be expanded.
///
/// `depends_on` is not included here because it's represented by the edges of the task graph.
#[derive(Debug, Serialize)]
pub struct ResolvedTaskConfig {
/// The command to run for this task, as a raw string.
///
/// The command may contain environment variables that need to be expanded later.
pub command: Str,
pub resolved_options: ResolvedTaskOptions,
}
#[derive(Debug, Serialize)]
pub struct ResolvedTaskOptions {
/// The working directory for the task
pub cwd: Arc<AbsolutePath>,
/// Cache-related config. None means caching is disabled.
pub cache_config: Option<CacheConfig>,
}
impl ResolvedTaskOptions {
/// Resolves from user-defined options and the directory path where the options are defined.
/// For user-defined tasks or scripts in package.json, `dir` is the package directory
/// For synthetic tasks, `dir` is the cwd of the original command (e.g. the cwd of `vp lint`).
pub fn resolve(user_options: UserTaskOptions, dir: &Arc<AbsolutePath>) -> Self {
let cwd: Arc<AbsolutePath> = match user_options.cwd_relative_to_package {
Some(ref cwd) if !cwd.as_str().is_empty() => dir.join(cwd).into(),
_ => Arc::clone(dir),
};
let cache_config = match user_options.cache_config {
UserCacheConfig::Disabled { cache: MustBe!(false) } => None,
UserCacheConfig::Enabled { cache: _, enabled_cache_config } => {
let mut pass_through_envs: FxHashSet<Str> = enabled_cache_config
.pass_through_envs
.unwrap_or_default()
.into_iter()
.collect();
pass_through_envs.extend(DEFAULT_PASSTHROUGH_ENVS.iter().copied().map(Str::from));
Some(CacheConfig {
env_config: EnvConfig {
fingerprinted_envs: enabled_cache_config
.envs
.map(|e| e.into_vec().into_iter().collect())
.unwrap_or_default(),
pass_through_envs,
},
})
}
};
Self { cwd, cache_config }
}
}
#[derive(Debug, Clone, Serialize)]
pub struct CacheConfig {
pub env_config: EnvConfig,
}
#[derive(Debug, Clone, Serialize)]
pub struct EnvConfig {
/// environment variable names to be fingerprinted and passed to the task, with defaults populated
pub fingerprinted_envs: FxHashSet<Str>,
/// environment variable names to be passed to the task without fingerprinting, with defaults populated
pub pass_through_envs: FxHashSet<Str>,
}
#[derive(Debug, thiserror::Error)]
pub enum ResolveTaskConfigError {
/// Both package.json script and vite.config.* task define commands for the task
#[error("Both package.json script and vite.config.* task define commands for the task")]
CommandConflict,
/// Neither package.json script nor vite.config.* task define a command for the task
#[error("Neither package.json script nor vite.config.* task define a command for the task")]
NoCommand,
}
impl ResolvedTaskConfig {
/// Resolve from package.json script only (no config entry for this task).
///
/// Always resolves with caching enabled (default settings).
/// The global cache config is applied at plan time, not here.
#[must_use]
pub fn resolve_package_json_script(
package_dir: &Arc<AbsolutePath>,
package_json_script: &str,
) -> Self {
Self {
command: package_json_script.into(),
resolved_options: ResolvedTaskOptions::resolve(UserTaskOptions::default(), package_dir),
}
}
/// Resolves from user config, package dir, and package.json script (if any).
///
/// # Errors
///
/// Returns [`ResolveTaskConfigError::CommandConflict`] if both the user config and
/// package.json define a command, or [`ResolveTaskConfigError::NoCommand`] if neither does.
pub fn resolve(
user_config: UserTaskConfig,
package_dir: &Arc<AbsolutePath>,
package_json_script: Option<&str>,
) -> Result<Self, ResolveTaskConfigError> {
let command = match (&user_config.command, package_json_script) {
(Some(_), Some(_)) => return Err(ResolveTaskConfigError::CommandConflict),
(None, None) => return Err(ResolveTaskConfigError::NoCommand),
(Some(cmd), None) => cmd.as_ref(),
(None, Some(script)) => script,
};
Ok(Self {
command: command.into(),
resolved_options: ResolvedTaskOptions::resolve(user_config.options, package_dir),
})
}
}
// Exact matches for common environment variables
// Referenced from Turborepo's implementation:
// https://github.com/vercel/turborepo/blob/26d309f073ca3ac054109ba0c29c7e230e7caac3/crates/turborepo-lib/src/task_hash.rs#L439
#[doc(hidden)] // exported for redacting snapshots in tests
pub const DEFAULT_PASSTHROUGH_ENVS: &[&str] = &[
// System and shell
"HOME",
"USER",
"TZ",
"LANG",
"SHELL",
"PWD",
"PATH",
// CI/CD environments
"CI",
// Node.js specific
"NODE_OPTIONS",
"COREPACK_HOME",
"NPM_CONFIG_STORE_DIR",
"PNPM_HOME",
// Library paths
"LD_LIBRARY_PATH",
"DYLD_FALLBACK_LIBRARY_PATH",
"LIBPATH",
// Terminal/display
"COLORTERM",
"TERM",
"TERM_PROGRAM",
"DISPLAY",
"FORCE_COLOR",
"NO_COLOR",
// Temporary directories
"TMP",
"TEMP",
// Vercel specific
"VERCEL",
"VERCEL_*",
"NEXT_*",
"USE_OUTPUT_FOR_EDGE_FUNCTIONS",
"NOW_BUILDER",
// Windows specific
"APPDATA",
"PROGRAMDATA",
"SYSTEMROOT",
"SYSTEMDRIVE",
"USERPROFILE",
"HOMEDRIVE",
"HOMEPATH",
// IDE specific (exact matches)
"ELECTRON_RUN_AS_NODE",
"JB_INTERPRETER",
"_JETBRAINS_TEST_RUNNER_RUN_SCOPE_TYPE",
"JB_IDE_*",
// VSCode specific
"VSCODE_*",
// Docker specific
"DOCKER_*",
"BUILDKIT_*",
"COMPOSE_*",
// Playwright specific
"PLAYWRIGHT_*",
// Token patterns
"*_TOKEN",
];