diff --git a/crates/fspy_client_unix/src/lib.rs b/crates/fspy_client_unix/src/lib.rs index 2186d0909..878603b88 100644 --- a/crates/fspy_client_unix/src/lib.rs +++ b/crates/fspy_client_unix/src/lib.rs @@ -119,6 +119,12 @@ impl<'a> Client<'a> { // SAFETY: raw_exec contains valid pointers to C strings and // null-terminated arrays, as provided by the caller. let mut exec = unsafe { raw_exec.to_exec(allocator) }; + if self.encoded_payload.is_overridden_in(&exec.envs) { + // A nested fspy session owns this child and traces it itself. + // This trace cannot see the child, so it must not be cached. + self.report_loss(); + return Ok((raw_exec, None)); + } let pre_exec = handle_exec(&mut exec, config, &self.encoded_payload, |path| { self.send(AccessMode::Read, path); })?; diff --git a/crates/fspy_shared_unix/src/payload.rs b/crates/fspy_shared_unix/src/payload.rs index d5ffb0ddc..c0b5a3eff 100644 --- a/crates/fspy_shared_unix/src/payload.rs +++ b/crates/fspy_shared_unix/src/payload.rs @@ -5,6 +5,8 @@ use bstr::BStr; use fspy_shared::ipc::{AccessChannelConf, IpcStr}; use wincode::{SchemaRead, SchemaWrite}; +use crate::exec::Env; + /// The payload as it travels between processes. /// /// A payload is a view: every path in it borrows from storage its producer @@ -51,6 +53,20 @@ pub struct EncodedPayload<'a> { pub encoded_string: &'a BStr, } +impl EncodedPayload<'_> { + /// Returns whether `envs` carries another tracer's payload. + /// + /// A traced process that runs its own fspy session (a nested `vp run`) + /// prepares each child for that inner session. Such an exec belongs to + /// the inner trace and must not be injected with this payload. + pub fn is_overridden_in(&self, envs: &[Env]) -> bool { + envs.iter().any(|(name, value)| { + name.as_slice() == PAYLOAD_ENV_NAME.as_bytes() + && value.as_deref() != Some(self.encoded_string.as_ref()) + }) + } +} + // Seals the view-only design: every field is an inert borrow of leaked // bytes. A retained allocator handle is interior-mutable and would fail // this assertion. diff --git a/crates/vt_bin/tests/e2e_snapshots/fixtures/nested_run_in_sequence/compile.txt b/crates/vt_bin/tests/e2e_snapshots/fixtures/nested_run_in_sequence/compile.txt new file mode 100644 index 000000000..7f6658be2 --- /dev/null +++ b/crates/vt_bin/tests/e2e_snapshots/fixtures/nested_run_in_sequence/compile.txt @@ -0,0 +1 @@ +compile input diff --git a/crates/vt_bin/tests/e2e_snapshots/fixtures/nested_run_in_sequence/lint.txt b/crates/vt_bin/tests/e2e_snapshots/fixtures/nested_run_in_sequence/lint.txt new file mode 100644 index 000000000..b338c5b3d --- /dev/null +++ b/crates/vt_bin/tests/e2e_snapshots/fixtures/nested_run_in_sequence/lint.txt @@ -0,0 +1 @@ +lint input diff --git a/crates/vt_bin/tests/e2e_snapshots/fixtures/nested_run_in_sequence/package.json b/crates/vt_bin/tests/e2e_snapshots/fixtures/nested_run_in_sequence/package.json new file mode 100644 index 000000000..3579b1fd6 --- /dev/null +++ b/crates/vt_bin/tests/e2e_snapshots/fixtures/nested_run_in_sequence/package.json @@ -0,0 +1,4 @@ +{ + "name": "nested-run-in-script", + "private": true +} diff --git a/crates/vt_bin/tests/e2e_snapshots/fixtures/nested_run_in_sequence/snapshots.toml b/crates/vt_bin/tests/e2e_snapshots/fixtures/nested_run_in_sequence/snapshots.toml new file mode 100644 index 000000000..591c67faa --- /dev/null +++ b/crates/vt_bin/tests/e2e_snapshots/fixtures/nested_run_in_sequence/snapshots.toml @@ -0,0 +1,28 @@ +[[e2e]] +name = "nested_run_in_sequence" +comment = """ +A cached task runs `vt run lint; vt run compile`. Unlike `&&`, `vt` doesn't split `;`, so a shell runs both nested `vt run` calls. The outer run traces the shell, and each inner run traces its own task. The inner runs' spawns must not fail because they already run under the outer run's tracing. +""" +steps = [ + { argv = [ + "vt", + "run", + "check", + ], comment = "inner tasks: cache miss" }, + { argv = [ + "vt", + "run", + "check", + ], comment = "inner tasks: cache hit" }, + { argv = [ + "vtt", + "write-file", + "compile.txt", + "changed compile input", + ], comment = "modify compile's input" }, + { argv = [ + "vt", + "run", + "check", + ], comment = "lint: cache hit; compile: cache miss, prints the new content" }, +] diff --git a/crates/vt_bin/tests/e2e_snapshots/fixtures/nested_run_in_sequence/snapshots/nested_run_in_sequence.md b/crates/vt_bin/tests/e2e_snapshots/fixtures/nested_run_in_sequence/snapshots/nested_run_in_sequence.md new file mode 100644 index 000000000..da8ad716a --- /dev/null +++ b/crates/vt_bin/tests/e2e_snapshots/fixtures/nested_run_in_sequence/snapshots/nested_run_in_sequence.md @@ -0,0 +1,56 @@ +# nested_run_in_sequence + +A cached task runs `vt run lint; vt run compile`. Unlike `&&`, `vt` doesn't split `;`, so a shell runs both nested `vt run` calls. The outer run traces the shell, and each inner run traces its own task. The inner runs' spawns must not fail because they already run under the outer run's tracing. + +## `vt run check` + +inner tasks: cache miss + +``` +$ vt run lint; vt run compile +$ vtt print-file lint.txt +lint input + +$ vtt print-file compile.txt +compile input +``` + +## `vt run check` + +inner tasks: cache hit + +``` +$ vt run lint; vt run compile +$ vtt print-file lint.txt ◉ cache hit, replaying +lint input + +--- +vt run: cache hit. +$ vtt print-file compile.txt ◉ cache hit, replaying +compile input + +--- +vt run: cache hit. +``` + +## `vtt write-file compile.txt 'changed compile input'` + +modify compile's input + +``` +``` + +## `vt run check` + +lint: cache hit; compile: cache miss, prints the new content + +``` +$ vt run lint; vt run compile ○ cache miss: 'compile.txt' modified, executing +$ vtt print-file lint.txt ◉ cache hit, replaying +lint input + +--- +vt run: cache hit. +$ vtt print-file compile.txt ○ cache miss: 'compile.txt' modified, executing +changed compile input +``` diff --git a/crates/vt_bin/tests/e2e_snapshots/fixtures/nested_run_in_sequence/vite-task.json b/crates/vt_bin/tests/e2e_snapshots/fixtures/nested_run_in_sequence/vite-task.json new file mode 100644 index 000000000..7b250cc78 --- /dev/null +++ b/crates/vt_bin/tests/e2e_snapshots/fixtures/nested_run_in_sequence/vite-task.json @@ -0,0 +1,16 @@ +{ + "tasks": { + "lint": { + "command": "vtt print-file lint.txt", + "cache": true + }, + "compile": { + "command": "vtt print-file compile.txt", + "cache": true + }, + "check": { + "command": "vt run lint; vt run compile", + "cache": true + } + } +}