@nx-lang/language-core

中文文档
0.10.0发布于 2026/11/02MITTypeScriptES Module构建来源已验证

Transport-free NX language query answering: host context, the snapshot cache, and an in-process NxLanguageService.

原始使用文档
文档目录5 节
  1. An in-process service
  2. Snapshots and the cache
  3. Host context
  4. Migrating from prelude
  5. Building and testing

@nx-lang/language-core

The half of NX language serving that has nothing to do with a transport: the host context, the content-keyed snapshot cache, the query dispatcher, and an in-process implementation of @nx-lang/language-protocol's NxLanguageService.

Two things sit on top of it, and neither can disagree with the other about a line number, because there is only one copy of the dispatcher and the queried text is analyzed exactly as it was sent:

The package imports nothing from node:. It runs in a browser, a Web Worker and Node alike.

An in-process service

import { createSnapshotLanguageService } from "@nx-lang/language-core";

const service = createSnapshotLanguageService({
  createSnapshot: (documents, { implicitImports }) =>
    host.createLanguageSnapshot(documents, { implicitImports }),
  context: {
    documents: [{ uri: "nx://host/catalog.nx", identity: "catalog.nx", source: catalogSource }],
    implicitImports: ["catalog.nx"]
  },
  cacheSize: 8
});

const hover = await service.hover({ documents, uri, position }, signal);

The result implements NxLanguageService, so @nx-lang/monaco and any other client of that interface takes it without knowing where the analysis happens.

sourceTree answers every piece of a document as a typed node, with the declarations the nodes refer to; the protocol's README describes the answer. It is unstable: its shape may change in any release until a later one commits to it. sourceEdit answers the text edits that make one change a reader asks for at a node of that tree, or why it cannot be made; it is unstable too. dispose() releases every cached analysis; the service answers again, analyzing afresh, afterwards.

Answers are produced synchronously on the calling thread, so a cancellation can only be observed before the work starts: an already-aborted signal rejects with an AbortError and asks nothing of the snapshot. Whoever runs the service off the caller's thread — the playground runs it in a worker — is what makes a cancel preemptive.

Snapshots and the cache

A SnapshotLike is the small surface the dispatcher needs: hover, completions, diagnostics, documentSymbols, sourceTree, sourceEdit, dispose. Both SDKs' snapshots satisfy it, and a test can hand over a fake.

documentSetKey keys an analysis by the document set's own content — URI, identity and source, each preceded by its length so no two sets can join into the same string. A document's version is left out on purpose: it is the editor's counter, not content, and the same text under a new number has the same analysis. The dispatcher puts the current request's versions back onto the answer, so a client that discards answers by version still sees its own.

SnapshotCache holds the most recently used analyses and disposes what it evicts. A hover storm sends the same text many times a second; keeping the last few sets is what makes answering cheap on one thread.

Host context

A host with declarations of its own — a catalog of external components, say — passes them as context: documents that join every query's document set without the client sending them, and implicitImports, the identities every queried document imports as if it began with a wildcard import of each.

Nothing is prepended and no position is ever rewritten:

  • a queried position is what the client sent, and the snapshot is asked exactly that;
  • a hover range, a document symbol's ranges and a diagnostic's range come back in the queried document's own lines and columns;
  • a diagnostic located in a context document is reported under that document's URI, with its own coordinates, so a fault in the host's declarations is never shown on a line the author wrote;
  • a related location needs no adjustment, wherever it points.

A query whose documents include one with a context document's URI or identity is refused: the client cannot replace the host's context. contextCollision(documents, context) is the check, and the HTTP handler answers 400 invalid-request naming the identity.

Migrating from prelude

The prelude option prepended the host's text to the queried document and shifted every position. It is removed, and a call that still passes it fails at construction with a message naming context. The replacement is two lines:

// Before
createSnapshotLanguageService({ createSnapshot, prelude: { source: catalogSource } });

// After
createSnapshotLanguageService({
  createSnapshot,
  context: {
    documents: [{ uri: "nx://host/catalog.nx", identity: "catalog.nx", source: catalogSource }],
    implicitImports: ["catalog.nx"]
  }
});

preludeOffsets, withPrelude, shiftPositionIn, shiftRangeOut and PRELUDE_ORIGIN are removed with it; nothing shifts any more, so there is nothing for them to do.

Building and testing

pnpm --filter @nx-lang/language-core build
pnpm --filter @nx-lang/language-core test

版本与发布记录

@nx-lang/language-core 共发布 10 个版本,首次发布于 2026/09/15;过去 12 个月发布了 10 个版本。最新版本附带 npm provenance 构建来源证明,可追溯到公开的 CI 构建记录。

版本发布日期距今
0.10.0latest2026/11/021 天前
0.9.02026/10/268 天前
0.8.02026/10/1816 天前
0.7.02026/10/1024 天前
0.6.02026/10/0628 天前
0.5.02026/09/291 个月前

关于 @nx-lang/language-core 的常见问题

@nx-lang/language-core 是什么?

无传输的NX语言查询回答:主机上下文、快照缓存和进程内的NxLanguageService。

如何安装 @nx-lang/language-core?

在项目目录运行 npm install @nx-lang/language-core,或使用 pnpm add / yarn add / bun add。

@nx-lang/language-core 支持 TypeScript 吗?

支持。包内自带 TypeScript 类型声明(package.json 声明了 types 入口),安装后即可获得类型提示,无需再安装 @types 包。

@nx-lang/language-core 支持 ESM 还是 CommonJS?

以 ES Module 形式发布,推荐使用 import 引入。在 CommonJS 项目中,Node.js 20.19+ / 22.12+ 可以直接 require() 不含顶层 await 的 ES Module,更早的版本需要改用动态 import()。

@nx-lang/language-core 需要什么版本的 Node.js?

package.json 的 engines 字段要求 Node.js >=22.0.0。版本不符时 npm 默认只给出警告,开启 engine-strict 后会拒绝安装。

@nx-lang/language-core 可以商用吗?

@nx-lang/language-core 使用 MIT 许可。宽松许可:允许商业使用、修改和再分发,通常只需在分发时保留原版权与许可声明。以上为概括,不构成法律意见,请以许可原文为准。

@nx-lang/language-core 还在维护吗?

最新版本 0.10.0 发布于 2026/11/02(1 天前);至今共发布 10 个版本,其中过去 12 个月发布了 10 个;首次发布于 2026/09/15。发布频率只是参考信号:功能稳定的工具可能很少需要更新,建议结合源码仓库的 issue 和提交记录判断。