Skip to content

Commit 751d664

Browse files
committed
session: a fixture directory is unique to its run, and the note that said "ignored" is gone
`a_class_scoped_by_another_files_macro_body_is_scoped_through_the_session` was reported failing on every rerun with `Unknown(NotDeclaredHere("studio::Widget"))`. It passes here — alone, in the whole `session::` group, in the full lib suite, cold and warm, release and debug — so the *first* failing run was not reproduced; what was found is the mechanism by which its result survives every rerun. The fixtures shared one tree per name (`%TEMP%\cppls-session-disk\body-pass`) and the cleanup was `let _ = std::fs::remove_dir_all(&root);`. The sessions write their summaries under `<fixture>/.cppls`, so a directory that could not be removed — a file held open by another process on Windows, a run that aborted before its `Drop` — was read as the *next* run's state: a summary of `widget.h` keyed by its own (unchanged) bytes, answering the old namespace, for every rerun. `a_fresh_fixture_directory` gives each fixture the name, the process id and a serial, so a stale directory is unreachable rather than merely unlikely, and two concurrent runs cannot delete each other's files. `a_fixture_directory_is_unique_to_its_run` pins both properties. The test's comment said "ignored while the refined reading is being fixed" and there was no `#[ignore]` — the note was written for a behaviour that later landed, so it is replaced with what the test now pins. Also recorded: the obvious fix — draining `refinements_wanted()` at the end of `index_everything` so `pending_work()` is zero on return — was written, measured and reverted. It makes this test pass and breaks `a_body_edit_does_not_cost_the_program_again` and `a_program_is_read_when_the_reader_stops_editing`: the unit read is deferred to the pump's idle step on purpose. `docs/buglist.md` section 33 and `docs/next.md` section 8 carry the measurement so the next attempt starts from it.
1 parent 592f86c commit 751d664

3 files changed

Lines changed: 145 additions & 7 deletions

File tree

‎crates/cpp_code_analysis/src/session/tests.rs‎

Lines changed: 55 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,26 @@ use crate::index::{Priority, StepOutcome};
88
use crate::symbol::Known;
99
use std::path::{Path, PathBuf};
1010

11+
/// **A fixture directory no other run can be using** — the name, the process id, and a serial.
12+
///
13+
/// The fixtures in this file used to share one tree per name (`%TEMP%\cppls-session-disk\body-pass`), and
14+
/// [`std::fs::remove_dir_all`]'s failure was ignored. A directory that could not be removed — a `.cppls` a previous run
15+
/// left, held open by another process on Windows — then became **this** run's state: the store loaded a summary keyed
16+
/// by a text the fixture had just rewritten, and the answer was the old one. Measured as a report of
17+
/// `a_class_scoped_by_another_files_macro_body_is_scoped_through_the_session` failing on every rerun with
18+
/// ``Unknown(NotDeclaredHere("studio::Widget"))`` — the summary under `<fixture>/.cppls` still said `std`, and nothing
19+
/// in the run could clear it. A unique name makes the stale directory unreachable rather than merely unlikely, and
20+
/// keeps two concurrent `cargo test` runs from deleting each other's files.
21+
fn a_fresh_fixture_directory(parent: &str, name: &str) -> PathBuf {
22+
static NEXT: std::sync::atomic::AtomicU32 = std::sync::atomic::AtomicU32::new(0);
23+
let serial = NEXT.fetch_add(1, std::sync::atomic::Ordering::Relaxed);
24+
let root = std::env::temp_dir()
25+
.join(parent)
26+
.join(format!("{name}-{}-{serial}", std::process::id()));
27+
let _ = std::fs::remove_dir_all(&root);
28+
root
29+
}
30+
1131
/// A project in memory: the files, the buffers in front of them, and the providers that join the two.
1232
///
1333
/// The fixture keeps the providers and hands each session a **clone**, which is what a caller outside a test
@@ -22,8 +42,7 @@ struct Memory<'a> {
2242

2343
impl<'a> Memory<'a> {
2444
fn new(name: &str, files: &'a MemoryFiles) -> Self {
25-
let root = std::env::temp_dir().join("cppls-session-tests").join(name);
26-
let _ = std::fs::remove_dir_all(&root);
45+
let root = a_fresh_fixture_directory("cppls-session-tests", name);
2746

2847
let documents = OpenDocuments::new();
2948
let providers = SessionFiles::new(documents.clone(), files.clone());
@@ -3596,8 +3615,7 @@ struct Project {
35963615

35973616
impl Project {
35983617
fn new(name: &str) -> Self {
3599-
let root = std::env::temp_dir().join("cppls-session-disk").join(name);
3600-
let _ = std::fs::remove_dir_all(&root);
3618+
let root = a_fresh_fixture_directory("cppls-session-disk", name);
36013619
std::fs::create_dir_all(&root).expect("the fixture directory");
36023620

36033621
Project { root }
@@ -3662,10 +3680,40 @@ fn the_scan_finds_the_projects_sources_and_leaves_the_rest() {
36623680
);
36633681
}
36643682

3683+
// **The ask this test needed is here** — `index_everything` reads a file through its timeline, so a class behind
3684+
// another file's macro body is scoped by the session (the defect and its reproducer are in `docs/indexing-review.md`
3685+
// §6, and `a_keystroke_in_a_body_reads_the_file_through_its_timeline` pins the keystroke half).
3686+
/// **Two fixtures with one name get two directories** — the state that made a failing run keep failing.
3687+
///
3688+
/// The sessions in this file write their summaries under `<fixture>/.cppls`, and the fixtures used to share one tree per
3689+
/// name. [`std::fs::remove_dir_all`]'s failure was ignored, so a directory that could not be removed was read as the new
3690+
/// run's state: a summary under the name of a file the fixture had just rewritten, answered with the old reading, and
3691+
/// nothing in the run could clear it. The name now carries the process id and a serial, which is what this pins.
3692+
#[test]
3693+
fn a_fixture_directory_is_unique_to_its_run() {
3694+
let first = a_fresh_fixture_directory("cppls-session-tests", "shared-name");
3695+
let second = a_fresh_fixture_directory("cppls-session-tests", "shared-name");
3696+
assert_ne!(
3697+
first, second,
3698+
"two fixtures of one name never share a directory"
3699+
);
3700+
3701+
// **And a directory the name happens to match is not read**: the fixture's own directory is a new one, so a stale
3702+
// `.cppls` beside it (or in it) belongs to nobody.
3703+
std::fs::create_dir_all(first.join(".cppls")).expect("the stale cache");
3704+
std::fs::write(first.join(".cppls").join("stale"), b"an older reading")
3705+
.expect("the stale cache");
3706+
let third = a_fresh_fixture_directory("cppls-session-tests", "shared-name");
3707+
assert_ne!(third, first, "a stale directory is not reused");
3708+
assert!(
3709+
!third.join(".cppls").exists(),
3710+
"and its cache is not handed over"
3711+
);
3712+
let _ = std::fs::remove_dir_all(&first);
3713+
let _ = std::fs::remove_dir_all(&third);
3714+
}
3715+
36653716
#[test]
3666-
// **Ignored while the refined reading is being fixed** — the defect and its reproducer are in `docs/indexing-review.md`
3667-
// §6: refining a file makes three `checks` fixtures see a reading their rules decline, so `index_everything` does not
3668-
// ask for it yet, and this test needs the ask. The assertion is right; the behaviour is what is wrong.
36693717
fn a_class_scoped_by_another_files_macro_body_is_scoped_through_the_session() {
36703718
// The store's second pass, on the closure a **session** builds one file at a time. MSVC's STL is
36713719
// written this way: `<vector>` opens `std` with `_STD_BEGIN`, whose replacement list is in `yvals_core.h`,

‎docs/buglist.md‎

Lines changed: 66 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1074,3 +1074,69 @@ The real project's shape is `std::counting_semaphore<4>` (its parameter `ptrdiff
10741074
`test_cpp:maybe(5).transform([](int value) { return value * 2; }).value_or(0)`: the hover itself says the type of the
10751075
chain is unknown, so `value_or` cannot resolve. `std::optional::transform` returns `auto`, so the answer needs the
10761076
deduction through the member template — the item `docs/next.md` §2 already names.
1077+
1078+
## 33. A fixture that shares one directory turns one failure into every failure
1079+
1080+
Reported: `session::tests::a_class_scoped_by_another_files_macro_body_is_scoped_through_the_session` fails on every run
1081+
with ``Unknown(NotDeclaredHere("studio::Widget"))`` at `session/tests.rs:3735`.
1082+
1083+
### What the test does and what it needs
1084+
1085+
`Project::new("body-pass")` writes three files — `open.h` defines `_STD_BEGIN`/`_STD_END`, `widget.h` opens `std`
1086+
through them, `main.cpp` includes `widget.h` — indexes, then **rewrites `open.h`** to open `studio` and indexes again:
1087+
`std::Widget` must be gone and `studio::Widget` must be found. The failure is the second half: the summary of
1088+
`widget.h` answered the **plain** (file-scope) reading, so the class is under no namespace at all.
1089+
1090+
The test's own comment (added in `cc69aa9b`) says it is "**ignored** while the refined reading is being fixed … the
1091+
assertion is right; the behaviour is what is wrong" — and there is **no `#[ignore]`**: the note was written for a
1092+
behaviour that later landed (`a1bbfaf`, `4e02cee`), and the attribute was never added, so the comment is stale in the
1093+
other direction.
1094+
1095+
### The part that is reproducible: the fixture's directory was shared
1096+
1097+
Every session fixture in that file used one tree per name — `%TEMP%\cppls-session-disk\body-pass` — and the cleanup
1098+
was `let _ = std::fs::remove_dir_all(&root);`. **An ignored failure is a stale store**: the sessions write their
1099+
summaries under `<fixture>/.cppls`, so a directory that could not be removed on Windows (a file held open by another
1100+
process, a scanner, or a run that aborted before its `Drop`) is read as the *next* run's state. A summary keyed by the
1101+
text of `widget.h` — which the test rewrites only through its **include**, not its own bytes — then answers the old
1102+
reading, and nothing a rerun does can clear it. That is precisely "always fails", and it is invisible on a machine whose
1103+
directory happens to be removable.
1104+
1105+
Measured with the current tree: the test passes alone, in the whole `session::` group, in the full lib suite (751
1106+
tests), cold and warm. So the first failing run was not reproduced here; what was found is the mechanism by which the
1107+
*result* of such a run survives every rerun.
1108+
1109+
### Fixed
1110+
1111+
* `a_fresh_fixture_directory` — the name, the process id and a serial — used by `Memory::new` and `Project::new`. A
1112+
stale directory is now unreachable rather than merely unlikely, and two concurrent `cargo test` runs cannot delete
1113+
each other's files.
1114+
* The stale "ignored" note is replaced with what the test pins, and `a_fixture_directory_is_unique_to_its_run` pins the
1115+
two properties that matter: two fixtures of one name get different directories, and an existing directory with
1116+
`.cppls` in it is not reused.
1117+
1118+
### Tried and rejected: drain the asks inside `index_everything`
1119+
1120+
The obvious fix — after the index queue and the cook queue are empty, drain `refinements_wanted()` in the same loop,
1121+
so that "index everything" means [`Session::pending_work`] is zero — was written, measured and **reverted**. It makes
1122+
this test pass, and it breaks two that pin the deferral:
1123+
1124+
```text
1125+
a_body_edit_does_not_cost_the_program_again "the reader has a file open, so there is a program to read"
1126+
a_program_is_read_when_the_reader_stops_editing "nothing read the program while the reader was typing: []"
1127+
```
1128+
1129+
A batch caller reading a file is **not** the reader stopping to have its program read; the unit read is deferred to the
1130+
pump's idle step on purpose, and a drain here consumes the work those two tests assert is still owed. So the ask stays
1131+
where it is, and the ordering question below stays open.
1132+
1133+
### What is still open
1134+
1135+
If the failure comes back with a fresh directory, the cause is the ordering the original note names: `index_everything`
1136+
reads a file as its **own root** even when the file renders through another file's macro body, so a header whose
1137+
include moved is indexed from its own text. The ask for the rendering is `docs/indexing-review.md` §6's item, and
1138+
`a_keystroke_in_a_body_reads_the_file_through_its_timeline` is the sibling that already passes — the difference
1139+
between the two is which of them the pump got to before the answer was asked for.
1140+
1141+
The same fixed-directory pattern is still in `fuzzy.rs`, `index/watch/tests`, `index/store/tests`, `index/worklist`,
1142+
`include/mod.rs` and `summary`'s round-trip test — none of them reported failing, and each is the same hazard.

‎docs/next.md‎

Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -179,6 +179,30 @@ From `docs/hover-render.md` §3, in the order they are likely to be met:
179179
layer answers Unknown while `members_of` resolves it.
180180
* **An `auto` return of a template member** (`optional::transform`) — the deduction item of §2.
181181

182+
## 8. The session fixtures' shared directory — fixed, and what would explain a repeat
183+
184+
`docs/buglist.md` §33: the session fixtures wrote their `.cppls` into one tree per **name**
185+
(`%TEMP%\cppls-session-disk\body-pass`) and ignored `remove_dir_all`'s failure, so a directory that could not be
186+
removed became the next run's state — a summary of `widget.h` keyed by its own (unchanged) bytes, answering the old
187+
namespace, for every rerun.
188+
189+
Fixed: `a_fresh_fixture_directory` gives each fixture the name, the process id and a serial; `Memory::new` and
190+
`Project::new` use it; `session::tests::a_fixture_directory_is_unique_to_its_run` pins it.
191+
192+
If `a_class_scoped_by_another_files_macro_body_is_scoped_through_the_session` fails again **with a fresh directory**, the
193+
cause is the ordering its original note names: `index_everything` indexes a header as its own root, so a header whose
194+
`#include` moved is read from its own text rather than from the rendering. That is `docs/indexing-review.md` §6's "ask",
195+
and it is the next thing to do here — with the failing assertion as the test.
196+
197+
**The obvious fix was tried and reverted**: draining `refinements_wanted()` at the end of `index_everything` so that
198+
`pending_work()` is zero on return. It makes the failing test pass and breaks
199+
`a_body_edit_does_not_cost_the_program_again` and `a_program_is_read_when_the_reader_stops_editing` — the unit read is
200+
deferred to the pump's idle step on purpose, and a drain consumes work those two assert is still owed. The measurements
201+
are in `docs/buglist.md` §33, so the next attempt starts from what it already knows.
202+
203+
The remaining fixed-directory helpers (`fuzzy`, `watch`, `store`, `worklist`, `include`, `summary`) are the same hazard
204+
and the same one-line change.
205+
182206
## The order I would take
183207

184208
1. §1.0 re-measure (30 minutes, decides whether §1 is a bug or a stale binary).

0 commit comments

Comments
 (0)