diff --git a/crates/vt_bin/src/lib.rs b/crates/vt_bin/src/lib.rs
index ebe44c02a..d1997a626 100644
--- a/crates/vt_bin/src/lib.rs
+++ b/crates/vt_bin/src/lib.rs
@@ -61,6 +61,10 @@ enum Args {
/// then synthesizes a cached invocation with the given arguments. The `--` separator,
/// if present, is stripped before forwarding.
Tool {
+ /// Run `vtt` in this directory, relative to the script's cwd. This exercises
+ /// a handler that changes the command's cwd, like `vp -C
`.
+ #[clap(short = 'C')]
+ dir: Option,
#[clap(trailing_var_arg = true, allow_hyphen_values = true)]
args: Vec,
},
@@ -89,7 +93,10 @@ impl vt::CommandHandler for CommandHandler {
std::iter::once(command.program.as_str()).chain(command.args.iter().map(Str::as_str)),
)?;
match args {
- Args::Tool { args } => {
+ Args::Tool { dir, args } => {
+ if let Some(dir) = dir {
+ command.cwd = command.cwd.join(dir.as_str()).into();
+ }
let program = find_executable(get_path_env(&command.envs), &command.cwd, "vtt")?;
Ok(HandledCommand::Synthesized(SyntheticPlanRequest {
program,
diff --git a/crates/vt_plan/src/lib.rs b/crates/vt_plan/src/lib.rs
index 6612b7375..123d2105b 100644
--- a/crates/vt_plan/src/lib.rs
+++ b/crates/vt_plan/src/lib.rs
@@ -167,7 +167,8 @@ pub trait PlanRequestParser: Debug {
/// - If it returns `Ok(Some(PlanRequest::Query))`, the command will be expanded as a `ExpandedExecution` with a task graph queried from the returned `TaskQuery`.
/// - If it returns `Ok(Some(PlanRequest::Synthetic))`, the command will become a `SpawnExecution` with the synthetic task.
///
- /// When a `PlanRequest` is returned, any mutations to `command` are discarded.
+ /// When a `PlanRequest` is returned, mutations to `command` are discarded, except that
+ /// a `PlanRequest::Synthetic` runs in the (potentially mutated) `command.cwd`.
async fn get_plan_request(
&mut self,
command: &mut plan_request::ScriptCommand,
diff --git a/crates/vt_plan/src/plan.rs b/crates/vt_plan/src/plan.rs
index 616d830a9..3594ab278 100644
--- a/crates/vt_plan/src/plan.rs
+++ b/crates/vt_plan/src/plan.rs
@@ -300,7 +300,7 @@ async fn plan_task_as_execution_node(
&and_item.envs,
synthetic_plan_request,
Some(task_execution_cache_key),
- &cwd,
+ &script_command.cwd,
package_path,
parent_cache_config,
context.resolved_remote_cache(),
diff --git a/crates/vt_plan/src/plan_request.rs b/crates/vt_plan/src/plan_request.rs
index dd876555d..01c080724 100644
--- a/crates/vt_plan/src/plan_request.rs
+++ b/crates/vt_plan/src/plan_request.rs
@@ -10,7 +10,9 @@ use vt_str::Str;
///
/// All fields use `Arc` for cheap reassignment. The implementation can mutate
/// these fields to modify how the command is executed when it falls through as a
-/// normal process (i.e., when `get_plan_request` returns `None`).
+/// normal process (i.e., when `get_plan_request` returns `None`). A mutated `cwd`
+/// also applies to synthetic requests. A query request uses the cwd that the
+/// parser puts into its query.
#[derive(Debug)]
pub struct ScriptCommand {
pub program: Str,
diff --git a/crates/vt_plan/tests/plan_snapshots/fixtures/cd_in_scripts/package.json b/crates/vt_plan/tests/plan_snapshots/fixtures/cd_in_scripts/package.json
index b38b6b7b8..c176af003 100644
--- a/crates/vt_plan/tests/plan_snapshots/fixtures/cd_in_scripts/package.json
+++ b/crates/vt_plan/tests/plan_snapshots/fixtures/cd_in_scripts/package.json
@@ -2,6 +2,7 @@
"scripts": {
"build": "vtt print-file package.json",
"cd-build": "cd src && vt run build",
- "cd-lint": "cd src && vt tool print lint"
+ "cd-lint": "cd src && vt tool print lint",
+ "dir-lint": "vt tool -C src print lint && vt tool print lint"
}
}
diff --git a/crates/vt_plan/tests/plan_snapshots/fixtures/cd_in_scripts/snapshots.toml b/crates/vt_plan/tests/plan_snapshots/fixtures/cd_in_scripts/snapshots.toml
index 036bded9e..08add8d76 100644
--- a/crates/vt_plan/tests/plan_snapshots/fixtures/cd_in_scripts/snapshots.toml
+++ b/crates/vt_plan/tests/plan_snapshots/fixtures/cd_in_scripts/snapshots.toml
@@ -7,3 +7,7 @@ args = ["run", "cd-build"]
[[plan]]
name = "cd_before_vt_lint_should_put_synthetic_task_under_cwd"
args = ["run", "cd-lint"]
+
+[[plan]]
+name = "handler_cwd_applies_to_synthetic_task_only"
+args = ["run", "dir-lint"]
diff --git a/crates/vt_plan/tests/plan_snapshots/fixtures/cd_in_scripts/snapshots/query_handler_cwd_applies_to_synthetic_task_only.jsonc b/crates/vt_plan/tests/plan_snapshots/fixtures/cd_in_scripts/snapshots/query_handler_cwd_applies_to_synthetic_task_only.jsonc
new file mode 100644
index 000000000..3ff06c911
--- /dev/null
+++ b/crates/vt_plan/tests/plan_snapshots/fixtures/cd_in_scripts/snapshots/query_handler_cwd_applies_to_synthetic_task_only.jsonc
@@ -0,0 +1,162 @@
+// run dir-lint
+{
+ "graph": [
+ {
+ "key": [
+ "/",
+ "dir-lint"
+ ],
+ "node": {
+ "task_display": {
+ "package_name": "",
+ "task_name": "dir-lint",
+ "package_path": "/"
+ },
+ "items": [
+ {
+ "execution_item_display": {
+ "task_display": {
+ "package_name": "",
+ "task_name": "dir-lint",
+ "package_path": "/"
+ },
+ "command": "vt tool -C src print lint",
+ "cwd": "/"
+ },
+ "kind": {
+ "Leaf": {
+ "Spawn": {
+ "cache_metadata": {
+ "spawn_fingerprint": {
+ "cwd": "src",
+ "program_fingerprint": {
+ "OutsideWorkspace": {
+ "program_name": "vtt"
+ }
+ },
+ "args": [
+ "print",
+ "lint"
+ ],
+ "env_fingerprints": {
+ "fingerprinted_envs": {},
+ "untracked_env_config": [
+ ""
+ ]
+ }
+ },
+ "execution_cache_key": {
+ "UserTask": {
+ "task_name": "dir-lint",
+ "command_item_index": 0,
+ "and_item_index": 0,
+ "extra_args": [],
+ "package_path": ""
+ }
+ },
+ "input_config": {
+ "includes_auto": true,
+ "positive_globs": [],
+ "negative_globs": []
+ },
+ "output_config": {
+ "includes_auto": true,
+ "positive_globs": [],
+ "negative_globs": []
+ },
+ "remote_cache": null
+ },
+ "spawn_command": {
+ "program_path": "/vtt",
+ "args": [
+ "print",
+ "lint"
+ ],
+ "spawn_envs": {
+ "FORCE_COLOR": "1",
+ "PATH": "/node_modules/.bin:",
+ "VP_RUN": "1"
+ },
+ "cwd": "/src"
+ }
+ }
+ }
+ }
+ },
+ {
+ "execution_item_display": {
+ "task_display": {
+ "package_name": "",
+ "task_name": "dir-lint",
+ "package_path": "/"
+ },
+ "command": "vt tool print lint",
+ "cwd": "/"
+ },
+ "kind": {
+ "Leaf": {
+ "Spawn": {
+ "cache_metadata": {
+ "spawn_fingerprint": {
+ "cwd": "",
+ "program_fingerprint": {
+ "OutsideWorkspace": {
+ "program_name": "vtt"
+ }
+ },
+ "args": [
+ "print",
+ "lint"
+ ],
+ "env_fingerprints": {
+ "fingerprinted_envs": {},
+ "untracked_env_config": [
+ ""
+ ]
+ }
+ },
+ "execution_cache_key": {
+ "UserTask": {
+ "task_name": "dir-lint",
+ "command_item_index": 0,
+ "and_item_index": 1,
+ "extra_args": [],
+ "package_path": ""
+ }
+ },
+ "input_config": {
+ "includes_auto": true,
+ "positive_globs": [],
+ "negative_globs": []
+ },
+ "output_config": {
+ "includes_auto": true,
+ "positive_globs": [],
+ "negative_globs": []
+ },
+ "remote_cache": null
+ },
+ "spawn_command": {
+ "program_path": "/vtt",
+ "args": [
+ "print",
+ "lint"
+ ],
+ "spawn_envs": {
+ "FORCE_COLOR": "1",
+ "PATH": "/node_modules/.bin:",
+ "VP_RUN": "1"
+ },
+ "cwd": "/"
+ }
+ }
+ }
+ }
+ }
+ ]
+ },
+ "neighbors": []
+ }
+ ],
+ "concurrency_limit": 4
+}
diff --git a/crates/vt_plan/tests/plan_snapshots/fixtures/cd_in_scripts/snapshots/task_graph.md b/crates/vt_plan/tests/plan_snapshots/fixtures/cd_in_scripts/snapshots/task_graph.md
index 38a31e0aa..d47fd911a 100644
--- a/crates/vt_plan/tests/plan_snapshots/fixtures/cd_in_scripts/snapshots/task_graph.md
+++ b/crates/vt_plan/tests/plan_snapshots/fixtures/cd_in_scripts/snapshots/task_graph.md
@@ -5,6 +5,7 @@ flowchart TD
task_0["/#build"]
task_1["/#cd-build"]
task_2["/#cd-lint"]
+ task_3["/#dir-lint"]
```
## `/#build`
@@ -127,3 +128,43 @@ flowchart TD
}
```
+## `/#dir-lint`
+
+```json
+{
+ "task_display": {
+ "package_name": "",
+ "task_name": "dir-lint",
+ "package_path": "/"
+ },
+ "resolved_config": {
+ "commands": [
+ "vt tool -C src print lint && vt tool print lint"
+ ],
+ "resolved_options": {
+ "cwd": "/",
+ "cache_config": {
+ "remote_cache_allowed": true,
+ "env_config": {
+ "fingerprinted_envs": [],
+ "untracked_env": [
+ ""
+ ]
+ },
+ "input_config": {
+ "includes_auto": true,
+ "positive_globs": [],
+ "negative_globs": []
+ },
+ "output_config": {
+ "includes_auto": true,
+ "positive_globs": [],
+ "negative_globs": []
+ }
+ }
+ }
+ },
+ "source": "PackageJsonScript"
+}
+```
+