Files
clice/tests/integration/test_import_completion.py
ykiko e239b0d32c feat: smart PCH rebuild, #include/import completion, rapid-edit robustness (#394)
## Summary

### Preamble completeness check
- `is_preamble_complete()` in `scan.cpp`: checks whether
`#include`/`import`/`export module` directives in the preamble region
are syntactically complete (have closing `>`/`"`/`;`)
- `ensure_pch` defers PCH rebuild when preamble is incomplete (user
still typing), reuses old PCH instead of failing

### #include / import completion
- Master intercepts completion requests in `#include "..."` / `#include
<...>` / `import ...` contexts before forwarding to worker
- `complete_include()`: searches include paths (from compile args via
`SearchConfig`) using `DirListingCache`, supports
quoted/angled/multi-level paths
- `complete_import()`: filters `path_to_module` map by prefix
- Word boundary checks prevent false matches (e.g. `important` not
treated as `import`)

### Detached compile task (rapid-edit fix)
- Compile operations (`ensure_deps` + `send_stateful` +
`publish_diagnostics`) run as detached tasks via `loop.schedule()`,
independent of the LSP request coroutine chain
- LSP `$/cancelRequest` can no longer kill in-flight compilations —
previously, cancellation would destroy the `ensure_compiled` coroutine
frame, leaving `doc.compiling` permanently set and hanging all
subsequent requests
- `CompileGuard` RAII ensures `doc.compiling` is always cleaned up even
if the detached task fails
- Stale feature requests (where `ast_dirty` became true after compile
finished) are dropped before forwarding to worker

### Other fixes
- `signal(SIGPIPE, SIG_IGN)` on POSIX: prevents server crash when LSP
client disconnects mid-write
- `CompilationUnitRef::file_path()` / `deps()`: null-check
`FileEntryRef` to prevent segfault on invalid FileID
- `stateless_worker.cpp`: log BuildPCH diagnostic errors for
debuggability
- Default worker counts changed to 2 stateful + 3 stateless
- `logging_dir` default changed to `.clice/logs` in config

### Tests
- 19 unit tests for `is_preamble_complete` (incomplete `#include`,
`import`, `export module`, mixed cases)
- Integration tests: `test_include_completion.py` (5 tests),
`test_import_completion.py` (4 tests), `test_rapid_edit.py` (2 tests),
`test_pch.py` (4 new tests)
- Smoke test: `rapid_edit.jsonl` — recorded VSCode session with 40 rapid
edits + 61 cancel requests

## Test plan
- [x] Unit tests: 463 passed
- [x] Integration tests: 104 passed
- [x] Smoke test (rapid_edit.jsonl): PASS
- [x] Manual VSCode testing with `#include <iostream>` project

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

---------

Co-authored-by: Claude Opus 4.6 (1M context) <noreply@anthropic.com>
2026-04-06 14:49:09 +08:00

143 lines
5.1 KiB
Python

"""Integration tests for import completion and buffer-aware module dependency features."""
import asyncio
import pytest
from lsprotocol.types import (
CompletionParams,
DidChangeTextDocumentParams,
HoverParams,
Position,
TextDocumentContentChangeWholeDocument,
TextDocumentIdentifier,
VersionedTextDocumentIdentifier,
)
def _doc(uri: str) -> TextDocumentIdentifier:
return TextDocumentIdentifier(uri=uri)
@pytest.mark.workspace("modules/chained_modules")
async def test_import_completion_basic(client, workspace):
"""Import completion should list known modules."""
# First open mod_a to ensure it's scanned and module A is registered.
await client.open_and_wait(workspace / "mod_a.cppm")
# Open mod_b and change its content to an incomplete import line.
uri_b, _ = client.open(workspace / "mod_b.cppm")
client.text_document_did_change(
DidChangeTextDocumentParams(
text_document=VersionedTextDocumentIdentifier(uri=uri_b, version=1),
content_changes=[TextDocumentContentChangeWholeDocument(text="import ")],
)
)
result = await client.text_document_completion_async(
CompletionParams(
text_document=_doc(uri_b),
position=Position(line=0, character=7),
)
)
assert result is not None
items = result.items if hasattr(result, "items") else result
labels = [item.label for item in items]
assert "A" in labels, f"Expected 'A' in completion labels, got: {labels}"
@pytest.mark.workspace("modules/chained_modules")
async def test_import_completion_with_prefix(client, workspace):
"""Import completion with prefix should filter to matching modules."""
# Open mod_a to register module A.
await client.open_and_wait(workspace / "mod_a.cppm")
# Open mod_b and type 'import A' (with prefix).
uri_b, _ = client.open(workspace / "mod_b.cppm")
client.text_document_did_change(
DidChangeTextDocumentParams(
text_document=VersionedTextDocumentIdentifier(uri=uri_b, version=1),
content_changes=[TextDocumentContentChangeWholeDocument(text="import A")],
)
)
result = await client.text_document_completion_async(
CompletionParams(
text_document=_doc(uri_b),
position=Position(line=0, character=8),
)
)
assert result is not None
items = result.items if hasattr(result, "items") else result
labels = [item.label for item in items]
assert "A" in labels, f"Expected 'A' in completion labels, got: {labels}"
@pytest.mark.workspace("modules/dotted_module_name")
async def test_import_completion_dotted_names(client, workspace):
"""Import completion should return dotted module names like my.app and my.io."""
# Open both module files to register them.
await client.open_and_wait(workspace / "io.cppm")
await client.open_and_wait(workspace / "app.cppm")
# Change app.cppm to an incomplete import with dotted prefix.
uri_app, _ = client.open(workspace / "app.cppm")
client.text_document_did_change(
DidChangeTextDocumentParams(
text_document=VersionedTextDocumentIdentifier(uri=uri_app, version=1),
content_changes=[TextDocumentContentChangeWholeDocument(text="import my.")],
)
)
result = await client.text_document_completion_async(
CompletionParams(
text_document=_doc(uri_app),
position=Position(line=0, character=10),
)
)
assert result is not None
items = result.items if hasattr(result, "items") else result
labels = [item.label for item in items]
assert "my.app" in labels or "my.io" in labels, (
f"Expected dotted module names in completion labels, got: {labels}"
)
@pytest.mark.workspace("modules/consumer_imports_module")
async def test_buffer_aware_module_deps(client, workspace):
"""Adding import in buffer (unsaved) should still build the needed PCM."""
# Open the module file first so it gets scanned.
await client.open_and_wait(workspace / "math.cppm")
# Open main.cpp with new content that imports Math (simulating unsaved edit).
uri, _ = client.open(workspace / "main.cpp")
client.text_document_did_change(
DidChangeTextDocumentParams(
text_document=VersionedTextDocumentIdentifier(uri=uri, version=1),
content_changes=[
TextDocumentContentChangeWholeDocument(
text="import Math;\nint x = add(1, 2);\n"
)
],
)
)
# Trigger compilation via hover (pull-based model).
event = client.wait_for_diagnostics(uri)
await client.text_document_hover_async(
HoverParams(
text_document=_doc(uri),
position=Position(line=0, character=0),
)
)
# Wait for diagnostics.
await asyncio.wait_for(event.wait(), timeout=60.0)
diags = client.diagnostics.get(uri, [])
# Should have no errors if Math PCM was built successfully from buffer scan.
errors = [d for d in diags if d.severity == 1]
assert len(errors) == 0, f"Expected no errors, got: {errors}"