Files
clice/tests/integration/test_modules.py
ykiko e24eff6c16 refactor: pull-based compilation for document lifecycle (#385)
## Summary

Replace the push-based compilation model with a pull-based (lazy) model
where compilation is driven entirely by feature requests.

### Server core (`master_server.cpp/h`)
- **Remove** `schedule_build()`, `run_build_drain()`, debounce timers,
and `DocumentState` flags (`build_running`, `build_requested`,
`drain_scheduled`)
- **Remove** `debounce_ms` config field
- `didOpen`/`didChange` only update `DocumentState` and mark `ast_dirty`
— no compilation triggered
- `didSave` marks dependent docs dirty via `CompileGraph::update()`,
invalidates PCH hashes, marks **all** open documents `ast_dirty` (header
saves), and queues background indexing
- **Implement** `ensure_compiled(path_id)` — the pull-based entry point
called by `forward_stateful()`/`forward_stateless()` before every
feature request:
  1. Fast-path if `!ast_dirty`
  2. Compile C++20 module deps via `compile_graph->compile_deps()`
  3. Build/reuse PCH via `ensure_pch()` (only attach on success)
  4. Send `CompileParams` to stateful worker
  5. Publish diagnostics, clear dirty, schedule indexing
  6. Generation mismatch → return `false`, keep dirty for retry
- `forward_stateless()` now also calls `compile_graph->compile_deps()`
before stateless requests (completion/signatureHelp)
- Move module-implementation-unit implicit dependency handling into
`resolve_fn` (was duplicated in `run_build_drain` and `ensure_compiled`)

### CompileGraph (`compile_graph.cpp/h`)
- **Add** `compile_deps(path_id)` — compiles all transitive module
dependencies but NOT the file itself (used for plain .cpp files that
`import` modules)
- Unify `compile`/`compile_deps` via `compile_impl(path_id, ancestors,
dispatch_self)` parameter
- `compile_deps` compiles dependencies concurrently via `when_all`
- Extract `finish()` lambda to deduplicate `compiling=false;
completion->set()` cleanup across all exit paths
- Use `std::ranges::remove` instead of legacy `std::remove`

### Test infrastructure (`conftest.py`)
- `open_and_wait()` now sends a hover request to trigger
`ensure_compiled()` (pull-based model requires a feature request to
compile)
- Fix URI handling: send percent-encoded URI on the wire, normalize for
internal lookups, store diagnostics under both raw and normalized URI
keys
- Add `_normalize_uri()` helper using `urllib.parse.unquote`

### Integration tests
- Update all tests for pull-based model: no more waiting on `didOpen`
diagnostics
- `_wait_for_index()` sends hover to trigger compilation before polling
`workspace/symbol`
- `test_hover_save_close` simplified — hover directly triggers
compilation
- `test_save_recompile` and `test_pch_*` wait for fresh diagnostics
after hover-triggered recompilation

### Unit tests (`compile_graph_tests.cpp`)
- Extract `compiled`/`graph` as TEST_SUITE members with
`std::optional<CompileGraph>`
- Extract `execute(callback)` helper to deduplicate event_loop
boilerplate
- Add 8 new `compile_deps` tests: no-deps, single dep, chain, diamond,
failure, plain-cpp, concurrent dedup, resolve-once
- Remove redundant `inline` on file-scope helpers

## Test plan
- [x] Unit tests: 426 passed, 5 skipped
- [x] Smoke tests: 1/1 passed
- [x] Integration tests: 69 passed, 0 failed, no hangs

🤖 Generated with [Claude Code](https://claude.com/claude-code)

---------

Co-authored-by: Claude Opus 4.6 (1M context) <noreply@anthropic.com>
2026-04-04 02:35:17 +08:00

278 lines
11 KiB
Python

"""Integration tests for C++20 module support."""
import asyncio
import shutil
import pytest
from tests.conftest import generate_cdb
from lsprotocol.types import (
DidCloseTextDocumentParams,
DidOpenTextDocumentParams,
HoverParams,
Position,
TextDocumentIdentifier,
TextDocumentItem,
)
@pytest.mark.workspace("modules/single_module_no_deps")
async def test_single_module_no_deps(client, workspace):
uri, _ = await client.open_and_wait(workspace / "mod_a.cppm")
diags = client.diagnostics.get(uri, [])
assert len(diags) == 0, f"Expected no diagnostics, got: {diags}"
@pytest.mark.workspace("modules/chained_modules")
async def test_chained_modules(client, workspace):
"""Opening mod_b that imports mod_a should trigger dependency compilation."""
uri, _ = await client.open_and_wait(workspace / "mod_b.cppm")
diags = client.diagnostics.get(uri, [])
assert len(diags) == 0, f"Expected no diagnostics, got: {diags}"
@pytest.mark.workspace("modules/diamond_modules")
async def test_diamond_modules(client, workspace):
uri, _ = await client.open_and_wait(workspace / "top.cppm")
diags = client.diagnostics.get(uri, [])
assert len(diags) == 0, f"Expected no diagnostics, got: {diags}"
@pytest.mark.workspace("modules/dotted_module_name")
async def test_dotted_module_name(client, workspace):
uri, _ = await client.open_and_wait(workspace / "app.cppm")
diags = client.diagnostics.get(uri, [])
assert len(diags) == 0, f"Expected no diagnostics, got: {diags}"
@pytest.mark.workspace("modules/module_implementation_unit")
async def test_module_implementation_unit(client, workspace):
"""Implementation unit (module M; without export) should compile using the interface PCM."""
uri, _ = await client.open_and_wait(workspace / "greeter_impl.cpp")
diags = client.diagnostics.get(uri, [])
assert len(diags) == 0, f"Expected no diagnostics, got: {diags}"
@pytest.mark.workspace("modules/consumer_imports_module")
async def test_consumer_imports_module(client, workspace):
"""A regular .cpp that imports a module should get PCM deps compiled first."""
uri, _ = await client.open_and_wait(workspace / "main.cpp")
diags = client.diagnostics.get(uri, [])
assert len(diags) == 0, f"Expected no diagnostics, got: {diags}"
@pytest.mark.workspace("modules/module_partitions")
async def test_module_partitions(client, workspace):
"""Partitions should be compiled in correct dependency order."""
uri, _ = await client.open_and_wait(workspace / "lib.cppm")
diags = client.diagnostics.get(uri, [])
assert len(diags) == 0, f"Expected no diagnostics, got: {diags}"
@pytest.mark.workspace("modules/partition_interface")
async def test_partition_interface(client, workspace):
uri, _ = await client.open_and_wait(workspace / "primary.cppm")
diags = client.diagnostics.get(uri, [])
assert len(diags) == 0, f"Expected no diagnostics, got: {diags}"
@pytest.mark.workspace("modules/partition_chain")
async def test_partition_chain(client, workspace):
uri, _ = await client.open_and_wait(workspace / "sys.cppm")
diags = client.diagnostics.get(uri, [])
assert len(diags) == 0, f"Expected no diagnostics, got: {diags}"
@pytest.mark.workspace("modules/re_export")
async def test_re_export(client, workspace):
"""Re-exported symbols (export import) should be accessible through the wrapper."""
uri, _ = await client.open_and_wait(workspace / "user.cppm")
diags = client.diagnostics.get(uri, [])
assert len(diags) == 0, f"Expected no diagnostics, got: {diags}"
@pytest.mark.workspace("modules/export_block")
async def test_export_block(client, workspace):
uri, _ = await client.open_and_wait(workspace / "consumer.cppm")
diags = client.diagnostics.get(uri, [])
assert len(diags) == 0, f"Expected no diagnostics, got: {diags}"
@pytest.mark.workspace("modules/global_module_fragment")
async def test_global_module_fragment(client, workspace):
uri, _ = await client.open_and_wait(workspace / "gmf.cppm")
diags = client.diagnostics.get(uri, [])
assert len(diags) == 0, f"Expected no diagnostics, got: {diags}"
@pytest.mark.workspace("modules/private_module_fragment")
async def test_private_module_fragment(client, workspace):
uri, _ = await client.open_and_wait(workspace / "priv.cppm")
diags = client.diagnostics.get(uri, [])
assert len(diags) == 0, f"Expected no diagnostics, got: {diags}"
@pytest.mark.workspace("modules/export_namespace")
async def test_export_namespace(client, workspace):
uri, _ = await client.open_and_wait(workspace / "calc.cppm")
diags = client.diagnostics.get(uri, [])
assert len(diags) == 0, f"Expected no diagnostics, got: {diags}"
@pytest.mark.workspace("modules/gmf_with_import")
async def test_gmf_with_import(client, workspace):
uri, _ = await client.open_and_wait(workspace / "combined.cppm")
diags = client.diagnostics.get(uri, [])
assert len(diags) == 0, f"Expected no diagnostics, got: {diags}"
@pytest.mark.workspace("modules/independent_modules")
async def test_independent_modules(client, workspace):
uri_x, _ = await client.open_and_wait(workspace / "x.cppm")
diags_x = client.diagnostics.get(uri_x, [])
assert len(diags_x) == 0, f"Expected no diagnostics for X, got: {diags_x}"
uri_y, _ = await client.open_and_wait(workspace / "y.cppm")
diags_y = client.diagnostics.get(uri_y, [])
assert len(diags_y) == 0, f"Expected no diagnostics for Y, got: {diags_y}"
@pytest.mark.workspace("modules/template_export")
async def test_template_export(client, workspace):
uri, _ = await client.open_and_wait(workspace / "use_tmpl.cppm")
diags = client.diagnostics.get(uri, [])
assert len(diags) == 0, f"Expected no diagnostics, got: {diags}"
@pytest.mark.workspace("modules/class_export_and_inheritance")
async def test_class_export_and_inheritance(client, workspace):
uri, _ = await client.open_and_wait(workspace / "circle.cppm")
diags = client.diagnostics.get(uri, [])
assert len(diags) == 0, f"Expected no diagnostics, got: {diags}"
async def test_save_recompile(client, test_data_dir, tmp_path):
"""Closing and reopening a modified module file should recompile without errors."""
src = test_data_dir / "modules" / "save_recompile"
for f in src.iterdir():
if f.is_file():
shutil.copy2(f, tmp_path / f.name)
generate_cdb(tmp_path)
await client.initialize(tmp_path)
# Open and compile Mid (which triggers Leaf PCM build).
mid_uri, _ = await client.open_and_wait(tmp_path / "mid.cppm")
diags = client.diagnostics.get(mid_uri, [])
assert len(diags) == 0
# Open Leaf and trigger compilation via hover.
leaf_uri, _ = client.open(tmp_path / "leaf.cppm")
await client.text_document_hover_async(
HoverParams(
text_document=TextDocumentIdentifier(uri=leaf_uri),
position=Position(line=0, character=0),
)
)
# Close Leaf, modify on disk, and reopen with new content.
client.text_document_did_close(
DidCloseTextDocumentParams(text_document=TextDocumentIdentifier(uri=leaf_uri))
)
new_content = "export module Leaf;\nexport int leaf() { return 100; }\n"
(tmp_path / "leaf.cppm").write_text(new_content)
client.text_document_did_open(
DidOpenTextDocumentParams(
text_document=TextDocumentItem(
uri=leaf_uri, language_id="cpp", version=1, text=new_content
)
)
)
# Send hover to trigger recompilation via pull-based model.
event = client.wait_for_diagnostics(leaf_uri)
await client.text_document_hover_async(
HoverParams(
text_document=TextDocumentIdentifier(uri=leaf_uri),
position=Position(line=0, character=0),
)
)
await asyncio.wait_for(event.wait(), timeout=30.0)
diags = client.diagnostics.get(leaf_uri, [])
assert len(diags) == 0, f"Expected no diagnostics after save, got: {diags}"
@pytest.mark.workspace("modules/module_compile_error")
async def test_module_compile_error(client, workspace):
uri, _ = await client.open_and_wait(workspace / "bad.cppm")
diags = client.diagnostics.get(uri, [])
assert len(diags) > 0, "Expected diagnostics for undefined symbol"
assert any(d.range.start.line == 4 and d.severity == 1 for d in diags), (
f"Expected an error diagnostic on line 4, got: {diags}"
)
@pytest.mark.workspace("modules/deep_chain")
async def test_deep_chain(client, workspace):
"""A 5-level module chain (m1->m2->...->m5) should compile correctly."""
uri, _ = await client.open_and_wait(workspace / "m5.cppm")
diags = client.diagnostics.get(uri, [])
assert len(diags) == 0, f"Expected no diagnostics, got: {diags}"
@pytest.mark.workspace("modules/partition_with_gmf")
async def test_partition_with_gmf(client, workspace):
uri, _ = await client.open_and_wait(workspace / "cfg.cppm")
diags = client.diagnostics.get(uri, [])
assert len(diags) == 0, f"Expected no diagnostics, got: {diags}"
@pytest.mark.workspace("modules/partition_with_external_import")
async def test_partition_with_external_import(client, workspace):
uri, _ = await client.open_and_wait(workspace / "app.cppm")
diags = client.diagnostics.get(uri, [])
assert len(diags) == 0, f"Expected no diagnostics, got: {diags}"
@pytest.mark.workspace("modules/hover_on_imported_symbol")
async def test_hover_on_imported_symbol(client, workspace):
"""Hover on a symbol imported from a module should return type info."""
uri, _ = await client.open_and_wait(workspace / "use.cpp")
diags = client.diagnostics.get(uri, [])
assert len(diags) == 0, f"Expected no diagnostics, got: {diags}"
hover = await client.text_document_hover_async(
HoverParams(
text_document=TextDocumentIdentifier(uri=uri),
position=Position(line=3, character=11),
)
)
assert hover is not None, "Hover on imported symbol should return info"
assert hover.contents is not None
@pytest.mark.workspace("modules/no_modules_plain_cpp")
async def test_no_modules_plain_cpp(client, workspace):
"""Plain .cpp with no modules should compile normally (CompileGraph null path)."""
uri, _ = await client.open_and_wait(workspace / "plain.cpp")
diags = client.diagnostics.get(uri, [])
assert len(diags) == 0, f"Expected no diagnostics, got: {diags}"
@pytest.mark.workspace("modules/circular_module_dependency")
async def test_circular_module_dependency(client, workspace):
"""Circular module imports should not hang the server.
The CompileGraph's cycle detection should prevent deadlock. We verify
the server remains responsive by opening a non-cyclic file afterwards.
"""
client.open(workspace / "cycle_a.cppm")
await asyncio.sleep(5.0)
uri_ok, _ = await client.open_and_wait(workspace / "ok.cppm")
diags = client.diagnostics.get(uri_ok, [])
assert len(diags) == 0, (
f"Non-cyclic module should compile fine after cycle attempt, got: {diags}"
)