diff --git a/.github/workflows/ci.yml b/.github/workflows/ci.yml index f41af53..dd64cd9 100644 --- a/.github/workflows/ci.yml +++ b/.github/workflows/ci.yml @@ -39,10 +39,10 @@ jobs: components: rustfmt, clippy, llvm-tools-preview # The profiler's .NET engine and the fixture its e2e tests profile build in - # `make test` (net8.0, rolled forward to this SDK's runtime). [PROF-TESTING] + # `make test` (net8.0, rolled forward to the SDK pinned in global.json). [PROF-TESTING] - uses: actions/setup-dotnet@a98b56852c35b8e3190ac28c8c2271da59106c68 # v6.0.0 with: - dotnet-version: "10.0.x" + global-json-file: global.json # crates/lsp-profiler/build.rs generates the wire model from # docs/models/profiler.td with typeDiagram. [PROF-WIRE-GEN] @@ -108,6 +108,20 @@ jobs: - name: Build run: make build + # release.yml runs this script only on a tag; packaging one runtime id + # here keeps a broken script from surfacing only at release time. + # [REL-CONSUMER-ASSETS] + - name: Release consumer assets (linux-x64) + run: | + set -eu + sh scripts/release-assets.sh ci "${RUNNER_TEMP}/assets" linux-x64 + tar -tzf "${RUNNER_TEMP}/assets/lsp-profiler-dotnet-engine-ci-linux-x64.tar.gz" | grep -qx './lsp-profiler-dotnet-engine' + kit=$(tar -tzf "${RUNNER_TEMP}/assets/lspkit-consumer-kit-ci.tar.gz") + for want in dotnet/fixtures/HotLeakApp/Program.cs docs/specs/LSP-PROFILER-SPEC.md docs/specs/LSP-PULL-SPEC.md test-support/state.rs scripts/typediagram/run-cli.mjs; do + printf '%s\n' "$kit" | grep -qx "lspkit-consumer-kit-ci/$want" + done + if printf '%s\n' "$kit" | grep -q '/bin/\|/obj/'; then echo "kit ships build output"; exit 1; fi + - name: Upload build artifacts uses: actions/upload-artifact@cf430e030ddbb5b0abf93d22962f4752f3646cd9 # v7.0.2 with: diff --git a/.github/workflows/release.yml b/.github/workflows/release.yml index 51115f3..3d7c2c2 100644 --- a/.github/workflows/release.yml +++ b/.github/workflows/release.yml @@ -64,6 +64,12 @@ jobs: - name: Install npm + typeDiagram run: npm install -g npm@11.21.1 typediagram@0.11.0 + # Builds the .NET profiler engine release assets (net8.0, rolled + # forward to the SDK pinned in global.json). [REL-CONSUMER-ASSETS] + - uses: actions/setup-dotnet@a98b56852c35b8e3190ac28c8c2271da59106c68 # v6.0.0 + with: + global-json-file: global.json + - name: Install version stamper and vuln gate uses: taiki-e/install-action@5488ac5a951645ce95062214902ac4b6bc1a1037 # v2.87.32 with: @@ -138,6 +144,13 @@ jobs: npm run build --prefix packages/lsp-profiler-ui npm pack ./packages/lsp-profiler-ui --pack-destination "${DIST}" + # What consumers would otherwise need a source checkout for: the .NET + # engine per runtime id, and the fixtures, specs and test-support kit. + # They land in DIST, so SHA256SUMS, cosign and the attestations cover + # them like every other asset. [REL-CONSUMER-ASSETS] + - name: Package consumer assets + run: sh scripts/release-assets.sh "${GITHUB_REF_NAME#v}" "${DIST}" + # One CycloneDX SBOM over the whole dependency graph. [SWR-SEC-SBOM] - name: Generate SBOM uses: anchore/sbom-action@66cbf4bc1f1c0d2edc94016e65bc221b6bb0ad6c # v0.24.3 diff --git a/Cargo.lock b/Cargo.lock index abe83d3..82ebf48 100644 --- a/Cargo.lock +++ b/Cargo.lock @@ -9,7 +9,7 @@ source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "5a15f179cd60c4584b8a8c596927aadc462e27f2ca70c04e0071964a73ba7a75" dependencies = [ "cfg-if", - "getrandom 0.3.4", + "getrandom", "once_cell", "version_check", "zerocopy", @@ -30,21 +30,6 @@ version = "0.2.21" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "683d7910e743518b0e34f1186f92494becacb047c7b6bf616c96772180fef923" -[[package]] -name = "android_system_properties" -version = "0.1.5" -source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "819e7219dbd41043ac279b19830f2efc897156490d7fd6ea916720117ee66311" -dependencies = [ - "libc", -] - -[[package]] -name = "anyhow" -version = "1.0.104" -source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "330a5ed07fa54e4702c9d6c4174f74427fc0ef6e214bbd677ae50a5099946470" - [[package]] name = "arrayvec" version = "0.7.8" @@ -68,12 +53,6 @@ version = "1.5.0" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "c08606f8c3cbf4ce6ec8e28fb0014a2c086708fe954eaa885384a6165172e7e8" -[[package]] -name = "base64" -version = "0.23.1" -source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "ac07cdecf99051d9a5238b80f35af32cdeba5b336e55d957b318b50137e18da5" - [[package]] name = "bitflags" version = "1.3.2" @@ -86,27 +65,12 @@ version = "2.11.1" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "c4512299f36f043ab09a583e57bceb5a5aab7a73db1805848e8fef3c9e8c78b3" -[[package]] -name = "block-buffer" -version = "0.12.1" -source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "d2f6c7dbe95a6ed67ad9f18e57daf93a2f034c524b99fd2b76d18fdfeb6660aa" -dependencies = [ - "hybrid-array", -] - [[package]] name = "boxcar" version = "0.2.14" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "36f64beae40a84da1b4b26ff2761a5b895c12adc41dc25aaee1c4f2bbfe97a6e" -[[package]] -name = "bumpalo" -version = "3.20.2" -source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "5d20789868f4b01b2f2caec9f5c4e0213b41e3e5702a50157d699ae31ced2fcb" - [[package]] name = "bytemuck" version = "1.25.2" @@ -135,39 +99,6 @@ version = "1.0.4" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "9330f8b2ff13f34540b44e946ef35111825727b38d33286ef986142615121801" -[[package]] -name = "chrono" -version = "0.4.44" -source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "c673075a2e0e5f4a1dde27ce9dee1ea4558c7ffe648f576438a20ca1d2acc4b0" -dependencies = [ - "iana-time-zone", - "num-traits", - "serde", - "windows-link", -] - -[[package]] -name = "const-oid" -version = "0.10.3" -source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "be95d4a884358a21a199689323c29e9256b4a1b315e22f183d155422041d3a9a" - -[[package]] -name = "core-foundation-sys" -version = "0.8.7" -source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "773648b94d0e5d620f64f280777445740e61fe701025087ec8b57f45c791888b" - -[[package]] -name = "cpufeatures" -version = "0.3.1" -source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "5ca28b0ae3115b884660db4118d803791fd6756b6e88f39c0f3f7859060d7566" -dependencies = [ - "libc", -] - [[package]] name = "crossbeam-deque" version = "0.8.8" @@ -202,49 +133,6 @@ version = "0.8.21" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "d0a5c400df2834b80a4c3327b3aad3a4c4cd4de0629063962b03235697506a28" -[[package]] -name = "crypto-common" -version = "0.2.2" -source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "ce6e4c961d6cd6c9a86db418387425e8bdeaf05b3c8bc1411e6dca4c252f1453" -dependencies = [ - "hybrid-array", -] - -[[package]] -name = "darling" -version = "0.24.1" -source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "ed17f5901b6630b993ca003def43f2f8ef4014fc13b047b57aad617ff32bc2ec" -dependencies = [ - "darling_core", - "darling_macro", -] - -[[package]] -name = "darling_core" -version = "0.24.1" -source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "6837e2cf7485aaae18f86181d2f0e9a7ed297a025e220aeabf63fdebd3a2ddff" -dependencies = [ - "ident_case", - "proc-macro2", - "quote", - "strsim", - "syn 3.0.9", -] - -[[package]] -name = "darling_macro" -version = "0.24.1" -source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "2ac7135c3ef02b2f7833bbeb1be5ba7f966dcde8a87c6b87f65a778d71a02785" -dependencies = [ - "darling_core", - "quote", - "syn 3.0.9", -] - [[package]] name = "dashmap" version = "6.2.1" @@ -259,17 +147,6 @@ dependencies = [ "parking_lot_core", ] -[[package]] -name = "digest" -version = "0.11.3" -source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "f1dd6dbb5841937940781866fa1281a1ff7bd3bf827091440879f9994983d5c2" -dependencies = [ - "block-buffer", - "const-oid", - "crypto-common", -] - [[package]] name = "dispatch2" version = "0.3.1" @@ -291,12 +168,6 @@ dependencies = [ "syn 3.0.9", ] -[[package]] -name = "dyn-clone" -version = "1.0.20" -source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "d0881ea181b1df73ff77ffaaf9c7544ecc11e82fba9b5f27b262a3c73a332555" - [[package]] name = "either" version = "1.19.0" @@ -340,12 +211,6 @@ dependencies = [ "bitflags 1.3.2", ] -[[package]] -name = "foldhash" -version = "0.1.5" -source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "d9c4f5dac5e15c24eb999c26181a6ca40b39fe946cbe4c263c7209467bc83af2" - [[package]] name = "foldhash" version = "0.2.0" @@ -361,54 +226,12 @@ dependencies = [ "percent-encoding", ] -[[package]] -name = "futures" -version = "0.3.32" -source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "8b147ee9d1f6d097cef9ce628cd2ee62288d963e16fb287bd9286455b241382d" -dependencies = [ - "futures-channel", - "futures-core", - "futures-executor", - "futures-io", - "futures-sink", - "futures-task", - "futures-util", -] - -[[package]] -name = "futures-channel" -version = "0.3.34" -source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "b1f9e3d69d39e4862ffed03ed071a76f9a13ba1d9109d355b0f0aa6b15e393c4" -dependencies = [ - "futures-core", - "futures-sink", -] - [[package]] name = "futures-core" version = "0.3.34" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "92d699e522242e69e3003b94ecc1f960f3a5e015aa7c5d7486e65ad01dd94f5e" -[[package]] -name = "futures-executor" -version = "0.3.32" -source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "baf29c38818342a3b26b5b923639e7b1f4a61fc5e76102d4b1981c6dc7a7579d" -dependencies = [ - "futures-core", - "futures-task", - "futures-util", -] - -[[package]] -name = "futures-io" -version = "0.3.34" -source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "53c0fa8157de1303bfffdaa1cc2a673bfffb60102f76b0ef4441659124373fed" - [[package]] name = "futures-macro" version = "0.3.34" @@ -438,13 +261,9 @@ version = "0.3.34" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "0d50a92467f8ba5dd6e3ee5d4bd04d73ab2e4e1c44474a0674821dfce14b79bc" dependencies = [ - "futures-channel", "futures-core", - "futures-io", "futures-macro", - "futures-sink", "futures-task", - "memchr", "pin-project-lite", "slab", ] @@ -457,38 +276,16 @@ checksum = "899def5c37c4fd7b2664648c28120ecec138e4d395b459e5ca34f9cce2dd77fd" dependencies = [ "cfg-if", "libc", - "r-efi 5.3.0", + "r-efi", "wasip2", ] -[[package]] -name = "getrandom" -version = "0.4.2" -source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "0de51e6874e94e7bf76d726fc5d13ba782deca734ff60d5bb2fb2607c7406555" -dependencies = [ - "cfg-if", - "libc", - "r-efi 6.0.0", - "wasip2", - "wasip3", -] - [[package]] name = "hashbrown" version = "0.14.5" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "e5274423e17b7c9fc20b6e7e208532f9b19825d82dfd615708b70edd83df41f1" -[[package]] -name = "hashbrown" -version = "0.15.5" -source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "9229cfe53dfd69f0609a49f65461bd93001ea1ef889cd5529dd176593f5338a1" -dependencies = [ - "foldhash 0.1.5", -] - [[package]] name = "hashbrown" version = "0.17.1" @@ -497,7 +294,7 @@ checksum = "ed5909b6e89a2db4456e54cd5f673791d7eca6732202bbf2a9cc504fe2f9b84a" dependencies = [ "allocator-api2", "equivalent", - "foldhash 0.2.0", + "foldhash", ] [[package]] @@ -509,51 +306,6 @@ dependencies = [ "hashbrown 0.17.1", ] -[[package]] -name = "heck" -version = "0.5.0" -source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "2304e00983f87ffb38b55b444b5e3b60a884b5d30c0fca7d82fe33449bbe55ea" - -[[package]] -name = "humantime" -version = "2.4.0" -source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "15cdd26707701c53297e2fa6afb323d55fbc1d0810c3aec078ae3ef0424c3c15" - -[[package]] -name = "hybrid-array" -version = "0.4.15" -source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "27f864f10dfb56725ce5ce5472bc52252c8f93a4ab86327122cebf62c5f59a17" -dependencies = [ - "typenum", -] - -[[package]] -name = "iana-time-zone" -version = "0.1.65" -source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "e31bc9ad994ba00e440a8aa5c9ef0ec67d5cb5e5cb0cc7f8b744a35b389cc470" -dependencies = [ - "android_system_properties", - "core-foundation-sys", - "iana-time-zone-haiku", - "js-sys", - "log", - "wasm-bindgen", - "windows-core", -] - -[[package]] -name = "iana-time-zone-haiku" -version = "0.1.2" -source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "f31827a206f56af32e590ba56d5d2d085f558508192593743f16b2306495269f" -dependencies = [ - "cc", -] - [[package]] name = "icu_collections" version = "2.3.0" @@ -637,18 +389,6 @@ dependencies = [ "zerovec", ] -[[package]] -name = "id-arena" -version = "2.3.0" -source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "3d3067d79b975e8844ca9eb072e16b31c3c1c36928edf9c6789548c524d0d954" - -[[package]] -name = "ident_case" -version = "1.0.1" -source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "b9e0384b61958566e926dc50660321d12159025e767c18e043daf26b70104c39" - [[package]] name = "idna" version = "1.1.0" @@ -678,8 +418,6 @@ checksum = "d466e9454f08e4a911e14806c24e16fba1b4c121d1ea474396f396069cf949d9" dependencies = [ "equivalent", "hashbrown 0.17.1", - "serde", - "serde_core", ] [[package]] @@ -739,18 +477,6 @@ version = "1.0.18" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "8f42a60cbdf9a97f5d2305f08a87dc4e09308d1276d28c869c684d7777685682" -[[package]] -name = "js-sys" -version = "0.3.98" -source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "67df7112613f8bfd9150013a0314e196f4800d3201ae742489d999db2f979f08" -dependencies = [ - "cfg-if", - "futures-util", - "once_cell", - "wasm-bindgen", -] - [[package]] name = "kqueue" version = "1.1.1" @@ -777,12 +503,6 @@ version = "1.5.0" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "bbd2bcb4c963f2ddae06a2efc7e9f3591312473c50c6685e1f298068316e66fe" -[[package]] -name = "leb128fmt" -version = "0.1.0" -source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "09edd9e8b54e49e587e4f6295a7d29c3ea94d469cb40ab8ca70b288248a81db2" - [[package]] name = "libc" version = "0.2.186" @@ -821,7 +541,6 @@ name = "lsp-profiler" version = "0.0.0-dev" dependencies = [ "async-trait", - "humantime", "inferno", "num-traits", "serde", @@ -869,7 +588,6 @@ dependencies = [ "dashmap", "futures-core", "futures-util", - "rmp-serde", "ropey", "serde", "serde_json", @@ -895,7 +613,6 @@ dependencies = [ "lsp-types", "lspkit", "notify", - "rmcp", "salsa", "serde", "serde_json", @@ -1096,12 +813,6 @@ dependencies = [ "windows-link", ] -[[package]] -name = "pastey" -version = "0.2.3" -source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "2ee67f1008b1ba2321834326597b8e186293b049a023cdef258527550b9935b4" - [[package]] name = "percent-encoding" version = "2.3.2" @@ -1129,16 +840,6 @@ dependencies = [ "zerovec", ] -[[package]] -name = "prettyplease" -version = "0.2.37" -source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "479ca8adacdd7ce8f1fb39ce9ecccbfe93a3f1344b3d0d97f20bc0196208f62b" -dependencies = [ - "proc-macro2", - "syn 2.0.117", -] - [[package]] name = "proc-macro2" version = "1.0.106" @@ -1172,12 +873,6 @@ version = "5.3.0" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "69cdb34c158ceb288df11e18b4bd39de994f6657d83847bdffdbd7f346754b0f" -[[package]] -name = "r-efi" -version = "6.0.0" -source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "f8dcc9c7d52a811697d2151c701e0d08956f92b0e24136cf4cf27b57a6a0d9bf" - [[package]] name = "rayon" version = "1.12.0" @@ -1207,26 +902,6 @@ dependencies = [ "bitflags 2.11.1", ] -[[package]] -name = "ref-cast" -version = "1.0.27" -source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "7e440fb4e4b4147295338efb76001ab9e4efc0e5839df2c47fc5ac2381d365c3" -dependencies = [ - "ref-cast-impl", -] - -[[package]] -name = "ref-cast-impl" -version = "1.0.27" -source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "92ecd8964f8453721699a1ed72037b0db49ce2f5a5138486ee89bed6f67cdf3a" -dependencies = [ - "proc-macro2", - "quote", - "syn 3.0.9", -] - [[package]] name = "regex" version = "1.12.3" @@ -1265,63 +940,6 @@ dependencies = [ "bytemuck", ] -[[package]] -name = "rmcp" -version = "3.10.0" -source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "b5949e504d5ab9944a168259afc0c39ac0230627b6ec5367ace194c59bcbe163" -dependencies = [ - "base64", - "chrono", - "futures", - "indexmap", - "libc", - "pastey", - "pin-project-lite", - "rmcp-macros", - "schemars", - "serde", - "serde_json", - "sha2", - "thiserror", - "tokio", - "tokio-util", - "tracing", - "uuid", -] - -[[package]] -name = "rmcp-macros" -version = "3.10.0" -source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "b98a686693ee91b665c6f89f182fca6e141c8a63dc113b11b38c1bf3491606b7" -dependencies = [ - "darling", - "proc-macro2", - "quote", - "serde_json", - "syn 3.0.9", -] - -[[package]] -name = "rmp" -version = "0.8.15" -source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "4ba8be72d372b2c9b35542551678538b562e7cf86c3315773cae48dfbfe7790c" -dependencies = [ - "num-traits", -] - -[[package]] -name = "rmp-serde" -version = "1.3.1" -source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "72f81bee8c8ef9b577d1681a70ebbc962c232461e397b22c208c43c04b67a155" -dependencies = [ - "rmp", - "serde", -] - [[package]] name = "ropey" version = "1.6.1" @@ -1409,44 +1027,12 @@ dependencies = [ "winapi-util", ] -[[package]] -name = "schemars" -version = "1.2.3" -source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "ab508826f74a77ca9d5aba6ff19b522583ee3eaf28a19384ff3d0e5835fadf6e" -dependencies = [ - "chrono", - "dyn-clone", - "ref-cast", - "schemars_derive", - "serde", - "serde_json", -] - -[[package]] -name = "schemars_derive" -version = "1.2.3" -source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "e1c3a92094fa7d61aa124645844facb6b554dfc797136d0f5fd1f890e2bffc69" -dependencies = [ - "proc-macro2", - "quote", - "serde_derive_internals", - "syn 3.0.9", -] - [[package]] name = "scopeguard" version = "1.2.0" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "94143f37725109f92c262ed2cf5e59bce7498c01bcc1502d7b9afe439a4e9f49" -[[package]] -name = "semver" -version = "1.0.28" -source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "8a7852d02fc848982e0c167ef163aaff9cd91dc640ba85e263cb1ce46fae51cd" - [[package]] name = "serde" version = "1.0.229" @@ -1477,24 +1063,13 @@ dependencies = [ "syn 3.0.9", ] -[[package]] -name = "serde_derive_internals" -version = "0.30.0" -source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "f852137cce035d6a4df67ccce505ff6b3e9fd3a10e3e52b24dc71e650bb1a9bd" -dependencies = [ - "proc-macro2", - "quote", - "syn 3.0.9", -] - [[package]] name = "serde_json" version = "1.0.154" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "e7e9cc8b1b85264074fbcc02a88680c4096b1e47df8f739dceb03bf482f04bd6" dependencies = [ - "foldhash 0.2.0", + "foldhash", "indexmap", "itoa", "memchr", @@ -1523,17 +1098,6 @@ dependencies = [ "serde_core", ] -[[package]] -name = "sha2" -version = "0.11.1" -source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "47d7069beb7d6ac7b9acd1039986e73443f24234f41074da099d6f994ac9ad19" -dependencies = [ - "cfg-if", - "cpufeatures", - "digest", -] - [[package]] name = "sharded-slab" version = "0.1.7" @@ -1611,12 +1175,6 @@ version = "0.1.9" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "2b2231b7c3057d5e4ad0156fb3dc807d900806020c5ffa3ee6ff2c8c76fb8520" -[[package]] -name = "strsim" -version = "0.11.1" -source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "7da8b5736845d9f2fcb837ea5d9e2628564b3b043a70948a3f0b778838c5fb4f" - [[package]] name = "syn" version = "2.0.117" @@ -1672,7 +1230,7 @@ source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "32497e9a4c7b38532efcdebeef879707aa9f794296a4f0244f6f69e9bc8574bd" dependencies = [ "fastrand", - "getrandom 0.3.4", + "getrandom", "once_cell", "rustix", "windows-sys", @@ -1912,24 +1470,12 @@ version = "1.0.3" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "bc7d623258602320d5c55d1bc22793b57daff0ec7efc270ea7d55ce1d5f5471c" -[[package]] -name = "typenum" -version = "1.20.1" -source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "b6f5e870be6c3b371b77fe0ee0bafb859fa4964b4404c27de1d380043c4dda20" - [[package]] name = "unicode-ident" version = "1.0.24" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "e6e4313cd5fcd3dad5cafa179702e2b244f760991f45397d14d4ebf38247da75" -[[package]] -name = "unicode-xid" -version = "0.2.6" -source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "ebc1c04c71510c7f702b52b7c350734c9ff1295c464a03335b00bb84fc54f853" - [[package]] name = "url" version = "2.5.8" @@ -1948,17 +1494,6 @@ version = "1.0.4" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "b6c140620e7ffbb22c2dee59cafe6084a59b5ffc27a8859a5f0d494b5d52b6be" -[[package]] -name = "uuid" -version = "1.28.0" -source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "7cc1186384beb7dd8eedea376413fd654937285ea6c9cfbb928dc3043ea4b606" -dependencies = [ - "getrandom 0.4.2", - "js-sys", - "wasm-bindgen", -] - [[package]] name = "valuable" version = "0.1.1" @@ -1993,95 +1528,7 @@ version = "1.0.3+wasi-0.2.9" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "20064672db26d7cdc89c7798c48a0fdfac8213434a1186e5ef29fd560ae223d6" dependencies = [ - "wit-bindgen 0.57.1", -] - -[[package]] -name = "wasip3" -version = "0.4.0+wasi-0.3.0-rc-2026-01-06" -source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "5428f8bf88ea5ddc08faddef2ac4a67e390b88186c703ce6dbd955e1c145aca5" -dependencies = [ - "wit-bindgen 0.51.0", -] - -[[package]] -name = "wasm-bindgen" -version = "0.2.121" -source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "49ace1d07c165b0864824eee619580c4689389afa9dc9ed3a4c75040d82e6790" -dependencies = [ - "cfg-if", - "once_cell", - "rustversion", - "wasm-bindgen-macro", - "wasm-bindgen-shared", -] - -[[package]] -name = "wasm-bindgen-macro" -version = "0.2.121" -source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "8e68e6f4afd367a562002c05637acb8578ff2dea1943df76afb9e83d177c8578" -dependencies = [ - "quote", - "wasm-bindgen-macro-support", -] - -[[package]] -name = "wasm-bindgen-macro-support" -version = "0.2.121" -source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "d95a9ec35c64b2a7cb35d3fead40c4238d0940c86d107136999567a4703259f2" -dependencies = [ - "bumpalo", - "proc-macro2", - "quote", - "syn 2.0.117", - "wasm-bindgen-shared", -] - -[[package]] -name = "wasm-bindgen-shared" -version = "0.2.121" -source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "c4e0100b01e9f0d03189a92b96772a1fb998639d981193d7dbab487302513441" -dependencies = [ - "unicode-ident", -] - -[[package]] -name = "wasm-encoder" -version = "0.244.0" -source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "990065f2fe63003fe337b932cfb5e3b80e0b4d0f5ff650e6985b1048f62c8319" -dependencies = [ - "leb128fmt", - "wasmparser", -] - -[[package]] -name = "wasm-metadata" -version = "0.244.0" -source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "bb0e353e6a2fbdc176932bbaab493762eb1255a7900fe0fea1a2f96c296cc909" -dependencies = [ - "anyhow", - "indexmap", - "wasm-encoder", - "wasmparser", -] - -[[package]] -name = "wasmparser" -version = "0.244.0" -source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "47b807c72e1bac69382b3a6fb3dbe8ea4c0ed87ff5629b8685ae6b9a611028fe" -dependencies = [ - "bitflags 2.11.1", - "hashbrown 0.15.5", - "indexmap", - "semver", + "wit-bindgen", ] [[package]] @@ -2240,100 +1687,12 @@ version = "1.0.3" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "0592e1c9d151f854e6fd382574c3a0855250e1d9b2f99d9281c6e6391af352f1" -[[package]] -name = "wit-bindgen" -version = "0.51.0" -source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "d7249219f66ced02969388cf2bb044a09756a083d0fab1e566056b04d9fbcaa5" -dependencies = [ - "wit-bindgen-rust-macro", -] - [[package]] name = "wit-bindgen" version = "0.57.1" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "1ebf944e87a7c253233ad6766e082e3cd714b5d03812acc24c318f549614536e" -[[package]] -name = "wit-bindgen-core" -version = "0.51.0" -source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "ea61de684c3ea68cb082b7a88508a8b27fcc8b797d738bfc99a82facf1d752dc" -dependencies = [ - "anyhow", - "heck", - "wit-parser", -] - -[[package]] -name = "wit-bindgen-rust" -version = "0.51.0" -source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "b7c566e0f4b284dd6561c786d9cb0142da491f46a9fbed79ea69cdad5db17f21" -dependencies = [ - "anyhow", - "heck", - "indexmap", - "prettyplease", - "syn 2.0.117", - "wasm-metadata", - "wit-bindgen-core", - "wit-component", -] - -[[package]] -name = "wit-bindgen-rust-macro" -version = "0.51.0" -source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "0c0f9bfd77e6a48eccf51359e3ae77140a7f50b1e2ebfe62422d8afdaffab17a" -dependencies = [ - "anyhow", - "prettyplease", - "proc-macro2", - "quote", - "syn 2.0.117", - "wit-bindgen-core", - "wit-bindgen-rust", -] - -[[package]] -name = "wit-component" -version = "0.244.0" -source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "9d66ea20e9553b30172b5e831994e35fbde2d165325bec84fc43dbf6f4eb9cb2" -dependencies = [ - "anyhow", - "bitflags 2.11.1", - "indexmap", - "log", - "serde", - "serde_derive", - "serde_json", - "wasm-encoder", - "wasm-metadata", - "wasmparser", - "wit-parser", -] - -[[package]] -name = "wit-parser" -version = "0.244.0" -source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "ecc8ac4bc1dc3381b7f59c34f00b67e18f910c2c0f50015669dde7def656a736" -dependencies = [ - "anyhow", - "id-arena", - "indexmap", - "log", - "semver", - "serde", - "serde_derive", - "serde_json", - "unicode-xid", - "wasmparser", -] - [[package]] name = "writeable" version = "0.6.4" diff --git a/Cargo.toml b/Cargo.toml index da7af9a..04d6de8 100644 --- a/Cargo.toml +++ b/Cargo.toml @@ -22,6 +22,12 @@ readme = "README.md" keywords = ["lsp", "mcp", "language-server", "tree-sitter", "json-rpc"] categories = ["development-tools"] +# Unused dependencies are dead code: a crate dependency or a +# `[workspace.dependencies]` entry nothing uses fails the build. +[workspace.lints.cargo] +unused_dependencies = "deny" +unused_workspace_dependencies = "deny" + [workspace.lints.rust] unsafe_code = "deny" missing_docs = "deny" @@ -119,13 +125,10 @@ async-trait = "0.1" # Serialization serde = { version = "1", features = ["derive"] } serde_json = "1" -rmp-serde = "1" # LSP protocol types (maintained; we ship our own server loop) lsp-types = "0.97" -# MCP SDK -rmcp = "3.10" # Document / state dashmap = "6" @@ -143,7 +146,6 @@ tracing = "0.1" tracing-subscriber = { version = "0.3", features = ["env-filter", "json"] } # Profiler ([PROF-INVARIANTS]: only the core and runtime crates use these) -humantime = "2" inferno = { version = "0.12", default-features = false } num-traits = "0.2" sysinfo = { version = "0.39.3", default-features = false, features = ["system", "user"] } @@ -160,6 +162,4 @@ stats_alloc = "0.1.10" # Versions are the `0.0.0-dev` placeholder; the release stamps the tag into # both `[workspace.package].version` and these requirements. [SWR-VERSION-BUILD-STAMPING] lspkit = { version = "0.0.0-dev", path = "crates/lspkit" } -lspkit-server = { version = "0.0.0-dev", path = "crates/lspkit-server" } lsp-profiler = { version = "0.0.0-dev", path = "crates/lsp-profiler" } -lsp-profiler-dotnet = { version = "0.0.0-dev", path = "crates/lsp-profiler-dotnet" } diff --git a/crates/lsp-profiler-dotnet/src/process.rs b/crates/lsp-profiler-dotnet/src/process.rs index 9c93a68..f842345 100644 --- a/crates/lsp-profiler-dotnet/src/process.rs +++ b/crates/lsp-profiler-dotnet/src/process.rs @@ -163,7 +163,7 @@ pub fn has_endpoint(pid: u32) -> Option { } #[cfg(windows)] -/// Search Windows named pipes for the target's CoreCLR diagnostics endpoint. +/// Search Windows named pipes for the target's `CoreCLR` diagnostics endpoint. fn endpoint_exists(pid: u32) -> Option { let exact = format!("dotnet-diagnostic-{pid}"); let prefixed = format!("{exact}-"); diff --git a/crates/lsp-profiler-dotnet/tests/support/mod.rs b/crates/lsp-profiler-dotnet/tests/support/mod.rs index 309ef6a..5fcda42 100644 --- a/crates/lsp-profiler-dotnet/tests/support/mod.rs +++ b/crates/lsp-profiler-dotnet/tests/support/mod.rs @@ -121,12 +121,16 @@ impl ProfilerEvents for Silent { async fn notify(&self, _method: &'static str, _params: Value) {} } -/// The built engine executable. +/// The engine under test: `LSP_PROFILER_DOTNET_ENGINE` when set, the way a +/// consumer points at a released engine asset, else the source build. pub fn engine() -> PathBuf { - built( - "dotnet/LspProfiler.DotNet.Engine/bin/Release/net8.0/lsp-profiler-dotnet-engine", - "make dotnet", - ) + match std::env::var_os(lsp_profiler_dotnet::engine::ENGINE_ENV) { + Some(released) => PathBuf::from(released), + None => built( + "dotnet/LspProfiler.DotNet.Engine/bin/Release/net8.0/lsp-profiler-dotnet-engine", + "make dotnet", + ), + } } /// The shared service with only the .NET runtime, writing under `output_dir`. diff --git a/crates/lsp-profiler/Cargo.toml b/crates/lsp-profiler/Cargo.toml index b711405..95f806b 100644 --- a/crates/lsp-profiler/Cargo.toml +++ b/crates/lsp-profiler/Cargo.toml @@ -23,7 +23,6 @@ workspace = true [dependencies] async-trait.workspace = true -humantime.workspace = true inferno.workspace = true num-traits.workspace = true serde.workspace = true diff --git a/crates/lsp-profiler/src/clock.rs b/crates/lsp-profiler/src/clock.rs new file mode 100644 index 0000000..f49f4fc --- /dev/null +++ b/crates/lsp-profiler/src/clock.rs @@ -0,0 +1,164 @@ +//! RFC 3339 UTC timestamps from [`SystemTime`] with std only. [PROF-CPU-SESSIONS] +//! +//! Replaces `humantime`, which RUSTSEC-2026-0542 marks unmaintained. + +use std::time::{SystemTime, UNIX_EPOCH}; + +/// The current time as RFC 3339 UTC, to the second. +#[must_use] +pub fn rfc3339_now() -> String { + rfc3339(SystemTime::now()) +} + +/// Seconds in one civil day. +const DAY_SECS: i128 = 86_400; +/// Days in one 400-year Gregorian era. +const ERA_DAYS: i128 = 146_097; +/// Days from 0000-03-01 to 1970-01-01. +const EPOCH_SHIFT: i128 = 719_468; +/// Seconds in one hour. +const HOUR_SECS: i128 = 3_600; +/// Seconds in one minute. +const MINUTE_SECS: i128 = 60; +/// Days in a common year. +const YEAR_DAYS: i128 = 365; +/// Days in four years, one of them leap. +const LEAP_CYCLE_DAYS: i128 = 1_460; +/// Days in a century that skips its leap day. +const CENTURY_DAYS: i128 = 36_524; +/// Years between Julian leap days. +const LEAP_YEAR_EVERY: i128 = 4; +/// Years in a century, whose leap day is skipped. +const CENTURY_YEARS: i128 = 100; +/// Years in a Gregorian era. +const ERA_YEARS: i128 = 400; +/// Hinnant's month-from-day-of-year line: month = (5·doy + 2) / 153. +const MONTH_SLOPE: (i128, i128, i128) = (5, 2, 153); +/// March-based months before the year rolls into January. +const MARCH_MONTHS_TO_YEAR_END: i128 = 10; +/// Calendar months in a year. +const YEAR_MONTHS: i128 = 12; +/// March as a calendar month number. +const MARCH: i128 = 3; + +/// `time` as RFC 3339 UTC, truncated (floored) to the second. +#[must_use] +pub fn rfc3339(time: SystemTime) -> String { + rfc3339_seconds(epoch_seconds(time)) +} + +/// Whole seconds since the Unix epoch as RFC 3339 UTC. Kept apart from +/// [`SystemTime`], which cannot hold every instant on every platform: on +/// Windows it starts at 1601-01-01. +fn rfc3339_seconds(secs: i128) -> String { + let (year, month, day) = civil(secs.div_euclid(DAY_SECS)); + let clock = secs.rem_euclid(DAY_SECS); + let (hour, minute, second) = ( + clock.div_euclid(HOUR_SECS), + clock.rem_euclid(HOUR_SECS).div_euclid(MINUTE_SECS), + clock.rem_euclid(MINUTE_SECS), + ); + format!("{year:04}-{month:02}-{day:02}T{hour:02}:{minute:02}:{second:02}Z") +} + +/// Whole seconds since the Unix epoch, floored, negative before it. +fn epoch_seconds(time: SystemTime) -> i128 { + match time.duration_since(UNIX_EPOCH) { + Ok(after) => i128::from(after.as_secs()), + Err(before) => { + let gap = before.duration(); + -i128::from(gap.as_secs()) - i128::from(gap.subsec_nanos() > 0) + } + } +} + +/// The proleptic Gregorian (year, month, day) of `days` since 1970-01-01. +fn civil(days: i128) -> (i128, i128, i128) { + let shifted = days + EPOCH_SHIFT; + let era = shifted.div_euclid(ERA_DAYS); + let day_of_era = shifted - era * ERA_DAYS; + let year_of_era = year_of_era(day_of_era); + let day_of_year = day_of_era + - (YEAR_DAYS * year_of_era + year_of_era.div_euclid(LEAP_YEAR_EVERY) + - year_of_era.div_euclid(CENTURY_YEARS)); + let (month, day) = month_day(day_of_year); + let year = year_of_era + era * ERA_YEARS + i128::from(month < MARCH); + (year, month, day) +} + +/// The March-based year (0..=399) within its 400-year era. +fn year_of_era(day_of_era: i128) -> i128 { + (day_of_era - day_of_era.div_euclid(LEAP_CYCLE_DAYS) + day_of_era.div_euclid(CENTURY_DAYS) + - day_of_era.div_euclid(ERA_DAYS - 1)) + .div_euclid(YEAR_DAYS) +} + +/// The calendar (month, day) of a March-based day of the year. +fn month_day(day_of_year: i128) -> (i128, i128) { + let (slope, offset, span) = MONTH_SLOPE; + let march_month = (slope * day_of_year + offset).div_euclid(span); + let day = day_of_year - (span * march_month + offset).div_euclid(slope) + 1; + let month = if march_month < MARCH_MONTHS_TO_YEAR_END { + march_month + MARCH + } else { + march_month + MARCH - YEAR_MONTHS + }; + (month, day) +} + +#[cfg(test)] +mod tests { + use super::*; + use std::time::Duration; + + /// Seconds after the epoch and the timestamp they must format as. + const KNOWN: [(u64, &str); 8] = [ + (0, "1970-01-01T00:00:00Z"), + (951_782_400, "2000-02-29T00:00:00Z"), + (951_868_799, "2000-02-29T23:59:59Z"), + (1_700_000_000, "2023-11-14T22:13:20Z"), + (4_107_542_399, "2100-02-28T23:59:59Z"), + (4_107_542_400, "2100-03-01T00:00:00Z"), + (13_574_606_400, "2400-02-29T12:00:00Z"), + (253_402_300_799, "9999-12-31T23:59:59Z"), + ]; + + /// Seconds before the epoch and the timestamp they must format as. + const BEFORE_EPOCH: [(u64, &str); 4] = [ + (2_208_988_800, "1900-01-01T00:00:00Z"), + (2_203_891_201, "1900-02-28T23:59:59Z"), + (2_203_891_200, "1900-03-01T00:00:00Z"), + (11_670_998_400, "1600-02-29T00:00:00Z"), + ]; + + #[test] + fn century_leap_rules_hold_before_the_epoch() { + for (secs, want) in BEFORE_EPOCH { + assert_eq!(rfc3339_seconds(-i128::from(secs)), want, "-{secs}s"); + // Windows' SystemTime starts at 1601, so the 1600 row exists + // there only as seconds; elsewhere it goes through SystemTime too. + if let Some(instant) = UNIX_EPOCH.checked_sub(Duration::from_secs(secs)) { + assert_eq!(rfc3339(instant), want, "-{secs}s as SystemTime"); + } + } + } + + #[test] + fn known_instants_format_as_rfc3339_utc_seconds() { + for (secs, want) in KNOWN { + let exact = UNIX_EPOCH + Duration::from_secs(secs); + assert_eq!(rfc3339(exact), want, "{secs}s"); + let later = exact + Duration::from_nanos(999_999_999); + assert_eq!(rfc3339(later), want, "{secs}s truncates sub-seconds"); + } + let before = UNIX_EPOCH - Duration::from_millis(1); + assert_eq!( + rfc3339(before), + "1969-12-31T23:59:59Z", + "floors before the epoch" + ); + let now = rfc3339_now(); + assert_eq!(now.len(), "2026-10-14T00:00:00Z".len(), "{now}"); + assert!(now.ends_with('Z') && now.get(10..11) == Some("T"), "{now}"); + } +} diff --git a/crates/lsp-profiler/src/lib.rs b/crates/lsp-profiler/src/lib.rs index 602c531..fd54a71 100644 --- a/crates/lsp-profiler/src/lib.rs +++ b/crates/lsp-profiler/src/lib.rs @@ -11,6 +11,7 @@ //! The wire types in [`wire`] are generated from `docs/models/profiler.td`. pub mod aggregate; +pub mod clock; pub mod commands; pub mod cooperative; pub mod diagnostics; diff --git a/crates/lsp-profiler/src/paths.rs b/crates/lsp-profiler/src/paths.rs index d7c82ac..26945a7 100644 --- a/crates/lsp-profiler/src/paths.rs +++ b/crates/lsp-profiler/src/paths.rs @@ -21,5 +21,8 @@ pub fn path_in_roots(candidate: &Path, roots: &[PathBuf]) -> bool { /// Canonicalize existing paths, retaining the original spelling if unavailable. #[must_use] pub fn normalize_path(path: &Path) -> PathBuf { - path.canonicalize().unwrap_or_else(|_| path.to_path_buf()) + match path.canonicalize() { + Ok(canonical) => canonical, + Err(_unresolvable) => path.to_path_buf(), + } } diff --git a/crates/lsp-profiler/src/session.rs b/crates/lsp-profiler/src/session.rs index 1a5485f..b1d2555 100644 --- a/crates/lsp-profiler/src/session.rs +++ b/crates/lsp-profiler/src/session.rs @@ -12,6 +12,7 @@ use tokio::sync::Mutex; use tracing::{debug, info}; use crate::aggregate::{saturating_i64, HotspotConfig, ProfileData}; +use crate::clock::rfc3339_now; use crate::error::ProfilerError; use crate::runtime::Runtime; use crate::sampler::{SampleBatch, SamplerHandle, SamplerStop}; @@ -376,12 +377,6 @@ pub fn mint_id(prefix: &str, sequence: u64) -> String { ) } -/// The current time as RFC 3339 UTC, to the second. -#[must_use] -pub fn rfc3339_now() -> String { - humantime::format_rfc3339_seconds(SystemTime::now()).to_string() -} - /// The function the program spends its own time in: the most self samples, /// ties broken by name. Ranked by inclusive samples, the root of every whole /// stack would always be the hottest [PROF-CPU-PROGRESS]. @@ -400,16 +395,6 @@ fn hottest_by_self(functions: &[HotFunction]) -> Option { mod tests { use super::*; - #[test] - fn rfc3339_now_is_utc_to_the_second() { - let now = rfc3339_now(); - assert_eq!(now.len(), "2026-10-14T00:00:00Z".len(), "{now}"); - assert!( - now.ends_with('Z') && now.as_bytes().get(10) == Some(&b'T'), - "{now}" - ); - } - fn hot(name: &str, samples: i64, self_samples: i64) -> HotFunction { HotFunction { name: name.to_owned(), diff --git a/crates/lspkit-server/Cargo.toml b/crates/lspkit-server/Cargo.toml index 7fe6ac8..3caac57 100644 --- a/crates/lspkit-server/Cargo.toml +++ b/crates/lspkit-server/Cargo.toml @@ -22,7 +22,6 @@ workspace = true lspkit.workspace = true async-trait.workspace = true -futures-util.workspace = true lsp-types.workspace = true notify.workspace = true serde.workspace = true @@ -32,12 +31,9 @@ tokio.workspace = true tokio-util.workspace = true tracing.workspace = true -# Optional: the MCP SDK stays out of LSP-only consumers. [CRATE-LAYOUT-FEATURES] -rmcp = { workspace = true, optional = true } salsa = { version = "0.28.5", optional = true } [features] -mcp = ["dep:rmcp"] pull = ["dep:salsa"] [dev-dependencies] diff --git a/crates/lspkit-server/examples/diagnostic-reference/session/handlers.rs b/crates/lspkit-server/examples/diagnostic-reference/session/handlers.rs index fcf4229..293fa4a 100644 --- a/crates/lspkit-server/examples/diagnostic-reference/session/handlers.rs +++ b/crates/lspkit-server/examples/diagnostic-reference/session/handlers.rs @@ -100,9 +100,11 @@ pub(super) fn close_document(server: &Server, params: Value) { tracing::warn!("recovering poisoned source registry to release closed document"); poisoned.into_inner() }); - let _ = documents.remove(params.text_document.uri.as_str()); + let uri = params.text_document.uri.as_str(); + let _ = documents.remove(uri); drop(documents); - if let Err(error) = server.pull.clear() { + // [LSP-PULL-FORGET] Only the closed document's memo goes; open ones stay warm. + if let Err(error) = server.pull.forget(uri) { tracing::warn!(%error, "closed document memo release failed"); } } diff --git a/crates/lspkit-server/src/jsonrpc.rs b/crates/lspkit-server/src/jsonrpc.rs index 00aaf9a..135e9b5 100644 --- a/crates/lspkit-server/src/jsonrpc.rs +++ b/crates/lspkit-server/src/jsonrpc.rs @@ -45,7 +45,8 @@ pub struct Message { } /// A JSON-RPC 2.0 error object. -#[derive(Debug, Clone, Serialize, Deserialize)] +#[derive(Debug, Clone, Serialize, Deserialize, thiserror::Error)] +#[error("JSON-RPC error {code}: {message}")] pub struct JsonRpcError { /// Numeric error code (see JSON-RPC spec for reserved ranges). pub code: i32, @@ -76,27 +77,27 @@ impl JsonRpcError { } impl Message { - /// Construct a request with `id`, `method`, and `params`. + /// Construct a request with `id`, `method`, and `params` (omitted when `null`). #[must_use] pub fn request(id: RequestId, method: impl Into, params: Value) -> Self { Self { jsonrpc: "2.0".to_owned(), id: Some(id), method: Some(method.into()), - params: Some(params), + params: wire_params(params), result: None, error: None, } } - /// Construct a notification with `method` and `params`. + /// Construct a notification with `method` and `params` (omitted when `null`). #[must_use] pub fn notification(method: impl Into, params: Value) -> Self { Self { jsonrpc: "2.0".to_owned(), id: None, method: Some(method.into()), - params: Some(params), + params: wire_params(params), result: None, error: None, } @@ -129,6 +130,11 @@ impl Message { } } +/// JSON-RPC 2.0 `params` is an object, an array, or omitted, never `null`. +fn wire_params(params: Value) -> Option { + (!params.is_null()).then_some(params) +} + /// Errors from reading or writing JSON-RPC messages. #[non_exhaustive] #[derive(Debug, thiserror::Error)] @@ -145,43 +151,114 @@ pub enum FramingError { /// Peer closed the stream. #[error("peer closed the stream")] Closed, + /// The peer claimed a body larger than the cap ([JSONRPC-FRAME-LIMIT]). + #[error("frame of {claimed} bytes exceeds the {max}-byte limit")] + FrameTooLarge { + /// The `Content-Length` the peer sent. + claimed: u64, + /// The cap it exceeded. + max: usize, + }, + /// The header block ran past [`MAX_HEADER_BYTES`] without its blank line + /// ([JSONRPC-FRAME-LIMIT]). + #[error("header block exceeds the {MAX_HEADER_BYTES}-byte limit")] + HeaderTooLarge, } -/// Read one Content-Length-framed JSON-RPC message from `reader`. +/// Largest header block, every line and its terminator included: 8 KiB. It +/// bounds both one endless line and endless short lines ([JSONRPC-FRAME-LIMIT]). +pub const MAX_HEADER_BYTES: u64 = 8 * 1024; + +/// Largest body [`read_message`] accepts: 64 MiB ([JSONRPC-FRAME-LIMIT]). +pub const MAX_FRAME_BYTES: usize = 64 * 1024 * 1024; + +/// Read one Content-Length-framed JSON-RPC message of at most +/// [`MAX_FRAME_BYTES`] from `reader`. /// /// # Errors -/// Returns [`FramingError`] on I/O failure, header parse failure, or invalid JSON. +/// Returns [`FramingError`] on I/O failure, header parse failure, an +/// oversized frame, or invalid JSON. pub async fn read_message(reader: &mut BufReader) -> Result where R: AsyncReadExt + Unpin, { - let mut content_length: Option = None; + read_message_within(reader, MAX_FRAME_BYTES).await +} + +/// [`read_message`] with a server-chosen body cap of `max_bytes`. +/// +/// # Errors +/// As [`read_message`], with [`FramingError::FrameTooLarge`] above `max_bytes`. +pub async fn read_message_within( + reader: &mut BufReader, + max_bytes: usize, +) -> Result +where + R: AsyncReadExt + Unpin, +{ + let claimed = content_length(reader).await?; + let length = usize::try_from(claimed) + .ok() + .filter(|length| *length <= max_bytes) + .ok_or(FramingError::FrameTooLarge { + claimed, + max: max_bytes, + })?; + // Memory follows the bytes that arrive, never the length the peer claims. + let mut body = Vec::new(); + let read = (&mut *reader).take(claimed).read_to_end(&mut body).await?; + if read < length { + return Err(io::Error::from(io::ErrorKind::UnexpectedEof).into()); + } + Ok(serde_json::from_slice(&body)?) +} + +/// Read headers up to the blank line; the `Content-Length` they declare. +async fn content_length(reader: &mut BufReader) -> Result +where + R: AsyncReadExt + Unpin, +{ + let mut content_length = None; let mut line = String::new(); + let mut budget = MAX_HEADER_BYTES; loop { line.clear(); - let read = reader.read_line(&mut line).await?; - if read == 0 { - return Err(FramingError::Closed); - } - let trimmed = line.trim_end_matches(['\r', '\n']); - if trimmed.is_empty() { - break; - } - if let Some(value) = trimmed.strip_prefix("Content-Length:") { - content_length = Some( - value - .trim() - .parse() - .map_err(|source| FramingError::BadHeader(trimmed.to_owned(), Some(source)))?, - ); + budget = header_line(reader, budget, &mut line).await?; + let header = line.trim_end_matches(['\r', '\n']); + if header.is_empty() { + return content_length + .ok_or_else(|| FramingError::BadHeader("missing".to_owned(), None)); } + content_length = declared_length(header)?.or(content_length); + } +} + +/// Read one header line into `line` from what is left of the header budget; +/// returns the budget that remains. Memory never passes [`MAX_HEADER_BYTES`]. +async fn header_line( + reader: &mut BufReader, + budget: u64, + line: &mut String, +) -> Result +where + R: AsyncReadExt + Unpin, +{ + let mut bounded = (&mut *reader).take(budget); + let read = bounded.read_line(line).await?; + match (read, line.ends_with('\n'), bounded.limit()) { + (_, false, 0) => Err(FramingError::HeaderTooLarge), + (0, _, _) => Err(FramingError::Closed), + (_, _, left) => Ok(left), } - let length = - content_length.ok_or_else(|| FramingError::BadHeader("missing".to_owned(), None))?; - let mut body = vec![0u8; length]; - let _ = reader.read_exact(&mut body).await?; - let message: Message = serde_json::from_slice(&body)?; - Ok(message) +} + +/// The length a `Content-Length` header declares; other headers declare none. +fn declared_length(header: &str) -> Result, FramingError> { + header + .strip_prefix("Content-Length:") + .map(|value| value.trim().parse()) + .transpose() + .map_err(|source| FramingError::BadHeader(header.to_owned(), Some(source))) } /// Writer half of a JSON-RPC connection, serialized so multiple tasks can send. diff --git a/crates/lspkit-server/src/lib.rs b/crates/lspkit-server/src/lib.rs index 00646de..cc305b0 100644 --- a/crates/lspkit-server/src/lib.rs +++ b/crates/lspkit-server/src/lib.rs @@ -21,8 +21,8 @@ pub mod diagnostics; pub mod dispatch; pub mod jsonrpc; pub mod live; -#[cfg(feature = "mcp")] pub mod mcp; +pub mod outbound; pub mod progress; #[cfg(feature = "pull")] pub mod pull; @@ -38,6 +38,7 @@ pub use crate::dispatch::{ CancellationHandle, DispatchError, Dispatcher, HandlerResult, RequestInterest, }; pub use crate::jsonrpc::{FramingError, JsonRpcError, Message, MessageWriter, RequestId}; +pub use crate::outbound::{Outbound, OutboundError, OutboundResponse, OutboundResult}; pub use crate::progress::{NoopReporter, ProgressReporter, ProgressSink, ProgressUpdate}; pub use crate::reply::{EmissionError, ReplyGate}; pub use crate::uri::{path_to_uri, uri_to_path, UriError}; diff --git a/crates/lspkit-server/src/live/watcher.rs b/crates/lspkit-server/src/live/watcher.rs index bbb213d..26dd7dd 100644 --- a/crates/lspkit-server/src/live/watcher.rs +++ b/crates/lspkit-server/src/live/watcher.rs @@ -156,11 +156,10 @@ impl RootAlias { /// The same file under the given root; a path outside the reported root is kept. fn rebase(&self, path: PathBuf) -> PathBuf { - let rebased = path - .strip_prefix(&self.reported) - .ok() - .map(|rest| self.given.join(rest)); - rebased.unwrap_or(path) + match path.strip_prefix(&self.reported) { + Ok(rest) => self.given.join(rest), + Err(_outside_the_reported_root) => path, + } } } diff --git a/crates/lspkit-server/src/mcp/adapter.rs b/crates/lspkit-server/src/mcp/adapter.rs index f55f3bc..2c92098 100644 --- a/crates/lspkit-server/src/mcp/adapter.rs +++ b/crates/lspkit-server/src/mcp/adapter.rs @@ -1,6 +1,6 @@ //! Adapter that mounts an [`lspkit::EngineApi`] implementation as an MCP server. //! -//! The adapter does not leak `rmcp::*` types into its public surface; every +//! The adapter exposes no MCP SDK types in its public surface; every //! request/response shape is wrapped behind newtypes defined in this crate. use std::sync::Arc; diff --git a/crates/lspkit-server/src/outbound.rs b/crates/lspkit-server/src/outbound.rs new file mode 100644 index 0000000..ca67b05 --- /dev/null +++ b/crates/lspkit-server/src/outbound.rs @@ -0,0 +1,153 @@ +//! Server→client requests (`window/showDocument`, `workspace/diagnostic/refresh`). +//! [LSP-OUTBOUND] +//! +//! Ids come from the leak-proofed sidecar correlator, so an abandoned wait +//! unregisters itself on drop exactly as [SIDECAR-REQUEST-LIFETIME] requires. + +use lspkit::sidecar::{CorrelationError, Correlator, PendingResponse}; +use serde_json::Value; + +use crate::jsonrpc::{JsonRpcError, Message, RequestId}; + +/// What the editor answered: its `result`, or its JSON-RPC error. +pub type OutboundResult = Result; + +/// Resolves when the editor answers; dropping it unregisters the request. +pub type OutboundResponse = PendingResponse; + +/// Errors from matching an inbound message to a server request. +#[non_exhaustive] +#[derive(Debug, thiserror::Error)] +pub enum OutboundError { + /// The message is a request or notification, not a response. + #[error("message is not a response")] + NotAResponse, + /// No waiting server request has this id: foreign, answered or abandoned. + #[error("no pending server request for id {0:?}")] + UnknownId(RequestId), + /// The correlator id no longer fits a JSON-RPC number on the wire. + #[error("server request id {0} exceeds the largest JSON-RPC number")] + IdsExhausted(u64), +} + +/// Allocates server request ids and routes the editor's answers back. +/// +/// Use exactly one handle per connection: ids are unique only within a handle, +/// so two handles on one connection would allocate the same ids. +#[derive(Debug)] +pub struct Outbound { + /// The only pending table; entries leave when answered or abandoned. + correlator: Correlator, +} + +impl Outbound { + /// A handle with no pending requests. + #[must_use] + pub fn new() -> Self { + Self { + correlator: Correlator::new(), + } + } + + /// Build the request frame to write and the wait for its answer. + /// + /// # Errors + /// [`OutboundError::IdsExhausted`] once ids pass the largest JSON-RPC + /// number; the registration is released, never reused. + pub fn request( + &self, + method: impl Into, + params: Value, + ) -> Result<(Message, OutboundResponse), OutboundError> { + let (id, response) = self.correlator.register(); + Ok(( + Message::request(wire_id(id.get())?, method, params), + response, + )) + } + + /// Deliver an inbound response to the request that is waiting for it. + /// + /// # Errors + /// [`OutboundError::NotAResponse`] for requests, notifications and + /// `"id": null` error responses (they name no request to wake), and + /// [`OutboundError::UnknownId`] when no waiting request has the id. + pub fn resolve(&self, message: Message) -> Result<(), OutboundError> { + let (Some(id), None) = (message.id, message.method) else { + return Err(OutboundError::NotAResponse); + }; + let answer = answer_of(message.error, message.result); + let Some(raw) = correlated(&id) else { + return Err(OutboundError::UnknownId(id)); + }; + self.correlator.resolve(raw.into(), answer).map_err( + |_answered_or_abandoned: CorrelationError| OutboundError::UnknownId(id), + ) + } + + /// Requests still waiting for an answer. + #[must_use] + pub fn pending_count(&self) -> usize { + self.correlator.pending_count() + } +} + +impl Default for Outbound { + fn default() -> Self { + Self::new() + } +} + +/// The wire id for correlator id `raw`; past `i64::MAX` it fails, never collides. +fn wire_id(raw: u64) -> Result { + i64::try_from(raw) + .map(RequestId::Number) + .map_err(|_beyond_wire_numbers| OutboundError::IdsExhausted(raw)) +} + +/// The editor's answer: its error, its result, or `null` when it sent neither. +fn answer_of(error: Option, result: Option) -> OutboundResult { + match (error, result) { + (Some(error), _) => Err(error), + (None, Some(result)) => Ok(result), + (None, None) => Ok(Value::Null), + } +} + +/// The correlator number behind a wire id this handle could have allocated. +fn correlated(id: &RequestId) -> Option { + match id { + RequestId::Number(raw) => u64::try_from(*raw).ok(), + RequestId::String(_) => None, + } +} + +// Reaching 2^63 real registrations is infeasible, so the boundary is isolated here. +#[cfg(test)] +mod tests { + use super::{correlated, wire_id, OutboundError, RequestId}; + + #[test] + fn ids_past_the_largest_json_rpc_number_fail_instead_of_colliding() { + let largest = u64::try_from(i64::MAX).expect("i64::MAX is non-negative"); + let last = wire_id(largest).expect("the largest number is still a valid id"); + assert_eq!(last, RequestId::Number(i64::MAX)); + assert_eq!( + correlated(&last), + Some(largest), + "the last valid id round-trips" + ); + for beyond in [largest + 1, largest + 2, u64::MAX] { + let error = wire_id(beyond).expect_err("no wire number left"); + assert!( + matches!(error, OutboundError::IdsExhausted(raw) if raw == beyond), + "{error:?}" + ); + assert_eq!( + error.to_string(), + format!("server request id {beyond} exceeds the largest JSON-RPC number") + ); + } + assert_eq!(wire_id(1).expect("first id"), RequestId::Number(1)); + } +} diff --git a/crates/lspkit-server/src/pull.rs b/crates/lspkit-server/src/pull.rs index 6369822..8d09afe 100644 --- a/crates/lspkit-server/src/pull.rs +++ b/crates/lspkit-server/src/pull.rs @@ -5,9 +5,9 @@ //! lock-free supersession/generation counters; protocol-side invalidation never //! waits behind a running compiler query. -use std::collections::HashMap; +use std::collections::{HashMap, HashSet}; use std::sync::atomic::{AtomicU64, Ordering}; -use std::sync::{Arc, Mutex}; +use std::sync::{Arc, Mutex, TryLockError}; use lsp_types::Diagnostic; use salsa::Setter; @@ -116,6 +116,9 @@ struct DocumentInput { uri: String, version: i32, retry_revision: u64, + /// `false` once retired. A flag, not an empty URI, so a consumer's own + /// `""` is still analyzed. [LSP-PULL-FORGET] + open: bool, } #[salsa::tracked(returns(clone))] @@ -123,7 +126,12 @@ fn analyze(db: &dyn DiagnosticDb, document: DocumentInput, workspace: WorkspaceI let _generation = workspace.generation(db); let _version = document.version(db); let _retry = document.retry_revision(db); - db.analyze(document.uri(db)) + if *document.open(db) { + db.analyze(document.uri(db)) + } else { + // A retired slot holds no document: its memo releases the old report. + Ok(Arc::default()) + } } /// Query state serialized behind one mutex. Diagnostics computation is the only @@ -136,6 +144,9 @@ struct QueryState { workspace: WorkspaceInput, /// Reusable per-document Salsa inputs keyed by canonical URI. documents: HashMap, + /// Forgotten inputs, emptied and reused by the next opened document, so + /// slots never outnumber the documents open at once. [LSP-PULL-FORGET] + retired: Vec, } impl QueryState { @@ -151,15 +162,54 @@ impl QueryState { database, workspace, documents: HashMap::new(), + retired: Vec::new(), + } + } + + /// The input for a newly opened `uri`: a retired slot when one exists. + fn admit(&mut self, uri: &str) -> DocumentInput { + let document = match self.retired.pop() { + Some(retired) => { + let _empty = retired.set_uri(&mut self.database).to(uri.to_owned()); + let _retired = retired.set_open(&mut self.database).to(true); + retired + } + None => DocumentInput::new(&self.database, uri.to_owned(), 0, 0, true), + }; + let _absent = self.documents.insert(uri.to_owned(), document); + document + } + + /// Retire each closed document: drop its URI and memo, keep its slot. + fn forget(&mut self, uris: &HashSet) { + let closed: Vec = uris + .iter() + .filter_map(|uri| self.documents.remove(uri)) + .collect(); + for document in closed { + let _closed_uri = document.set_uri(&mut self.database).to(String::new()); + let _open = document.set_open(&mut self.database).to(false); + let _released = analyze(&self.database, document, self.workspace); + // Salsa frees a replaced memo only when its next revision starts; + // a write to this retired slot alone starts it. + self.retry(document); + self.retired.push(document); } } + /// Retire every URI queued in `pending`. + fn forget_pending(&mut self, pending: &Mutex>) -> Result<(), PullError> { + let uris = std::mem::take(&mut *pending.lock().map_err(poisoned)?); + self.forget(&uris); + Ok(()) + } + /// Get or create the document handle, then refresh its editor version. fn document(&mut self, uri: &str, version: i32) -> DocumentInput { - let document = *self - .documents - .entry(uri.to_owned()) - .or_insert_with(|| DocumentInput::new(&self.database, uri.to_owned(), 0, 0)); + let document = match self.documents.get(uri) { + Some(open) => *open, + None => self.admit(uri), + }; if *document.version(&self.database) != version { let _previous_version = document.set_version(&mut self.database).to(version); } @@ -221,6 +271,9 @@ pub struct PullStore { revision: Arc, /// Engine generation feeding Salsa without superseding in-flight snapshots. generation: Arc, + /// Closed URIs the next query retires, so a close never waits behind the + /// compiler. Drained by every query. [LSP-PULL-FORGET] + forgotten: Arc>>, } impl PullStore { @@ -240,6 +293,7 @@ impl PullStore { analyzer, revision: Arc::new(AtomicU64::new(1)), generation: Arc::new(AtomicU64::new(1)), + forgotten: Arc::default(), } } @@ -263,6 +317,39 @@ impl PullStore { Ok(()) } + /// Release one closed document's input and memos; every other memo stays + /// warm and no snapshot is superseded. [LSP-PULL-FORGET] + /// + /// When no query holds the lock, the document's diagnostics are freed + /// before this returns. It never waits behind compiler work: while a query + /// runs, the release is queued and the next query retires the document + /// first. A later pull of `uri` recomputes. Forgetting an unknown or + /// already forgotten URI is `Ok`. + /// + /// # Errors + /// Returns an error if a store lock was poisoned. + pub fn forget(&self, uri: &str) -> Result<(), PullError> { + let _queued = self + .forgotten + .lock() + .map_err(poisoned)? + .insert(uri.to_owned()); + self.retire_forgotten() + } + + /// Retire every queued close now, unless a query holds the state: that + /// query calls this again once it releases the state, so none waits for a + /// later pull. Lock order: database owner, query state, then the queue. + fn retire_forgotten(&self) -> Result<(), PullError> { + let shared = Arc::clone(&*self.query.lock().map_err(poisoned)?); + let applied = match shared.try_lock() { + Ok(mut query) => query.forget_pending(&self.forgotten), + Err(TryLockError::WouldBlock) => Ok(()), + Err(TryLockError::Poisoned(source)) => Err(poisoned(source).into()), + }; + applied + } + /// Supersede in-flight work and invalidate inter-file dependencies. /// /// Editor/input changes advance both counters. The pending Salsa generation @@ -325,8 +412,22 @@ impl PullStore { cancel: &CancellationToken, ) -> Result { uncancelled(cancel)?; + let pulled = self.pull_locked(uri, version, cancel); + // Closes that arrived while this query held the state retire now. + self.retire_forgotten()?; + pulled + } + + /// One pull under the query lock, retiring queued closes first. + fn pull_locked( + &self, + uri: &str, + version: i32, + cancel: &CancellationToken, + ) -> Result { let shared = Arc::clone(&*self.query.lock().map_err(poisoned)?); let mut query = shared.lock().map_err(poisoned)?; + query.forget_pending(&self.forgotten)?; let revision = self.query_revision(&shared)?; let generation = self.generation.load(Ordering::Acquire); let report = query.compute(uri, version, revision, generation, cancel)?; @@ -372,12 +473,17 @@ mod memory_tests { #[test] fn cleared_database_cannot_publish_under_the_replacement_revision() { let store = PullStore::new(Arc::new(|_| Ok(Arc::new(Vec::new())))); - let _report = store - .diagnostics("file:///closed.json", 1) - .expect("populate Salsa input"); - let queued = Arc::clone(&store.query.lock().expect("database owner")); + let populated = store.diagnostics("file:///closed.json", 1); + assert!(populated.is_ok(), "populate Salsa input: {populated:?}"); + let owner = store.query.lock(); + assert!(owner.is_ok(), "database owner lock"); + let Ok(owner) = owner else { + return; + }; + let queued = Arc::clone(&*owner); + drop(owner); let allocation = Arc::downgrade(&queued); - store.clear().expect("discard closed inputs"); + assert_eq!(store.clear(), Ok(()), "discard closed inputs"); assert_eq!(store.query_revision(&queued), Err(PullError::Superseded)); drop(queued); assert_eq!( diff --git a/crates/lspkit-server/src/uri.rs b/crates/lspkit-server/src/uri.rs index f2d50ad..64d37ca 100644 --- a/crates/lspkit-server/src/uri.rs +++ b/crates/lspkit-server/src/uri.rs @@ -1,7 +1,8 @@ -//! URI ↔ `PathBuf` conversion. +//! URI ↔ `PathBuf` conversion: the one place lspkit turns file paths into URIs +//! and back. //! //! Handles `file://`, URI authorities, percent-decoding, Windows drive letters, -//! UNC paths and extended-length prefixes ([URI-AUTHORITY]). +//! UNC paths and extended-length prefixes ([URI-AUTHORITY], [URI-WINDOWS]). use std::path::{Path, PathBuf}; @@ -12,7 +13,7 @@ pub enum UriError { /// URI scheme is not `file://`. #[error("unsupported URI scheme: {0}")] UnsupportedScheme(String), - /// Percent-decoded URI is not valid UTF-8, retaining conversion failures. + /// Percent-decoded URI is not valid UTF-8, or holds a malformed `%` escape. #[error("URI is not valid UTF-8 after percent decoding")] InvalidUtf8(#[source] Option), /// Path could not be converted to a URI. @@ -33,6 +34,15 @@ const UNC_PREFIX: &str = r"\\"; const VERBATIM_UNC_PREFIX: &str = r"\\?\UNC\"; /// Windows extended-length prefix of a drive path. const VERBATIM_PREFIX: &str = r"\\?\"; +/// Domain Windows uses to spell an IPv6 literal as a UNC server name. +const IPV6_LITERAL_SUFFIX: &str = ".ipv6-literal.net"; +/// Characters a UNC server name cannot hold, including a URI's userinfo and port. +const HOST_FORBIDDEN: [char; 12] = ['\\', '/', ':', '*', '?', '"', '<', '>', '|', '@', '[', ']']; +/// Device names Win32 resolves in place of a file whose name starts with one. +const DOS_DEVICES: [&str; 22] = [ + "CON", "PRN", "AUX", "NUL", "COM1", "COM2", "COM3", "COM4", "COM5", "COM6", "COM7", "COM8", + "COM9", "LPT1", "LPT2", "LPT3", "LPT4", "LPT5", "LPT6", "LPT7", "LPT8", "LPT9", +]; /// Path syntax of the platform a conversion targets. #[derive(Clone, Copy, Debug)] @@ -53,7 +63,9 @@ const NATIVE: Style = if cfg!(windows) { /// Convert a `file://` URI to an absolute `PathBuf` ([URI-AUTHORITY]). /// /// An empty or `localhost` authority names a local path; any other host -/// names a UNC path on Windows. +/// names a UNC path on Windows. On Windows the drive letter is upper case, +/// the server name lower case, and the `\\?\` form is returned only when +/// Win32 would otherwise open a different file ([URI-WINDOWS]). /// /// # Errors /// Returns [`UriError`] for non-`file` schemes, invalid percent encoding, or @@ -67,90 +79,281 @@ fn uri_to_native(uri: &str, style: Style) -> Result { let rest = uri .strip_prefix(FILE_SCHEME) .ok_or_else(|| UriError::UnsupportedScheme(uri.to_owned()))?; - let (authority, path) = rest.split_at(rest.find('/').unwrap_or(rest.len())); + let (authority, path) = match rest.find('/') { + Some(at) => rest.split_at(at), + None => (rest, ""), + }; let host = percent_decode(authority)?; - let path = percent_decode(path)?; let local = host.is_empty() || host.eq_ignore_ascii_case(LOCAL_HOST); + let no_path = || UriError::NoLocalPath(uri.to_owned()); + let segments = decode_segments(path, style)?.ok_or_else(no_path)?; match (style, local) { - (Style::Unix, true) if path.is_empty() => Ok("/".to_owned()), - (Style::Unix, true) => Ok(path), - (Style::Windows, true) => windows_drive_path(uri, &path), - (Style::Windows, false) if path.trim_matches('/').is_empty() => { - Err(UriError::NoLocalPath(uri.to_owned())) - } - (Style::Windows, false) => Ok(format!("{UNC_PREFIX}{host}{}", path.replace('/', "\\"))), - (Style::Unix, false) => Err(UriError::NoLocalPath(uri.to_owned())), + (Style::Unix, true) => Ok(format!("/{}", segments.join("/"))), + (Style::Unix, false) => Err(no_path()), + (Style::Windows, _) => WindowsPath::from_uri((!local).then_some(host.as_str()), segments) + .map(|parsed| parsed.native()) + .ok_or_else(no_path), } } -/// `/C:/repo/a.rs` → `C:\repo\a.rs`; anything without a drive root is rejected. -fn windows_drive_path(uri: &str, path: &str) -> Result { - let native = path.strip_prefix('/').unwrap_or(path).replace('/', "\\"); - match native.as_bytes() { - [letter, b':', b'\\', ..] if letter.is_ascii_alphabetic() => Ok(native), - _ => Err(UriError::NoLocalPath(uri.to_owned())), +/// Decodes each path segment on its own, so an encoded `%2F` (or `%5C` on +/// Windows) stays inside its name; `None` when a decoded name holds one. +fn decode_segments(path: &str, style: Style) -> Result>, UriError> { + let segments = path + .split('/') + .skip(1) + .map(percent_decode) + .collect::, _>>()?; + let split = segments + .iter() + .any(|segment| segment.contains(style.separators())); + Ok((!split).then_some(segments)) +} + +impl Style { + /// Characters that separate path segments, so never appear inside one. + const fn separators(self) -> &'static [char] { + match self { + Self::Unix => &['/'], + Self::Windows => &['/', '\\'], + } } } /// Convert an absolute filesystem path to a `file://` URI ([URI-AUTHORITY]). /// /// A Windows UNC path keeps its server as the URI authority, and the -/// extended-length prefixes `\\?\` and `\\?\UNC\` are removed. +/// extended-length prefixes `\\?\` and `\\?\UNC\` are removed. One file has +/// one URI: drive letters are upper case, server names lower case, and +/// repeated separators collapse ([URI-WINDOWS]). /// /// # Errors -/// Returns [`UriError::InvalidPath`] if the path is not absolute or contains -/// components that cannot be expressed in a URI. +/// Returns [`UriError::InvalidPath`] if the path is not absolute, is a +/// Windows device path (`\\.\`, `\\?\Volume{…}`), or contains components +/// that cannot be expressed in a URI. pub fn path_to_uri(path: &Path) -> Result { - if !path.is_absolute() { - return Err(UriError::InvalidPath(path.to_path_buf())); - } - path.to_str() - .map(|text| native_to_uri(text, NATIVE)) + Some(path) + .filter(|path| path.is_absolute()) + .and_then(Path::to_str) + .and_then(|text| native_to_uri(text, NATIVE)) .ok_or_else(|| UriError::InvalidPath(path.to_path_buf())) } -/// Renders an absolute path written in `style` as a `file://` URI. -fn native_to_uri(path: &str, style: Style) -> String { +/// Renders an absolute path written in `style` as a `file://` URI, or `None` +/// when it names no file a URI can reach. +fn native_to_uri(path: &str, style: Style) -> Option { match style { - Style::Unix => format!("{FILE_SCHEME}{}", percent_encode(path)), - Style::Windows => windows_to_uri(&without_verbatim(path).replace('\\', "/")), + Style::Unix => Some(format!("{FILE_SCHEME}{}", percent_encode(path))), + Style::Windows => WindowsPath::from_native(path).map(|parsed| parsed.uri()), } } -/// `//server/share/a.rs` → `file://server/share/a.rs`; `C:/a.rs` → `file:///C:/a.rs`. -fn windows_to_uri(forward: &str) -> String { - match forward.strip_prefix("//") { - Some(unc) => { - let (host, path) = unc.split_at(unc.find('/').unwrap_or(unc.len())); - format!( - "{FILE_SCHEME}{}{}", +/// A native Windows path split on both separators. +fn native_parts(path: &str) -> impl Iterator + '_ { + path.split(['\\', '/']).map(str::to_owned) +} + +/// Where an absolute Windows path is rooted. +enum Root { + /// `C:\`; the letter is upper case. + Drive(char), + /// `\\server\share`; the server name is lower case. + Unc { + /// Server name. + host: String, + /// Share name. + share: String, + }, +} + +/// An absolute Windows path with no empty, `.` or `..` segments: the single +/// form both conversion directions parse into and render from. +struct WindowsPath { + /// Drive or network root. + root: Root, + /// Folder and file names below the root. + segments: Vec, + /// Whether the path ends in a separator (`C:\repo\`). + trailing: bool, +} + +impl WindowsPath { + /// Parses a URI's decoded path segments; `host` is `None` for the local machine. + /// `file:////server/share` (an empty authority before a UNC path) is accepted. + fn from_uri(host: Option<&str>, segments: Vec) -> Option { + let mut parts = segments.into_iter(); + match host { + Some(host) => Self::unc(host, parts), + None => match parts.next()? { + first if first.is_empty() => { + let host = parts.next()?; + Self::unc(&host, parts) + } + first => Self::drive(&first, parts), + }, + } + } + + /// Parses an absolute Windows path, removing an extended-length prefix. + /// Device paths (`\\.\`, `\\?\Volume{…}`) name no file and give `None`. + fn from_native(path: &str) -> Option { + if let Some(unc) = path.strip_prefix(VERBATIM_UNC_PREFIX) { + let mut parts = native_parts(unc); + let host = parts.next()?; + return Self::unc(&host, parts); + } + let (verbatim, body) = match path.strip_prefix(VERBATIM_PREFIX) { + Some(drive) => (true, drive), + None => (false, path), + }; + let mut parts = native_parts(body); + let first = parts.next()?; + if verbatim || !first.is_empty() { + return Self::drive(&first, parts); + } + Self::unc_after_root(parts) + } + + /// A leading separator must be the pair that opens `\\server\share`. + fn unc_after_root(mut parts: impl Iterator) -> Option { + let host = parts + .next() + .filter(String::is_empty) + .and_then(|_| parts.next())?; + Self::unc(&host, parts) + } + + /// `C:` followed by at least one separator. + fn drive(first: &str, rest: impl Iterator) -> Option { + let letter = match first.as_bytes() { + [letter, b':'] if letter.is_ascii_alphabetic() => { + char::from(letter.to_ascii_uppercase()) + } + _ => return None, + }; + let mut rest = rest.peekable(); + rest.peek() + .is_some() + .then(|| Self::below(Root::Drive(letter), rest)) + } + + /// `\\server\share`, with a reachable server name and a non-empty share. + fn unc(host: &str, mut rest: impl Iterator) -> Option { + let host = unc_host(host)?; + let share = rest.next().filter(|share| !share.is_empty())?; + Some(Self::below(Root::Unc { host, share }, rest)) + } + + /// Resolves `.`, `..` and empty segments lexically, as Win32 does; `..` + /// never climbs above the root. + fn below(root: Root, raw: impl Iterator) -> Self { + let mut segments = Vec::new(); + let mut trailing = false; + for segment in raw { + trailing = segment.is_empty(); + match segment.as_str() { + "" | "." => {} + ".." => segments.truncate(segments.len().saturating_sub(1)), + _ => segments.push(segment), + } + } + Self { + root, + segments, + trailing, + } + } + + /// The native path, extended-length only when Win32 would otherwise + /// rewrite a segment into a different file. + fn native(&self) -> String { + let verbatim = self.segments.iter().any(|segment| win32_rewrites(segment)); + let head = match (&self.root, verbatim) { + (Root::Drive(letter), false) => format!(r"{letter}:\"), + (Root::Drive(letter), true) => format!(r"{VERBATIM_PREFIX}{letter}:\"), + (Root::Unc { host, share }, false) => format!(r"{UNC_PREFIX}{host}\{share}"), + (Root::Unc { host, share }, true) => format!(r"{VERBATIM_UNC_PREFIX}{host}\{share}"), + }; + self.render(head, '\\', str::to_owned) + } + + /// The canonical `file://` URI. + fn uri(&self) -> String { + let head = match &self.root { + Root::Drive(letter) => format!("{FILE_SCHEME}/{letter}:/"), + Root::Unc { host, share } => format!( + "{FILE_SCHEME}{}/{}", percent_encode(host), - percent_encode(path) - ) + percent_encode(share) + ), + }; + self.render(head, '/', percent_encode) + } + + /// Writes `head`, then each segment through `spell`, separated by `separator`. + fn render(&self, head: String, separator: char, spell: impl Fn(&str) -> String) -> String { + let mut out = head; + for segment in &self.segments { + if !out.ends_with(separator) { + out.push(separator); + } + out.push_str(&spell(segment)); + } + if self.trailing && !out.ends_with(separator) { + out.push(separator); } - None => format!("{FILE_SCHEME}/{}", percent_encode(forward)), + out } } -/// Removes the extended-length prefix `std::fs::canonicalize` adds on Windows. -fn without_verbatim(path: &str) -> String { - path.strip_prefix(VERBATIM_UNC_PREFIX) - .map(|unc| format!("{UNC_PREFIX}{unc}")) - .or_else(|| path.strip_prefix(VERBATIM_PREFIX).map(str::to_owned)) - .unwrap_or_else(|| path.to_owned()) +/// The lower-case UNC server name for a URI host or path server, with an +/// IPv6 literal `[fe80::1]` spelled `fe80--1.ipv6-literal.net`. Device +/// namespaces (`.`, `?`), ports and userinfo name no server. +fn unc_host(raw: &str) -> Option { + let host = match raw + .strip_prefix('[') + .and_then(|inner| inner.strip_suffix(']')) + { + Some(ipv6) => format!( + "{}{IPV6_LITERAL_SUFFIX}", + ipv6.replace(':', "-").replace('%', "s") + ), + None => raw.to_owned(), + }; + let reachable = !matches!(host.as_str(), "" | "." | "?") && !host.contains(HOST_FORBIDDEN); + reachable.then(|| host.to_ascii_lowercase()) } -/// Decodes complete percent triplets and preserves incomplete trailing input. +/// Whether Win32 opens something other than `segment` when it is written +/// without `\\?\`: a trailing dot or space is dropped, and a DOS device name +/// opens the device. +fn win32_rewrites(segment: &str) -> bool { + let stem = match segment.split_once('.') { + Some((stem, _extension)) => stem, + None => segment, + } + .trim_end_matches(' '); + segment.ends_with(['.', ' ']) + || DOS_DEVICES + .iter() + .any(|device| device.eq_ignore_ascii_case(stem)) +} + +/// Decodes `%XX` escapes; a `%` not followed by two hex digits is an error. fn percent_decode(input: &str) -> Result { let mut out = Vec::with_capacity(input.len()); let mut rest = input.as_bytes(); while let Some((&byte, tail)) = rest.split_first() { - rest = if let (b'%', [hi, lo, after @ ..]) = (byte, tail) { - out.push((hex_digit(*hi)? << 4) | hex_digit(*lo)?); - after - } else { - out.push(byte); - tail + rest = match (byte, tail) { + (b'%', [hi, lo, after @ ..]) => { + out.push((hex_digit(*hi)? << 4) | hex_digit(*lo)?); + after + } + (b'%', _) => return Err(UriError::InvalidUtf8(None)), + _ => { + out.push(byte); + tail + } }; } String::from_utf8(out).map_err(|source| UriError::InvalidUtf8(Some(source))) diff --git a/crates/lspkit-server/src/uri_cases.rs b/crates/lspkit-server/src/uri_cases.rs new file mode 100644 index 0000000..cd23d5a --- /dev/null +++ b/crates/lspkit-server/src/uri_cases.rs @@ -0,0 +1,244 @@ +//! [URI-WINDOWS] The Windows conversion table from issue #49, kept once and +//! run twice: by `uri_style_tests.rs` against the Windows rules on every OS, +//! and by `tests/uri_windows.rs` through the public API on a Windows build. +//! Test data only; nothing here ships in the library. + +/// A URI, the exact Windows path it decodes to, and the canonical URI that +/// path encodes back to (which decodes to the same path again). +pub(crate) const ROUND_TRIPS: [(&str, &str, &str); 35] = [ + // Drive letters: one URI per drive, the letter upper case. + ( + "file:///C:/repo/a.rs", + r"C:\repo\a.rs", + "file:///C:/repo/a.rs", + ), + ( + "file:///c:/repo/a.rs", + r"C:\repo\a.rs", + "file:///C:/repo/a.rs", + ), + ( + "file:///C%3A/repo/a.rs", + r"C:\repo\a.rs", + "file:///C:/repo/a.rs", + ), + ( + "file:///c%3a/my%20repo/a.rs", + r"C:\my repo\a.rs", + "file:///C:/my%20repo/a.rs", + ), + ("file:///C:/", r"C:\", "file:///C:/"), + ( + "file:///D:/deep/x/y/z/a.rs", + r"D:\deep\x\y\z\a.rs", + "file:///D:/deep/x/y/z/a.rs", + ), + ( + "file://localhost/C:/repo/a.rs", + r"C:\repo\a.rs", + "file:///C:/repo/a.rs", + ), + ( + "file://LOCALHOST/C:/repo/a.rs", + r"C:\repo\a.rs", + "file:///C:/repo/a.rs", + ), + ("file:///C:/repo/", r"C:\repo\", "file:///C:/repo/"), + ( + "file:///C:/repo/./sub/../a.rs", + r"C:\repo\a.rs", + "file:///C:/repo/a.rs", + ), + ( + "file:///C:/PROGRA~1/a.rs", + r"C:\PROGRA~1\a.rs", + "file:///C:/PROGRA~1/a.rs", + ), + // UNC and network: the host is the server, lower case. + ( + "file://server/share/a.rs", + r"\\server\share\a.rs", + "file://server/share/a.rs", + ), + ( + "file://server/share/", + r"\\server\share\", + "file://server/share/", + ), + ( + "file://server/share", + r"\\server\share", + "file://server/share", + ), + ( + "file:////server/share/a.rs", + r"\\server\share\a.rs", + "file://server/share/a.rs", + ), + ( + "file://SERVER/Share/a.rs", + r"\\server\Share\a.rs", + "file://server/Share/a.rs", + ), + ( + "file://wsl.localhost/Ubuntu/home/a.rs", + r"\\wsl.localhost\Ubuntu\home\a.rs", + "file://wsl.localhost/Ubuntu/home/a.rs", + ), + ( + "file://wsl$/Ubuntu/home/a.rs", + r"\\wsl$\Ubuntu\home\a.rs", + "file://wsl%24/Ubuntu/home/a.rs", + ), + ( + "file://192.168.1.5/share/a.rs", + r"\\192.168.1.5\share\a.rs", + "file://192.168.1.5/share/a.rs", + ), + ( + "file://fe80--1.ipv6-literal.net/share/a.rs", + r"\\fe80--1.ipv6-literal.net\share\a.rs", + "file://fe80--1.ipv6-literal.net/share/a.rs", + ), + ( + "file://[FE80::1%25eth0]/share/a.rs", + r"\\fe80--1seth0.ipv6-literal.net\share\a.rs", + "file://fe80--1seth0.ipv6-literal.net/share/a.rs", + ), + ( + "file://build%2Dhost/share%20one/a.rs", + r"\\build-host\share one\a.rs", + "file://build-host/share%20one/a.rs", + ), + // Names Win32 would rewrite keep the `\\?\` form, so they open the same file. + ( + "file:///C:/repo/name./a.rs", + r"\\?\C:\repo\name.\a.rs", + "file:///C:/repo/name./a.rs", + ), + ( + "file:///C:/repo/name%20/a.rs", + r"\\?\C:\repo\name \a.rs", + "file:///C:/repo/name%20/a.rs", + ), + ( + "file:///C:/repo/NUL", + r"\\?\C:\repo\NUL", + "file:///C:/repo/NUL", + ), + ( + "file:///C:/repo/CON/a.rs", + r"\\?\C:\repo\CON\a.rs", + "file:///C:/repo/CON/a.rs", + ), + ( + "file:///C:/repo/aux.rs", + r"\\?\C:\repo\aux.rs", + "file:///C:/repo/aux.rs", + ), + ( + "file://server/share/name./a.rs", + r"\\?\UNC\server\share\name.\a.rs", + "file://server/share/name./a.rs", + ), + // Characters that must be percent-encoded, and a literal `%20` in a name. + ( + "file:///C:/a%20%23%25%3F%26%2B%3B%3D%40%5B%5D%7B%7D%27%60%5E~/b.rs", + r"C:\a #%?&+;=@[]{}'`^~\b.rs", + "file:///C:/a%20%23%25%3F%26%2B%3B%3D%40%5B%5D%7B%7D%27%60%5E~/b.rs", + ), + ( + "file:///C:/a%2520b.rs", + r"C:\a%20b.rs", + "file:///C:/a%2520b.rs", + ), + ( + "file:///C:/a%252Fb%255Cc.rs", + r"C:\a%2Fb%5Cc.rs", + "file:///C:/a%252Fb%255Cc.rs", + ), + // Unicode: precomposed, CJK, astral, combining mark, right-to-left. + ( + "file:///C:/caf%C3%A9.rs", + "C:\\caf\u{e9}.rs", + "file:///C:/caf%C3%A9.rs", + ), + ( + "file:///C:/%E6%97%A5%E6%9C%AC%E8%AA%9E/%F0%9F%A6%80.rs", + "C:\\\u{65e5}\u{672c}\u{8a9e}\\\u{1f980}.rs", + "file:///C:/%E6%97%A5%E6%9C%AC%E8%AA%9E/%F0%9F%A6%80.rs", + ), + ( + "file:///C:/e%CC%81.rs", + "C:\\e\u{301}.rs", + "file:///C:/e%CC%81.rs", + ), + ( + "file:///C:/%D7%A9%D7%9C%D7%95%D7%9D.rs", + "C:\\\u{5e9}\u{5dc}\u{5d5}\u{5dd}.rs", + "file:///C:/%D7%A9%D7%9C%D7%95%D7%9D.rs", + ), +]; + +/// Windows paths a URI is made from, and the one canonical URI each gets. +pub(crate) const PATHS_TO_URIS: [(&str, &str); 14] = [ + (r"C:/repo\sub/a.rs", "file:///C:/repo/sub/a.rs"), + (r"C:\repo\\double\a.rs", "file:///C:/repo/double/a.rs"), + (r"C:\Repo\a.rs", "file:///C:/Repo/a.rs"), + (r"c:\Repo\a.rs", "file:///C:/Repo/a.rs"), + (r"\\?\C:\repo\a.rs", "file:///C:/repo/a.rs"), + (r"\\?\c:\repo\a.rs", "file:///C:/repo/a.rs"), + (r"\\?\UNC\server\share\a.rs", "file://server/share/a.rs"), + (r"\\?\C:\repo\name.", "file:///C:/repo/name."), + (r"\\server\share\a.rs", "file://server/share/a.rs"), + (r"\\SERVER\share\a.rs", "file://server/share/a.rs"), + (r"\\server\share\", "file://server/share/"), + (r"//server/share/a.rs", "file://server/share/a.rs"), + (r"\\wsl$\Ubuntu\home\a.rs", "file://wsl%24/Ubuntu/home/a.rs"), + ( + r"\\wsl.localhost\Ubuntu\home\a.rs", + "file://wsl.localhost/Ubuntu/home/a.rs", + ), +]; + +/// Windows paths that name no file a URI can reach: device namespaces, +/// volume GUIDs, a server with no share, and paths without a drive root. +pub(crate) const PATHS_REJECTED: [&str; 9] = [ + r"\\.\C:\a.rs", + r"\\?\Volume{00000000-0000-0000-0000-000000000000}\", + r"\\?\GLOBALROOT\Device\x", + r"\\server\", + r"\\.\pipe\name", + r"C:", + r"C:a.rs", + r"\a.rs", + r"repo\a.rs", +]; + +/// URIs that name no absolute Windows path, with the `UriError` variant each +/// is rejected with. None may decode to a relative path or panic. +pub(crate) const URIS_REJECTED: [(&str, &str); 23] = [ + ("file:repo/a.rs", "UnsupportedScheme"), + ("http://x/a.rs", "UnsupportedScheme"), + ("", "UnsupportedScheme"), + ("file://", "NoLocalPath"), + ("file:///", "NoLocalPath"), + ("file:///repo/a.rs", "NoLocalPath"), + ("file:///tmp/a.rs", "NoLocalPath"), + ("file:///C:", "NoLocalPath"), + ("file://server", "NoLocalPath"), + ("file://server/", "NoLocalPath"), + ("file://server:8080/share/a.rs", "NoLocalPath"), + ("file://user@server/share/a.rs", "NoLocalPath"), + ("file://./C:/a.rs", "NoLocalPath"), + ("file://%3F/C:/a.rs", "NoLocalPath"), + ("file:///C:/a%2Fb.rs", "NoLocalPath"), + ("file:///C:/a%5Cb.rs", "NoLocalPath"), + ("file://host%5Cshare%5Cx/a.rs", "NoLocalPath"), + ("file://host%2Fshare/a.rs", "NoLocalPath"), + ("file:///C:/repo/a%2F..%2F..%2Fwindows.rs", "NoLocalPath"), + ("file:///C:/a%ZZ.rs", "InvalidUtf8"), + ("file:///C:/a%", "InvalidUtf8"), + ("file:///C:/a%2", "InvalidUtf8"), + ("file:///C:/%FF%FE.rs", "InvalidUtf8"), +]; diff --git a/crates/lspkit-server/src/uri_style_tests.rs b/crates/lspkit-server/src/uri_style_tests.rs index 3582396..0af9edd 100644 --- a/crates/lspkit-server/src/uri_style_tests.rs +++ b/crates/lspkit-server/src/uri_style_tests.rs @@ -1,75 +1,69 @@ -//! [URI-AUTHORITY] Path-style rules, isolated so the Linux CI runs the Windows -//! rules too: the public functions only take the Windows branch on a Windows build. +//! [URI-AUTHORITY] [URI-WINDOWS] Path-style rules, isolated so the Linux CI +//! runs the Windows rules too: the public functions only take the Windows +//! branch on a Windows build, where `tests/uri_windows.rs` runs the same table. use super::{native_to_uri, uri_to_native, Style, UriError}; -/// URIs, the Windows path each names, and the URI that path encodes back to. -const WINDOWS_ROUND_TRIPS: [(&str, &str, &str); 5] = [ - ( - "file:///C:/repo/a.rs", - r"C:\repo\a.rs", - "file:///C:/repo/a.rs", - ), - ( - "file:///c%3A/my%20repo/a.rs", - r"c:\my repo\a.rs", - "file:///c:/my%20repo/a.rs", - ), - ("file://localhost/D:/a.rs", r"D:\a.rs", "file:///D:/a.rs"), - ( - "file://server/share/a.rs", - r"\\server\share\a.rs", - "file://server/share/a.rs", - ), - ( - "file://wsl.localhost/Ubuntu/home/a.rs", - r"\\wsl.localhost\Ubuntu\home\a.rs", - "file://wsl.localhost/Ubuntu/home/a.rs", - ), -]; - -/// Extended-length paths `std::fs::canonicalize` returns, and their URIs. -const VERBATIM_PATHS: [(&str, &str); 2] = [ - (r"\\?\C:\repo\a.rs", "file:///C:/repo/a.rs"), - (r"\\?\UNC\server\share\a.rs", "file://server/share/a.rs"), -]; +#[path = "uri_cases.rs"] +mod cases; -/// URIs that name no absolute Windows path. -const NO_WINDOWS_PATH: [&str; 4] = [ - "file:///tmp/a.rs", - "file:///C:", - "file://server", - "file://server/", -]; +#[test] +fn windows_uris_decode_to_exact_paths_and_round_trip() { + for (uri, path, canonical) in cases::ROUND_TRIPS { + assert_eq!( + uri_to_native(uri, Style::Windows).ok().as_deref(), + Some(path), + "{uri}" + ); + assert_eq!( + native_to_uri(path, Style::Windows).as_deref(), + Some(canonical), + "{path}" + ); + assert_eq!( + uri_to_native(canonical, Style::Windows).ok().as_deref(), + Some(path), + "{canonical} round-trips" + ); + } +} #[test] -fn windows_uris_decode_to_absolute_drive_and_unc_paths_and_round_trip() { - for (uri, path, encoded) in WINDOWS_ROUND_TRIPS { - let decoded = uri_to_native(uri, Style::Windows).expect("a Windows URI decodes"); - assert_eq!(decoded, path, "{uri}"); - assert_eq!(native_to_uri(&decoded, Style::Windows), encoded, "{path}"); - let again = uri_to_native(encoded, Style::Windows).expect("re-decodes"); - assert_eq!(again, path, "{encoded} round-trips"); +fn windows_paths_encode_to_one_canonical_uri() { + for (path, uri) in cases::PATHS_TO_URIS { + assert_eq!( + native_to_uri(path, Style::Windows).as_deref(), + Some(uri), + "{path}" + ); + let decoded = uri_to_native(uri, Style::Windows); + assert!( + decoded.as_ref().is_ok_and(|native| !native.contains('/')), + "{uri} decodes with native separators: {decoded:?}" + ); } } #[test] -fn windows_extended_length_prefixes_are_removed_from_uris() { - for (path, uri) in VERBATIM_PATHS { - assert_eq!(native_to_uri(path, Style::Windows), uri, "{path}"); - let decoded = uri_to_native(uri, Style::Windows).expect("decodes"); - assert!(!decoded.contains('?'), "{uri} decodes without a prefix"); +fn windows_device_and_rootless_paths_have_no_uri() { + for path in cases::PATHS_REJECTED { + assert_eq!(native_to_uri(path, Style::Windows), None, "{path}"); } } #[test] fn windows_uris_without_an_absolute_path_are_rejected() { - for uri in NO_WINDOWS_PATH { - let err = uri_to_native(uri, Style::Windows).expect_err("no absolute path"); + for (uri, variant) in cases::URIS_REJECTED { + let rejected = uri_to_native(uri, Style::Windows); assert!( - matches!(&err, UriError::NoLocalPath(rejected) if rejected == uri), - "{uri}: {err:?}" + rejected + .as_ref() + .is_err_and(|err| format!("{err:?}").starts_with(variant)), + "{uri} must be {variant}: {rejected:?}" ); + if let Err(UriError::NoLocalPath(named)) = &rejected { + assert_eq!(named, uri, "the error names the URI"); + } } } @@ -85,21 +79,31 @@ const UNIX_ROUND_TRIPS: [(&str, &str); 3] = [ fn both_styles_share_one_authority_parser() { for (uri, path) in UNIX_ROUND_TRIPS { assert_eq!( - uri_to_native(uri, Style::Unix).expect("local"), - path, + uri_to_native(uri, Style::Unix).ok().as_deref(), + Some(path), "{uri}" ); } assert_eq!( - native_to_uri("/tmp/a b.rs", Style::Unix), - "file:///tmp/a%20b.rs" + native_to_uri("/tmp/a b.rs", Style::Unix).as_deref(), + Some("file:///tmp/a%20b.rs") ); for style in [Style::Unix, Style::Windows] { - let remote = uri_to_native("file://server/", style).expect_err("no share path"); - assert!(matches!(remote, UriError::NoLocalPath(_)), "{style:?}"); - let scheme = uri_to_native("https://server/a.rs", style).expect_err("not file"); + let remote = uri_to_native("file://server/", style); + assert!(matches!(remote, Err(UriError::NoLocalPath(_))), "{style:?}"); + let scheme = uri_to_native("https://server/a.rs", style); + assert!( + matches!(scheme, Err(UriError::UnsupportedScheme(_))), + "{style:?}" + ); + let encoded = uri_to_native("file:///C:/a%2Fb", style); + assert!( + matches!(encoded, Err(UriError::NoLocalPath(_))), + "{style:?}" + ); + let truncated = uri_to_native("file:///C:/a%", style); assert!( - matches!(scheme, UriError::UnsupportedScheme(_)), + matches!(truncated, Err(UriError::InvalidUtf8(None))), "{style:?}" ); } diff --git a/crates/lspkit-server/tests/common/held_source/windows.rs b/crates/lspkit-server/tests/common/held_source/windows.rs index 38f9de9..585a570 100644 --- a/crates/lspkit-server/tests/common/held_source/windows.rs +++ b/crates/lspkit-server/tests/common/held_source/windows.rs @@ -23,8 +23,12 @@ impl NativeSource { /// Write the real held source and start the owner holding it. pub fn new(source: &Path) -> StepResult { std::fs::write(source, b"{\"unfinished\":")?; - let mut child = spawn_owner(source); - let stdout = child.stdout.take().ok_or("oplock stdout")?; + let mut child = spawn_owner(source)?; + let Some(stdout) = child.stdout.take() else { + child.kill()?; + let _killed = child.wait()?; + return Err("oplock stdout".into()); + }; let mut held = HeldRead { child, output: BufReader::new(stdout), @@ -42,7 +46,7 @@ impl NativeSource { } /// Run the owned native fixture invisibly, with no persistent policy changes. -fn spawn_owner(source: &Path) -> Child { +fn spawn_owner(source: &Path) -> std::io::Result { let script = Path::new(env!("CARGO_MANIFEST_DIR")).join("../../test-support/windows-oplock/hold.ps1"); owner::spawn(&script, source) diff --git a/crates/lspkit-server/tests/jsonrpc.rs b/crates/lspkit-server/tests/jsonrpc.rs index 09712f1..de04e34 100644 --- a/crates/lspkit-server/tests/jsonrpc.rs +++ b/crates/lspkit-server/tests/jsonrpc.rs @@ -1,5 +1,6 @@ //! End-to-end tests for [`lspkit_server::jsonrpc`] framing and types. +use lspkit_server::jsonrpc::{read_message_within, MAX_FRAME_BYTES, MAX_HEADER_BYTES}; use lspkit_server::{FramingError, JsonRpcError, Message, MessageWriter, RequestId}; use serde_json::{json, Value}; use tokio::io::BufReader; @@ -298,3 +299,134 @@ fn message_clone_preserves_fields() { assert_eq!(cloned.method, msg.method); assert_eq!(cloned.id, msg.id); } + +/// A frame whose header claims `length` bytes and that carries `body`. +fn framed(length: impl std::fmt::Display, body: &[u8]) -> Vec { + let mut raw = format!("Content-Length: {length}\r\n\r\n").into_bytes(); + raw.extend_from_slice(body); + raw +} + +/// A notification whose JSON body is exactly `bytes` long. +fn body_of_exactly(bytes: usize) -> Vec { + let shell = br#"{"jsonrpc":"2.0","method":"x","params":[""]}"#.len(); + let pad = "a".repeat(bytes - shell); + format!(r#"{{"jsonrpc":"2.0","method":"x","params":["{pad}"]}}"#).into_bytes() +} + +/// Decode `raw` under a server-chosen cap of `max` body bytes. +async fn decode_within(raw: Vec, max: usize) -> Result { + read_message_within(&mut BufReader::new(std::io::Cursor::new(raw)), max).await +} + +/// [JSONRPC-FRAME-LIMIT] A hostile or garbled `Content-Length` is an error, +/// never a panic and never an allocation of the claimed size. +#[tokio::test] +async fn oversized_content_length_is_rejected_before_any_body_is_read() { + let over_default = u64::try_from(MAX_FRAME_BYTES).expect("the cap fits u64") + 1; + for claimed in [u64::MAX, over_default] { + let err = decode_err(framed(claimed, b"{}")) + .await + .expect_err("over the cap"); + assert!( + matches!(err, FramingError::FrameTooLarge { claimed: c, max } if c == claimed && max == MAX_FRAME_BYTES), + "{claimed}: {err:?}" + ); + assert_eq!( + err.to_string(), + format!("frame of {claimed} bytes exceeds the {MAX_FRAME_BYTES}-byte limit") + ); + } + let beyond_u64 = decode_err(framed("18446744073709551616", b"{}")) + .await + .expect_err("not a u64"); + assert!( + matches!(beyond_u64, FramingError::BadHeader(..)), + "{beyond_u64:?}" + ); +} + +/// [JSONRPC-FRAME-LIMIT] A cap admits a body of exactly its size, rejects one +/// byte more, and a body shorter than its claim is an unexpected EOF. +#[tokio::test] +async fn a_cap_admits_its_limit_and_rejects_one_byte_more() { + const CAP: usize = 256; + let at_cap = body_of_exactly(CAP); + assert_eq!(at_cap.len(), CAP); + let decoded = decode_within(framed(CAP, &at_cap), CAP) + .await + .expect("a body at the cap decodes"); + assert_eq!(decoded.method.as_deref(), Some("x")); + let over = body_of_exactly(CAP + 1); + let err = decode_within(framed(CAP + 1, &over), CAP) + .await + .expect_err("one byte over the cap"); + assert!( + matches!( + err, + FramingError::FrameTooLarge { + claimed: 257, + max: CAP + } + ), + "{err:?}" + ); + let largest = body_of_exactly(MAX_FRAME_BYTES); + let decoded = decode_ok(framed(MAX_FRAME_BYTES, &largest)) + .await + .expect("a body at the default cap decodes"); + assert_eq!(decoded.method.as_deref(), Some("x")); + let short = decode_within(framed(CAP, b"{}"), CAP) + .await + .expect_err("fewer bytes than claimed"); + assert!( + matches!(&short, FramingError::Io(io) if io.kind() == std::io::ErrorKind::UnexpectedEof), + "{short:?}" + ); +} + +/// [`MAX_HEADER_BYTES`] as a length; the test checks the two agree. +const HEADER_CAP: usize = 8 * 1024; + +/// [JSONRPC-FRAME-LIMIT] An endless header line, or endless short header +/// lines, end in `HeaderTooLarge`; a block of exactly the cap still decodes. +#[tokio::test] +async fn header_blocks_past_the_cap_are_rejected_and_one_at_the_cap_decodes() { + assert_eq!(u64::try_from(HEADER_CAP), Ok(MAX_HEADER_BYTES)); + let endless_line = format!("Content-Length: 2\r\nX-Junk: {}", "A".repeat(HEADER_CAP)); + let many_lines = format!("Content-Length: 2\r\n{}", "X: y\r\n".repeat(HEADER_CAP)); + for (name, raw) in [ + ("one endless line", endless_line), + ("endless short lines", many_lines), + ] { + let err = decode_err(raw.into_bytes()) + .await + .expect_err("past the header cap"); + assert!( + matches!(err, FramingError::HeaderTooLarge), + "{name}: {err:?}" + ); + assert_eq!( + err.to_string(), + format!("header block exceeds the {MAX_HEADER_BYTES}-byte limit") + ); + } + let head = "Content-Length: 2\r\nX-Pad: "; + let tail = "\r\n\r\n"; + let pad = "p".repeat(HEADER_CAP - head.len() - tail.len()); + let at_cap = format!("{head}{pad}{tail}"); + assert_eq!(at_cap.len(), HEADER_CAP); + let mut raw = at_cap.clone().into_bytes(); + raw.extend_from_slice(b"{}"); + let decoded = decode_err(raw).await; + assert!( + matches!(&decoded, Err(FramingError::BadJson(_))), + "a header block at the cap is read in full and reaches the body: {decoded:?}" + ); + let mut over = format!("{head}{pad}p{tail}").into_bytes(); + over.extend_from_slice(b"{}"); + let err = decode_err(over) + .await + .expect_err("one byte over the header cap"); + assert!(matches!(err, FramingError::HeaderTooLarge), "{err:?}"); +} diff --git a/crates/lspkit-server/tests/jsonrpc_frame_heap.rs b/crates/lspkit-server/tests/jsonrpc_frame_heap.rs new file mode 100644 index 0000000..4500bc3 --- /dev/null +++ b/crates/lspkit-server/tests/jsonrpc_frame_heap.rs @@ -0,0 +1,68 @@ +//! [JSONRPC-FRAME-LIMIT] [MEM-FIX-SCOPE] A body shorter than its claimed length +//! costs only the bytes that arrived. Single-test binary: the instrumented +//! allocator sees no sibling tests. + +use std::io::{Cursor, ErrorKind}; + +use lspkit_server::jsonrpc::{read_message, MAX_FRAME_BYTES, MAX_HEADER_BYTES}; +use lspkit_server::{FramingError, Message}; +use tokio::io::BufReader; + +#[path = "../../../test-support/state.rs"] +mod state; + +/// The bytes that actually arrive before the peer hangs up. +const SENT: &[u8] = br#"{"jsonrpc":"2.0","method""#; +/// Far above a short read's real cost, far below the claimed body. +const HEAP_BOUND: usize = 64 * 1024; + +/// An endless header line: 64 header caps of `A` and no newline. +const ENDLESS_HEADER_BYTES: usize = 64 * 8 * 1024; + +/// One read of `raw`, a header line that never ends; the caller owns its bytes. +fn endless_header(runtime: &tokio::runtime::Runtime, raw: &[u8]) -> Result { + runtime.block_on(read_message(&mut BufReader::new(raw))) +} + +/// One read of a frame claiming the largest allowed body but carrying `SENT`. +fn short_read(runtime: &tokio::runtime::Runtime) -> Result { + let mut raw = format!("Content-Length: {MAX_FRAME_BYTES}\r\n\r\n").into_bytes(); + raw.extend_from_slice(SENT); + runtime.block_on(read_message(&mut BufReader::new(Cursor::new(raw)))) +} + +#[test] +fn a_short_body_allocates_what_arrived_not_what_was_claimed() { + let runtime = tokio::runtime::Builder::new_current_thread() + .build() + .expect("a runtime"); + let mut outcome = None; + let stats = state::heap_change(|| outcome = Some(short_read(&runtime))); + assert!( + stats.bytes_allocated < HEAP_BOUND, + "a {}-byte body claiming {MAX_FRAME_BYTES} allocated {} bytes", + SENT.len(), + stats.bytes_allocated + ); + let err = outcome + .expect("the read ran") + .expect_err("the body is short"); + assert!( + matches!(&err, FramingError::Io(io) if io.kind() == ErrorKind::UnexpectedEof), + "{err:?}" + ); + state::assert_releases_heap(|| drop(short_read(&runtime))); + + let raw = vec![b'A'; ENDLESS_HEADER_BYTES]; + let mut header = None; + let stats = state::heap_change(|| header = Some(endless_header(&runtime, &raw))); + assert!( + stats.bytes_allocated < HEAP_BOUND, + "a {ENDLESS_HEADER_BYTES}-byte header line under a {MAX_HEADER_BYTES}-byte cap allocated {} bytes", + stats.bytes_allocated + ); + let err = header + .expect("the read ran") + .expect_err("the header never ends"); + assert!(matches!(err, FramingError::HeaderTooLarge), "{err:?}"); +} diff --git a/crates/lspkit-server/tests/mcp_adapter.rs b/crates/lspkit-server/tests/mcp_adapter.rs index 092fcec..0773310 100644 --- a/crates/lspkit-server/tests/mcp_adapter.rs +++ b/crates/lspkit-server/tests/mcp_adapter.rs @@ -1,7 +1,5 @@ //! Coverage for [`lspkit_server::mcp::Adapter`] construction, dispatch, and errors. -#![cfg(feature = "mcp")] - #[path = "mcp_common/mod.rs"] mod common; diff --git a/crates/lspkit-server/tests/mcp_tools.rs b/crates/lspkit-server/tests/mcp_tools.rs index 1d64deb..8f0e1aa 100644 --- a/crates/lspkit-server/tests/mcp_tools.rs +++ b/crates/lspkit-server/tests/mcp_tools.rs @@ -1,7 +1,5 @@ //! Coverage for `ToolDescription` and `ToolRegistry`. -#![cfg(feature = "mcp")] - use lspkit_server::mcp::{ToolDescription, ToolRegistry}; use serde_json::json; diff --git a/crates/lspkit-server/tests/outbound.rs b/crates/lspkit-server/tests/outbound.rs new file mode 100644 index 0000000..cd6ed6e --- /dev/null +++ b/crates/lspkit-server/tests/outbound.rs @@ -0,0 +1,344 @@ +//! [LSP-OUTBOUND] Server→client requests over real Content-Length framing: +//! the server allocates the id, the editor answers on the wire, and the +//! decoded response resolves the waiting caller. Abandoned waits unregister. + +use lspkit_server::jsonrpc::read_message; +use lspkit_server::{ + JsonRpcError, Message, MessageWriter, Outbound, OutboundError, OutboundResponse, RequestId, +}; +use serde_json::{json, Value}; +use tokio::io::{duplex, BufReader, DuplexStream}; + +/// A wire step that fails the session with its cause. +type StepResult = Result>; + +/// Enough buffer for every frame these sessions exchange. +const PIPE_BYTES: usize = 64 * 1024; + +/// One direction of the editor connection, framed exactly as on stdio. +struct Wire { + writer: MessageWriter, + reader: BufReader, +} + +/// A connected server/editor pair. +fn connection() -> (Wire, Wire) { + let (server_out, editor_in) = duplex(PIPE_BYTES); + let (editor_out, server_in) = duplex(PIPE_BYTES); + ( + Wire { + writer: MessageWriter::new(server_out), + reader: BufReader::new(server_in), + }, + Wire { + writer: MessageWriter::new(editor_out), + reader: BufReader::new(editor_in), + }, + ) +} + +/// The editor reads one request and asserts its shape. +async fn editor_receives(editor: &mut Wire, method: &str, params: &Value) -> StepResult { + let request = read_message(&mut editor.reader).await?; + assert_eq!(request.jsonrpc, "2.0"); + assert_eq!(request.method.as_deref(), Some(method)); + // `"params": null` decodes as absent; both mean "no params" on the wire. + match request.params.as_ref() { + Some(sent) => assert_eq!(sent, params), + None => assert_eq!(params, &Value::Null, "only null params are omitted"), + } + let id = request.id.ok_or("a server request carries an id")?; + assert!(matches!(id, RequestId::Number(raw) if raw > 0), "{id:?}"); + Ok(id) +} + +/// The server sends `method`, and the editor reads exactly what was sent. +async fn round_trip( + outbound: &Outbound, + server: &Wire, + editor: &mut Wire, + method: &str, + params: Value, +) -> StepResult<(RequestId, OutboundResponse)> { + let (message, pending) = outbound.request(method, params.clone())?; + server.writer.write_message(&message).await?; + let id = editor_receives(editor, method, ¶ms).await?; + assert_eq!( + message.id.as_ref(), + Some(&id), + "the wire carries the allocated id" + ); + Ok((id, pending)) +} + +/// The editor writes `reply`; the server reads it off the wire and resolves it. +async fn answer( + outbound: &Outbound, + server: &mut Wire, + editor: &Wire, + reply: &Message, +) -> StepResult> { + editor.writer.write_message(reply).await?; + Ok(outbound.resolve(read_message(&mut server.reader).await?)) +} + +/// [LSP-OUTBOUND] One editor session answering with a result, `null` and an error. +#[tokio::test] +async fn server_requests_resolve_from_the_editors_wire_answers() { + let (mut server, mut editor) = connection(); + let outbound = Outbound::new(); + assert_eq!(outbound.pending_count(), 0); + + // Success: window/showDocument answered with a result. + let show = json!({ "uri": "file:///tmp/a.rs", "takeFocus": true }); + let (show_id, show_wait) = + round_trip(&outbound, &server, &mut editor, "window/showDocument", show) + .await + .expect("request reaches the editor"); + assert_eq!(outbound.pending_count(), 1); + let shown = Message::response(show_id.clone(), json!({ "success": true })); + answer(&outbound, &mut server, &editor, &shown) + .await + .expect("wire") + .expect("answer matches"); + let shown = show_wait + .await + .expect("delivered") + .expect("the editor showed it"); + assert_eq!(shown, json!({ "success": true })); + assert_eq!(outbound.pending_count(), 0); + + // A `null` result (workspace/diagnostic/refresh) resolves to Value::Null. + let refresh = "workspace/diagnostic/refresh"; + let (refresh_id, refresh_wait) = + round_trip(&outbound, &server, &mut editor, refresh, Value::Null) + .await + .expect("request reaches the editor"); + assert_ne!(refresh_id, show_id, "ids are never reused"); + // JSON-RPC 2.0: `params` is an object, an array, or omitted, never `null`. + let (refresh_frame, refresh_unsent) = outbound + .request(refresh, Value::Null) + .expect("an id is available"); + let wire = serde_json::to_value(&refresh_frame).expect("the frame serializes"); + assert_eq!( + wire.get("params"), + None, + "a parameterless request omits params: {wire}" + ); + assert_eq!(wire.get("method"), Some(&json!(refresh))); + drop(refresh_unsent); + let null = Message::response(refresh_id, Value::Null); + answer(&outbound, &mut server, &editor, &null) + .await + .expect("wire") + .expect("null matches"); + assert_eq!( + refresh_wait + .await + .expect("delivered") + .expect("null is success"), + Value::Null + ); + + // A frame with no `result` key at all is also success with Value::Null. + let (bare_id, bare_wait) = round_trip(&outbound, &server, &mut editor, refresh, Value::Null) + .await + .expect("request reaches the editor"); + let bare: Message = serde_json::from_value(json!({ "jsonrpc": "2.0", "id": bare_id })) + .expect("a result-less response frame decodes"); + assert!(bare.result.is_none() && bare.error.is_none()); + answer(&outbound, &mut server, &editor, &bare) + .await + .expect("wire") + .expect("bare answer matches"); + assert_eq!( + bare_wait + .await + .expect("delivered") + .expect("absent is success"), + Value::Null + ); + + // Error: the editor's JsonRpcError reaches the caller, and it is a std error. + let missing = json!({ "uri": "file:///missing" }); + let (error_id, error_wait) = round_trip( + &outbound, + &server, + &mut editor, + "window/showDocument", + missing, + ) + .await + .expect("request reaches the editor"); + let refusal = + JsonRpcError::new(-32603, "document refused").with_data(json!({ "why": "closed" })); + let refused = Message::response_error(error_id, refusal); + answer(&outbound, &mut server, &editor, &refused) + .await + .expect("wire") + .expect("error matches"); + let error = error_wait + .await + .expect("delivered") + .expect_err("the editor refused"); + assert_eq!( + (error.code, error.message.as_str()), + (-32603, "document refused") + ); + assert_eq!(error.data, Some(json!({ "why": "closed" }))); + assert_eq!(error.to_string(), "JSON-RPC error -32603: document refused"); + let boxed: Box = Box::new(error); + assert_eq!(boxed.to_string(), "JSON-RPC error -32603: document refused"); + assert_eq!(outbound.pending_count(), 0); +} + +/// [LSP-OUTBOUND] [SIDECAR-REQUEST-LIFETIME] Abandoned waits leave no live entry, +/// and answers nobody waits for are rejected visibly, never dropped silently. +#[tokio::test] +async fn abandoned_waits_unregister_and_unmatched_messages_are_rejected() { + let (mut server, mut editor) = connection(); + let outbound = Outbound::default(); + assert_eq!(outbound.pending_count(), 0, "a default handle starts empty"); + let late_params = json!({ "uri": "file:///late" }); + let (late_id, late_wait) = round_trip( + &outbound, + &server, + &mut editor, + "window/showDocument", + late_params, + ) + .await + .expect("request reaches the editor"); + assert_eq!(outbound.pending_count(), 1); + drop(late_wait); + assert_eq!( + outbound.pending_count(), + 0, + "an abandoned wait leaves no live entry" + ); + let late_reply = Message::response(late_id.clone(), json!({ "success": true })); + let late = answer(&outbound, &mut server, &editor, &late_reply) + .await + .expect("wire") + .expect_err("nobody waits"); + assert!( + matches!(&late, OutboundError::UnknownId(id) if *id == late_id), + "{late:?}" + ); + assert_eq!( + late.to_string(), + format!("no pending server request for id {late_id:?}") + ); + + // Ids the server never allocated, and non-responses, are rejected visibly. + for foreign in [ + RequestId::String("editor-7".to_owned()), + RequestId::Number(-1), + RequestId::Number(9_999), + ] { + let rejected = outbound + .resolve(Message::response(foreign.clone(), Value::Null)) + .expect_err("foreign id"); + assert!( + matches!(&rejected, OutboundError::UnknownId(id) if *id == foreign), + "{rejected:?}" + ); + } + let notification = outbound + .resolve(Message::notification("$/progress", json!({}))) + .expect_err("not a response"); + assert!(matches!(notification, OutboundError::NotAResponse)); + let null_id: Message = serde_json::from_value(json!({ + "jsonrpc": "2.0", "id": null, "error": { "code": -32700, "message": "parse error" } + })) + .expect("a null-id error frame decodes"); + let unaddressed = outbound.resolve(null_id).expect_err("names no request"); + assert!(matches!(unaddressed, OutboundError::NotAResponse)); + let hover = Message::request(RequestId::Number(1), "textDocument/hover", json!({})); + let request = outbound.resolve(hover).expect_err("a request"); + assert!(matches!(request, OutboundError::NotAResponse)); + assert_eq!(request.to_string(), "message is not a response"); + assert_eq!(outbound.pending_count(), 0); + + // The editor answers the same id twice: the duplicate is unmatched. + let (twice_id, twice_wait) = round_trip( + &outbound, + &server, + &mut editor, + "window/showDocument", + json!({}), + ) + .await + .expect("request reaches the editor"); + let reply = Message::response(twice_id.clone(), json!({ "success": true })); + answer(&outbound, &mut server, &editor, &reply) + .await + .expect("wire") + .expect("first answer"); + let again = answer(&outbound, &mut server, &editor, &reply) + .await + .expect("wire") + .expect_err("duplicate"); + assert!( + matches!(&again, OutboundError::UnknownId(id) if *id == twice_id), + "{again:?}" + ); + assert!(twice_wait.await.expect("first delivered").is_ok()); + + // Shutdown: dropping the handle closes every live wait instead of hanging it. + let (_, orphaned) = round_trip( + &outbound, + &server, + &mut editor, + "window/showDocument", + json!({}), + ) + .await + .expect("request reaches the editor"); + drop(outbound); + assert!( + orphaned.await.is_err(), + "a wait outliving its handle ends with RecvError" + ); +} + +/// [LSP-OUTBOUND] Three requests in flight, answered out of order on the wire: +/// each editor answer wakes exactly the wait whose id it carries, never another. +#[tokio::test] +async fn out_of_order_answers_wake_only_the_wait_with_their_id() { + let (mut server, mut editor) = connection(); + let outbound = Outbound::new(); + let mut waits = Vec::new(); + for name in ["first", "second", "third"] { + let params = json!({ "uri": format!("file:///{name}.rs") }); + let (id, wait) = round_trip( + &outbound, + &server, + &mut editor, + "window/showDocument", + params, + ) + .await + .expect("request reaches the editor"); + waits.push((name, id, wait)); + } + assert_eq!(outbound.pending_count(), 3); + // The editor answers third, then first, then second; each answer names its request. + for index in [2, 0, 1] { + let (name, id, _) = waits.get(index).expect("a sent request"); + let reply = Message::response(id.clone(), json!({ "answered": name })); + answer(&outbound, &mut server, &editor, &reply) + .await + .expect("wire") + .expect("the answer's id is pending"); + } + assert_eq!(outbound.pending_count(), 0); + for (name, id, wait) in waits { + let got = wait.await.expect("delivered").expect("success"); + assert_eq!( + got, + json!({ "answered": name }), + "wait for {id:?} got another request's answer" + ); + } +} diff --git a/crates/lspkit-server/tests/outbound_heap_leak.rs b/crates/lspkit-server/tests/outbound_heap_leak.rs new file mode 100644 index 0000000..3e0776c --- /dev/null +++ b/crates/lspkit-server/tests/outbound_heap_leak.rs @@ -0,0 +1,71 @@ +//! [LSP-OUTBOUND] [MEM-FIX-SCOPE]: every server-request outcome frees its heap. +//! Single-test binary: the instrumented allocator sees no sibling tests. + +use lspkit_server::{JsonRpcError, Message, Outbound, OutboundError, RequestId}; +use serde_json::{json, Value}; + +#[path = "../../../test-support/state.rs"] +mod state; + +/// Enough lifetimes per outcome to expose cumulative retention. +const ROUNDS: usize = 32; +/// Heap bytes carried by each request's params and each answer. +const PAYLOAD_BYTES: usize = 16 * 1024; + +/// A payload that owns real heap memory. +fn payload() -> Value { + json!({ "blob": "x".repeat(PAYLOAD_BYTES) }) +} + +/// A lifetime whose outcome differs from the expected one. +type Mismatch = Box; + +/// Fail the lifetime with `what` unless `held`. +fn ensure(held: bool, what: &str) -> Result<(), Mismatch> { + if held { + Ok(()) + } else { + Err(what.into()) + } +} + +/// The id the server put on `sent`. +fn sent_id(sent: &Message) -> Result { + sent.id + .clone() + .ok_or_else(|| "a server request carries an id".into()) +} + +/// Answered, refused, abandoned and unmatched lifetimes, each fully released. +fn lifetimes(outbound: &Outbound) -> Result<(), Mismatch> { + for _ in 0..ROUNDS { + let (sent, mut answered) = outbound.request("window/showDocument", payload())?; + outbound.resolve(Message::response(sent_id(&sent)?, payload()))?; + ensure(matches!(answered.try_recv(), Ok(Ok(_))), "answered")?; + let (sent, mut refused) = outbound.request("window/showDocument", payload())?; + let refusal = JsonRpcError::new(-32603, "refused").with_data(payload()); + outbound.resolve(Message::response_error(sent_id(&sent)?, refusal))?; + ensure(matches!(refused.try_recv(), Ok(Err(_))), "refused")?; + let (sent, abandoned) = outbound.request("window/showDocument", payload())?; + drop(abandoned); + let late = outbound.resolve(Message::response(sent_id(&sent)?, payload())); + ensure( + matches!(late, Err(OutboundError::UnknownId(_))), + "abandoned", + )?; + let foreign = Message::response(RequestId::String("editor".to_owned()), payload()); + ensure( + matches!(outbound.resolve(foreign), Err(OutboundError::UnknownId(_))), + "foreign", + )?; + } + Ok(()) +} + +#[test] +fn every_outbound_outcome_frees_its_heap_before_the_handle_drops() { + let outbound = Outbound::new(); + lifetimes(&outbound).expect("warm the pending table's capacity"); + state::assert_releases_heap(|| lifetimes(&outbound).expect("measured lifetimes")); + assert_eq!(outbound.pending_count(), 0); +} diff --git a/crates/lspkit-server/tests/pull_forget.rs b/crates/lspkit-server/tests/pull_forget.rs new file mode 100644 index 0000000..3206059 --- /dev/null +++ b/crates/lspkit-server/tests/pull_forget.rs @@ -0,0 +1,211 @@ +//! [LSP-PULL-FORGET] Closing one document releases only its memos: every other +//! open document stays warm, and the closed one recomputes when reopened. +#![cfg(feature = "pull")] + +use std::collections::HashMap; +use std::sync::{Arc, Mutex}; + +use lsp_types::Diagnostic; +use lspkit_server::pull::PullStore; + +/// Compiler calls per URI. +type Calls = Arc>>; + +const A: &str = "file:///repo/a.cs"; +const B: &str = "file:///repo/b.cs"; +const C: &str = "file:///repo/c.cs"; +const D: &str = "file:///repo/d.cs"; + +/// A store whose compiler reports one diagnostic naming the URI it analyzed. +fn counting_store() -> (PullStore, Calls) { + let calls: Calls = Arc::default(); + let counted = Arc::clone(&calls); + let store = PullStore::new(Arc::new(move |uri| { + if let Ok(mut calls) = counted.lock() { + *calls.entry(uri.to_owned()).or_default() += 1; + } + let named = Diagnostic { + message: uri.to_owned(), + ..Diagnostic::default() + }; + Ok(Arc::new(vec![named])) + })); + (store, calls) +} + +/// How many times the compiler ran, over every URI. +fn total_calls(calls: &Calls) -> usize { + match calls.lock() { + Ok(counts) => counts.values().sum(), + Err(poisoned) => poisoned.into_inner().values().sum(), + } +} + +/// How many times the compiler analyzed `uri`. +fn calls_for(calls: &Calls, uri: &str) -> usize { + let counts = match calls.lock() { + Ok(counts) => counts, + Err(poisoned) => poisoned.into_inner(), + }; + match counts.get(uri) { + Some(count) => *count, + None => 0, + } +} + +/// Pull `uri` and assert the report names it, never another document. +fn pull(store: &PullStore, uri: &str) -> String { + let pulled = store.diagnostics(uri, 1); + assert!(pulled.is_ok(), "{uri}: {pulled:?}"); + let Ok(report) = pulled else { + return String::new(); + }; + let messages: Vec<&str> = report + .items + .iter() + .map(|item| item.message.as_str()) + .collect(); + assert_eq!(messages, [uri], "{uri} reports only its own diagnostics"); + report.result_id +} + +#[test] +fn forgetting_one_document_keeps_every_other_memo_warm() { + let (store, calls) = counting_store(); + let [_id_a, id_b, id_c] = [A, B, C].map(|uri| pull(&store, uri)); + for uri in [A, B, C] { + let _warm = pull(&store, uri); + assert_eq!(calls_for(&calls, uri), 1, "{uri} is memoized"); + } + let revision = store.revision(); + let before = total_calls(&calls); + assert_eq!(store.forget(A), Ok(()), "forget a closed document"); + assert_eq!(store.revision(), revision, "forgetting supersedes nothing"); + assert_eq!( + total_calls(&calls), + before, + "a close never runs the compiler" + ); + assert_eq!(calls_for(&calls, ""), 0, "a retired slot is never analyzed"); + for (uri, id) in [(B, id_b), (C, id_c)] { + assert_eq!(pull(&store, uri), id, "{uri} keeps its snapshot"); + assert_eq!(calls_for(&calls, uri), 1, "{uri} stays a cache hit"); + } + let _reopened = pull(&store, A); + assert_eq!(calls_for(&calls, A), 2, "a forgotten document recomputes"); + let _memo = pull(&store, A); + assert_eq!(calls_for(&calls, A), 2, "and is memoized again"); +} + +#[test] +fn forget_is_idempotent_and_a_recycled_input_never_replays_old_truth() { + let (store, calls) = counting_store(); + for uri in [A, B, C] { + let _first = pull(&store, uri); + } + assert_eq!( + store.forget("file:///never/opened.cs"), + Ok(()), + "an unknown URI" + ); + assert_eq!(store.forget(B), Ok(()), "first forget"); + assert_eq!(store.forget(B), Ok(()), "a repeated forget"); + let _fresh = pull(&store, D); + assert_eq!( + calls_for(&calls, D), + 1, + "a new document computes its own truth" + ); + let _back = pull(&store, B); + assert_eq!(calls_for(&calls, B), 2, "B recomputes after its forget"); + for uri in [A, C] { + let _warm = pull(&store, uri); + assert_eq!(calls_for(&calls, uri), 1, "{uri} never recomputed"); + } + assert_eq!(store.clear(), Ok(()), "clear still drops everything"); + let _cold = pull(&store, C); + assert_eq!(calls_for(&calls, C), 2, "clear recomputes every document"); +} + +#[test] +fn forget_frees_the_closed_documents_diagnostics_before_it_returns() { + let (store, _calls) = counting_store(); + let held = |uri| { + let pulled = store.diagnostics(uri, 1); + assert!(pulled.is_ok(), "{uri}: {pulled:?}"); + pulled.map(|report| Arc::downgrade(&report.items)).ok() + }; + let (a, b) = (held(A), held(B)); + let strong = |weak: &Option>>| { + weak.as_ref().map(std::sync::Weak::strong_count) + }; + assert_eq!( + (strong(&a), strong(&b)), + (Some(1), Some(1)), + "only the memos hold them" + ); + assert_eq!(store.forget(A), Ok(()), "forget A"); + assert_eq!( + strong(&a), + Some(0), + "A's diagnostics are freed with no later pull" + ); + assert_eq!(strong(&b), Some(1), "B's memo is untouched"); +} + +#[test] +fn forget_returns_while_a_compiler_query_runs_and_the_next_pull_applies_it() { + let started = Arc::new(std::sync::Barrier::new(2)); + let release = Arc::new(std::sync::Barrier::new(2)); + let calls: Calls = Arc::default(); + let (in_flight, resume, counted) = ( + Arc::clone(&started), + Arc::clone(&release), + Arc::clone(&calls), + ); + let store = PullStore::new(Arc::new(move |uri| { + let seen = counted.lock().map(|mut calls| { + let count = calls.entry(uri.to_owned()).or_default(); + *count += 1; + *count + }); + if uri == B && matches!(seen, Ok(1)) { + let _started = in_flight.wait(); + let _released = resume.wait(); + } + Ok(Arc::new(vec![Diagnostic { + message: uri.to_owned(), + ..Diagnostic::default() + }])) + })); + let _a = pull(&store, A); + let worker = { + let store = store.clone(); + std::thread::spawn(move || store.diagnostics(B, 1).map(|report| report.result_id)) + }; + let _computing = started.wait(); + assert_eq!( + store.forget(A), + Ok(()), + "forget never waits for B's compiler" + ); + let _done = release.wait(); + assert!(matches!(worker.join(), Ok(Ok(_))), "B completes"); + let _reopened = pull(&store, A); + assert_eq!( + calls_for(&calls, A), + 2, + "the queued forget applied before A's pull" + ); +} + +#[test] +fn an_empty_uri_is_analyzed_never_mistaken_for_a_forgotten_slot() { + let (store, calls) = counting_store(); + let _empty = pull(&store, ""); + assert_eq!( + calls_for(&calls, ""), + 1, + "the compiler decides what \"\" means" + ); +} diff --git a/crates/lspkit-server/tests/pull_forget_heap.rs b/crates/lspkit-server/tests/pull_forget_heap.rs new file mode 100644 index 0000000..95b8c2a --- /dev/null +++ b/crates/lspkit-server/tests/pull_forget_heap.rs @@ -0,0 +1,131 @@ +//! [LSP-PULL-FORGET] [MEM-FIX-SCOPE] Open/forget cycles of distinct URIs leave +//! no heap behind. Single-test binary: the instrumented allocator sees no +//! sibling tests. +#![cfg(feature = "pull")] + +use std::sync::{Arc, Barrier}; + +use lspkit_server::pull::{PullError, PullStore}; + +#[path = "../../../test-support/state.rs"] +mod state; + +/// Cycles that size every reusable buffer before measurement. +const WARM_CYCLES: usize = 1_000; +/// Measured cycles, each on a URI never seen before. +const CYCLES: usize = 1_000; + +/// Documents left open, then closed while the store is idle. +const OPEN_DOCUMENTS: usize = 32; +/// Heap each open document's report owns. +const REPORT_BYTES: usize = 64 * 1024; +/// Closes of one URI with no pull in between. +const REPEATED_CLOSES: usize = 1_000; + +/// A store whose every report owns `REPORT_BYTES` of heap. +fn heavy_store() -> PullStore { + PullStore::new(Arc::new(|_uri| { + Ok(Arc::new(vec![lsp_types::Diagnostic { + message: "x".repeat(REPORT_BYTES), + ..lsp_types::Diagnostic::default() + }])) + })) +} + +/// The document whose analysis is held in flight. +const SLOW: &str = "file:///repo/slow.cs"; + +/// A heavy store whose analysis of `SLOW` waits on `started` then `release`. +fn held_store(started: &Arc, release: &Arc) -> PullStore { + let (started, release) = (Arc::clone(started), Arc::clone(release)); + PullStore::new(Arc::new(move |uri| { + if uri == SLOW { + let _started = started.wait(); + let _released = release.wait(); + } + Ok(Arc::new(vec![lsp_types::Diagnostic { + message: "x".repeat(REPORT_BYTES), + ..lsp_types::Diagnostic::default() + }])) + })) +} + +/// The URI of open document `index`. +fn open_uri(index: usize) -> String { + format!("file:///repo/open-{index:06}.cs") +} + +/// Open, pull and close `count` distinct documents starting at `first`. +fn cycles(store: &PullStore, first: usize, count: usize) -> Result<(), PullError> { + (first..first + count).try_for_each(|index| { + // One width for every URI: Salsa frees a replaced memo one revision + // late, so equal sizes keep that one-cycle lag byte-neutral. + let uri = format!("file:///repo/doc-{index:06}.cs"); + let _report = store.diagnostics(&uri, 1)?; + store.forget(&uri) + }) +} + +#[test] +fn open_forget_cycles_of_distinct_uris_release_their_heap() { + let store = PullStore::new(Arc::new(|uri| { + Ok(Arc::new(vec![lsp_types::Diagnostic { + message: uri.repeat(16), + ..lsp_types::Diagnostic::default() + }])) + })); + cycles(&store, 0, WARM_CYCLES).expect("warm-up cycles"); + let mut outcome = Ok(()); + state::assert_releases_heap(|| outcome = cycles(&store, WARM_CYCLES, CYCLES)); + outcome.expect("measured cycles"); + + // Close every tab and go idle: the reports are freed by the closes + // themselves, not by some later pull. + let heavy = heavy_store(); + for index in 0..OPEN_DOCUMENTS { + let _open = heavy + .diagnostics(&open_uri(index), 1) + .expect("an open pull"); + } + let uris: Vec = (0..OPEN_DOCUMENTS).map(open_uri).collect(); + let mut closed = Ok(()); + let stats = state::heap_change(|| { + closed = uris.iter().try_for_each(|uri| heavy.forget(uri)); + }); + closed.expect("idle closes"); + assert!( + stats.bytes_deallocated >= OPEN_DOCUMENTS * REPORT_BYTES, + "closing {OPEN_DOCUMENTS} idle documents freed {} of their {} report bytes", + stats.bytes_deallocated, + OPEN_DOCUMENTS * REPORT_BYTES + ); + + // A close that arrives while a pull holds the state is retired by that + // pull as it finishes, with no later pull. + let (started, release) = (Arc::new(Barrier::new(2)), Arc::new(Barrier::new(2))); + let held = held_store(&started, &release); + // The caller drops its copy, so only the memo still owns the report. + drop(held.diagnostics(&open_uri(0), 1).expect("an open pull")); + let worker = { + let held = held.clone(); + std::thread::spawn(move || held.diagnostics(SLOW, 1)) + }; + let _in_flight = started.wait(); + held.forget(&open_uri(0)).expect("a close during a pull"); + let stats = state::heap_change(|| { + let _release = release.wait(); + let _slow = worker.join(); + }); + assert!( + stats.bytes_deallocated >= REPORT_BYTES, + "a close during a pull freed {} of its {REPORT_BYTES} report bytes", + stats.bytes_deallocated + ); + + // Repeated closes with no pull in between keep nothing queued. + let mut repeated = Ok(()); + state::assert_releases_heap(|| { + repeated = (0..REPEATED_CLOSES).try_for_each(|_| heavy.forget(&open_uri(0))); + }); + repeated.expect("repeated closes"); +} diff --git a/crates/lspkit-server/tests/uri.rs b/crates/lspkit-server/tests/uri.rs index 9e8a398..735afa2 100644 --- a/crates/lspkit-server/tests/uri.rs +++ b/crates/lspkit-server/tests/uri.rs @@ -1,131 +1,170 @@ //! End-to-end tests for [`lspkit_server::uri`] conversions. +use std::path::Path; +#[cfg(not(windows))] use std::path::PathBuf; use lspkit_server::{path_to_uri, uri_to_path, UriError}; +/// `path` encodes to exactly `uri`, which decodes back to exactly `path`. +#[cfg(not(windows))] +fn assert_round_trip(path: &Path, uri: &str) { + assert_eq!( + path_to_uri(path).ok().as_deref(), + Some(uri), + "{}", + path.display() + ); + assert_eq!(uri_to_path(uri).ok().as_deref(), Some(path), "{uri}"); +} + #[cfg(not(windows))] #[test] fn round_trip_simple_unix_path() { - let path = PathBuf::from("/tmp/foo.txt"); - let uri = path_to_uri(&path).expect("encoded"); - assert_eq!(uri, "file:///tmp/foo.txt"); - let decoded = uri_to_path(&uri).expect("decoded"); - assert_eq!(decoded, path); + assert_round_trip(Path::new("/tmp/foo.txt"), "file:///tmp/foo.txt"); } #[cfg(not(windows))] #[test] fn round_trip_path_with_spaces() { - let path = PathBuf::from("/tmp/my project/foo bar.txt"); - let uri = path_to_uri(&path).expect("encoded"); - assert!(uri.contains("%20")); - let decoded = uri_to_path(&uri).expect("decoded"); - assert_eq!(decoded, path); + assert_round_trip( + Path::new("/tmp/my project/foo bar.txt"), + "file:///tmp/my%20project/foo%20bar.txt", + ); } #[cfg(not(windows))] #[test] fn round_trip_path_with_unicode() { - let path = PathBuf::from("/tmp/caf\u{00e9}/file.txt"); - let uri = path_to_uri(&path).expect("encoded"); - let decoded = uri_to_path(&uri).expect("decoded"); - assert_eq!(decoded, path); + assert_round_trip( + Path::new("/tmp/caf\u{00e9}/file.txt"), + "file:///tmp/caf%C3%A9/file.txt", + ); } #[cfg(not(windows))] #[test] fn round_trip_path_with_hash_and_question() { - let path = PathBuf::from("/tmp/a#b?c.txt"); - let uri = path_to_uri(&path).expect("encoded"); - assert!(uri.contains("%23")); - assert!(uri.contains("%3F")); - let decoded = uri_to_path(&uri).expect("decoded"); - assert_eq!(decoded, path); + assert_round_trip(Path::new("/tmp/a#b?c.txt"), "file:///tmp/a%23b%3Fc.txt"); } #[test] fn relative_path_rejected() { - let err = path_to_uri(&PathBuf::from("relative/foo.txt")).expect_err("must reject"); - match err { - UriError::InvalidPath(p) => assert_eq!(p, PathBuf::from("relative/foo.txt")), - other => assert!( - matches!(other, UriError::InvalidPath(_)), - "wrong variant: {other:?}" - ), - } + let rejected = path_to_uri(Path::new("relative/foo.txt")); + assert!( + matches!(&rejected, Err(UriError::InvalidPath(p)) if p == Path::new("relative/foo.txt")), + "{rejected:?}" + ); } #[test] fn non_file_scheme_rejected() { - let err = uri_to_path("https://example.com/foo").expect_err("must reject"); - match err { - UriError::UnsupportedScheme(s) => assert_eq!(s, "https://example.com/foo"), - other => assert!( - matches!(other, UriError::UnsupportedScheme(_)), - "wrong variant: {other:?}" - ), - } + let rejected = uri_to_path("https://example.com/foo"); + assert!( + matches!(&rejected, Err(UriError::UnsupportedScheme(s)) if s == "https://example.com/foo"), + "{rejected:?}" + ); } #[test] fn invalid_percent_encoding_rejected() { - let err = uri_to_path("file:///tmp/%ZZbroken").expect_err("must reject"); - match err { - UriError::InvalidUtf8(_) => {} - other => assert!( - matches!(other, UriError::InvalidUtf8(_)), - "wrong variant: {other:?}" - ), - } + let rejected = uri_to_path("file:///tmp/%ZZbroken"); + assert!( + matches!(rejected, Err(UriError::InvalidUtf8(_))), + "{rejected:?}" + ); } +/// [URI-WINDOWS] A `%` not followed by two hex digits is malformed on every +/// platform; it is never passed through into the path. #[test] -fn truncated_percent_encoding_passes_through_bytes() { - // Only two-char-followed percents are decoded; a stray `%` near end falls through. - let decoded = uri_to_path("file:///tmp/a%").expect("decoded"); - assert!(decoded.to_string_lossy().contains('%')); +fn truncated_percent_encoding_rejected() { + for uri in ["file:///tmp/a%", "file:///tmp/a%2"] { + let rejected = uri_to_path(uri); + assert!( + matches!(rejected, Err(UriError::InvalidUtf8(None))), + "{uri}: {rejected:?}" + ); + } } #[cfg(not(windows))] #[test] fn lowercase_hex_percent_decodes() { - let decoded = uri_to_path("file:///tmp/a%2fb").expect("decoded"); - assert_eq!(decoded, PathBuf::from("/tmp/a/b")); + assert_eq!( + uri_to_path("file:///tmp/a%2cb%c3%a9").ok(), + Some(PathBuf::from("/tmp/a,b\u{e9}")) + ); } #[cfg(not(windows))] #[test] fn uppercase_hex_percent_decodes() { - let decoded = uri_to_path("file:///tmp/a%2Fb").expect("decoded"); - assert_eq!(decoded, PathBuf::from("/tmp/a/b")); + assert_eq!( + uri_to_path("file:///tmp/a%2Cb%C3%A9").ok(), + Some(PathBuf::from("/tmp/a,b\u{e9}")) + ); +} + +/// [URI-WINDOWS] An encoded `/` stays inside its file name, which no Unix +/// file can hold, so the URI is rejected instead of gaining a folder. +#[cfg(not(windows))] +#[test] +fn encoded_separator_is_rejected_never_split_on() { + for uri in ["file:///tmp/a%2fb", "file:///tmp/a%2Fb"] { + let rejected = uri_to_path(uri); + assert!( + matches!(&rejected, Err(UriError::NoLocalPath(named)) if named == uri), + "{uri}: {rejected:?}" + ); + } + assert_eq!( + uri_to_path("file:///tmp/a%5Cb").ok(), + Some(PathBuf::from(r"/tmp/a\b")), + "a Unix name may hold a backslash" + ); + assert_eq!( + uri_to_path("file:///tmp/a%252Fb").ok(), + Some(PathBuf::from("/tmp/a%2Fb")), + "decoded once, never twice" + ); + let host = uri_to_path("file://host%2Fshare/a.rs"); + assert!( + matches!(host, Err(UriError::NoLocalPath(_))), + "an encoded / in the host: {host:?}" + ); } #[test] fn invalid_utf8_after_decode_rejected() { // %FF %FE are not valid UTF-8. - let err = uri_to_path("file:///tmp/%FF%FE").expect_err("must reject"); - match err { - UriError::InvalidUtf8(_) => {} - other => assert!( - matches!(other, UriError::InvalidUtf8(_)), - "wrong variant: {other:?}" - ), - } + let rejected = uri_to_path("file:///tmp/%FF%FE"); + assert!( + matches!(rejected, Err(UriError::InvalidUtf8(_))), + "{rejected:?}" + ); } #[test] fn error_display_lists_offending_input() { - let err = uri_to_path("ftp://x").expect_err("must reject"); - let rendered = format!("{err}"); - assert!(rendered.contains("ftp://x")); + let rejected = uri_to_path("ftp://x"); + assert!( + rejected + .as_ref() + .is_err_and(|err| err.to_string().contains("ftp://x")), + "{rejected:?}" + ); } #[test] fn error_debug_works() { - let err = path_to_uri(&PathBuf::from("rel")).expect_err("must reject"); - let rendered = format!("{err:?}"); - assert!(rendered.contains("InvalidPath")); + let rejected = path_to_uri(Path::new("rel")); + assert!( + rejected + .as_ref() + .is_err_and(|err| format!("{err:?}").contains("InvalidPath")), + "{rejected:?}" + ); } /// [URI-AUTHORITY] A URI's host is its authority, never the first path segment. @@ -138,35 +177,54 @@ fn uri_authority_is_parsed_not_read_as_a_path_segment() { "file:///tmp/a.rs", ]; for uri in local_forms { - let decoded = uri_to_path(uri).expect("a local URI decodes"); - assert_eq!(decoded, PathBuf::from("/tmp/a.rs"), "{uri}"); + let decoded = uri_to_path(uri); + assert_eq!( + decoded.as_deref().ok(), + Some(Path::new("/tmp/a.rs")), + "{uri}" + ); assert!( - decoded.is_absolute(), + decoded.as_ref().is_ok_and(|path| path.is_absolute()), "{uri} must decode to an absolute path" ); assert_eq!( - path_to_uri(&decoded).expect("encoded"), - "file:///tmp/a.rs", + decoded + .as_deref() + .ok() + .and_then(|path| path_to_uri(path).ok()) + .as_deref(), + Some("file:///tmp/a.rs"), "{uri} re-encodes without its host" ); } for uri in ["file://", "file://localhost"] { - let decoded = uri_to_path(uri).expect("a URI with no path names the root"); - assert_eq!(decoded, PathBuf::from("/"), "{uri}"); + assert_eq!( + uri_to_path(uri).ok(), + Some(PathBuf::from("/")), + "{uri} names the root" + ); } - let err = uri_to_path("file://%ZZhost/a.rs").expect_err("a malformed host"); - assert!(matches!(err, UriError::InvalidUtf8(None)), "{err:?}"); + let host = uri_to_path("file://%ZZhost/a.rs"); + assert!( + matches!(host, Err(UriError::InvalidUtf8(None))), + "a malformed host: {host:?}" + ); for uri in [ "file://server/share/a.rs", "file://wsl.localhost/Ubuntu/a.rs", "file://server", ] { - let err = uri_to_path(uri).expect_err("a remote host has no local path here"); + let rejected = uri_to_path(uri); + assert!( + matches!(&rejected, Err(UriError::NoLocalPath(named)) if named == uri), + "{uri}: {rejected:?}" + ); assert!( - matches!(&err, UriError::NoLocalPath(rejected) if rejected == uri), - "{uri}: {err:?}" + rejected + .as_ref() + .is_err_and(|err| err.to_string().contains(uri)), + "the error names {uri}" ); - assert!(format!("{err}").contains(uri), "the error names {uri}"); } } @@ -176,16 +234,15 @@ fn uri_authority_is_parsed_not_read_as_a_path_segment() { fn non_utf8_path_rejected() { use std::{ffi::OsStr, os::unix::ffi::OsStrExt}; let path = PathBuf::from("/tmp").join(OsStr::from_bytes(&[0x66, 0xFF, 0x6F])); - let err = path_to_uri(&path).expect_err("must reject"); + let rejected = path_to_uri(&path); assert!( - matches!(&err, UriError::InvalidPath(rejected) if *rejected == path), - "{err:?}" + matches!(&rejected, Err(UriError::InvalidPath(named)) if *named == path), + "{rejected:?}" ); } #[cfg(not(windows))] #[test] fn empty_path_after_scheme_decodes_to_root() { - let decoded = uri_to_path("file:///").expect("decoded"); - assert_eq!(decoded, PathBuf::from("/")); + assert_eq!(uri_to_path("file:///").ok(), Some(PathBuf::from("/"))); } diff --git a/crates/lspkit-server/tests/uri_authority.rs b/crates/lspkit-server/tests/uri_authority.rs new file mode 100644 index 0000000..76c1b52 --- /dev/null +++ b/crates/lspkit-server/tests/uri_authority.rs @@ -0,0 +1,36 @@ +//! [URI-AUTHORITY] Issue #49 through the public API only, so this file also +//! compiles against the pre-fix crate and fails there on the exact symptom. +//! Unix-only: the Windows rows live in `uri_windows.rs`. +#![cfg(not(windows))] + +use std::path::Path; + +use lspkit_server::{path_to_uri, uri_to_path}; + +/// A `localhost` or empty authority is the host, never the first path folder. +#[test] +fn localhost_authority_is_the_host_not_a_folder() { + for uri in [ + "file://localhost/tmp/a.rs", + "file://LOCALHOST/tmp/a.rs", + "file:///tmp/a.rs", + ] { + let path = uri_to_path(uri); + assert_eq!(path.as_deref().ok(), Some(Path::new("/tmp/a.rs")), "{uri}"); + assert!( + path.as_ref().is_ok_and(|path| path.is_absolute()), + "{uri} never yields a relative path" + ); + assert_eq!( + path.as_deref() + .ok() + .and_then(|path| path_to_uri(path).ok()) + .as_deref(), + Some("file:///tmp/a.rs") + ); + } + assert!( + uri_to_path("file://server/share/a.rs").is_err(), + "a remote host has no Unix path" + ); +} diff --git a/crates/lspkit-server/tests/uri_windows.rs b/crates/lspkit-server/tests/uri_windows.rs new file mode 100644 index 0000000..5b4e0f0 --- /dev/null +++ b/crates/lspkit-server/tests/uri_windows.rs @@ -0,0 +1,265 @@ +//! [URI-WINDOWS] Issue #49's Windows verification plan, through the public +//! API only: `uri_to_path` and `path_to_uri` on a real Windows build, where +//! `cfg(windows)` selects the Windows rules. Only the public functions are +//! used, so this file also compiles against the pre-fix crate. +#![cfg(windows)] + +use std::fmt::Debug; +use std::path::{Path, PathBuf}; +use std::process::Command; + +use lspkit_server::{path_to_uri, uri_to_path}; + +#[path = "../src/uri_cases.rs"] +mod cases; + +/// A failed check, naming the input that failed and why. +type Checked = Result; + +/// Fails with `what` unless `holds`. +fn ensure(holds: bool, what: impl FnOnce() -> String) -> Checked<()> { + if holds { + Ok(()) + } else { + Err(what()) + } +} + +/// Fails with both values and `what` unless they are equal. +fn same(left: T, right: T, what: &str) -> Checked<()> { + ensure(left == right, move || { + format!("{what}\n left: {left:?}\n right: {right:?}") + }) +} + +/// Names a failed filesystem step. +fn io(step: &str, result: std::io::Result) -> Checked { + result.map_err(|err| format!("{step}: {err}")) +} + +/// `uri_to_path`, naming the URI when it is rejected. +fn decode(uri: &str) -> Checked { + uri_to_path(uri).map_err(|err| format!("{uri} decodes: {err:?}")) +} + +/// `path_to_uri`, naming the path when it is rejected. +fn encode(path: &Path) -> Checked { + path_to_uri(path).map_err(|err| format!("{} encodes: {err:?}", path.display())) +} + +/// Decodes `uri`, checks the path is absolute and that its canonical URI +/// decodes to the same path again, and returns the path. +fn round_trip(uri: &str, canonical: &str) -> Checked { + let path = decode(uri)?; + ensure(path.is_absolute(), || { + format!("{uri} decodes to the relative {}", path.display()) + })?; + same(encode(&path)?.as_str(), canonical, uri)?; + same( + decode(canonical)?.as_os_str(), + path.as_os_str(), + &format!("{canonical} round-trips"), + )?; + Ok(path) +} + +#[test] +fn table_uris_decode_to_exact_absolute_paths_and_round_trip() -> Checked<()> { + for (uri, path, canonical) in cases::ROUND_TRIPS { + same( + round_trip(uri, canonical)?.as_os_str(), + std::ffi::OsStr::new(path), + uri, + )?; + } + Ok(()) +} + +#[test] +fn table_paths_encode_to_one_canonical_uri() -> Checked<()> { + for (path, uri) in cases::PATHS_TO_URIS { + same(encode(Path::new(path))?.as_str(), uri, path)?; + let decoded = round_trip(uri, uri)?; + ensure(!decoded.as_os_str().to_string_lossy().contains('/'), || { + format!("{uri} decodes with native separators") + })?; + } + Ok(()) +} + +#[test] +fn table_device_and_rootless_paths_are_errors() { + for path in cases::PATHS_REJECTED { + let result = path_to_uri(Path::new(path)); + assert!(result.is_err(), "{path} gave {result:?}"); + } +} + +#[test] +fn table_uris_without_an_absolute_path_are_errors_never_relative_paths() { + for (uri, variant) in cases::URIS_REJECTED { + let rejected = uri_to_path(uri); + assert!( + rejected + .as_ref() + .is_err_and(|err| format!("{err:?}").starts_with(variant)), + "{uri} must be {variant}, got {rejected:?}" + ); + } +} + +/// A fresh directory, and its path as `std::fs::canonicalize` spells it (`\\?\C:\…`). +fn canonical_temp_dir() -> Checked<(tempfile::TempDir, PathBuf)> { + let dir = io("temp dir", tempfile::tempdir())?; + let canonical = io("canonicalize", std::fs::canonicalize(dir.path()))?; + ensure( + canonical.as_os_str().to_string_lossy().starts_with(r"\\?\"), + || format!("{} is extended-length", canonical.display()), + )?; + Ok((dir, canonical)) +} + +/// Writes `contents` at the canonical `path`, then proves its URI decodes to +/// a path that opens the same file and round-trips. +fn real_file_round_trips(path: &Path, contents: &str) -> Checked<()> { + io("write", std::fs::write(path, contents))?; + let uri = encode(path)?; + ensure(!uri.contains('?') && !uri.contains("%3F"), || { + format!("{uri} keeps no verbatim prefix") + })?; + let decoded = round_trip(&uri, &uri)?; + same( + std::fs::read_to_string(&decoded).ok().as_deref(), + Some(contents), + &format!("{uri} → {} opens the file it names", decoded.display()), + ) +} + +/// A canonical folder's URI decodes to a path that resolves to the same folder. +fn real_dir_round_trips(dir: &Path) -> Checked<()> { + let uri = encode(dir)?; + let decoded = round_trip(&uri, &uri)?; + same( + std::fs::canonicalize(&decoded).ok().as_deref(), + Some(dir), + &format!("{uri} → {}", decoded.display()), + ) +} + +#[test] +fn canonicalized_files_and_folders_round_trip_and_open() -> Checked<()> { + let (_dir, root) = canonical_temp_dir()?; + real_file_round_trips(&root.join("a.rs"), "plain")?; + let spaced = root.join("my repo"); + io("mkdir", std::fs::create_dir(&spaced))?; + real_file_round_trips(&spaced.join("b c.rs"), "spaced")?; + real_file_round_trips( + &root.join("caf\u{e9} \u{65e5}\u{672c} \u{1f980}.rs"), + "unicode", + )?; + real_file_round_trips(&root.join("a #%&+;=@[]{}'`^~ %20.rs"), "reserved")?; + real_dir_round_trips(&root)?; + real_dir_round_trips(&spaced) +} + +#[test] +fn names_win32_would_rewrite_round_trip_and_open_the_same_file() -> Checked<()> { + let (_dir, root) = canonical_temp_dir()?; + for name in [ + "trailing dot.", + "trailing space ", + "aux.rs", + "NUL", + "CON", + "com1.txt", + ] { + real_file_round_trips(&root.join(name), name)?; + } + let folder = root.join("CON"); + io("remove the CON file", std::fs::remove_file(&folder))?; + io("mkdir CON", std::fs::create_dir(&folder))?; + real_file_round_trips(&folder.join("a.rs"), "inside CON")?; + ensure(root.join("NUL").is_file(), || { + "NUL stayed a file, never the device".to_owned() + }) +} + +#[test] +fn a_path_longer_than_max_path_round_trips_and_opens() -> Checked<()> { + let (_dir, root) = canonical_temp_dir()?; + let mut deep = root; + while deep.as_os_str().len() < 300 { + deep.push("a-forty-character-folder-name-0123456789"); + } + io( + "mkdir under the verbatim root", + std::fs::create_dir_all(&deep), + )?; + let file = deep.join("long.rs"); + real_file_round_trips(&file, "long")?; + let uri = encode(&file)?; + ensure(uri.len() > 260, || format!("{uri} is past MAX_PATH")) +} + +#[test] +fn a_junction_round_trips_and_opens_through_its_target() -> Checked<()> { + let (_dir, root) = canonical_temp_dir()?; + let target = root.join("target"); + io("mkdir", std::fs::create_dir(&target))?; + io("write", std::fs::write(target.join("a.rs"), "via junction"))?; + let plain = |path: &Path| { + path.as_os_str() + .to_string_lossy() + .trim_start_matches(r"\\?\") + .to_owned() + }; + let link = root.join("link"); + let mklink = Command::new("cmd") + .args(["/C", "mklink", "/J", &plain(&link), &plain(&target)]) + .output(); + let mklink = io("mklink runs", mklink)?; + ensure(mklink.status.success(), || format!("mklink /J: {mklink:?}"))?; + let through = link.join("a.rs"); + let uri = encode(&through)?; + let decoded = round_trip(&uri, &uri)?; + same( + std::fs::read_to_string(&decoded).ok().as_deref(), + Some("via junction"), + &uri, + )?; + let resolved = io("canonicalize", std::fs::canonicalize(&through))?; + real_file_round_trips(&resolved, "via junction") +} + +#[test] +fn drive_letter_case_never_gives_one_file_two_uris() -> Checked<()> { + let (_dir, root) = canonical_temp_dir()?; + let file = root.join("Case.rs"); + io("write", std::fs::write(&file, "x"))?; + let upper = encode(&file)?; + let text = file.as_os_str().to_string_lossy().into_owned(); + let mut chars = text.trim_start_matches(r"\\?\").chars(); + let drive = chars.next().ok_or("a drive letter")?.to_ascii_lowercase(); + let lower = format!("{drive}{}", chars.as_str()); + ensure(drive.is_ascii_lowercase(), || { + format!("{lower} starts with a lower-case drive") + })?; + same(encode(Path::new(&lower))?.as_str(), upper.as_str(), &lower) +} + +#[test] +fn an_8_3_short_name_round_trips_and_opens() -> Checked<()> { + let uri = encode(Path::new(r"C:\PROGRA~1"))?; + same(uri.as_str(), "file:///C:/PROGRA~1", "8.3 name")?; + let decoded = round_trip(&uri, &uri)?; + let long = io( + "Program Files exists", + std::fs::canonicalize(r"C:\Program Files"), + )?; + same( + std::fs::canonicalize(&decoded).ok().as_deref(), + Some(long.as_path()), + "C:\\PROGRA~1 opens Program Files (8.3 names are on for the system drive)", + )?; + real_dir_round_trips(&long) +} diff --git a/crates/lspkit/Cargo.toml b/crates/lspkit/Cargo.toml index 6768c03..5575afe 100644 --- a/crates/lspkit/Cargo.toml +++ b/crates/lspkit/Cargo.toml @@ -23,7 +23,6 @@ workspace = true async-trait.workspace = true dashmap.workspace = true futures-core.workspace = true -rmp-serde.workspace = true ropey.workspace = true serde.workspace = true thiserror.workspace = true diff --git a/crates/lspkit/src/sidecar/correlator.rs b/crates/lspkit/src/sidecar/correlator.rs index dc0b5c9..407069f 100644 --- a/crates/lspkit/src/sidecar/correlator.rs +++ b/crates/lspkit/src/sidecar/correlator.rs @@ -25,6 +25,13 @@ impl RequestId { } } +/// Rebuild an id from the raw number a peer echoes back on the wire. +impl From for RequestId { + fn from(raw: u64) -> Self { + Self(raw) + } +} + /// Errors from the correlator. #[non_exhaustive] #[derive(Debug, thiserror::Error)] diff --git a/crates/lspkit/tests/sidecar_correlator.rs b/crates/lspkit/tests/sidecar_correlator.rs index f20fb53..5dc3f0e 100644 --- a/crates/lspkit/tests/sidecar_correlator.rs +++ b/crates/lspkit/tests/sidecar_correlator.rs @@ -205,3 +205,29 @@ async fn pending_response_is_nameable_from_the_sidecar_facade() { assert_eq!(pending.await.expect("response delivered"), "answer"); assert_eq!(correlator.pending_count(), 0); } + +/// A response decoded off the wire carries only the raw number; it rebuilds the id. [SIDECAR-REQUEST-LIFETIME] +#[tokio::test] +async fn raw_wire_ids_rebuild_the_request_id_and_resolve() { + let correlator: Correlator<&str> = Correlator::new(); + let (first, first_wait) = correlator.register(); + let (second, second_wait) = correlator.register(); + let wire = serde_json::json!({ "id": second.get(), "result": "second" }); + let raw = wire + .get("id") + .and_then(serde_json::Value::as_u64) + .expect("numeric wire id"); + assert_eq!(RequestId::from(raw), second); + correlator + .resolve(RequestId::from(raw), "second") + .expect("rebuilt id resolves"); + correlator + .resolve(RequestId::from(first.get()), "first") + .expect("first resolves"); + assert_eq!(second_wait.await.expect("second delivered"), "second"); + assert_eq!(first_wait.await.expect("first delivered"), "first"); + assert!( + matches!(correlator.resolve(RequestId::from(raw), "again"), Err(CorrelationError::UnknownId(id)) if id == raw) + ); + assert_eq!(correlator.pending_count(), 0); +} diff --git a/crates/lspkit/tests/sidecar_disconnect_session.rs b/crates/lspkit/tests/sidecar_disconnect_session.rs index 9a0d2ab..dcf04fc 100644 --- a/crates/lspkit/tests/sidecar_disconnect_session.rs +++ b/crates/lspkit/tests/sidecar_disconnect_session.rs @@ -39,6 +39,12 @@ async fn produced_frame() -> TestResult<(Vec, Value)> { Ok((output.stdout, expected)) } +/// `result`'s value, or a failed assertion naming `what` and the error. +fn ok(result: Result, what: &str) -> Option { + assert!(result.is_ok(), "{what}: {:?}", result.as_ref().err()); + result.ok() +} + /// A corrupt native stream produces one visible error, then releases both streams. fn assert_failure(events: &[Value]) { let failures: Vec<_> = events @@ -46,8 +52,13 @@ fn assert_failure(events: &[Value]) { .filter(|event| at(event, "/kind") == "error") .collect(); assert_eq!(failures.len(), 1, "one terminal native read error"); - let failure = failures.first().copied().unwrap_or(&Value::Null); - assert_ne!(at(failure, "/message").as_str().unwrap_or_default(), ""); + let message = failures + .first() + .and_then(|failure| at(failure, "/message").as_str()); + assert!( + message.is_some_and(|text| !text.is_empty()), + "the error names its cause: {message:?}" + ); frame_events::assert_closed(events, None); } @@ -79,23 +90,30 @@ fn assert_valid_then_truncated(events: &[Value], expected: &Value) { async fn native_disconnects_preserve_prefix_and_payload_failures_after_valid_traffic() { let empty = frame_events::output(&["--backend"], &[]).await.stdout; assert!(empty.is_empty(), "real backend EOF has no synthetic reply"); - let clean = received(&empty).await.expect("receive clean native EOF"); + let Some(clean) = ok(received(&empty).await, "receive clean native EOF") else { + return; + }; assert_eq!(clean.len(), 1, "zero-byte EOF is normal"); frame_events::assert_closed(&clean, None); - let (frame, expected) = produced_frame().await.expect("produce native frame"); + let Some((frame, expected)) = ok(produced_frame().await, "produce native frame") else { + return; + }; assert!(frame.len() > 5, "actual backend emitted a complete frame"); - let prefix = received(frame.get(..2).unwrap_or_default()) - .await - .expect("receive torn prefix"); + let (Some(torn_prefix), Some(torn_body)) = (frame.get(..2), frame.get(..5)) else { + return; + }; + let Some(prefix) = ok(received(torn_prefix).await, "receive torn prefix") else { + return; + }; assert_truncation(&prefix); - let body = received(frame.get(..5).unwrap_or_default()) - .await - .expect("receive torn body"); + let Some(body) = ok(received(torn_body).await, "receive torn body") else { + return; + }; assert_truncation(&body); - let bytes = [frame.as_slice(), frame.get(..5).unwrap_or_default()].concat(); - let events = received(&bytes) - .await - .expect("receive valid then torn frames"); + let bytes = [frame.as_slice(), torn_body].concat(); + let Some(events) = ok(received(&bytes).await, "receive valid then torn frames") else { + return; + }; assert_valid_then_truncated(&events, &expected); } @@ -103,10 +121,60 @@ async fn native_disconnects_preserve_prefix_and_payload_failures_after_valid_tra #[cfg(unix)] #[tokio::test] async fn native_initial_read_failure_stays_visible() { - let directory = tempfile::TempDir::new().expect("real directory input"); - let source = directory.path().to_str().expect("UTF-8 fixture path"); + let Some(directory) = ok(tempfile::TempDir::new(), "real directory input") else { + return; + }; + let source = directory.path().to_str(); + assert!(source.is_some(), "UTF-8 fixture path"); + let Some(source) = source else { + return; + }; let events = frame_events::journal(&["--receive", source], &[]).await; assert_failure(&events); assert_eq!(events.len(), 2); assert_eq!(at(entry(&events, 0), "/kind"), "error"); } + +#[cfg(windows)] +#[path = "../../../test-support/windows-oplock/owner.rs"] +mod owner; + +/// Windows has no EISDIR. Another process's exclusive byte-range lock lets the +/// open succeed and makes the first read fail with `ERROR_LOCK_VIOLATION`. +#[cfg(windows)] +#[tokio::test] +async fn native_initial_read_failure_stays_visible_on_windows() { + let Some((events, released)) = ok(locked_source_events().await, "receive a locked source") + else { + return; + }; + assert!(released.success(), "the lock holder exits cleanly"); + assert_failure(&events); + assert_eq!(events.len(), 2); + assert_eq!(at(entry(&events, 0), "/kind"), "error"); + let message = at(entry(&events, 0), "/message"); + assert!( + message + .as_str() + .is_some_and(|text| text.contains("(os error 33)")), + "the open succeeded and the first read hit the lock: {message}" + ); +} + +/// Receive a source another process holds under an exclusive byte-range lock, +/// then release the lock. Readiness comes from the holder's state line. +#[cfg(windows)] +async fn locked_source_events() -> TestResult<(Vec, std::process::ExitStatus)> { + let fixture = tempfile::TempDir::new()?; + let path = fixture.path().join("locked.bin"); + std::fs::write(&path, b"locked bytes")?; + let script = std::path::Path::new(env!("CARGO_MANIFEST_DIR")) + .join("../../test-support/windows-oplock/lock.ps1"); + let mut holder = owner::spawn(&script, &path)?; + let stdout = holder.stdout.take().ok_or("lock holder stdout")?; + owner::state(&mut std::io::BufReader::new(stdout), "locked")?; + let source = path.to_str().ok_or("capture path is not UTF-8")?; + let events = frame_events::journal(&["--receive", source], &[]).await; + drop(holder.stdin.take()); + Ok((events, holder.wait()?)) +} diff --git a/docs/plans/crate-consolidation-plan.md b/docs/plans/crate-consolidation-plan.md index 0f29112..27e7383 100644 --- a/docs/plans/crate-consolidation-plan.md +++ b/docs/plans/crate-consolidation-plan.md @@ -21,7 +21,7 @@ graph TD - **`[CRATE-LAYOUT-LAYERS]`** An L1 crate depends only on its own tree's L0 crate. An L0 crate has no internal dependencies. - **`[CRATE-LAYOUT-MODULES]`** Each absorbed crate becomes a top-level module with the same name: `lspkit::config`, `lspkit::vfs`, `lspkit::treesitter`, `lspkit::sidecar`, `lspkit_server::live`, `lspkit_server::mcp`. -- **`[CRATE-LAYOUT-FEATURES]`** `lspkit-server`'s `mcp` cargo feature (default off) keeps the MCP dependency out of LSP-only consumers. `treesitter` and `sidecar` become features of `lspkit` only if their dependencies are heavy. +- **`[CRATE-LAYOUT-FEATURES]`** `lspkit-server`'s `mcp` module is always on: it has no dependencies of its own since the unused `rmcp` SDK was deleted. `treesitter` and `sidecar` become features of `lspkit` only if their dependencies are heavy. - **`[CRATE-LAYOUT-HELPER]`** `lsp-profiler-helper` becomes a `[[bin]]` target of `lsp-profiler-python`. ## Retiring published names `[CRATE-RETIRE]` diff --git a/docs/plans/issue-fix-evidence-21-26-48-49.md b/docs/plans/issue-fix-evidence-21-26-48-49.md new file mode 100644 index 0000000..7b27ad7 --- /dev/null +++ b/docs/plans/issue-fix-evidence-21-26-48-49.md @@ -0,0 +1,365 @@ +# Red→green evidence for issues #21, #26, #48 and #49 + +Every fix below is proven the same way: put the pre-fix code back, run the named test and watch it fail on the issue's exact symptom, then restore the code and watch the same test pass. Run all commands from the repo root on branch `fix/issues-21-26-48-49`. Results were captured on macOS (aarch64), Rust 1.100.0. + +The reference-server suites drive the real compiled `diagnostic-reference` binary. Build it after every source change, because it links `lspkit`: + +```sh +cargo build -p lspkit-server --example diagnostic-reference --features pull --locked +export LSPKIT_REFERENCE_BIN=$PWD/target/debug/examples/diagnostic-reference +``` + +| Issue | Fixed in | Released | Regression test | +|---|---|---|---| +| #21 truncated frame read as clean EOF | `5590a52` (PR #27, added `next_frame` in `crates/lspkit-sidecar/src/inbound.rs`, now `crates/lspkit/src/sidecar/inbound.rs:109`) | v0.1.0 onward, including 0.5.0 | `crates/lspkit/tests/sidecar_disconnect_session.rs:79` `native_disconnects_preserve_prefix_and_payload_failures_after_valid_traffic` | +| #26 cancellation test drops the earlier partial | `741e806` (PR #33, `crates/lspkit-server/tests/cli_sessions/dispatcher.rs:90`) | v0.1.0 onward, including 0.5.0 | `crates/lspkit-server/tests/cli_sessions/dispatcher.rs:119` `cancel_during_a_scan_stops_the_stream_with_the_standard_error` | +| #49 URI host read as a path folder; UNC and `\\?\` paths | `384b341` (PR #50) | not yet; ships in v0.6.0 | `crates/lspkit-server/tests/uri_authority.rs` `localhost_authority_is_the_host_not_a_folder`; the Windows plan's table in `crates/lspkit-server/src/uri_cases.rs`, run by `src/uri_style_tests.rs` (every OS) and `tests/uri_windows.rs` (Windows, public API); see [Windows](#windows) | +| #48 no server→client requests | this branch | ships in v0.6.0 | `crates/lspkit-server/tests/outbound.rs:84` and `:183`, `crates/lspkit-server/tests/outbound_heap_leak.rs:66`, `crates/lspkit/tests/sidecar_correlator.rs:211` | + +## #21: a truncated frame is an error, not a clean end of stream + +The old reader turned every `UnexpectedEof` into "stream ended", including a peer that closes mid-prefix or mid-payload. The fix reads one byte first: zero bytes is a clean end, and anything else goes through the frame decoder, so a torn frame surfaces as an error. + +Red: put the old behaviour back, rebuild the reference binary, run the test. + +```diff +diff --git a/crates/lspkit/src/sidecar/inbound.rs b/crates/lspkit/src/sidecar/inbound.rs +index 0058be5..eb47a94 100644 +--- a/crates/lspkit/src/sidecar/inbound.rs ++++ b/crates/lspkit/src/sidecar/inbound.rs +@@ -3,7 +3,7 @@ + //! Codec and envelope classification belong to the caller. This layer only + //! separates response flow from unsolicited backend events. + +-use tokio::io::{AsyncRead, AsyncReadExt}; ++use tokio::io::{AsyncRead}; + use tokio::sync::mpsc; + use tokio::task::JoinHandle; + +@@ -110,18 +110,13 @@ async fn next_frame( + reader: &mut R, + max_frame: usize, + ) -> Result>, String> { +- let mut first = [0]; +- if reader +- .read(&mut first) +- .await +- .map_err(|error| error.to_string())? +- == 0 +- { +- return Ok(None); ++ match read_frame(reader, max_frame).await { ++ Ok(frame) => Ok(Some(frame)), ++ Err(crate::sidecar::transport::TransportError::Io(e)) ++ if e.kind() == std::io::ErrorKind::UnexpectedEof => ++ { ++ Ok(None) ++ } ++ Err(error) => Err(error.to_string()), + } +- let mut prefixed = std::io::Cursor::new(first).chain(reader); +- read_frame(&mut prefixed, max_frame) +- .await +- .map(Some) +- .map_err(|error| error.to_string()) + } +``` + +```sh +git apply red-21.patch # the diff above +cargo build -p lspkit-server --example diagnostic-reference --features pull --locked +cargo nextest run -p lspkit --all-features --test sidecar_disconnect_session +``` + +```text + FAIL [ 0.549s] (2/2) lspkit::sidecar_disconnect_session native_disconnects_preserve_prefix_and_payload_failures_after_valid_traffic + thread 'native_disconnects_preserve_prefix_and_payload_failures_after_valid_traffic' (1754426) panicked at crates/lspkit/tests/sidecar_disconnect_session.rs:48:5: + left: 0 + right: 1 + FAIL [ 0.549s] (2/2) lspkit::sidecar_disconnect_session native_disconnects_preserve_prefix_and_payload_failures_after_valid_traffic +``` + +`sidecar_disconnect_session.rs:48` asserts exactly one terminal read error: the old code produced zero, so the torn frame looked like a clean EOF. Green, after `git checkout crates/lspkit/src/sidecar/inbound.rs` and a rebuild: + +```text + Summary [ 0.640s] 2 tests run: 2 passed, 0 skipped +``` + +## #26: the earlier partial result is kept when the scan is cancelled + +#26 was a bug in the test, not the server: the test consumed the first partial as a barrier and then checked only the partials that came after it. Correct server behaviour therefore failed the count. The fix chains the barrier back into the checked list. The server streams one partial per document before the hold, and the test asserts all 64, that every one is a distinct scan document, that the held document never streamed, and that the terminal reply is the standard `-32800`. + +Red: put the old assertion back and run it against the correct server. + +```diff +diff --git a/crates/lspkit-server/tests/cli_sessions/dispatcher.rs b/crates/lspkit-server/tests/cli_sessions/dispatcher.rs +index d521142..d75f212 100644 +--- a/crates/lspkit-server/tests/cli_sessions/dispatcher.rs ++++ b/crates/lspkit-server/tests/cli_sessions/dispatcher.rs +@@ -87,7 +87,8 @@ fn assert_streamed_up_to_the_hold( + barrier: &Value, + partials: &[Value], + ) { +- let streamed: Vec<&Value> = std::iter::once(barrier).chain(partials).collect(); ++ let _ = barrier; ++ let streamed: Vec<&Value> = partials.iter().collect(); + for partial in &streamed { + assert_partial_document(partial); + } +``` + +```sh +cargo nextest run -p lspkit-server --all-features --test diagnostic_reference cancel_during_a_scan +``` + +```text + FAIL [ 0.027s] (1/1) lspkit-server::diagnostic_reference dispatcher::cancel_during_a_scan_stops_the_stream_with_the_standard_error + thread 'dispatcher::cancel_during_a_scan_stops_the_stream_with_the_standard_error' (1757217) panicked at crates/lspkit-server/tests/cli_sessions/dispatcher.rs:100:5: + left: 63 + right: 64 + FAIL [ 0.027s] (1/1) lspkit-server::diagnostic_reference dispatcher::cancel_during_a_scan_stops_the_stream_with_the_standard_error +``` + +63 is not 64 because the old check dropped `scan-0000.json`, the earlier partial. Green, after `git checkout crates/lspkit-server/tests/cli_sessions/dispatcher.rs`: + +```text + Summary [ 0.028s] 1 test run: 1 passed, 25 skipped +``` + +## #49: a URI's host is a host, never a path folder + +Red: restore the pre-fix `uri.rs` and run the public-API test. + +```sh +git show 384b341^:crates/lspkit-server/src/uri.rs > crates/lspkit-server/src/uri.rs +cargo nextest run -p lspkit-server --test uri_authority +``` + +```text + FAIL [ 0.007s] (1/1) lspkit-server::uri_authority localhost_authority_is_the_host_not_a_folder + thread 'localhost_authority_is_the_host_not_a_folder' (1760196) panicked at crates/lspkit-server/tests/uri_authority.rs:18:9: + left: "/localhost/tmp/a.rs" + right: "/tmp/a.rs" +``` + +This is the issue's row `file://localhost/tmp/a.rs` → `/localhost/tmp/a.rs`. Green, after `git checkout crates/lspkit-server/src/uri.rs`: + +```text + Summary [ 0.015s] 1 test run: 1 passed, 0 skipped +``` + +The Windows rows (UNC `file://server/share/a.rs` ↔ `\\server\share\a.rs`, `file://wsl.localhost/…`, and `\\?\C:\…` and `\\?\UNC\…` → URI) and a percent-encoded host (`file://build%2Dhost/share%20one/a.rs` ↔ `\\build-host\share one\a.rs`) are unit tests in `uri_style_tests.rs`. They call the Windows conversion directly, so Linux and macOS CI run them on every PR. CI has no Windows runner, so the `cfg(windows)` selection of that branch is not executed in CI. + +## #48: the server can send its own requests to the editor + +`lspkit_server::Outbound` allocates a numeric JSON-RPC id, returns the request frame and an `OutboundResponse` future, and `resolve` delivers the editor's answer. The tests run over real Content-Length framing on a duplex stream. + +- A `window/showDocument` request reaches the editor with its method, params and id, the editor's `{"success": true}` comes back to the caller, and ids are never reused. +- A `null` result and a frame with no `result` key both resolve to `Value::Null` (`workspace/diagnostic/refresh`). +- An editor `JsonRpcError` reaches the caller with its code, message and data. It renders as `JSON-RPC error -32603: document refused` through `Display` and through `Box`. +- Dropping a wait unregisters it at once (`pending_count` 1 → 0), and the late answer is `UnknownId`. +- String, negative and never-allocated ids are `UnknownId`. Notifications, requests and `"id": null` errors are `NotAResponse`. +- `outbound_heap_leak.rs` runs 32 rounds of answered, refused, abandoned and foreign lifetimes and asserts that allocated heap bytes equal freed bytes. Replacing `drop(abandoned)` with `std::mem::forget(abandoned)` makes it fail, which shows the test detects retention. +- A parameterless request (`workspace/diagnostic/refresh`) omits `params` on the wire instead of sending `"params": null`, which JSON-RPC 2.0 forbids. `Message::request` and `Message::notification` both omit `null` params. Red before the fix: `outbound.rs` asserted `left: Some(Null) right: None` on `{"id":3,"jsonrpc":"2.0","method":"workspace/diagnostic/refresh","params":null}`. +- A duplicate answer for an already answered id is `UnknownId`, and the first answer still reaches its caller. +- Dropping the `Outbound` handle (server shutdown) ends every live wait with `RecvError` instead of hanging it. +- A client that never answers keeps exactly one entry until the caller drops its wait. Dropping is how a server cancels its own request; sending `$/cancelRequest` to the editor stays the caller's choice. +- `sidecar::RequestId::from(raw)` rebuilds an id from the raw wire number and resolves. + +### Behavioural red: an answer wakes only its own request + +`outbound.rs` `out_of_order_answers_wake_only_the_wait_with_their_id` sends three requests, and the editor answers them third, first, second. Each wait must receive the answer carrying its own id. Mutate `resolve` so it rotates ids, so that every answer wakes another pending request while every resolve still succeeds: + +```diff +diff --git a/crates/lspkit-server/src/outbound.rs b/crates/lspkit-server/src/outbound.rs +index 73cb512..9eeedd2 100644 +--- a/crates/lspkit-server/src/outbound.rs ++++ b/crates/lspkit-server/src/outbound.rs +@@ -71,7 +71,7 @@ impl Outbound { + let Some(raw) = correlated(&id) else { + return Err(OutboundError::UnknownId(id)); + }; +- self.correlator.resolve(raw.into(), answer).map_err( ++ self.correlator.resolve(((raw % 3) + 1).into(), answer).map_err( + |_answered_or_abandoned: CorrelationError| OutboundError::UnknownId(id), + ) + } +``` + +```text + thread 'out_of_order_answers_wake_only_the_wait_with_their_id' (1855494) panicked at crates/lspkit-server/tests/outbound.rs:333:9: + assertion `left == right` failed: wait for Number(1) got another request's answer + left: Object {"answered": String("third")} + right: Object {"answered": String("first")} + Summary [ 0.010s] 1 test run: 0 passed, 1 failed, 2 skipped +``` + +Restored, all outbound tests pass. + +### Behavioural red: ids past `i64::MAX` fail instead of colliding + +The unit test `outbound::tests::ids_past_the_largest_json_rpc_number_fail_instead_of_colliding` (`crates/lspkit-server/src/outbound.rs`) checks the id boundary directly, because reaching 2^63 real registrations is not feasible. Before the fix `wire_id` saturated: + +```text +panicked at crates/lspkit-server/src/outbound.rs:132:41: +no wire number left: Number(9223372036854775807) +``` + +So id 2^63 went out as `9223372036854775807`, the same number as id 2^63 − 1. Now `request` returns `OutboundError::IdsExhausted(raw)` and drops the registration. + +### Compile red: the API did not exist + +Remove the fix and keep the tests. + +```sh +git diff main -- crates/lspkit-server/src crates/lspkit/src > fix-48.patch +git apply -R fix-48.patch +cargo test -p lspkit-server --test outbound --test outbound_heap_leak --no-run +cargo test -p lspkit --test sidecar_correlator --no-run +``` + +```text +error[E0432]: unresolved imports `lspkit_server::Outbound`, `lspkit_server::OutboundError` +error[E0432]: unresolved imports `lspkit_server::Outbound`, `lspkit_server::OutboundError`, `lspkit_server::OutboundResponse` + | --------------- ^^^ expected `RequestId`, found `u64` + | --------------- ^^^ expected `RequestId`, found `u64` + | --------------- ^^^^^^^^^^^ expected `RequestId`, found `u64` + | --------------- ^^^ expected `RequestId`, found `u64` +error[E0308]: mismatched types +``` + +Green, after `git apply fix-48.patch`: + +```text + Summary [ 0.010s] 3 tests run: 3 passed, 0 skipped + Summary [ 0.012s] 12 tests run: 12 passed, 0 skipped +``` + +## #53: an oversized `Content-Length` is an error, never a panic or an up-front allocation + +[JSONRPC-FRAME-LIMIT]. Red against a stub (`read_message_within` existing but ignoring its cap), so the tests compile and fail on behaviour: + +```text +oversized_content_length_is_rejected_before_any_body_is_read ... FAILED + panicked at library/alloc/src/raw_vec/mod.rs:29:5: capacity overflow +a_cap_admits_its_limit_and_rejects_one_byte_more ... FAILED + one byte over the cap: Message { method: Some("x"), .. } +a_short_body_allocates_what_arrived_not_what_was_claimed ... FAILED (tests/jsonrpc_frame_heap.rs) + a 25-byte body claiming 67108864 allocated 67117299 bytes +``` + +Green after the cap check and the incremental `take(..).read_to_end` body read: `tests/jsonrpc.rs` 20/20, `tests/jsonrpc_frame_heap.rs` 1/1 (under 64 KiB allocated, and all of it released). Clippy is clean on the host and on `x86_64-pc-windows-msvc`. + +## #52: closing one document keeps every other memo warm + +[LSP-PULL-FORGET]. Red against a stub `forget` that returns `Ok(())` and does nothing: + +```text +forgetting_one_document_keeps_every_other_memo_warm ... FAILED + a forgotten document recomputes left: 1 right: 2 +forget_is_idempotent_and_a_recycled_input_never_replays_old_truth ... FAILED + B recomputes after its forget left: 1 right: 2 +open_forget_cycles_of_distinct_uris_release_their_heap ... FAILED (tests/pull_forget_heap.rs) + completed work still retains heap bytes: allocations 12089, deallocations 4072, + bytes_allocated 1439392, bytes_deallocated 334016 +``` + +Green with retired-slot reuse: `pull_forget` 2/2 (B and C remain cache hits with unchanged result ids, A recomputes once, a recycled slot reports only its new URI), `pull_forget_heap` 1/1 (1000 distinct open/forget cycles, 17000 allocations and 17000 deallocations, equal bytes), and `pull_store` 4/4. Disabling `forget` again turns all three red. + +### #53 follow-up: the header block is capped too + +DeslopClaude1 measured 544 MB RSS for a 512 MiB header line with no newline at `0dc2f04`. The red came from stub `HeaderTooLarge` and `MAX_HEADER_BYTES` items that nothing enforced: + +```text +header_blocks_past_the_cap_are_rejected_and_one_at_the_cap_decodes ... FAILED + one endless line: Closed +a_short_body_allocates_what_arrived_not_what_was_claimed ... FAILED (tests/jsonrpc_frame_heap.rs) + a 524288-byte header line under a 8192-byte cap allocated 1056768 bytes +``` + +Green once every header line is read through `take(budget)`: `tests/jsonrpc.rs` 21/21 and `jsonrpc_frame_heap` 1/1, each under 64 KiB. The test covers one endless line, endless `X: y` lines, a block exactly at the cap reaching the body, and one byte over it. + +### #52 follow-up: closes made during a pull, and repeated closes + +SharpLsp1 found that deleting the release `analyze` call in `d2a22c4` left all tests green. `029e583` (Windows agent) then retired documents on close via `try_lock`, using an `open` flag and a retry write. On top of it, this change retires closes queued during an in-flight pull as soon as that pull releases the state, instead of waiting for a later pull, and makes the queue a set, so repeated closes are one entry. `pull_forget_heap` asserts that closing 32 idle 64 KiB documents frees every report byte, that a close during an in-flight pull is freed by that pull as it finishes, and that 1000 repeated closes keep no heap. Red at `d2a22c4`: + +```text +closing 32 idle documents freed 0 of their 2097152 report bytes +``` + +On the merged code, each fix line mutated on its own turns the test red: + +```text +release `analyze` removed → freed 7072 of their 2097152 report bytes +retry write removed → freed 2050592 of their 2097152 report bytes +retire-on-close removed → freed 1532 of their 2097152 report bytes +post-query retire removed → a close during a pull freed 160 of its 65536 report bytes +``` + +`set_open(..).to(false)` removed → `forgetting_one_document_keeps_every_other_memo_warm` fails with "a close never runs the compiler: left 4, right 3" (SharpLsp1's mutant, now killed). + +## Windows + +Captured natively on Microsoft Windows 11 Enterprise (NT 10.0.26300, AMD64), Rust 1.100.0 `x86_64-pc-windows-msvc`, .NET SDK 10. On this build, `cfg(windows)` selects the real Windows code paths. Linux rows were checked in WSL (`UbuntuDev`) and by CI. + +### #49: the issue's Windows verification plan + +The plan's tables live once, in `crates/lspkit-server/src/uri_cases.rs`: 35 round trips, 14 paths → URIs, 9 rejected paths and 23 rejected URIs. Two suites run them: + +- `src/uri_style_tests.rs` runs them through `Style::Windows`, on every OS. +- `tests/uri_windows.rs` (`#![cfg(windows)]`) runs them through the public `uri_to_path` and `path_to_uri` only, so it also compiles against the pre-fix crate. It adds real-filesystem rows: + - `std::fs::canonicalize` output for a temp file, a temp dir, a dir with spaces, Unicode and reserved-character names; + - a file under a path longer than 300 characters, created through `\\?\`; + - files named `trailing dot.`, `trailing space `, `aux.rs`, `NUL`, `CON` and `com1.txt`, and a file inside a `CON` folder, each read back with its contents through the decoded path; + - a `mklink /J` junction; + - `C:\PROGRA~1`; + - drive-letter case. + +```sh +git show 384b341~1:crates/lspkit-server/src/uri.rs > crates/lspkit-server/src/uri.rs # or 384b341 for PR #50 +cargo nextest run -p lspkit-server --test uri_windows +git checkout crates/lspkit-server/src/uri.rs +``` + +Failing rows, out of the table size: + +| `uri.rs` | `uri_windows` tests | Round trips | Paths → URIs | Rejected paths accepted | Rejected URIs accepted | +|---|---|---|---|---|---| +| pre-fix `384b341~1` | 0 / 10 | 22 / 34 | 12 / 14 | 4 / 9 | 15 / 20 | +| PR #50 `384b341` | 4 / 10 | 12 / 34 | 4 / 14 | 4 / 9 | 8 / 20 | +| `7f37353` and later | 10 / 10 | 0 / 35 | 0 / 14 | 0 / 9 | 0 / 23 | + +The per-row tallies were taken before the `%252F` round trip and the encoded-host rejections joined the tables. + +The PR #50 rows that failed on Windows: + +- `file://%3F/C:/a.rs` decoded to the verbatim path `\\?\C:\a.rs`, and `file://./C:/a.rs` to the device path `\\.\C:\a.rs`. +- `\\.\pipe\name`, `\\?\Volume{…}` and `\\?\GLOBALROOT\…` encoded to URIs. +- `%2F` and `%5C` inside a name became real separators. +- A URI with a port or userinfo decoded to a UNC server name. +- A lower-case drive letter or server name gave one file a second URI. +- A real file named `trailing dot.` decoded to a path Win32 opens as `trailing dot` (NotFound). +- A real file named `NUL` decoded to a path that opens the NUL device on Windows 11. + +The full row-by-row output is on #49 (comment 6959676768). + +Two more Windows-only failures came out of the same work: + +- `tests/uri_authority.rs` did not compile on Windows (`-D unused-imports` once its only test was gated off). +- `tests/uri.rs` `truncated_percent_encoding_passes_through_bytes` failed there, because `/tmp/a%` has no drive. + +### #52 on Windows + +`pull_forget` 6/6 and `pull_forget_heap` 1/1 pass. `forget_frees_the_closed_documents_diagnostics_before_it_returns` holds a `Weak` to the forgotten report and was red on `d2a22c4`: `left: 1, right: 0`, because the memo stayed alive until a later pull and then until Salsa's next revision. It goes red again if either the immediate apply or the tombstone revision write is removed. `an_empty_uri_is_analyzed_never_mistaken_for_a_forgotten_slot` was red on `d2a22c4` with `left: [] right: [""]`. + +### Windows-only regressions found by running CI's steps here + +| Step | Failure on Windows | Fix | +|---|---|---| +| `make lint` | `clippy::doc_markdown` in a `cfg(windows)` doc comment in `lsp-profiler-dotnet/src/process.rs`. Linux never compiles it. | `13c6f82` | +| `make website-test` | `FileNotFoundError` from `subprocess.run(["npm", …])`: `CreateProcess` cannot find `npm.cmd` by its bare name. Pages were also read in the Windows code page. | `13c6f82` | +| `make test` | `clock::tests::century_leap_rules_hold_before_the_epoch` panicked with "overflow when subtracting duration from `SystemTime`". Windows `SystemTime` starts at 1601, so the 1600-02-29 row cannot be a `SystemTime` there, and fail-fast stopped the run. | `0dc2f04` | + +### #42 on Windows + +`sh scripts/release-assets.sh 0.0.0-test win-x64 linux-x64` produces both engine archives and the consumer kit. The kit holds no `bin/` or `obj/` output. With `LSP_PROFILER_DOTNET_ENGINE` pointing at the extracted `win-x64` `lsp-profiler-dotnet-engine.exe`, `cargo nextest run -p lsp-profiler-dotnet --all-features` passes 47 of 48, every .NET e2e session included. The one failure is `engine::tests::a_missing_engine_names_every_place_it_looked`, which checks the message for an absent engine and so needs the variable unset; it passes in `make test`. diff --git a/docs/plans/review-v0.5.0-to-v0.6.0.md b/docs/plans/review-v0.5.0-to-v0.6.0.md new file mode 100644 index 0000000..6a5a38a --- /dev/null +++ b/docs/plans/review-v0.5.0-to-v0.6.0.md @@ -0,0 +1,35 @@ +# Review: v0.5.0 → fix/issues-21-26-48-49 (2b2a2b4) + +Scope: `git diff v0.5.0..2b2a2b4`, 31 files, +1335/−75. The production code under review is `uri.rs` (#49, PR #50), `outbound.rs` and `jsonrpc.rs` (#48), `correlator.rs`, and `clock.rs` (which replaces humantime). The rmcp and humantime removals, the `mcp` feature removal and the toolchain pin are also covered. + +## Findings, most severe first + +- [ ] **URI-DECODE-SEPARATOR** `crates/lspkit-server/src/uri.rs:70-81`: the URI is percent-decoded before it is split into segments. `%2F` and `%5C` therefore become real separators: `file:///C:/a%5Cb.rs` decodes to `C:\a\b.rs`, and on Unix `file:///tmp/a%2Fb` decodes to `/tmp/a/b`. The existing tests `lowercase_hex_percent_decodes` and `uppercase_hex_percent_decodes` (`tests/uri.rs:91-102`) assert this behaviour, but the #49 Windows plan requires the opposite: an encoded separator never becomes one. Fix: reject a decoded `/` or `\` inside a segment with `UriError::NoLocalPath`, and replace those two tests with rejection tests (a deliberate behaviour change, recorded in the spec). +- [ ] **URI-HOST-INJECTION** `uri.rs:71,79`: the authority is percent-decoded and then pasted into a UNC prefix, so `file://host%5Cshare%5Cx/a.rs` becomes `\\host\share\x\a.rs`. Fixed: an encoded `/` or `\` in the host is `NoLocalPath`. +- [ ] **UNWRAP-OR** `uri.rs:70,88,124,140`: four `unwrap_or`/`unwrap_or_else` calls in new production code, which the user has banned. Replace them with `split_once`/`match`. +- [ ] **URI-NON-ROUNDTRIP** `uri.rs:117-140`: `path_to_uri` returns `Ok` for Windows paths that `uri_to_path` then rejects: `\\server` (no share) gives `file://server`, and `\\?\Volume{GUID}\x` gives `file:///Volume%7B…`. `path_to_uri` should return `InvalidPath` for both rather than emit a URI that never decodes. +- [ ] **BREAKING-MCP-FEATURE** `crates/lspkit-server/Cargo.toml`: removing the `mcp` feature breaks `cargo add lspkit-server --features mcp` for existing consumers with "unknown feature". This was intentional (rmcp was unused and pulled the unmaintained chrono), but the v0.6.0 release notes must say so. +- [ ] **BREAKING-PROFILER-PATH** `crates/lsp-profiler`: `lsp_profiler::session::rfc3339_now` moved to `lsp_profiler::clock::rfc3339_now`. No in-repo consumer uses it; release notes. +- [ ] **TIMING-TESTS** `crates/lsp-profiler-dotnet/tests/dotnet_e2e.rs:270,296`: both tests assert `elapsed < 5s` and fail under load (5.5 s seen at load average 10), which breaks the repo's "Deterministic, no timing" rule. This predates the diff but blocks local `make ci` on a busy machine. + +## URI fixes: red → green + +The four `uri.rs` findings were reproduced red and fixed locally. The Windows agent's rewrite of `uri.rs` (see the #49 "Windows results" comment) fixes all four and more, so this branch takes that rewrite instead of a second, conflicting change. These red results stand as cross-check tests to apply on top of it: + +```text +uri::style_tests::encoded_separators_never_become_path_boundaries ... FAILED + forged separator: "C:\\a\\b.rs" +uri::style_tests::windows_paths_no_uri_decodes_back_to_are_rejected ... FAILED + left: Some("file://server") right: None +tests/uri.rs encoded_slash_is_rejected_not_split_into_folders ... FAILED + an encoded slash: "/tmp/a/b" +``` + +- [ ] Once the Windows rewrite lands, apply the cross-check tests (Unix `%2F` rejection, `%252F` stays literal, Unix `%5C` is a name byte, encoded separators in the host) and confirm they pass. + +## Verified sound + +- `Outbound`: a registration released on `IdsExhausted` (`PendingResponse` drop), unknown/duplicate/foreign ids rejected visibly, heap proven by `outbound_heap_leak.rs`, routing proven by the 1↔2 swap mutant. +- `wire_params`: null `params` omitted per JSON-RPC 2.0; `"result": null` is still emitted for responses. +- `clock.rs`: Hinnant civil-from-days, floor before the epoch, century leap rows (1600/1900/2000/2100/2400), mutants killed (DeslopClaude1, SharpLsp1). +- Dependency removals: `cargo deny` and `osv-scanner` clean; rmcp was never called. diff --git a/docs/specs/LSP-PULL-SPEC.md b/docs/specs/LSP-PULL-SPEC.md index 018a884..7024042 100644 --- a/docs/specs/LSP-PULL-SPEC.md +++ b/docs/specs/LSP-PULL-SPEC.md @@ -17,11 +17,31 @@ identity of the same snapshot that produced its diagnostics. Document versions and workspace revisions invalidate the tracked query. Closing a document must release its retained inputs and diagnostic memos while -the connection stays alive. `PullStore::clear` discards the complete Salsa database -because Salsa input slots cannot be deleted individually. Consumers call it on -`didClose` or an equivalent scope eviction. Other documents recompute under the -advanced workspace identity. Active queries retain discarded storage only until -they finish; they return superseded. Clearing never waits behind compiler work. +the connection stays alive. Consumers call `PullStore::forget(uri)` on `didClose` +([LSP-PULL-FORGET]). `PullStore::clear` discards the complete Salsa database for +a scope eviction such as a solution switch; other documents then recompute under +the advanced workspace identity. Active queries retain discarded storage only +until they finish; they return superseded. Clearing never waits behind compiler work. + +## [LSP-PULL-FORGET] Per-Document Eviction + +`PullStore::forget(uri)` releases one closed document. Every other document's memo +stays warm, and no snapshot is superseded. Salsa input slots cannot be deleted, so +the document's input is retired instead: +- Its URI is emptied and it is flagged closed (a flag, not the empty URI, so a + consumer's own `""` is still analyzed). +- Its memo is recomputed to an empty report, and the analyzer never sees a retired slot. +- One write to the retired slot starts Salsa's next revision, which frees the + replaced memo. +- The slot is reused by the next newly opened document. + +`forget` retires the document before it returns, so its diagnostics are freed at +once, whenever no query holds the lock. It never waits behind compiler work: while +a query runs, the URI waits in a set behind its own lock (repeated closes are one +entry), and that query retires it as it releases the state, so closing every tab +during a pull and going idle still frees every report. Slots never outnumber the documents open at once, and open/forget cycles of +distinct URIs free exactly the heap they allocate. A later pull of a forgotten URI +recomputes. Forgetting an unknown or already forgotten URI is `Ok`. ## [LSP-PULL-CONCURRENCY] Nonblocking Invalidation diff --git a/docs/specs/lspkit-spec.md b/docs/specs/lspkit-spec.md index 32a18e5..5cec1b7 100644 --- a/docs/specs/lspkit-spec.md +++ b/docs/specs/lspkit-spec.md @@ -11,7 +11,7 @@ These are non-negotiable. Any patch violating one is illegal. - **Generic only.** Toolkit code, docs, READMEs, comments, and examples MUST NOT reference any specific consumer, domain, language ecosystem, or product. A grep for known consumer names returns zero hits in `crates/` and `README.md`. - **No language-specific backend code.** No Roslyn, FCS, Pyright, Ruff, tree-sitter grammar bundling, nor any other language ecosystem glue lives in any crate. `lspkit::sidecar` is pure transport — its public surface MUST NOT name any backend technology. - **Maintained bedrock or self-owned.** Toolkit MUST NOT depend on unmaintained crates for core protocol handling. `tower-lsp` is forbidden (last release 2023). The toolkit depends on `lsp-types` (maintained) and ships its own JSON-RPC server loop inside `lspkit-server`. -- **Third-party LSP/MCP types stay hidden.** No `rmcp::*` or `lsp_types::*` request/response type appears in a `pub` signature of `lspkit` or `lspkit-server`. Newtype wrappers are required at every boundary. +- **Third-party LSP/MCP types stay hidden.** No MCP SDK or `lsp_types::*` request/response type appears in a `pub` signature of `lspkit` or `lspkit-server`. Newtype wrappers are required at every boundary. - **No wire-format coupling.** The toolkit ships no typeDiagram-generated types. Consumers define their own wire schemas case-by-case. - **Locks never span `.await`.** `lspkit::vfs` MUST scope all `DashMap` and `Mutex` guards inside synchronous blocks; methods MUST NOT return guards. @@ -22,7 +22,7 @@ Four published crates. The split is locked. | Crate | Public surface (intent) | |---|---| | **lspkit** | Façade. Exposes `EngineApi` trait + `Generation`, `Snapshot`, `GenerationEvent`, `Cause`, `RescanScope`, `RescanTicket` types. Feature `tracing-setup` exposes a stdio-safe `tracing-subscriber` builder. `prelude` module re-exports the headline items. Modules `config` (layered TOML loader), `vfs` (concurrent open-document store), `sidecar` (framed IPC transport) and `treesitter` (parse cache, behind the `treesitter` feature). | -| **lspkit-server** | LSP server scaffolding built on `lsp-types`: Content-Length framing, JSON-RPC request/response/notification dispatcher, capability builder, URI ↔ `PathBuf` helpers, workspace-folders abstraction, diagnostics fan-out bus, progress sink, cancellation plumbing. Consumers register handlers; the crate routes them to an `EngineApi` + VFS pair. Modules `live` (file-watcher → debouncer → single-flight scheduler → session lifecycle) and `mcp` (`rmcp` adapter, behind the `mcp` feature). | +| **lspkit-server** | LSP server scaffolding built on `lsp-types`: Content-Length framing, JSON-RPC request/response/notification dispatcher, capability builder, URI ↔ `PathBuf` helpers, workspace-folders abstraction, diagnostics fan-out bus, progress sink, cancellation plumbing. Consumers register handlers; the crate routes them to an `EngineApi` + VFS pair. Modules `live` (file-watcher → debouncer → single-flight scheduler → session lifecycle) and `mcp` (tool adapter, always on; it takes no MCP SDK, since every `rmcp` release pulls unmaintained `chrono`, RUSTSEC-2026-0543). | | **lsp-profiler** | Language-neutral CPU and memory profiler core: sessions, hotspots, flame graphs, speedscope/V8 exports, heap snapshots, leak scoring, one JSON-RPC protocol. Spec: [LSP-PROFILER-SPEC.md](LSP-PROFILER-SPEC.md). | | **lsp-profiler-dotnet** | The profiler's .NET runtime: EventPipe CPU sampling and heap snapshots through the engine in `dotnet/`. | @@ -33,7 +33,7 @@ Implementation tracker: [docs/plans/crate-consolidation-plan.md](../plans/crate- - **[CRATE-LAYOUT]** The workspace publishes exactly four crates in dependency order: `lsp-profiler`, `lsp-profiler-dotnet`, `lspkit`, `lspkit-server`. `lsp-profiler-python` is parked: it stays in the repo, excluded from the workspace and unpublished. - **[CRATE-LAYOUT-LAYERS]** An L1 crate depends only on its own tree's L0 crate. An L0 crate has no internal dependencies. - **[CRATE-LAYOUT-MODULES]** Each absorbed crate becomes a top-level module with the same name: `lspkit::config`, `lspkit::vfs`, `lspkit::treesitter`, `lspkit::sidecar`, `lspkit_server::live`, `lspkit_server::mcp`. Each module keeps its absorbed crate's coverage floor as a `::` key in `coverage-thresholds.json`. -- **[CRATE-LAYOUT-FEATURES]** Heavy dependency cones are opt-in cargo features, all default off: `lspkit-server/mcp` (`rmcp`), `lspkit-server/pull` (`salsa`), `lspkit/treesitter` (`tree-sitter`, a C runtime) and `lspkit/tracing-setup` (`tracing-subscriber`). `lspkit::sidecar` is always on; its dependencies are already in `lspkit`'s core. +- **[CRATE-LAYOUT-FEATURES]** Heavy dependency cones are opt-in cargo features, all default off: `lspkit-server/pull` (`salsa`), `lspkit/treesitter` (`tree-sitter`, a C runtime) and `lspkit/tracing-setup` (`tracing-subscriber`). `lspkit::sidecar` is always on; its dependencies are already in `lspkit`'s core. - **[CRATE-LAYOUT-HELPER]** `lsp-profiler-helper` is a `[[bin]]` target of `lsp-profiler-python` (`src/bin/lsp-profiler-helper/`), not a crate. - **[CRATE-RETIRE]** The six absorbed `lspkit-*` names are already on crates.io at 0.0.1. After the consolidated release ships, delete `lspkit-config`, `lspkit-treesitter`, `lspkit-sidecar`, `lspkit-mcp` and `lspkit-live` on crates.io, and yank `lspkit-vfs` 0.0.1 with a deprecation README pointing to `lspkit::vfs`. @@ -91,9 +91,16 @@ pub trait EngineApi: Send + Sync + 'static { ## Cross-cutting helpers — required behavior - **[SIDECAR-REQUEST-LIFETIME]** Dropping a correlator response future immediately unregisters and frees its response channel without another correlator call or a runtime. Abandoned IDs are no longer pending, so later responses return `UnknownId`; no cancellation tombstones accumulate. `register` returns `PendingResponse`, supporting `Future` and `try_recv`. A receiver holds only a weak registry reference, so it cannot prolong the correlator's lifetime. Inbound readers terminate when both destination receivers close, even without EOF or another frame. +- **[JSONRPC-FRAME-LIMIT]** `read_message` accepts a body of at most `MAX_FRAME_BYTES` (64 MiB); `read_message_within` takes a server-chosen cap. A larger `Content-Length` is `FramingError::FrameTooLarge` before any body byte is read, a length beyond `u64` is `BadHeader`, and the body is read incrementally, so memory follows the bytes that arrive, never the claimed length. A body shorter than its claim is `Io(UnexpectedEof)`. The header block, every line and terminator included, is read through one `MAX_HEADER_BYTES` (8 KiB) budget, so one endless line and endless short lines both end in `HeaderTooLarge` with memory bounded by the cap. Peer input never panics. +- **[LSP-OUTBOUND]** Server→client requests (`lspkit-server::outbound::Outbound`): `request` allocates a numeric JSON-RPC id from the sidecar correlator and returns the request frame plus an `OutboundResponse` future; once ids pass `i64::MAX` it returns `IdsExhausted` instead of reusing a number. `null` params are omitted from the frame. `resolve` routes an inbound response to its waiter as the editor's `result` (absent or `null` is `Value::Null`) or its `JsonRpcError`. Requests and notifications are `NotAResponse`; ids never allocated, already answered or abandoned are `UnknownId`. Dropping a wait unregisters it ([SIDECAR-REQUEST-LIFETIME]). `sidecar::RequestId` rebuilds from its raw wire number with `From`, and `JsonRpcError` implements `Display` and `std::error::Error`. - **Position encoding** (`lspkit::vfs::PositionEncoding`): negotiates encoding at `initialize` per LSP 3.17 client capabilities; converts on every read; round-trips byte/char offsets correctly across multi-byte scalars and surrogate pairs. - **URI ↔ `PathBuf`** (`lspkit-server::uri`): handles `file://`, percent-decoding, Windows drive letters, UNC paths, and non-UTF-8 paths. - **[URI-AUTHORITY]** A URI's authority is its host, never a path segment. An empty or `localhost` host names a local path; any other host names a UNC path (`file://server/share/a.rs` ↔ `\\server\share\a.rs`) on Windows and is an error elsewhere. `uri_to_path` never returns a relative path. `path_to_uri` removes the extended-length prefixes `\\?\` and `\\?\UNC\` that `std::fs::canonicalize` adds. + - **[URI-WINDOWS]** All path ↔ URI handling in the lspkit tree lives in `lspkit-server::uri`; nothing else parses or builds a file path from a URI. On Windows one file has one URI, and every URI decodes to one exact, absolute path: + - The drive letter is upper case and the UNC server name lower case, in both directions. Repeated separators collapse, and `.` and `..` resolve lexically without climbing above the drive or share. + - `file:////server/share` (an empty authority before a UNC path) decodes like `file://server/share`. An IPv6 host `[fe80::1%25eth0]` becomes the server `fe80--1seth0.ipv6-literal.net`. + - `uri_to_path` returns the `\\?\` form only when Win32 would otherwise open a different file: a segment ending in `.` or a space, or a DOS device name (`CON`, `NUL`, `aux.rs`, `COM1`). + - These are errors, never a relative path: a URI with no drive or share; a host with a port, userinfo, or the device namespaces `.` and `?`; a `%2F` or `%5C` inside a name; and, on every platform, a `%` not followed by two hex digits. `path_to_uri` rejects device paths (`\\.\…`, `\\?\Volume{…}`, `\\?\GLOBALROOT\…`). - **Cancellation** (`lspkit-server::dispatch`): a `CancellationToken` is plumbed through every request handler and triggered when the client sends `$/cancelRequest`. - **Progress** (`lspkit-server::progress`): wraps LSP `window/workDoneProgress`; the `Progress` sink is a no-op when the client did not advertise the capability. - **Diagnostics bus** (`lspkit-server::diagnostics`): fan-out from one source to LSP `publishDiagnostics` AND MCP resource-updated notifications, atomically per generation. @@ -103,9 +110,8 @@ pub trait EngineApi: Send + Sync + 'static { - `tokio` 1.x, `tracing` + `tracing-subscriber`, `serde` + `serde_derive`, `async-trait`. - `lsp-types` (current major). -- `rmcp` (current minor, pinned). - `notify` 7.x, `ropey` 1.x, `dashmap` 6.x, `tokio-util` (for `CancellationToken`). -- `rmp-serde` available as the default wire format for `lspkit::sidecar`; transport is generic over `serde`. +- `lspkit::sidecar` carries opaque length-prefixed payloads; the consumer picks the encoding (JSON, MessagePack, CBOR) and its crate. - **No `tower-lsp`.** Ever. ## Public API hygiene @@ -140,6 +146,11 @@ lspkit runs a **release-only** regime modelled on [Shipwright](https://github.co Every `uses:` is pinned to a 40-character commit SHA. Workflows default to `permissions: contents: read` and elevate per job. Installs are frozen (`npm ci`, `--locked`), and the release job uses no caches. - **[REL-GITHUB]** The release attaches every crate tarball, every npm tarball, a CycloneDX SBOM and one `SHA256SUMS` over them all, cosign keyless-signed (`SHA256SUMS.sigstore.json`). Build provenance and the SBOM are attested against `SHA256SUMS`, so consumers can run `gh attestation verify --repo Nimblesite/lspkit`. `[SWR-SEC-CHECKSUM]` `[SWR-SEC-PROVENANCE]` `[SWR-SEC-SBOM]` +- **[REL-CONSUMER-ASSETS]** Nothing a consumer needs requires a source checkout. `scripts/release-assets.sh` attaches two more kinds of asset to the release, which `SHA256SUMS`, cosign and the attestations cover like every other asset: + - `lsp-profiler-dotnet-engine--.tar.gz` for `linux-x64`, `linux-arm64`, `osx-x64`, `osx-arm64`, `win-x64` and `win-arm64`: the framework-dependent .NET engine that `lsp-profiler-dotnet` launches. Point `LSP_PROFILER_DOTNET_ENGINE` at the extracted `lsp-profiler-dotnet-engine` executable, or place it beside the language server ([PROF-DOTNET-ENGINE-LOCATE]). + - `lspkit-consumer-kit-.tar.gz`: the profiler fixtures (`dotnet/fixtures`, as source), `LSP-PROFILER-SPEC.md`, `LSP-PULL-SPEC.md`, `test-support/` and `scripts/typediagram/run-cli.mjs`. + + The `lsp-profiler-dotnet` e2e suites honour `LSP_PROFILER_DOTNET_ENGINE`, so they run unchanged against a released engine. - **[REL-OIDC]** crates.io and npm both publish through OIDC Trusted Publishing from `release.yml` in the protected `release` environment. The repo stores no `CARGO_REGISTRY_TOKEN` or `NPM_TOKEN`. npm packages also publish with `--provenance`. `[SWR-SEC-OIDC-PUBLISH]` - **[REL-ORDER]** Crates publish in dependency order: `lsp-profiler`, `lsp-profiler-dotnet`, `lspkit`, `lspkit-server`. `cargo publish` waits for each crate to appear in the index, so no sleeps are needed. The npm package (`@nimblesite/lsp-profiler-ui`) publishes after the crates. Both publish wrappers skip a version that is already on the registry, so a re-run after a failure partway through is safe. - **[REL-METADATA]** Every published crate and npm package declares `license = "MIT"` and a repository. The root `LICENSE-MIT` is staged into each crate (`LICENSE-MIT`) and each npm package (`LICENSE`) at publish time, so the declared license ships with the package. `[SWR-REL-LICENSE]` @@ -156,7 +167,7 @@ A patch is acceptable iff: 3. `cargo doc --workspace --no-deps` produces docs with zero `missing_docs` warnings. 4. `cargo clippy --workspace -- -D warnings` is clean. 5. `rg -i -e 'basilisk' -e 'deslop' -e 'sharplsp' -e 'lql' -e 'tower-lsp' -e 'roslyn' -e 'pyright' crates/` returns **zero hits**. The crates stay generic; the README and website name consumers only as showcased examples. -6. `grep -R 'tower_lsp\|rmcp::' crates/*/src/lib.rs` returns **zero hits** in `pub` signatures (manual review against the rule). +6. `grep -R 'tower_lsp' crates/*/src/lib.rs` returns **zero hits** in `pub` signatures (manual review against the rule). 7. The `EngineApi` trait skeleton compiles and is exercised by a smoke test that constructs a no-op implementation and calls each method. 8. Each of the four source repos has a "Migration to `lspkit`" section in its AI-guidance file. 9. **[REL-OIDC]** `.github/workflows/release.yml` publishes crates.io (`rust-lang/crates-io-auth-action`) and npm (`--provenance`) under `environment: release`, references no registry token secret, and pins every `uses:` to a 40-char SHA. diff --git a/global.json b/global.json new file mode 100644 index 0000000..89ddff2 --- /dev/null +++ b/global.json @@ -0,0 +1,6 @@ +{ + "sdk": { + "version": "10.0.404", + "rollForward": "disable" + } +} diff --git a/packages/lsp-profiler-ui/CHANGELOG.md b/packages/lsp-profiler-ui/CHANGELOG.md new file mode 100644 index 0000000..b546741 --- /dev/null +++ b/packages/lsp-profiler-ui/CHANGELOG.md @@ -0,0 +1,17 @@ +# Changelog + +## Unreleased + +- The profiler's .NET engine, fixtures, specs and test-support ship as GitHub Release assets (`lsp-profiler-dotnet-engine--.tar.gz`, `lspkit-consumer-kit-.tar.gz`), so consumers no longer need a source checkout. + +## 0.4.0 + +### Breaking + +These functions now return a `Promise` and must be awaited: + +- `buildFlamegraphHtml(result)` now returns `Promise`. +- `applyProfileDecorations(result)`, `applyMemoryDecorations(result)` and `applyLeakDecorations(result)` now return `Promise`. +- `removeTestDir(dir)` (from `testing`) now returns `Promise`. + +A caller that does not await them gets a pending `Promise` instead of the HTML string, or runs ahead before the decorations are applied or the directory is removed. diff --git a/packages/lsp-profiler-ui/package.json b/packages/lsp-profiler-ui/package.json index a7bb9db..da9ec1a 100644 --- a/packages/lsp-profiler-ui/package.json +++ b/packages/lsp-profiler-ui/package.json @@ -7,6 +7,7 @@ "main": "./out/src/index.js", "types": "./out/src/index.d.ts", "files": [ + "CHANGELOG.md", "out/src", "!out/src/test", "!out/src/debug/test", diff --git a/packages/lsp-profiler-ui/src/debug/adapter-resolution.ts b/packages/lsp-profiler-ui/src/debug/adapter-resolution.ts index 8d0186d..8d00ef8 100644 --- a/packages/lsp-profiler-ui/src/debug/adapter-resolution.ts +++ b/packages/lsp-profiler-ui/src/debug/adapter-resolution.ts @@ -48,20 +48,70 @@ function firstNonEmpty(values: readonly (string | undefined)[]): string | undefi return values.find((value) => hasText(value)); } +/** Extensions Windows runs when PATHEXT is unset. */ +const DEFAULT_PATHEXT = ".COM;.EXE;.BAT;.CMD"; + +/** Path rules of `platform`, never of the host running this code. */ +function pathRules(platform: NodeJS.Platform): typeof path.posix { + return platform === "win32" ? path.win32 : path.posix; +} + +/** + * The environment variable `name`. Windows variable names are case-insensitive + * (PATH is usually spelled `Path`), and a copied environment object is not. + */ +function envValue(env: NodeJS.ProcessEnv, name: string, platform: NodeJS.Platform): string { + const key = + platform === "win32" ? Object.keys(env).find((candidate) => candidate.toUpperCase() === name) : name; + const value = key === undefined ? undefined : env[key]; + return hasText(value) ? value : ""; +} + /** Split PATH into its non-empty directories. */ -function pathDirectories(env: NodeJS.ProcessEnv): string[] { - return (env.PATH ?? "").split(path.delimiter).filter((dir) => dir !== ""); +function pathDirectories(env: NodeJS.ProcessEnv, platform: NodeJS.Platform): string[] { + return envValue(env, "PATH", platform) + .split(pathRules(platform).delimiter) + .filter((dir) => dir !== ""); +} + +/** + * File names `command` can have on disk. On Windows a bare command only runs + * with one of the PATHEXT extensions (`netcoredbg` is `netcoredbg.exe`). + */ +function executableNames(command: string, env: NodeJS.ProcessEnv, platform: NodeJS.Platform): string[] { + if (platform !== "win32" || pathRules(platform).extname(command) !== "") { + return [command]; + } + const pathext = envValue(env, "PATHEXT", platform); + return (hasText(pathext) ? pathext : DEFAULT_PATHEXT) + .split(";") + .filter((extension) => extension !== "") + .map((extension) => `${command}${extension.toLowerCase()}`); +} + +/** Where a bare command is looked up: PATH and PATHEXT from `env`, the path rules of `platform`. */ +export type PathLookup = Pick; + +/** + * First directory on PATH that contains `command`, or undefined. PATH, its + * separator and executable extensions follow `lookup.platform`, not the host. + */ +export function findOnPath(command: string, lookup: PathLookup): string | undefined { + const { env, existsSync: fileExists, platform } = lookup; + const rules = pathRules(platform); + const names = executableNames(command, env, platform); + return pathDirectories(env, platform) + .flatMap((dir) => names.map((name) => rules.join(dir, name))) + .find((candidate) => fileExists(candidate)); } -/** First directory on PATH that contains `command`, or undefined. */ +/** {@link findOnPath} with this host's platform rules. */ export function findExecutableOnPath( command: string, env: NodeJS.ProcessEnv, fileExists: (filePath: string) => boolean, ): string | undefined { - return pathDirectories(env) - .map((dir) => path.join(dir, command)) - .find((candidate) => fileExists(candidate)); + return findOnPath(command, { env, existsSync: fileExists, platform: process.platform }); } function resolveConfigured( @@ -72,7 +122,7 @@ function resolveConfigured( if (looksLikePath(configured)) { return fileExists(configured) ? configured : undefined; } - return findExecutableOnPath(configured, req.env, fileExists); + return findOnPath(configured, req); } /** Every bare command name to try on PATH, primary name first. */ @@ -81,9 +131,8 @@ function adapterCommandNames(req: AdapterResolutionRequest): string[] { } function resolveFromPathNames(req: AdapterResolutionRequest): string | undefined { - const { existsSync: fileExists } = req; return adapterCommandNames(req) - .map((name) => findExecutableOnPath(name, req.env, fileExists)) + .map((name) => findOnPath(name, req)) .find((found) => hasText(found)); } diff --git a/packages/lsp-profiler-ui/src/debug/test/adapter-resolution.test.ts b/packages/lsp-profiler-ui/src/debug/test/adapter-resolution.test.ts index 315dffd..add9f25 100644 --- a/packages/lsp-profiler-ui/src/debug/test/adapter-resolution.test.ts +++ b/packages/lsp-profiler-ui/src/debug/test/adapter-resolution.test.ts @@ -1,7 +1,9 @@ import * as assert from "node:assert"; +import path from "node:path"; import type { AdapterResolutionRequest } from "../adapter-resolution"; import { findExecutableOnPath, + findOnPath, looksLikePath, missingAdapterMessage, resolveAdapterCommand, @@ -32,25 +34,64 @@ suite("looksLikePath", () => { }); }); -suite("findExecutableOnPath", () => { +suite("findOnPath", () => { test("returns the first PATH dir that holds the command", () => { - const found = findExecutableOnPath( - "lldb-dap", - { PATH: ["/a", "/b"].join(":") }, - (candidate) => candidate === "/b/lldb-dap", - ); + const found = findOnPath("lldb-dap", { env: { PATH: ["/a", "/b"].join(":") }, existsSync: (candidate) => candidate === "/b/lldb-dap", platform: "linux" }); assert.strictEqual(found, "/b/lldb-dap"); }); test("skips empty PATH segments and returns undefined when absent", () => { - const found = findExecutableOnPath( - "lldb-dap", - { PATH: ":/x:" }, - () => false, - ); + const found = findOnPath("lldb-dap", { env: { PATH: ":/x:" }, existsSync: () => false, platform: "linux" }); assert.strictEqual(found, undefined); }); test("tolerates an unset PATH", () => { - assert.strictEqual(findExecutableOnPath("x", {}, () => true), undefined); + assert.strictEqual(findOnPath("x", { env: {}, existsSync: () => true, platform: "linux" }), undefined); + assert.strictEqual(findOnPath("x", { env: {}, existsSync: () => true, platform: "win32" }), undefined); + }); + test("follows the requested platform's rules, never the host's", () => { + const posix = findOnPath("lldb-dap", { env: { PATH: "/a:/b" }, existsSync: (candidate) => candidate === "/b/lldb-dap", platform: "darwin" }); + assert.strictEqual(posix, "/b/lldb-dap"); + }); + test("on Windows, splits on ';', reads Path in any case and adds PATHEXT", () => { + const env = { Path: String.raw`C:\a;;C:\tools`, PATHEXT: ".COM;.EXE" }; + const found = findOnPath("netcoredbg", { env, existsSync: (candidate) => candidate === String.raw`C:\tools\netcoredbg.exe`, platform: "win32" }); + assert.strictEqual(found, String.raw`C:\tools\netcoredbg.exe`); + }); + test("on Windows, a bare name without an extension never matches", () => { + const found = findOnPath("netcoredbg", { env: { PATH: String.raw`C:\tools` }, existsSync: (candidate) => candidate === String.raw`C:\tools\netcoredbg`, platform: "win32" }); + assert.strictEqual(found, undefined); + }); + test("on Windows, unset PATHEXT falls back to .COM .EXE .BAT .CMD", () => { + const found = findOnPath("lldb-dap", { env: { PATH: String.raw`C:\llvm\bin` }, existsSync: (candidate) => candidate === String.raw`C:\llvm\bin\lldb-dap.cmd`, platform: "win32" }); + assert.strictEqual(found, String.raw`C:\llvm\bin\lldb-dap.cmd`); + }); + test("on Windows, a command that already has an extension is used as given", () => { + const found = findOnPath("lldb-dap.exe", { env: { PATH: String.raw`C:\llvm\bin`, PATHEXT: ".EXE" }, existsSync: (candidate) => candidate === String.raw`C:\llvm\bin\lldb-dap.exe`, platform: "win32" }); + assert.strictEqual(found, String.raw`C:\llvm\bin\lldb-dap.exe`); + }); +}); + +suite("findExecutableOnPath", () => { + test("looks up with this host's separator and executable names", () => { + const tool = process.platform === "win32" ? "tool.exe" : "tool"; + const dirs = [path.join("a", "bin"), path.join("b", "bin")]; + const found = findExecutableOnPath( + "tool", + { PATH: dirs.join(path.delimiter), PATHEXT: ".EXE" }, + (candidate) => candidate === path.join("b", "bin", tool), + ); + assert.strictEqual(found, path.join("b", "bin", tool)); + }); +}); + +suite("resolveAdapterPath on Windows", () => { + test("a setting's bare name resolves through Path and PATHEXT", () => { + const request = req({ + platform: "win32", + settingValue: "lldb-dap", + env: { Path: String.raw`C:\llvm\bin`, PATHEXT: ".EXE" }, + existsSync: (candidate) => candidate === String.raw`C:\llvm\bin\lldb-dap.exe`, + }); + assert.strictEqual(resolveAdapterPath(request), String.raw`C:\llvm\bin\lldb-dap.exe`); }); }); diff --git a/scripts/release-assets.sh b/scripts/release-assets.sh new file mode 100644 index 0000000..910ef02 --- /dev/null +++ b/scripts/release-assets.sh @@ -0,0 +1,62 @@ +#!/bin/sh +# [REL-CONSUMER-ASSETS] Package what consumers would otherwise need a source +# checkout for (#42), as GitHub Release assets beside the crates and npm package: +# +# lsp-profiler-dotnet-engine--.tar.gz +# The .NET engine `lsp-profiler-dotnet` launches, one per runtime id. +# Framework-dependent (net8.0, RollForward=LatestMajor). Point +# LSP_PROFILER_DOTNET_ENGINE at the extracted executable, or place it +# beside the language server. +# lspkit-consumer-kit-.tar.gz +# The profiler fixtures (dotnet/fixtures), the specs consumers cite +# (LSP-PROFILER-SPEC.md, LSP-PULL-SPEC.md), test-support/ and +# scripts/typediagram/run-cli.mjs. +# +# Usage: scripts/release-assets.sh [rid ...] +set -eu + +if [ "$#" -lt 2 ]; then + echo "usage: $0 [rid ...]" >&2 + exit 2 +fi +version=$1 +dist=$2 +shift 2 +if [ "$#" -eq 0 ]; then + set -- linux-x64 linux-arm64 osx-x64 osx-arm64 win-x64 win-arm64 +fi + +root=$(cd "$(dirname "$0")/.." && pwd) +work=$(mktemp -d) +trap 'rm -rf "$work"' EXIT +mkdir -p "$dist" +dist=$(cd "$dist" && pwd) + +for rid in "$@"; do + out="$work/engine-$rid" + dotnet publish "$root/dotnet/LspProfiler.DotNet.Engine" \ + --configuration Release --runtime "$rid" --self-contained false \ + --output "$out" --nologo + case "$rid" in + win-*) executable=lsp-profiler-dotnet-engine.exe ;; + *) executable=lsp-profiler-dotnet-engine ;; + esac + if [ ! -f "$out/$executable" ]; then + echo "release-assets: $rid publish has no $executable" >&2 + exit 1 + fi + cp "$root/LICENSE-MIT" "$out/LICENSE" + tar -czf "$dist/lsp-profiler-dotnet-engine-$version-$rid.tar.gz" -C "$out" . +done + +kit="$work/lspkit-consumer-kit-$version" +mkdir -p "$kit/dotnet" "$kit/docs/specs" "$kit/scripts/typediagram" +# Fixtures ship as source; their build output never does. +tar -cf - -C "$root/dotnet" --exclude=bin --exclude=obj fixtures | tar -xf - -C "$kit/dotnet" +cp "$root/docs/specs/LSP-PROFILER-SPEC.md" "$root/docs/specs/LSP-PULL-SPEC.md" "$kit/docs/specs/" +cp -R "$root/test-support" "$kit/test-support" +cp "$root/scripts/typediagram/run-cli.mjs" "$kit/scripts/typediagram/" +cp "$root/LICENSE-MIT" "$kit/LICENSE" +tar -czf "$dist/lspkit-consumer-kit-$version.tar.gz" -C "$work" "lspkit-consumer-kit-$version" + +ls -l "$dist" diff --git a/scripts/typediagram/run-cli.mjs b/scripts/typediagram/run-cli.mjs index f81b5e1..01311d9 100644 --- a/scripts/typediagram/run-cli.mjs +++ b/scripts/typediagram/run-cli.mjs @@ -28,13 +28,27 @@ export function captureStdout(command, args) { function runInto(command, args, file) { const stdout = openSync(file, "w"); + // On Windows the npm shim is a `.cmd`, which only resolves through cmd.exe. + // A shell joins arguments into one command line unquoted, so each argument + // is quoted for it; a real executable runs directly, with Node's own quoting. + const shell = process.platform === "win32" && !/\.(exe|com)$/i.test(command); try { - // On Windows the npm shim is a `.cmd`, which only resolves through a shell. - execFileSync(command, args, { + const [program, argv] = shell ? [quoteForCmd(command), args.map(quoteForCmd)] : [command, args]; + execFileSync(program, argv, { stdio: ["ignore", stdout, "inherit"], - shell: process.platform === "win32", + shell, }); } finally { closeSync(stdout); } } + +/** + * One argument as the Windows C runtime parses it back from a command line: + * wrapped in quotes, with embedded quotes and the backslashes before them escaped. + * @param {string} arg + */ +export function quoteForCmd(arg) { + const escaped = arg.replace(/(\\*)"/g, '$1$1\\"').replace(/(\\+)$/, "$1$1"); + return `"${escaped}"`; +} diff --git a/scripts/typediagram/run-cli.test.mjs b/scripts/typediagram/run-cli.test.mjs index 777bb14..a8104eb 100644 --- a/scripts/typediagram/run-cli.test.mjs +++ b/scripts/typediagram/run-cli.test.mjs @@ -1,9 +1,12 @@ // Tests [PROF-WIRE-GEN]: the generator must see every byte the typeDiagram CLI // prints, or it writes a truncated wire model that no longer compiles. import assert from "node:assert/strict"; +import { mkdtempSync, rmSync, writeFileSync } from "node:fs"; +import { tmpdir } from "node:os"; +import { join } from "node:path"; import test from "node:test"; -import { captureStdout } from "./run-cli.mjs"; +import { captureStdout, quoteForCmd } from "./run-cli.mjs"; // Writes `size` bytes then exits at once, as the typeDiagram CLI does. Node // writes to a macOS pipe asynchronously, so an exit straight after the write @@ -31,3 +34,28 @@ test("a failing CLI fails the capture instead of yielding partial output", () => assert.throws(() => captureStdout(process.execPath, writeThenExit(1 << 16, 3))); assert.throws(() => captureStdout("typediagram-does-not-exist", [])); }); + +// The generator passes an absolute model path, which holds spaces when the +// checkout does. On Windows a `.cmd` shim runs through cmd.exe, which must +// hand the program back exactly the arguments it was given. +test("arguments reach a Windows .cmd shim intact", { skip: process.platform !== "win32" }, () => { + const dir = mkdtempSync(join(tmpdir(), "run-cli-shim-")); + try { + const shim = join(dir, "echo args.cmd"); + const echo = join(dir, "echo.js"); + writeFileSync(echo, "process.stdout.write(JSON.stringify(process.argv.slice(2)));"); + writeFileSync(shim, `@"${process.execPath}" "${echo}" %*\r\n`); + const args = ["--to", "rust", String.raw`C:\checkout with spaces\model.td`, 'a "quoted" word', String.raw`trailing\\`.slice(0, -1)]; + assert.deepEqual(JSON.parse(captureStdout(shim, args)), args); + } finally { + rmSync(dir, { recursive: true, force: true }); + } +}); + +test("quoting for cmd.exe escapes quotes and the backslashes before them", () => { + assert.equal(quoteForCmd("plain"), '"plain"'); + assert.equal(quoteForCmd("two words"), '"two words"'); + assert.equal(quoteForCmd('say "hi"'), '"say \\"hi\\""'); + assert.equal(quoteForCmd('a\\"b'), '"a\\\\\\"b"'); + assert.equal(quoteForCmd("dir\\"), '"dir\\\\"'); +}); diff --git a/test-support/reference_program.rs b/test-support/reference_program.rs index b69feaa..fc5db07 100644 --- a/test-support/reference_program.rs +++ b/test-support/reference_program.rs @@ -1,6 +1,6 @@ //! Locate exactly the real producer built for this test profile. -use std::path::{Path, PathBuf}; +use std::path::PathBuf; use std::process::Stdio; use std::time::Duration; use tokio::process::{Child, Command}; @@ -24,17 +24,21 @@ pub fn reference_binary() -> Result { } } -/// Resolve the producer in this test executable's exact profile directory. +/// Resolve the producer in this test executable's own profile directory. +/// +/// Cargo's build-dir layout moves test executables (`/deps/` before +/// 1.100, `/build///out/` since), while examples stay in +/// `/examples/`. The nearest ancestor holding the example is the +/// profile, so either layout finds the producer built with this test. fn profile_binary() -> Result { let executable = std::env::current_exe().map_err(|error| error.to_string())?; - let profile = executable - .parent() - .and_then(Path::parent) - .ok_or("test executable has no Cargo profile directory")?; - Ok(profile.join("examples").join(format!( - "diagnostic-reference{}", - std::env::consts::EXE_SUFFIX - ))) + let name = format!("diagnostic-reference{}", std::env::consts::EXE_SUFFIX); + executable + .ancestors() + .skip(1) + .map(|directory| directory.join("examples").join(&name)) + .find(|candidate| candidate.is_file()) + .ok_or_else(|| format!("no examples/{name} above {}", executable.display())) } /// Both test clients use the same actual producer and native stdio setup. diff --git a/test-support/state.rs b/test-support/state.rs index 2c86184..ae4b95a 100644 --- a/test-support/state.rs +++ b/test-support/state.rs @@ -1,17 +1,22 @@ //! Direct heap accounting shared by isolated, single-test executables. [MEM-FIX-SCOPE] -use stats_alloc::{Region, StatsAlloc, INSTRUMENTED_SYSTEM}; +use stats_alloc::{Region, Stats, StatsAlloc, INSTRUMENTED_SYSTEM}; use std::alloc::System; /// Each test executable has its own instrumented allocator and no sibling tests. #[global_allocator] static ALLOCATOR: &StatsAlloc = &INSTRUMENTED_SYSTEM; -/// Measure real allocation/deallocation calls before any registry inspection or teardown. -pub(crate) fn assert_releases_heap(operation: impl FnOnce()) { +/// The heap traffic of `operation` alone. +pub(crate) fn heap_change(operation: impl FnOnce()) -> Stats { let region = Region::new(ALLOCATOR); operation(); - let stats = region.change(); + region.change() +} + +/// Measure real allocation/deallocation calls before any registry inspection or teardown. +pub(crate) fn assert_releases_heap(operation: impl FnOnce()) { + let stats = heap_change(operation); assert!( stats.bytes_allocated > 0, "the probe must allocate real heap memory" diff --git a/test-support/windows-oplock/lock.ps1 b/test-support/windows-oplock/lock.ps1 new file mode 100644 index 0000000..7181029 --- /dev/null +++ b/test-support/windows-oplock/lock.ps1 @@ -0,0 +1,11 @@ +param([string]$Source) +# Holds an exclusive byte-range lock over $Source, so another process can open +# the file but its first read fails with ERROR_LOCK_VIOLATION. Windows has no +# EISDIR; this is its "opens, then cannot be read" source. [LSP-PULL-TRANSPORT] +$ErrorActionPreference='Stop' +$stream=[IO.File]::Open($Source,'Open','ReadWrite','ReadWrite') +try { + $stream.Lock(0,[Math]::Max(1,$stream.Length)) + Write-Output '{"state":"locked"}' + $null=[Console]::In.ReadLine() +} finally { $stream.Dispose() } diff --git a/test-support/windows-oplock/owner.rs b/test-support/windows-oplock/owner.rs index 992bbf7..5a0113a 100644 --- a/test-support/windows-oplock/owner.rs +++ b/test-support/windows-oplock/owner.rs @@ -4,8 +4,11 @@ use std::os::windows::process::CommandExt; use std::path::Path; use std::process::{Child, ChildStdout, Command, Stdio}; +/// `CREATE_NO_WINDOW`: the owner never opens a console window. +const CREATE_NO_WINDOW: u32 = 0x0800_0000; + /// Run only the owned oplock fixture, invisibly and without persistent policy changes. -pub fn spawn(script: &Path, source: &Path) -> Child { +pub fn spawn(script: &Path, source: &Path) -> io::Result { Command::new("powershell.exe") .args([ "-NoProfile", @@ -14,7 +17,7 @@ pub fn spawn(script: &Path, source: &Path) -> Child { "Bypass", "-File", ]) - .creation_flags(0x0800_0000) + .creation_flags(CREATE_NO_WINDOW) .arg(script) .arg("-Source") .arg(source) @@ -22,7 +25,6 @@ pub fn spawn(script: &Path, source: &Path) -> Child { .stdout(Stdio::piped()) .stderr(Stdio::piped()) .spawn() - .expect("start owned native source owner") } /// Parse fixture state, never infer readiness from elapsed time. @@ -30,7 +32,7 @@ pub fn state(output: &mut BufReader, expected: &str) -> io::Result< let mut line = String::new(); let _length = output.read_line(&mut line)?; let value: serde_json::Value = serde_json::from_str(&line).map_err(io::Error::other)?; - if value["state"] != expected { + if value.get("state").and_then(serde_json::Value::as_str) != Some(expected) { return Err(io::Error::other("unexpected native source state")); } Ok(()) diff --git a/website/_site/assets/css/styles.css b/website/_site/assets/css/styles.css new file mode 100644 index 0000000..b8ea5cd --- /dev/null +++ b/website/_site/assets/css/styles.css @@ -0,0 +1,198 @@ +/* [WEB-DESIGN-COLOR] Fieldwork tokens; structural layout comes from techdoc. */ +:root { + color-scheme: light; + --color-bg: #f4f1e9; + --color-text: #202720; + --color-primary: #ad321e; + --color-border: #c9ccbf; + --color-muted: #60665b; + --color-panel: #e8ebdf; + --max-width: 1200px; + --content-width: 68ch; + --header-height: 89px; + --font-mono: "SFMono-Regular", Consolas, monospace; +} +[data-theme="dark"] { + color-scheme: dark; + --color-bg: #191c19; + --color-text: #f4f1e9; + --color-primary: #ff997f; + --color-border: #454b41; + --color-muted: #b3b9ac; + --color-panel: #252c24; +} +/* [WEB-DESIGN-TYPE] Local fonts, no network font dependencies. */ +body { background: var(--color-bg); color: var(--color-text); font: 18px/1.7 Arial, Helvetica, sans-serif; } +a { color: inherit; text-underline-offset: 5px; } +a:hover { color: var(--color-primary); } +button { color: inherit; cursor: pointer; } +::selection { color: var(--color-bg); background: var(--color-text); } +:focus-visible { outline: 3px solid var(--color-primary); outline-offset: 5px; } +h1, h2, h3, p, figure { margin: 0; } +h1, h2, h3 { text-wrap: balance; } +h1 { font-size: clamp(40px, 5.5vw, 72px); line-height: 1.05; letter-spacing: -.05em; } +h2 { font-size: clamp(28px, 3.5vw, 44px); line-height: 1.12; letter-spacing: -.04em; } +h3 { font-size: 25px; line-height: 1.25; letter-spacing: -.025em; } +/* [WEB-DESIGN-COMPONENTS] Shared masthead and footer. */ +.site-header { background: var(--color-bg); border-bottom: 1px solid var(--color-border); } +.nav { min-height: var(--header-height); padding: 20px 32px; } +.logo { display: flex; align-items: center; gap: 12px; font-size: 29px; font-weight: 800; letter-spacing: -.06em; text-decoration: none; } +.logo::before { content: ""; flex: 0 0 44px; width: 44px; height: 44px; background: url("/assets/images/logo.png") center / contain no-repeat; } +[data-theme="dark"] .logo::before { background-color: #f4f1e9; background-size: 40px; border-radius: 6px; } +.nav-links { margin-left: auto; gap: 32px; } +.nav-link { display: inline-flex; min-height: 44px; align-items: center; font-size: 14px; text-decoration: none; } +.nav-link.active { color: var(--color-primary); text-decoration: underline; } +.nav-actions { margin-left: 24px; } +.theme-toggle { display: grid; place-items: center; width: 44px; height: 44px; border: 1px solid var(--color-border); background: transparent; font-size: 22px; } +.theme-icon-dark { display: none; } +[data-theme="dark"] .theme-icon-dark { display: inline; } +[data-theme="dark"] .theme-icon-light { display: none; } +.site-footer { border-top: 1px solid var(--color-border); } +.footer-content { padding: 32px; } +.footer-grid { display: block; } +.footer-section { display: flex; justify-content: space-between; gap: 24px; } +.footer-section h3 { font-size: 16px; letter-spacing: -.02em; } +.footer-section ul { display: flex; gap: 24px; font-size: 13px; } +/* [WEB-DESIGN-LAYOUT] Homepage editorial grid. */ +.home-shell { max-width: var(--max-width); margin: auto; padding: 0 32px; } +.hero { display: grid; grid-template-columns: 1.8fr 1fr; align-items: end; gap: 64px; padding: 64px 0 48px; } +.eyebrow { font: 12px/1.5 var(--font-mono); letter-spacing: .1em; text-transform: uppercase; color: var(--color-muted); } +.hero .eyebrow { display: flex; align-items: center; gap: 10px; margin-bottom: 28px; } +.signal { height: 7px; width: 7px; background: var(--color-primary); } +.hero h1 { font-size: clamp(44px, 5.6vw, 80px); line-height: .96; letter-spacing: -.065em; } +.hero h1 span { color: var(--color-primary); } +.hero-aside { padding-bottom: 6px; } +.hero-aside p { color: var(--color-muted); font-size: 17px; line-height: 1.6; margin-bottom: 24px; } +.text-link { display: inline-flex; gap: 28px; align-items: center; min-height: 44px; font-size: 14px; font-weight: bold; } +.registries { display: flex; flex-wrap: wrap; gap: 0 56px; padding: 20px 0; border-top: 1px solid var(--color-border); } +.registry, .registry ul { display: flex; flex-wrap: wrap; align-items: center; gap: 8px 20px; padding: 0; list-style: none; } +.registry-name { display: inline-flex; align-items: center; gap: 10px; font: 12px/1.5 var(--font-mono); letter-spacing: .1em; text-transform: uppercase; color: var(--color-muted); } +.registry svg { width: 22px; height: 22px; } +.registry a { display: inline-flex; min-height: 44px; align-items: center; gap: 6px; font: 14px var(--font-mono); text-decoration: none; } +.registry a span { color: var(--color-primary); } +.hero-links { display: flex; flex-wrap: wrap; align-items: center; gap: 12px 32px; } +.repo-link { display: inline-flex; align-items: center; gap: 10px; min-height: 44px; padding: 0 18px; background: var(--color-text); color: var(--color-bg); font-size: 14px; font-weight: bold; text-decoration: none; } +.repo-link svg { width: 18px; height: 18px; } +.footer-bottom { display: none; } +.engineering-plate { border-top: 1px solid var(--color-border); } +.engineering-plate img { width: 100%; height: auto; display: block; } +.engineering-plate figcaption { display: flex; justify-content: space-between; gap: 20px; padding: 14px 0; border-bottom: 1px solid var(--color-border); font: 10px/1.5 var(--font-mono); letter-spacing: .08em; text-transform: uppercase; color: var(--color-muted); } +.journal-preview { padding: 56px 0 72px; } +.section-heading { display: flex; justify-content: space-between; align-items: center; gap: 24px; margin-bottom: 32px; } +.journal-row { display: grid; grid-template-columns: 160px 1fr 150px; gap: 32px; border-top: 1px solid var(--color-border); padding: 28px 0; } +.journal-row h3 a { text-decoration: none; } +.journal-row h3 span { color: var(--color-primary); } +.journal-row p { margin-top: 12px; color: var(--color-muted); font-size: 16px; max-width: 54ch; } +.journal-row time { font: 12px/1.7 var(--font-mono); color: var(--color-muted); text-align: right; } +/* Shared techdoc article, list and documentation surfaces. */ +main > .blog-container { max-width: var(--max-width); margin: auto; padding: 56px 32px 72px; } +.blog-header { padding-bottom: 32px; } +.blog-header .eyebrow { margin-bottom: 20px; } +.journal-entry { display: grid; grid-template-columns: 220px minmax(0, 1fr); gap: 32px; padding-block: 32px; border-top: 1px solid var(--color-border); } +.journal-entry h2 { font-size: clamp(28px, 3.5vw, 44px); } +.journal-entry h2 a { text-decoration: none; } +.journal-entry h2 a:hover { text-decoration: underline; } +.journal-entry p:not(.eyebrow) { max-width: 54ch; margin-block: 20px 12px; color: var(--color-muted); } +.blog-subtitle { color: var(--color-muted); margin-top: 24px; } +.blog-nav { display: flex; flex-wrap: wrap; gap: 24px; padding-bottom: 24px; font: 13px var(--font-mono); } +.blog-nav-link { padding: 12px 0; } +.post-list .blog-post { max-width: none; padding: 32px 0; border-top: 1px solid var(--color-border); } +.post-title { font-size: 32px; line-height: 1.2; letter-spacing: -.035em; font-weight: bold; } +.post-meta, .blog-post-meta { font: 12px/1.7 var(--font-mono); color: var(--color-muted); margin: 16px 0; } +.post-excerpt { margin: 16px 0; } +.tag, .tag-link { display: inline-flex; align-items: center; min-height: 44px; padding: 8px 12px; border: 1px solid var(--color-border); font: 12px/1.7 var(--font-mono); } +.post-tags, .tag-cloud { display: flex; flex-wrap: wrap; gap: 8px; } +.category-grid { display: grid; grid-template-columns: repeat(auto-fit, minmax(min(100%, 240px), 1fr)); gap: 24px; } +.category-card { display: block; padding: 24px 0; border-block: 1px solid var(--color-border); text-decoration: none; } +.category-card p { margin-top: 12px; color: var(--color-muted); } +main > .blog-post, .article-shell { width: 100%; max-width: 960px; margin-inline: auto; padding: 48px 32px 72px; } +.blog-post-header { max-width: 800px; } +.article-header { margin-bottom: 32px; } +.article-header .eyebrow { margin-bottom: 16px; } +.article-description { color: var(--color-muted); max-width: 56ch; margin-top: 24px; } +.page-illustration { margin: 0 0 32px; background: var(--color-panel); } +.page-illustration img { display: block; width: 100%; height: auto; } +.btn { display: inline-flex; align-items: center; min-height: 44px; padding: 8px 20px; border: 1px solid var(--color-border); } +.docs-layout { grid-template-columns: 180px minmax(0, 1fr); padding: 48px 32px 80px; gap: 48px; } +.sidebar { position: sticky; top: calc(var(--header-height) + 24px); font-size: 14px; } +.sidebar a { display: block; min-height: 44px; padding: 10px 12px; text-decoration: none; border-left: 2px solid var(--color-border); } +.sidebar a.active { color: var(--color-primary); border-color: var(--color-primary); background: var(--color-panel); } +.docs-content { min-width: 0; max-width: none; } +/* [WEB-ACCESS-PROSE] Every long-form surface uses this single prose class. */ +.prose { width: 100%; max-width: var(--content-width); margin-inline: auto; overflow-wrap: anywhere; } +.prose > * + * { margin-top: 24px; } +.prose > :first-child { margin-top: 0; } +.prose :is(h1, h2, h3, h4) { scroll-margin-top: calc(var(--header-height) + 24px); } +.prose :is(h2, h3, h4) { margin-top: 48px; margin-bottom: 16px; } +.prose h2 { font-size: clamp(28px, 3vw, 36px); } +.prose h3 { font-size: 25px; } +.prose h4 { font-size: 20px; line-height: 1.3; } +.prose .header-anchor { text-decoration: none; } +.prose .header-anchor:hover { text-decoration: underline; text-decoration-thickness: 1px; } +.prose :is(ul, ol) { padding-left: 1.5em; } +.prose li + li { margin-top: 8px; } +.prose li > :is(ul, ol) { margin-block: 12px; } +.prose li::marker { color: var(--color-primary); } +.prose blockquote { margin-inline: 0; border-left: 3px solid var(--color-primary); padding: 12px 24px; font-size: clamp(20px, 2.5vw, 24px); letter-spacing: -.02em; } +.prose blockquote p + p { margin-top: 16px; } +.prose :is(code, pre) { font-family: var(--font-mono); background: var(--color-panel); } +.prose code { padding: 3px 6px; font-size: .85em; } +.prose pre { max-width: 100%; padding: 24px; overflow-x: auto; border: 1px solid var(--color-border); line-height: 1.6; } +.prose pre code { padding: 0; white-space: pre; overflow-wrap: normal; } +.prose .table-scroll { max-width: 100%; overflow-x: auto; } +.prose table { width: 100%; border-collapse: collapse; font-size: 16px; text-align: left; } +.prose :is(th, td) { padding: 16px; vertical-align: top; border-bottom: 1px solid var(--color-border); } +.prose thead { background: var(--color-panel); } +.prose img { display: block; max-width: 100%; height: auto; } +/* [WEB-EXAMPLES] Example videos and screenshot grid. */ +.home-proof { max-width: none; padding: 48px 0; border-top: 1px solid var(--color-border); } +.prose .example-video { margin: 32px 0; } +.prose video { display: block; width: 100%; height: auto; background: var(--color-panel); border: 1px solid var(--color-border); } +.prose .example-shots { display: grid; grid-template-columns: repeat(2, minmax(0, 1fr)); gap: 24px; margin: 32px 0; } +.prose .example-shots img { border: 1px solid var(--color-border); } +.prose .example-shots figure:only-child { grid-column: 1 / -1; } +@media (max-width: 768px) { .prose .example-shots { grid-template-columns: 1fr; } } +.prose figcaption { margin-top: 12px; color: var(--color-muted); font: 12px/1.6 var(--font-mono); } +.prose .example-project { display: block; margin-bottom: 4px; color: var(--color-text); font: 15px/1.5 Arial, Helvetica, sans-serif; text-decoration: none; } +.prose .example-project strong { color: var(--color-primary); } +.prose hr { margin-block: 40px; border: 0; border-top: 1px solid var(--color-border); } +.prose details { padding: 16px 20px; border-block: 1px solid var(--color-border); } +.prose summary { cursor: pointer; font-weight: bold; min-height: 44px; padding-block: 8px; } +.prose details > p { margin-block: 16px; } +.token.comment { color: var(--color-muted); } +.token.keyword, .token.function, .token.string { color: var(--color-primary); } +.skip-link { position: fixed; top: -100px; left: 16px; z-index: 200; padding: 12px 20px; background: var(--color-text); color: var(--color-bg); } +.skip-link:focus { top: 12px; } +/* [WEB-ACCESS] Mobile uses the plugin's real menu interaction. */ +@media (max-width: 768px) { + :root { --header-height: 85px; } + .nav, .footer-content { padding: 20px; } + .nav-actions { margin-left: auto; } + .nav-links { margin: 0; background: var(--color-bg); border-bottom: 1px solid var(--color-border); gap: 8px; padding: 20px; max-height: calc(100dvh - var(--header-height)); overflow-y: auto; } + .mobile-menu-toggle { min-width: 44px; min-height: 44px; align-items: center; justify-content: center; } + .home-shell { padding: 0 20px; } + .hero { grid-template-columns: 1fr; gap: 28px; padding: 40px 0 32px; } + .hero-aside { max-width: 42ch; } + .hero .eyebrow { font-size: 10px; margin-bottom: 24px; } + .hero-aside p { margin-bottom: 12px; } + .engineering-plate figcaption { font-size: 8px; } + .engineering-plate figcaption span:last-child { display: none; } + .journal-preview { padding: 40px 0; } + .section-heading { align-items: start; gap: 16px; } + .section-heading .text-link { flex-shrink: 0; gap: 10px; } + .journal-row { grid-template-columns: 1fr; gap: 16px; } + .journal-entry { grid-template-columns: minmax(0, 1fr); gap: 16px; } + .journal-row time { text-align: left; } + .footer-section, .footer-section ul { flex-wrap: wrap; } + main > .blog-container, main > .blog-post, .article-shell { padding: 32px 20px 48px; } + .docs-layout { grid-template-columns: minmax(0, 1fr); padding: 24px 20px 48px; gap: 32px; } + .docs-layout .sidebar { position: static; width: auto; height: auto; transform: none; transition: none; padding: 0; border: 0; z-index: auto; } + .docs-nav > ul { display: flex; flex-wrap: wrap; gap: 8px; } + .sidebar a { border: 1px solid var(--color-border); padding: 8px 12px; } + .article-header h1, .blog-post-header h1, .blog-header h1 { font-size: clamp(36px, 8vw, 56px); overflow-wrap: anywhere; } + .prose { font-size: 17px; } + .prose :is(h2, h3, h4) { margin-top: 36px; } + .prose :is(th, td) { padding: 12px; min-width: 120px; } + .prose pre { padding: 16px; } + .prose blockquote { padding: 8px 16px; } +} diff --git a/website/_site/assets/images/apple-touch-icon.png b/website/_site/assets/images/apple-touch-icon.png new file mode 100644 index 0000000..5df4722 Binary files /dev/null and b/website/_site/assets/images/apple-touch-icon.png differ diff --git a/website/_site/assets/images/engine.svg b/website/_site/assets/images/engine.svg new file mode 100644 index 0000000..135e3e4 --- /dev/null +++ b/website/_site/assets/images/engine.svg @@ -0,0 +1,32 @@ + + One engine, two surfaces + An architectural drawing of an orange core connected to two dark terminals. + + + + + + + + + + + + + + + + + + + + + + + + + + + + + diff --git a/website/_site/assets/images/examples/osprey-flamegraph.webp b/website/_site/assets/images/examples/osprey-flamegraph.webp new file mode 100644 index 0000000..9a5d9f7 Binary files /dev/null and b/website/_site/assets/images/examples/osprey-flamegraph.webp differ diff --git a/website/_site/assets/images/examples/osprey-heatmap.webp b/website/_site/assets/images/examples/osprey-heatmap.webp new file mode 100644 index 0000000..826cec9 Binary files /dev/null and b/website/_site/assets/images/examples/osprey-heatmap.webp differ diff --git a/website/_site/assets/images/favicon.ico b/website/_site/assets/images/favicon.ico new file mode 100644 index 0000000..4ed0ce8 Binary files /dev/null and b/website/_site/assets/images/favicon.ico differ diff --git a/website/_site/assets/images/logo.png b/website/_site/assets/images/logo.png new file mode 100644 index 0000000..05bce1e Binary files /dev/null and b/website/_site/assets/images/logo.png differ diff --git a/website/_site/assets/images/readme-hero.svg b/website/_site/assets/images/readme-hero.svg new file mode 100644 index 0000000..504ef4b --- /dev/null +++ b/website/_site/assets/images/readme-hero.svg @@ -0,0 +1,47 @@ + + lspkit — The foundation for any language's tooling. + The lspkit wordmark and headline beside an engineering plate: one vermilion engine routes to two terminal surfaces, LSP and MCP. + + + + + + + + + + + lspkit + THE LANGUAGE SERVER TOOLKIT + The foundation + for any language's + tooling. + RUST · LSP · MCP · PROFILER + FIG. 01 — ONE ENGINE, TWO SURFACES + + + + + + + + + Your engine + + LSP + + MCP + + PROFILE + diff --git a/website/_site/blog/categories/field-notes/index.html b/website/_site/blog/categories/field-notes/index.html new file mode 100644 index 0000000..6d97f70 --- /dev/null +++ b/website/_site/blog/categories/field-notes/index.html @@ -0,0 +1,254 @@ + + + + + + + + + Field-notes + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + +
+
+
+

Field-notes

+

← All categories

+
+ +
+ + + +
+
+ +
+ + + + + + + + + diff --git a/website/_site/blog/categories/index.html b/website/_site/blog/categories/index.html new file mode 100644 index 0000000..f74a2cc --- /dev/null +++ b/website/_site/blog/categories/index.html @@ -0,0 +1,229 @@ + + + + + + + + + Categories + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + +
+
+
+

Categories

+

Browse blog posts by category.

+
+ + + + +
+ +
+ + + + + + + + + diff --git a/website/_site/blog/index.html b/website/_site/blog/index.html new file mode 100644 index 0000000..4aaf325 --- /dev/null +++ b/website/_site/blog/index.html @@ -0,0 +1,236 @@ + + + + + + + + + Journal — lspkit + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + +
+ +
+
+

The lspkit journal

+

Notes from the workbench.

+

Ideas, decisions and working notes on building better developer tools.

+
+
+ An orange shared engine connects to two terminal surfaces through circuit paths. +
+ +
+ + + +
+
+ +
+ + + + + + + + + diff --git a/website/_site/blog/one-engine/index.html b/website/_site/blog/one-engine/index.html new file mode 100644 index 0000000..c6be5eb --- /dev/null +++ b/website/_site/blog/one-engine/index.html @@ -0,0 +1,321 @@ + + + + + + + + + Build an MCP server and a language server from one Rust engine. + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + +
+ + + + + + + +
+
+
+ +

Build an MCP server and a language server from one Rust engine.

+ + + +
+ +
+ +
+ An orange shared engine connects to two terminal surfaces through circuit paths. +
+ +
+

Your VS Code extension talks to a language server over the Language Server Protocol (LSP). AI coding agents such as Claude Code, Cursor and Codex talk to tools over the Model Context Protocol (MCP). If you build developer tools, you now need both, and they should give the same answers.

+

What is an MCP server?

+

An MCP server exposes tools and data to an AI agent over a small JSON-RPC protocol. Where a language server answers "what's the type at this cursor?" for an editor, an MCP server answers "find every caller of this function" for an agent. Both need the same thing underneath: a live, accurate model of the code.

+

The trap: two pipelines

+

The obvious build is a language server with its own caches, then an MCP server bolted on with another set. Now two pipelines parse the same files, watch the same folders and drift apart. The agent sees stale results the editor has already fixed.

+

One engine, two surfaces

+

lspkit puts analysis behind one Rust trait, EngineApi. The lspkit-server crate serves it over LSP to VS Code; its mcp feature serves the same engine over MCP to Claude Code, Cursor or Codex.

+
cargo add lspkit lspkit-server --features lspkit-server/mcp
+

Every report carries a generation number, so the editor and the agent can both tell which state of the workspace produced an answer. Cancellation and progress live at the same boundary, so a slow query from the agent never blocks the editor.

+

Find the memory leak before your users do

+

Long-running language servers leak. lspkit ships lsp-profiler, a CPU and memory profiler that runs inside VS Code: hot lines painted in your source, a flame graph beside it, and a live heap timeline that ranks memory leak suspects. SharpLsp uses it for C# and F#; Osprey uses it for a native functional language. Watch both on the examples page.

+
SharpLsp · C# and F# language server for VS Code ↗Finding and fixing a memory leak in a running C# app. Watch the walkthrough ↗
+ +

Start building

+ +
+ + +
+ + +
+
+ +
+ + + + + + + + + diff --git a/website/_site/blog/tags/index.html b/website/_site/blog/tags/index.html new file mode 100644 index 0000000..43dacc3 --- /dev/null +++ b/website/_site/blog/tags/index.html @@ -0,0 +1,244 @@ + + + + + + + + + Tags + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + +
+ + +
+ + + + + + + + + diff --git a/website/_site/blog/tags/language-server-protocol/index.html b/website/_site/blog/tags/language-server-protocol/index.html new file mode 100644 index 0000000..0c4e054 --- /dev/null +++ b/website/_site/blog/tags/language-server-protocol/index.html @@ -0,0 +1,254 @@ + + + + + + + + + Posts tagged 'language-server-protocol' + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + +
+
+
+

Posts tagged "language-server-protocol"

+

← All tags

+
+ +
+ + + +
+
+ +
+ + + + + + + + + diff --git a/website/_site/blog/tags/mcp-server/index.html b/website/_site/blog/tags/mcp-server/index.html new file mode 100644 index 0000000..eb04efd --- /dev/null +++ b/website/_site/blog/tags/mcp-server/index.html @@ -0,0 +1,254 @@ + + + + + + + + + Posts tagged 'mcp-server' + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + +
+
+
+

Posts tagged "mcp-server"

+

← All tags

+
+ +
+ + + +
+
+ +
+ + + + + + + + + diff --git a/website/_site/blog/tags/rust/index.html b/website/_site/blog/tags/rust/index.html new file mode 100644 index 0000000..d41bd56 --- /dev/null +++ b/website/_site/blog/tags/rust/index.html @@ -0,0 +1,254 @@ + + + + + + + + + Posts tagged 'rust' + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + +
+
+
+

Posts tagged "rust"

+

← All tags

+
+ +
+ + + +
+
+ +
+ + + + + + + + + diff --git a/website/_site/blog/tags/vscode-extension/index.html b/website/_site/blog/tags/vscode-extension/index.html new file mode 100644 index 0000000..1729cb6 --- /dev/null +++ b/website/_site/blog/tags/vscode-extension/index.html @@ -0,0 +1,254 @@ + + + + + + + + + Posts tagged 'vscode-extension' + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + +
+
+
+

Posts tagged "vscode-extension"

+

← All tags

+
+ +
+ + + +
+
+ +
+ + + + + + + + + diff --git a/website/_site/docs/crates/index.html b/website/_site/docs/crates/index.html new file mode 100644 index 0000000..bf818a8 --- /dev/null +++ b/website/_site/docs/crates/index.html @@ -0,0 +1,392 @@ + + + + + + + + + Crates. + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + +
+ + + + + + + + +
+ + +
+
+

Documentation

+

Crates.

+

What each published crate and package provides.

+
+ +
+ An orange shared engine connects to two terminal surfaces through circuit paths. +
+ +
+ + + + + + + + + + + + + + + + + + + + + + + + + +
CrateWhat it gives you
lspkitEngineApi and its types, plus the config, vfs and sidecar modules.
lspkit-serverJSON-RPC LSP server scaffolding on lsp-types: capabilities, dispatch, URIs, diagnostics, progress, cancellation and live file watching.
lsp-profilerLanguage-neutral CPU and memory profiler for language servers.
lsp-profiler-dotnetThe profiler's .NET runtime, using EventPipe.
+
SharpLsp sidebar with the Processes view listing a running .NET process with Profile CPU and Track Memory actions
SharpLsp · C# and F# language server for VS Code ↗Processes view: profile any running process.
SharpLsp Profiler results panel with summary cards, an embedded flame graph and the hot functions table
SharpLsp · C# and F# language server for VS Code ↗Results panel, flame graph and hot functions.
+

Features

+ + + + + + + + + + + + + + + + + + + + + + + + + + + + + + +
CrateFeatureAdds
lspkittreesitterTree-sitter parsing helpers.
lspkittracing-setupA ready-made tracing subscriber.
lspkit-servermcpThe MCP adapter over the same engine.
lspkit-serverpullPull-based incremental analysis with salsa.
+

Editor package

+

@nimblesite/lsp-profiler-ui is the VS Code UI for the profiler: processes, CPU heat map, flame graph, memory dashboard and retention graph. It also carries Debug Adapter Protocol glue for extension tests.

+
npm install @nimblesite/lsp-profiler-ui
+ +
+ + + + +
+
+ +
+ + + + + + + + + diff --git a/website/_site/docs/examples/index.html b/website/_site/docs/examples/index.html new file mode 100644 index 0000000..99ae53f --- /dev/null +++ b/website/_site/docs/examples/index.html @@ -0,0 +1,343 @@ + + + + + + + + + Built with lspkit. + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + +
+ + + + + + + + +
+ + +
+
+

Documentation

+

Built with lspkit.

+

SharpLsp and Osprey ship the lspkit profiler in VS Code today.

+
+ +
+ An orange shared engine connects to two terminal surfaces through circuit paths. +
+ +
+

Two language servers run on lspkit today. Both mount the same lsp-profiler engine and the same @nimblesite/lsp-profiler-ui panels; only the runtime crate differs.

+

SharpLsp: C# and F#

+

SharpLsp is a C# and F# language server for VS Code. Its profiler attaches to any running .NET process through lsp-profiler-dotnet, paints hot lines in your code, and ranks leak suspects while the app runs.

+
SharpLsp · C# and F# language server for VS Code ↗SharpLsp profiling a real .NET app in VS Code. Watch the walkthrough ↗
+ +
SharpLsp · C# and F# language server for VS Code ↗The same profiler on F#: pipeline stages keep their compiler names. Watch the walkthrough ↗
+

Read the SharpLsp profiler guide ↗

+

Osprey: a native functional language

+

Osprey is a functional language with algebraic effects that compiles to native code. Its VS Code extension uses the same profiler to run and profile the open file, attach to a running program, and check for leaks.

+
Osprey · native functional language with a VS Code extension ↗Osprey's VS Code extension running the lspkit profiler. Watch the walkthrough ↗
+ +

Watch the Osprey profiler tutorial ↗

+

Add your language

+

A new language needs only a runtime crate that samples its processes. See Profiler for how the pieces fit.

+
+ + + + +
+
+ +
+ + + + + + + + + diff --git a/website/_site/docs/getting-started/index.html b/website/_site/docs/getting-started/index.html new file mode 100644 index 0000000..53b44c2 --- /dev/null +++ b/website/_site/docs/getting-started/index.html @@ -0,0 +1,364 @@ + + + + + + + + + Getting started. + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + +
+ + + + + + + + +
+ + +
+
+

Documentation

+

Getting started.

+

Add lspkit to a language server and implement the engine.

+
+ +
+ An orange shared engine connects to two terminal surfaces through circuit paths. +
+ +
+

This is what you are building toward: your language server, profiled inside VS Code.

+
Osprey · native functional language with a VS Code extension ↗Osprey's VS Code extension running the lspkit profiler. Watch the walkthrough ↗
+

Add the crates

+
cargo add lspkit lspkit-server
+

Turn on the features you need:

+
cargo add lspkit --features treesitter
+cargo add lspkit-server --features mcp
+

Implement the engine

+

Your analysis implements EngineApi. It defines its own report, query and error types, and four operations:

+
#[async_trait]
+pub trait EngineApi: Send + Sync + 'static {
+    type Report: Send + Sync + 'static;
+    type Query: Send + Sync;
+    type Error: StdError + Send + Sync + 'static;
+
+    fn generation(&self) -> Generation;
+    async fn report(&self, query: Self::Query, cancel: CancellationToken)
+        -> Result<Snapshot<Self::Report>, Self::Error>;
+    async fn rescan(&self, scope: RescanScope, progress: Progress)
+        -> Result<RescanTicket, Self::Error>;
+    fn subscribe(&self) -> GenerationEventStream;
+    async fn shutdown(&self) -> Result<(), Self::Error>;
+}
+
    +
  • generation increases on every successful rescan.
  • +
  • report answers a query from a snapshot and stops promptly when cancelled.
  • +
  • rescan refreshes the analysis and may coalesce concurrent requests.
  • +
  • subscribe streams generation changes to listeners.
  • +
  • shutdown drains in-flight work; every later call returns an error.
  • +
+

Serve it

+

Hand one engine instance to the lspkit-server scaffolding for the editor and, with the mcp feature, to the MCP adapter for agents. Both read the same snapshots.

+ +

Learn more

+

The full behavioural contract is in the spec, and the API reference is on docs.rs.

+
+ + + + +
+
+ +
+ + + + + + + + + diff --git a/website/_site/docs/index.html b/website/_site/docs/index.html new file mode 100644 index 0000000..258ab36 --- /dev/null +++ b/website/_site/docs/index.html @@ -0,0 +1,346 @@ + + + + + + + + + A small foundation. + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + +
+ + + + + + + + +
+ + +
+
+

Documentation

+

A small foundation.

+

One engine behind your language server and MCP server, with a profiler built into VS Code.

+
+ +
+ An orange shared engine connects to two terminal surfaces through circuit paths. +
+ +
+

lspkit is a set of Rust crates for building language servers. Your analysis runs once, in one engine, and both an LSP server and an MCP server read from it.

+
SharpLsp · C# and F# language server for VS Code ↗SharpLsp profiling a real .NET app in VS Code. Watch the walkthrough ↗
+

One engine

+

EngineApi is the boundary between your analysis and the protocols. Every report carries a generation number, so a client always knows which state of the workspace produced an answer.

+

Two surfaces

+

The LSP server and the MCP adapter consume the same engine. Protocol handling stays at the edges; analysis lives in one place.

+

Shared plumbing

+

File tracking, configuration, sidecar processes, tree-sitter parsing, transport and profiling are ready-made. Use the parts that fit your server.

+ +

Next steps

+ +
+ + + + +
+
+ +
+ + + + + + + + + diff --git a/website/_site/docs/profiler/index.html b/website/_site/docs/profiler/index.html new file mode 100644 index 0000000..fcdad2b --- /dev/null +++ b/website/_site/docs/profiler/index.html @@ -0,0 +1,350 @@ + + + + + + + + + Profiler. + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + +
+ + + + + + + + +
+ + +
+
+

Documentation

+

Profiler.

+

One CPU and memory profiler for every language server.

+
+ +
+ An orange shared engine connects to two terminal surfaces through circuit paths. +
+ +
+

lsp-profiler profiles a running language server without leaving the editor. A language plugs in a runtime crate; sessions, reports and the protocol are shared.

+
SharpLsp · C# and F# language server for VS Code ↗Unedited, in real time: Profile CPU on a running process, then the results open. Watch the walkthrough ↗
+

What it measures

+
    +
  • CPU: sampling sessions, hotspots and flame graphs, exported as speedscope or V8 profiles.
  • +
  • Memory: heap snapshots, retention paths and leak scoring.
  • +
+ + +
SharpLsp · C# and F# language server for VS Code ↗Memory tracking, sped up three times: the heap timeline climbs and leak suspects appear. Watch the walkthrough ↗
+

How it fits together

+
    +
  1. Your server mounts lsp-profiler and a runtime crate, such as lsp-profiler-dotnet for .NET.
  2. +
  3. The editor talks to it over one JSON-RPC protocol.
  4. +
  5. @nimblesite/lsp-profiler-ui renders the results in VS Code.
  6. +
+ +

Add it

+
cargo add lsp-profiler
+cargo add lsp-profiler-dotnet   # for .NET language servers
+

See it running in SharpLsp and Osprey. The full design is in the profiler spec.

+
+ + + + +
+
+ +
+ + + + + + + + + diff --git a/website/_site/feed.xml b/website/_site/feed.xml new file mode 100644 index 0000000..ab36378 --- /dev/null +++ b/website/_site/feed.xml @@ -0,0 +1,18 @@ + + + lspkit — The foundation for any language's tooling. + lspkit is the language server toolkit: write your language analysis once in Rust, serve it over LSP and as an MCP server, and profile it with a CPU and memory profiler built into VS Code. + + + 2026-10-31T00:00:00.000Z + http://localhost:8080/ + + + Build an MCP server and a language server from one Rust engine. + + http://localhost:8080/blog/one-engine/ + 2026-10-31T00:00:00.000Z + http://localhost:8080/%3C!--%20[WEB-CONTENT]%20[WEB-SEO-KEYWORDS]%20Keywords%20from%20Google%20Trends,%2012%20months%20to%202026-10:%20%22mcp%20server%22,%20%22what%20is%20mcp%20server%22,%20%22claude%20code%22,%20%22cursor%22,%20%22codex%22,%20%22language%20server%20protocol%22,%20%22rust%20language%20server%22,%20%22vscode%20extension%22,%20%22memory%20leak%22.%20--%3E%3Cp%3EYour%20VS%20Code%20extension%20talks%20to%20a%20%3Cstrong%3Elanguage%20server%3C/strong%3E%20over%20the%20%3Cstrong%3ELanguage%20Server%20Protocol%3C/strong%3E%20(LSP).%20AI%20coding%20agents%20such%20as%20%3Cstrong%3EClaude%20Code%3C/strong%3E,%20%3Cstrong%3ECursor%3C/strong%3E%20and%20%3Cstrong%3ECodex%3C/strong%3E%20talk%20to%20tools%20over%20the%20%3Cstrong%3EModel%20Context%20Protocol%3C/strong%3E%20(MCP).%20If%20you%20build%20developer%20tools,%20you%20now%20need%20both,%20and%20they%20should%20give%20the%20same%20answers.%3C/p%3E%3Ch2%20id=%22what-is-an-mcp-server%22%20tabindex=%22-1%22%3E%3Ca%20class=%22header-anchor%22%20href=%22#what-is-an-mcp-server%22%3EWhat%20is%20an%20MCP%20server?%3C/a%3E%3C/h2%3E%3Cp%3EAn%20MCP%20server%20exposes%20tools%20and%20data%20to%20an%20AI%20agent%20over%20a%20small%20JSON-RPC%20protocol.%20Where%20a%20language%20server%20answers%20&quot;what's%20the%20type%20at%20this%20cursor?&quot;%20for%20an%20editor,%20an%20MCP%20server%20answers%20&quot;find%20every%20caller%20of%20this%20function&quot;%20for%20an%20agent.%20Both%20need%20the%20same%20thing%20underneath:%20a%20live,%20accurate%20model%20of%20the%20code.%3C/p%3E%3Ch2%20id=%22the-trap-two-pipelines%22%20tabindex=%22-1%22%3E%3Ca%20class=%22header-anchor%22%20href=%22#the-trap-two-pipelines%22%3EThe%20trap:%20two%20pipelines%3C/a%3E%3C/h2%3E%3Cp%3EThe%20obvious%20build%20is%20a%20language%20server%20with%20its%20own%20caches,%20then%20an%20MCP%20server%20bolted%20on%20with%20another%20set.%20Now%20two%20pipelines%20parse%20the%20same%20files,%20watch%20the%20same%20folders%20and%20drift%20apart.%20The%20agent%20sees%20stale%20results%20the%20editor%20has%20already%20fixed.%3C/p%3E%3Ch2%20id=%22one-engine-two-surfaces%22%20tabindex=%22-1%22%3E%3Ca%20class=%22header-anchor%22%20href=%22#one-engine-two-surfaces%22%3EOne%20engine,%20two%20surfaces%3C/a%3E%3C/h2%3E%3Cp%3Elspkit%20puts%20analysis%20behind%20one%20Rust%20trait,%20%3Ccode%3EEngineApi%3C/code%3E.%20The%20%3Ccode%3Elspkit-server%3C/code%3E%20crate%20serves%20it%20over%20LSP%20to%20VS%20Code;%20its%20%3Ccode%3Emcp%3C/code%3E%20feature%20serves%20the%20same%20engine%20over%20MCP%20to%20Claude%20Code,%20Cursor%20or%20Codex.%3C/p%3E%3Cpre%20class=%22language-sh%22%3E%3Ccode%20class=%22language-sh%22%3E%3Cspan%20class=%22token%20function%22%3Ecargo%3C/span%3E%20%3Cspan%20class=%22token%20function%22%3Eadd%3C/span%3E%20lspkit%20lspkit-server%20%3Cspan%20class=%22token%20parameter%20variable%22%3E--features%3C/span%3E%20lspkit-server/mcp%3C/code%3E%3C/pre%3E%3Cp%3EEvery%20report%20carries%20a%20%3Cstrong%3Egeneration%3C/strong%3E%20number,%20so%20the%20editor%20and%20the%20agent%20can%20both%20tell%20which%20state%20of%20the%20workspace%20produced%20an%20answer.%20Cancellation%20and%20progress%20live%20at%20the%20same%20boundary,%20so%20a%20slow%20query%20from%20the%20agent%20never%20blocks%20the%20editor.%3C/p%3E%3Ch2%20id=%22find-the-memory-leak-before-your-users-do%22%20tabindex=%22-1%22%3E%3Ca%20class=%22header-anchor%22%20href=%22#find-the-memory-leak-before-your-users-do%22%3EFind%20the%20memory%20leak%20before%20your%20users%20do%3C/a%3E%3C/h2%3E%3Cp%3ELong-running%20language%20servers%20leak.%20lspkit%20ships%20%3Ccode%3Elsp-profiler%3C/code%3E,%20a%20CPU%20and%20memory%20profiler%20that%20runs%20inside%20VS%20Code:%20hot%20lines%20painted%20in%20your%20source,%20a%20flame%20graph%20beside%20it,%20and%20a%20live%20heap%20timeline%20that%20ranks%20%3Cstrong%3Ememory%20leak%3C/strong%3E%20suspects.%20%3Ca%20href=%22https://sharplsp.dev/docs/profiler/%22%3ESharpLsp%3C/a%3E%20uses%20it%20for%20C#%20and%20F#;%20%3Ca%20href=%22https://www.ospreylang.dev/docs/tutorials/profiler/%22%3EOsprey%3C/a%3E%20uses%20it%20for%20a%20native%20functional%20language.%20%3Ca%20href=%22/docs/examples/%22%3EWatch%20both%20on%20the%20examples%20page%3C/a%3E.%3C/p%3E%3Cfigure%20class=%22example-video%22%3E%3Cvideo%20controls%20preload=%22metadata%22%20playsinline%20width=%221920%22%20height=%221200%22%20poster=%22https://sharplsp.dev/assets/videos/profiler-fix-memory-leak-poster.webp%22%20aria-label=%22Fixing%20a%20C#%20memory%20leak%20in%20VS%20Code%20with%20the%20SharpLsp%20memory%20profiler,%20from%20leak%20suspects%20to%20the%20retaining%20path%22%3E%3Csource%20src=%22https://sharplsp.dev/assets/videos/profiler-fix-memory-leak.webm%22%20type=%22video/webm%22%3E%3Csource%20src=%22https://sharplsp.dev/assets/videos/profiler-fix-memory-leak.mp4%22%20type=%22video/mp4%22%3E%3Ctrack%20kind=%22captions%22%20src=%22https://sharplsp.dev/assets/videos/profiler-fix-memory-leak.en.vtt%22%20srclang=%22en%22%20label=%22English%22%3E%3C/video%3E%3Cfigcaption%3E%3Ca%20class=%22example-project%22%20href=%22https://sharplsp.dev%22%3E%3Cstrong%3ESharpLsp%3C/strong%3E%20%C2%B7%20C#%20and%20F#%20language%20server%20for%20VS%20Code%20%E2%86%97%3C/a%3EFinding%20and%20fixing%20a%20memory%20leak%20in%20a%20running%20C#%20app.%20%3Ca%20href=%22https://sharplsp.dev/docs/memory-leaks/%22%3EWatch%20the%20walkthrough%20%E2%86%97%3C/a%3E%3C/figcaption%3E%3C/figure%3E%3Cdiv%20class=%22example-shots%22%3E%3Cfigure%3E%3Cimg%20src=%22https://sharplsp.dev/assets/screenshots/profiler/profiler-leaks.webp%22%20width=%222400%22%20height=%221500%22%20loading=%22lazy%22%20alt=%22Suspected%20leaks%20ranked%20HIGH%20with%20growth%20in%20megabytes%20and%20object%20counts,%20and%20an%20inline%20leak%20marker%20on%20the%20CustomerSession%20declaration%22%3E%3Cfigcaption%3E%3Ca%20class=%22example-project%22%20href=%22https://sharplsp.dev%22%3E%3Cstrong%3ESharpLsp%3C/strong%3E%20%C2%B7%20C#%20and%20F#%20language%20server%20for%20VS%20Code%20%E2%86%97%3C/a%3ERanked%20leak%20suspects,%20marked%20in%20the%20source.%3C/figcaption%3E%3C/figure%3E%3Cfigure%3E%3Cimg%20src=%22/assets/images/examples/osprey-heatmap.webp%22%20width=%221276%22%20height=%22714%22%20loading=%22lazy%22%20alt=%22busy.osp%20in%20VS%20Code%20with%20inline%20heat%20bars:%20collatz%2076.4%20percent,%20longest%2073.0%20percent,%20fib%2022.5%20percent%22%3E%3Cfigcaption%3E%3Ca%20class=%22example-project%22%20href=%22https://www.ospreylang.dev%22%3E%3Cstrong%3EOsprey%3C/strong%3E%20%C2%B7%20native%20functional%20language%20with%20a%20VS%20Code%20extension%20%E2%86%97%3C/a%3EHeat%20on%20Osprey%20source.%3C/figcaption%3E%3C/figure%3E%3C/div%3E%3Ch2%20id=%22start-building%22%20tabindex=%22-1%22%3E%3Ca%20class=%22header-anchor%22%20href=%22#start-building%22%3EStart%20building%3C/a%3E%3C/h2%3E%3Cul%3E%3Cli%3E%3Ca%20href=%22/docs/getting-started/%22%3EGetting%20started%3C/a%3E:%20add%20the%20crates%20and%20implement%20%3Ccode%3EEngineApi%3C/code%3E.%3C/li%3E%3Cli%3E%3Ca%20href=%22/docs/crates/%22%3ECrates%3C/a%3E:%20what%20each%20crate%20and%20feature%20gives%20you.%3C/li%3E%3Cli%3E%3Ca%20href=%22https://github.com/Nimblesite/lspkit%22%3ESource%20on%20GitHub%20%E2%86%97%3C/a%3E%3C/li%3E%3C/ul%3E + How to give Claude Code, Cursor and Codex the same live code intelligence as your VS Code extension, with one Rust engine behind an MCP server and a language server. + + diff --git a/website/_site/index.html b/website/_site/index.html new file mode 100644 index 0000000..79d7b1c --- /dev/null +++ b/website/_site/index.html @@ -0,0 +1,259 @@ + + + + + + + + + lspkit — The foundation for any language's tooling. + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + +
+ +
+
+
+

The language server toolkit

+

The foundation for
any language's tooling.

+
+
+

Write your language analysis once, as one Rust trait. lspkit serves it to your VS Code extension over the Language Server Protocol and to Claude Code, Cursor and Codex as an MCP server. Find the hot path and the memory leak with a profiler built into VS Code.

+ +
+
+ +
+ + + + + +
+
+

See it running.

+

SharpLsp and Osprey ship the lspkit profiler in VS Code today: hot lines in the source, a flame graph beside it, and ranked memory leak suspects. See both examples.

+
SharpLsp · C# and F# language server for VS Code ↗SharpLsp profiling a real .NET app in VS Code. Watch the walkthrough ↗
+ +
+
+ An orange engine connects through precise circuit paths to two dark terminal surfaces, representing LSP and MCP. +
FIG. A — One engine, two surfaces.LSP / MCP / PROFILER
+
+
+

From the workbench.

All notes
+ + + +
+
+ +
+ + + + + + + + + diff --git a/website/_site/llms.txt b/website/_site/llms.txt new file mode 100644 index 0000000..9afa2d4 --- /dev/null +++ b/website/_site/llms.txt @@ -0,0 +1,27 @@ +# lspkit — The foundation for any language's tooling. + +> lspkit is the language server toolkit: write your language analysis once in Rust, serve it over LSP and as an MCP server, and profile it with a CPU and memory profiler built into VS Code. + +## Documentation + +- [Crates.](http://localhost:8080/docs/crates/) + +- [Built with lspkit.](http://localhost:8080/docs/examples/) + +- [Getting started.](http://localhost:8080/docs/getting-started/) + +- [A small foundation.](http://localhost:8080/docs/) + +- [Profiler.](http://localhost:8080/docs/profiler/) + + +## Blog Posts + +- [Build an MCP server and a language server from one Rust engine.](http://localhost:8080/blog/one-engine/) + + +## Navigation +- Home: http://localhost:8080/ +- Documentation: http://localhost:8080/docs/ +- Blog: http://localhost:8080/blog/ +- API Reference: http://localhost:8080/api/ diff --git a/website/_site/robots.txt b/website/_site/robots.txt new file mode 100644 index 0000000..a1655c0 --- /dev/null +++ b/website/_site/robots.txt @@ -0,0 +1,60 @@ +# lspkit — The foundation for any language's tooling. +# http://localhost:8080 + +# Allow all crawlers +User-agent: * +Allow: / +Disallow: /assets/ +Disallow: /search? + +# AI Crawlers - Welcome! +User-agent: GPTBot +Allow: / + +User-agent: ChatGPT-User +Allow: / + +User-agent: Google-Extended +Allow: / + +User-agent: Googlebot +Allow: / + +User-agent: Bingbot +Allow: / + +User-agent: ClaudeBot +Allow: / + +User-agent: Anthropic-AI +Allow: / + +User-agent: PerplexityBot +Allow: / + +User-agent: Cohere-ai +Allow: / + +User-agent: Meta-ExternalAgent +Allow: / + +User-agent: Meta-ExternalFetcher +Allow: / + +User-agent: Bytespider +Allow: / + +User-agent: CCBot +Allow: / + +User-agent: Applebot +Allow: / + +User-agent: Amazonbot +Allow: / + +# Sitemaps +Sitemap: http://localhost:8080/sitemap.xml + +# AI Context File +# See https://llmstxt.org for the llms.txt specification diff --git a/website/_site/sitemap.xml b/website/_site/sitemap.xml new file mode 100644 index 0000000..77a5d1e --- /dev/null +++ b/website/_site/sitemap.xml @@ -0,0 +1,75 @@ + + + + http://localhost:8080/blog/one-engine/ + 2026-10-31T00:00:00.000Z + 0.7 + monthly + + + http://localhost:8080/docs/crates/ + 2026-11-16T10:00:55.240Z + 0.8 + monthly + + + http://localhost:8080/docs/examples/ + 2026-11-16T10:00:55.240Z + 0.8 + monthly + + + http://localhost:8080/docs/getting-started/ + 2026-11-16T10:00:55.240Z + 0.8 + monthly + + + http://localhost:8080/docs/ + 2026-11-16T10:00:55.240Z + 0.8 + monthly + + + http://localhost:8080/docs/profiler/ + 2026-11-16T10:00:55.241Z + 0.8 + monthly + + + http://localhost:8080/ + 2026-11-16T10:00:55.241Z + 1.0 + weekly + + + http://localhost:8080/blog/categories/field-notes/ + 2026-11-16T11:01:04.305Z + 0.7 + monthly + + + http://localhost:8080/blog/categories/ + 2026-11-16T11:01:04.305Z + 0.7 + monthly + + + http://localhost:8080/blog/ + 2026-11-16T11:01:04.305Z + 0.7 + monthly + + + http://localhost:8080/blog/tags/language-server-protocol/ + 2026-11-16T11:01:04.305Z + 0.7 + monthly + + + http://localhost:8080/blog/tags/ + 2026-11-16T11:01:04.305Z + 0.7 + monthly + + diff --git a/website/_site/techdoc/css/layout.css b/website/_site/techdoc/css/layout.css new file mode 100644 index 0000000..06ccd91 --- /dev/null +++ b/website/_site/techdoc/css/layout.css @@ -0,0 +1,243 @@ +/** + * ╔══════════════════════════════════════════════════════════════════════════╗ + * ║ ⚠️ DO NOT MODIFY THIS FILE DIRECTLY! ⚠️ ║ + * ╠══════════════════════════════════════════════════════════════════════════╣ + * ║ This file is part of the techdoc npm package. ║ + * ║ Changes here will be OVERWRITTEN when techdoc is updated. ║ + * ║ ║ + * ║ HOW TO CUSTOMIZE LAYOUT: ║ + * ║ 1. Override CSS vars: --max-width, --sidebar-width, --content-width ║ + * ║ 2. Add rules with higher specificity in YOUR styles.css ║ + * ║ 3. Use !important sparingly if truly needed ║ + * ║ ║ + * ║ Example in your styles.css: ║ + * ║ :root { --max-width: 1400px; --sidebar-width: 280px; } ║ + * ╚══════════════════════════════════════════════════════════════════════════╝ + * + * techdoc layout.css + * Structural layout only - NO colors, NO decorative styles + * Sites must provide all visual styling + */ + +/* Site structure */ +body { + margin: 0; + min-height: 100vh; + display: flex; + flex-direction: column; +} + +main { + flex: 1; +} + +/* Theme-owned resets — scoped so reset.css can stay global-free and never + clobber the consuming site's content lists, buttons, or spacing. */ +.nav-links, +.sidebar ul, +.footer-section ul { + list-style: none; + margin: 0; + padding: 0; +} + +.theme-toggle, +.language-btn, +.mobile-menu-toggle { + font: inherit; +} + +/* Header */ +.site-header { + position: sticky; + top: 0; + z-index: 100; +} + +.nav { + display: flex; + align-items: center; + justify-content: space-between; + gap: var(--space-4, 1rem); + max-width: var(--max-width, 1200px); + margin-inline: auto; + padding: var(--space-4, 1rem); +} + +.nav-links { + display: flex; + gap: var(--space-4, 1rem); +} + +.nav-actions { + display: flex; + align-items: center; + gap: var(--space-2, 0.5rem); +} + +/* Docs layout - two column */ +.docs-layout { + display: grid; + grid-template-columns: var(--sidebar-width, 250px) 1fr; + max-width: var(--max-width, 1200px); + margin-inline: auto; + gap: var(--space-8, 2rem); + padding: var(--space-4, 1rem); +} + +.sidebar { + align-self: start; +} + +.sidebar-section { + margin-bottom: var(--space-6, 1.5rem); +} + +.docs-content { + max-width: var(--content-width, 65ch); +} + +/* Keep content media inside the theme's content columns. Scoped to theme + containers so the host site's images elsewhere are never affected. */ +.docs-content img, +.blog-post img { + max-width: 100%; +} + +/* Blog layout */ +.blog-post { + max-width: var(--content-width, 65ch); + margin-inline: auto; + padding: var(--space-4, 1rem); +} + +.blog-container { + width: 100%; +} + +.blog-post-header { + margin-bottom: var(--space-8, 2rem); +} + +.blog-post-tags { + display: flex; + flex-wrap: wrap; + gap: var(--space-2, 0.5rem); + margin-top: var(--space-4, 1rem); +} + +.blog-post-footer { + margin-top: var(--space-8, 2rem); + padding-top: var(--space-4, 1rem); + text-align: center; +} + +/* Docs navigation footer */ +.docs-nav-footer { + display: flex; + justify-content: space-between; + margin-top: var(--space-8, 2rem); + padding-top: var(--space-4, 1rem); +} + +/* Footer */ +.site-footer { + margin-top: auto; +} + +.footer-content { + max-width: var(--max-width, 1200px); + margin-inline: auto; + padding: var(--space-8, 2rem) var(--space-4, 1rem); +} + +.footer-grid { + display: grid; + grid-template-columns: repeat(auto-fit, minmax(150px, 1fr)); + gap: var(--space-8, 2rem); +} + +.footer-bottom { + margin-top: var(--space-8, 2rem); + padding-top: var(--space-4, 1rem); + text-align: center; +} + +/* Language dropdown */ +.language-switcher { + position: relative; +} + +.language-dropdown { + display: none; + position: absolute; + top: 100%; + right: 0; + min-width: 120px; + z-index: 200; + list-style: none; + margin: 0; + padding: 0; +} + +.language-btn[aria-expanded="true"] + .language-dropdown { + display: block; +} + +/* Mobile menu toggle */ +.mobile-menu-toggle { + display: none; + flex-direction: column; + gap: 4px; + background: none; + border: none; + cursor: pointer; + padding: var(--space-2, 0.5rem); +} + +.mobile-menu-toggle span { + display: block; + width: 24px; + height: 2px; + background: currentColor; +} + +/* Mobile responsive */ +@media (max-width: 768px) { + .nav-links { + display: none; + position: absolute; + top: 100%; + left: 0; + right: 0; + flex-direction: column; + padding: var(--space-4, 1rem); + } + + .nav-links.open { + display: flex; + } + + .mobile-menu-toggle { + display: flex; + } + + .docs-layout { + grid-template-columns: 1fr; + } + + .sidebar { + position: fixed; + top: var(--header-height, 60px); + left: 0; + width: var(--sidebar-width, 250px); + height: calc(100vh - var(--header-height, 60px)); + transform: translateX(-100%); + transition: transform 0.3s ease; + z-index: 50; + } + + .sidebar.open { + transform: translateX(0); + } +} diff --git a/website/_site/techdoc/css/reset.css b/website/_site/techdoc/css/reset.css new file mode 100644 index 0000000..bce8768 --- /dev/null +++ b/website/_site/techdoc/css/reset.css @@ -0,0 +1,41 @@ +/** + * ╔══════════════════════════════════════════════════════════════════════════╗ + * ║ ⚠️ DO NOT MODIFY THIS FILE DIRECTLY! ⚠️ ║ + * ╠══════════════════════════════════════════════════════════════════════════╣ + * ║ This file is part of the techdoc npm package. ║ + * ║ Changes here will be OVERWRITTEN when techdoc is updated. ║ + * ║ ║ + * ║ HOW TO CUSTOMIZE CSS: ║ + * ║ 1. ALL visual styling goes in YOUR site's styles.css ║ + * ║ 2. Override any techdoc CSS with higher specificity ║ + * ║ 3. Use CSS custom properties (--var-name) for theming ║ + * ║ ║ + * ║ Example in your styles.css: ║ + * ║ .nav { background: var(--color-bg); } // override nav bg ║ + * ╚══════════════════════════════════════════════════════════════════════════╝ + * + * techdoc reset.css + * Deliberately minimal so the theme NEVER clobbers the consuming site's CSS. + * + * This file intentionally contains NO global content overrides: + * - NO `* { margin: 0; padding: 0 }` (would wipe the host site's spacing) + * - NO `ul, ol { list-style: none }` (would strip bullets from host content) + * - NO line-height / font-smoothing / overflow-wrap (typography is the host's) + * - NO img / table / form element overrides (content is the host's) + * + * The theme resets ONLY the structural elements it actually owns (nav, sidebar, + * footer lists, theme buttons). Those scoped resets live in layout.css. The + * single global rule below is box-sizing, which every consuming site expects, + * is required by techdoc's grid/flex layout, and is overridable per-element. + */ + +*, +*::before, +*::after { + box-sizing: border-box; +} + +/* Prevent iOS text-size inflation. Not a visual/typographic choice. */ +html { + -webkit-text-size-adjust: 100%; +} diff --git a/website/_site/techdoc/css/utilities.css b/website/_site/techdoc/css/utilities.css new file mode 100644 index 0000000..490a6a3 --- /dev/null +++ b/website/_site/techdoc/css/utilities.css @@ -0,0 +1,58 @@ +/** + * ╔══════════════════════════════════════════════════════════════════════════╗ + * ║ ⚠️ DO NOT MODIFY THIS FILE DIRECTLY! ⚠️ ║ + * ╠══════════════════════════════════════════════════════════════════════════╣ + * ║ This file is part of the techdoc npm package. ║ + * ║ Changes here will be OVERWRITTEN when techdoc is updated. ║ + * ║ ║ + * ║ TO ADD UTILITIES: Create them in YOUR site's styles.css ║ + * ╚══════════════════════════════════════════════════════════════════════════╝ + * + * techdoc utilities.css + * Accessibility helpers - NO colors, NO decorative styles + */ + +/* Screen reader only */ +.sr-only { + position: absolute; + width: 1px; + height: 1px; + padding: 0; + margin: -1px; + overflow: hidden; + clip: rect(0, 0, 0, 0); + white-space: nowrap; + border: 0; +} + +/* Skip link - visible on focus */ +.skip-link { + position: absolute; + top: -100%; + left: 50%; + transform: translateX(-50%); + z-index: 1000; + padding: var(--space-2, 0.5rem) var(--space-4, 1rem); +} + +.skip-link:focus { + top: var(--space-2, 0.5rem); +} + +/* Focus visible */ +:focus-visible { + outline: 2px solid currentColor; + outline-offset: 2px; +} + +/* Reduced motion */ +@media (prefers-reduced-motion: reduce) { + *, + *::before, + *::after { + animation-duration: 0.01ms !important; + animation-iteration-count: 1 !important; + transition-duration: 0.01ms !important; + scroll-behavior: auto !important; + } +} diff --git a/website/_site/techdoc/js/language-switcher.js b/website/_site/techdoc/js/language-switcher.js new file mode 100644 index 0000000..3e5fdee --- /dev/null +++ b/website/_site/techdoc/js/language-switcher.js @@ -0,0 +1,31 @@ +/** + * ╔══════════════════════════════════════════════════════════════════════════╗ + * ║ ⚠️ DO NOT MODIFY THIS FILE DIRECTLY! ⚠️ ║ + * ╠══════════════════════════════════════════════════════════════════════════╣ + * ║ This file is part of the techdoc npm package. ║ + * ║ Changes here will be OVERWRITTEN when techdoc is updated. ║ + * ║ ║ + * ║ TO OVERRIDE: Create src/assets/js/custom.js in YOUR site and ║ + * ║ redefine initLanguageSwitcher() or add your own event listeners. ║ + * ╚══════════════════════════════════════════════════════════════════════════╝ + * + * Language switcher - handles dropdown toggle and outside click + */ +export function initLanguageSwitcher() { + const btn = document.querySelector(".language-btn"); + const dropdown = document.querySelector(".language-dropdown"); + if (!btn || !dropdown) { + return; + } + btn.addEventListener("click", () => { + const expanded = btn.getAttribute("aria-expanded") === "true"; + btn.setAttribute("aria-expanded", !expanded); + }); + + document.addEventListener("click", (e) => { + if (!e.target.closest(".language-switcher")) { + btn.setAttribute("aria-expanded", "false"); + } + }); +} +// SYMLINK TEST - This proves changes flow through! diff --git a/website/_site/techdoc/js/main.js b/website/_site/techdoc/js/main.js new file mode 100644 index 0000000..6b79e24 --- /dev/null +++ b/website/_site/techdoc/js/main.js @@ -0,0 +1,25 @@ +/** + * ╔══════════════════════════════════════════════════════════════════════════╗ + * ║ ⚠️ DO NOT MODIFY THIS FILE DIRECTLY! ⚠️ ║ + * ╠══════════════════════════════════════════════════════════════════════════╣ + * ║ This file is part of the techdoc npm package. ║ + * ║ Changes here will be OVERWRITTEN when techdoc is updated. ║ + * ║ ║ + * ║ HOW TO CUSTOMIZE: ║ + * ║ 1. Create your own JS file in YOUR site: src/assets/js/custom.js ║ + * ║ 2. Add in your layout ║ + * ║ (use {% block scripts %} in base.njk) ║ + * ║ 3. Your custom.js can override any behavior by running AFTER this ║ + * ║ ║ + * ║ NEED TO MODIFY techdoc ITSELF? ║ + * ║ Edit the source in: techdoc/assets/js/ ║ + * ║ Then run: ./update-techdoc.sh to sync changes ║ + * ╚══════════════════════════════════════════════════════════════════════════╝ + */ +import { initThemeToggle } from "./theme-toggle.js"; +import { initMobileMenu } from "./mobile-menu.js"; +import { initLanguageSwitcher } from "./language-switcher.js"; + +initThemeToggle(); +initMobileMenu(); +initLanguageSwitcher(); diff --git a/website/_site/techdoc/js/mobile-menu.js b/website/_site/techdoc/js/mobile-menu.js new file mode 100644 index 0000000..572af5c --- /dev/null +++ b/website/_site/techdoc/js/mobile-menu.js @@ -0,0 +1,29 @@ +/** + * ╔══════════════════════════════════════════════════════════════════════════╗ + * ║ ⚠️ DO NOT MODIFY THIS FILE DIRECTLY! ⚠️ ║ + * ╠══════════════════════════════════════════════════════════════════════════╣ + * ║ This file is part of the techdoc npm package. ║ + * ║ Changes here will be OVERWRITTEN when techdoc is updated. ║ + * ║ ║ + * ║ TO OVERRIDE: Create src/assets/js/custom.js in YOUR site and ║ + * ║ redefine initMobileMenu() or add your own event listeners. ║ + * ╚══════════════════════════════════════════════════════════════════════════╝ + * + * Mobile menu toggle - handles sidebar visibility on mobile + */ +export function initMobileMenu() { + const toggle = document.getElementById("mobile-menu-toggle"); + const sidebar = document.querySelector(".sidebar, .docs-sidebar"); + const navLinks = document.querySelector(".nav-links"); + if (!toggle) { + return; + } + + toggle.addEventListener("click", () => { + const expanded = toggle.getAttribute("aria-expanded") === "true"; + toggle.setAttribute("aria-expanded", String(!expanded)); + sidebar?.classList.toggle("open"); + navLinks?.classList.toggle("open"); + document.body.classList.toggle("menu-open"); + }); +} diff --git a/website/_site/techdoc/js/theme-toggle.js b/website/_site/techdoc/js/theme-toggle.js new file mode 100644 index 0000000..7f3b1ce --- /dev/null +++ b/website/_site/techdoc/js/theme-toggle.js @@ -0,0 +1,26 @@ +/** + * ╔══════════════════════════════════════════════════════════════════════════╗ + * ║ ⚠️ DO NOT MODIFY THIS FILE DIRECTLY! ⚠️ ║ + * ╠══════════════════════════════════════════════════════════════════════════╣ + * ║ This file is part of the techdoc npm package. ║ + * ║ Changes here will be OVERWRITTEN when techdoc is updated. ║ + * ║ ║ + * ║ TO OVERRIDE: Create src/assets/js/custom.js in YOUR site and ║ + * ║ redefine initThemeToggle() or add your own event listeners. ║ + * ╚══════════════════════════════════════════════════════════════════════════╝ + * + * Theme toggle - switches between light/dark modes + */ +export function initThemeToggle() { + const toggle = document.getElementById("theme-toggle"); + if (!toggle) { + return; + } + + toggle.addEventListener("click", () => { + const current = document.documentElement.getAttribute("data-theme"); + const next = current === "dark" ? "light" : "dark"; + document.documentElement.setAttribute("data-theme", next); + localStorage.setItem("theme", next); + }); +} diff --git a/website/src/blog/one-engine.md b/website/src/blog/one-engine.md index aba8f01..5589b80 100644 --- a/website/src/blog/one-engine.md +++ b/website/src/blog/one-engine.md @@ -21,10 +21,10 @@ The obvious build is a language server with its own caches, then an MCP server b ## One engine, two surfaces -lspkit puts analysis behind one Rust trait, `EngineApi`. The `lspkit-server` crate serves it over LSP to VS Code; its `mcp` feature serves the same engine over MCP to Claude Code, Cursor or Codex. +lspkit puts analysis behind one Rust trait, `EngineApi`. The `lspkit-server` crate serves it over LSP to VS Code; its `mcp` module serves the same engine over MCP to Claude Code, Cursor or Codex. ```sh -cargo add lspkit lspkit-server --features lspkit-server/mcp +cargo add lspkit lspkit-server ``` Every report carries a **generation** number, so the editor and the agent can both tell which state of the workspace produced an answer. Cancellation and progress live at the same boundary, so a slow query from the agent never blocks the editor. diff --git a/website/src/docs/getting-started.md b/website/src/docs/getting-started.md index 8b77cb5..14130e5 100644 --- a/website/src/docs/getting-started.md +++ b/website/src/docs/getting-started.md @@ -21,7 +21,7 @@ Turn on the features you need: ```sh cargo add lspkit --features treesitter -cargo add lspkit-server --features mcp +cargo add lspkit-server ``` ## Implement the engine diff --git a/website/tests/prose_test.py b/website/tests/prose_test.py index cf1e0d0..4bb0ecc 100644 --- a/website/tests/prose_test.py +++ b/website/tests/prose_test.py @@ -17,7 +17,7 @@ class Document(HTMLParser): def __init__(self, path): super().__init__() self.elements = [] - self.feed(path.read_text()) + self.feed(path.read_text(encoding="utf-8")) def handle_starttag(self, tag, attrs): self.elements.append((tag, dict(attrs))) @@ -30,7 +30,11 @@ class ProsePages(unittest.TestCase): @classmethod def setUpClass(cls): shutil.rmtree(OUTPUT, ignore_errors=True) - subprocess.run(["npm", "run", "build"], cwd=SITE, check=True) + # npm is npm.cmd on Windows, which CreateProcess only finds by its full name. + npm = shutil.which("npm") + if npm is None: + raise RuntimeError("npm is not on PATH") + subprocess.run([npm, "run", "build"], cwd=SITE, check=True) def check_page(self, path): page = Document(path) @@ -109,7 +113,7 @@ def test_reading_pages_have_header_images_and_explicit_prose_classes(self): self.assertEqual(image["src"], "/assets/images/engine.svg") self.assertEqual((image["width"], image["height"]), ("1440", "600")) css = OUTPUT / "assets/css/styles.css" - self.assertLessEqual(len(css.read_text().splitlines()), 2000) + self.assertLessEqual(len(css.read_text(encoding="utf-8").splitlines()), 2000) def test_journal_has_illustrated_posts_and_docs_journal_and_repo_navigation(self): for path in OUTPUT.rglob("*.html"):