Skip to content

Commit 63140ee

Browse files
committed
fix: inject VP_COMMAND in Rust resolver for synthesized subcommands
When vp run synthesizes inner commands like vp build, VP_COMMAND stayed as 'run' from the parent process, causing defineConfig to skip the plugins factory. Now the resolver injects VP_COMMAND with the actual subcommand name into child process envs.
1 parent fa53fec commit 63140ee

File tree

2 files changed

+36
-1
lines changed
  • packages/cli

2 files changed

+36
-1
lines changed

packages/cli/binding/src/cli.rs

Lines changed: 34 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -123,6 +123,24 @@ pub enum SynthesizableSubcommand {
123123
},
124124
}
125125

126+
impl SynthesizableSubcommand {
127+
/// Return the command name string for use in `VP_COMMAND` env var.
128+
fn command_name(&self) -> &'static str {
129+
match self {
130+
Self::Lint { .. } => "lint",
131+
Self::Fmt { .. } => "fmt",
132+
Self::Build { .. } => "build",
133+
Self::Test { .. } => "test",
134+
Self::Pack { .. } => "pack",
135+
Self::Dev { .. } => "dev",
136+
Self::Preview { .. } => "preview",
137+
Self::Doc { .. } => "doc",
138+
Self::Install { .. } => "install",
139+
Self::Check { .. } => "check",
140+
}
141+
}
142+
}
143+
126144
/// Top-level CLI argument parser for vite-plus.
127145
#[derive(Debug, Parser)]
128146
#[command(name = "vp", disable_help_subcommand = true)]
@@ -233,6 +251,22 @@ impl SubcommandResolver {
233251
resolved_vite_config: Option<&ResolvedUniversalViteConfig>,
234252
envs: &Arc<FxHashMap<Arc<OsStr>, Arc<OsStr>>>,
235253
cwd: &Arc<AbsolutePath>,
254+
) -> anyhow::Result<ResolvedSubcommand> {
255+
let command_name = subcommand.command_name();
256+
let mut resolved = self.resolve_inner(subcommand, resolved_vite_config, envs, cwd).await?;
257+
// Inject VP_COMMAND so that defineConfig's plugin factory knows which command is running,
258+
// even when the subcommand is synthesized inside `vp run`.
259+
let envs = Arc::make_mut(&mut resolved.envs);
260+
envs.insert(Arc::from(OsStr::new("VP_COMMAND")), Arc::from(OsStr::new(command_name)));
261+
Ok(resolved)
262+
}
263+
264+
async fn resolve_inner(
265+
&self,
266+
subcommand: SynthesizableSubcommand,
267+
resolved_vite_config: Option<&ResolvedUniversalViteConfig>,
268+
envs: &Arc<FxHashMap<Arc<OsStr>, Arc<OsStr>>>,
269+
cwd: &Arc<AbsolutePath>,
236270
) -> anyhow::Result<ResolvedSubcommand> {
237271
match subcommand {
238272
SynthesizableSubcommand::Lint { mut args } => {
Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,2 +1,3 @@
11
> vp run build-task
2-
[1]> cat dist/index.html | grep 'run-build-plugin-injected' # vp run build should load plugins from factory
2+
> cat dist/index.html | grep 'run-build-plugin-injected' # vp run build should load plugins from factory
3+
<!-- run-build-plugin-injected --></body>

0 commit comments

Comments
 (0)