Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
12 changes: 8 additions & 4 deletions rust/crates/core/src/tests/declarative_plugins.rs
Original file line number Diff line number Diff line change
Expand Up @@ -910,9 +910,10 @@ fn plugin_backend_is_planned_without_a_workspace_and_names_its_host_services() {

/// A `backend` process starts without a page, its plan names the `backend`
/// version so the host may start it on `onWorkspaceOpen`, and
/// `workspace.metadata` grants the `workspace` host service. A host that does
/// not offer `backend` refuses the manifest instead of starting a process it
/// would misuse.
/// `workspace.metadata` grants the `workspace` host service and the
/// `getHostPaths` method grant; a host that does not serve that method drops
/// the grant when it starts the backend. A host that does not offer `backend`
/// refuses the manifest instead of starting a process it would misuse.
#[test]
fn workspace_backend_is_planned_with_its_version_and_workspace_service() {
let outcome = validate(fixture("backend-workspace.plugin.json"));
Expand All @@ -930,7 +931,10 @@ fn workspace_backend_is_planned_with_its_version_and_workspace_service() {
assert_eq!(launch["status"], "ok", "{launch}");
assert_eq!(
launch["plan"]["backend"],
json!({ "hostServices": ["workspace"], "version": 0 })
json!({
"hostServices": ["lithe/host/workspace/getHostPaths", "workspace"],
"version": 0
})
);

let hosted = |host: Value| {
Expand Down
38 changes: 25 additions & 13 deletions rust/crates/lsp-engine/src/function_relay_tests.rs
Original file line number Diff line number Diff line change
Expand Up @@ -490,6 +490,31 @@ fn a_declared_exported_call_reaches_the_callee_and_returns_its_value() {
});
}

/// Both backends run on this machine for a remote workspace too, so a call
/// naming a remote context reaches the callee like a local one (API-35).
#[test]
fn a_call_for_a_remote_workspace_reaches_the_callee() {
let fixture = Fixture::new();
let python = fixture.start(Launch::new(PYTHON));
let go = fixture.start(Launch::new(GO).serving(&[FORMAT]));
fixture.configure(snapshot(
vec![
plugin(PYTHON, &[], &[(FORMAT, 1)]),
plugin(GO, &[(FORMAT, 1)], &[]),
],
"remote",
));

python.ask("call-1", INVOKE, call_params(GO, FORMAT));
let (wire_id, received) = go.await_invoke();
assert_eq!(received["context"], workspace_context());
go.answer(&wire_id, json!({ "edits": 1 }));
assert_eq!(
python.reply("call-1")["result"],
json!({ "value": { "edits": 1 } })
);
}

#[test]
fn a_caller_field_in_the_host_request_is_refused() {
let fixture = Fixture::new();
Expand Down Expand Up @@ -728,19 +753,6 @@ fn refusals() -> Vec<Refusal> {
code: "plugin.host.contextExpired",
rpc_code: -32011,
},
Refusal {
name: "the workspace is remote",
snapshot: snapshot(
vec![
plugin(PYTHON, &[], &[(FORMAT, 1)]),
plugin(GO, &[(FORMAT, 1)], &[]),
],
"remote",
),
params: call_params(GO, FORMAT),
code: "plugin.host.authorityUnsupported",
rpc_code: -32012,
},
Refusal {
name: "the params are above the relay limit",
snapshot: standard_snapshot(),
Expand Down
39 changes: 15 additions & 24 deletions rust/crates/plugin-function-relay/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -84,7 +84,11 @@ pub struct RelayContext {
#[serde(rename = "contextID")]
pub(crate) context_id: String,
pub(crate) generation: u64,
pub(crate) authority: ContextAuthority,
/// Checked as part of the snapshot, but the relay does not depend on it:
/// both backends run on this machine for a remote workspace too, and the
/// callee's host requests are routed by the context's authority (API-35).
#[serde(rename = "authority")]
pub(crate) _authority: ContextAuthority,
}

/// A function an enabled plugin exports.
Expand Down Expand Up @@ -639,12 +643,6 @@ impl FunctionRelay {
codes::HOST_CONTEXT_EXPIRED,
"The workspace context is no longer current.",
)),
Some(context) if context.authority == ContextAuthority::Remote => {
Err(RelayFailure::new(
codes::HOST_AUTHORITY_UNSUPPORTED,
"Plugin functions do not run for remote workspaces.",
))
}
Some(_) => Ok(()),
}
}
Expand Down Expand Up @@ -924,7 +922,7 @@ mod tests {
RelayContext {
context_id: "workspace-1".to_string(),
generation: 3,
authority: ContextAuthority::Local,
_authority: ContextAuthority::Local,
}
}

Expand Down Expand Up @@ -1228,25 +1226,18 @@ mod tests {
assert_eq!(refusal(&mut relay, unknown), codes::HOST_CONTEXT_EXPIRED);
}

/// Both backends run on this machine for a remote workspace too, so a call
/// naming a remote context is dispatched like a local one (API-35).
#[test]
fn a_remote_workspace_is_refused() {
let mut relay = relay();
fn a_remote_workspace_is_relayed() {
let mut relay = FunctionRelay::default();
relay
.configure(RelaySnapshot {
plugins: vec![
plugin(PYTHON, &[], &[(FUNCTION, 1)]),
plugin(GO, &[(FUNCTION, 1)], &[]),
],
contexts: vec![RelayContext {
authority: ContextAuthority::Remote,
..local_context()
}],
})
.configure(snapshot_with_contexts(vec![RelayContext {
_authority: ContextAuthority::Remote,
..local_context()
}]))
.unwrap();
assert_eq!(
refusal(&mut relay, request("py", PYTHON, GO, 1)),
codes::HOST_AUTHORITY_UNSUPPORTED
);
admitted_call(&mut relay, request("py", PYTHON, GO, 1), Instant::now());
}

#[test]
Expand Down
5 changes: 5 additions & 0 deletions rust/crates/plugin-rpc/src/failures.rs
Original file line number Diff line number Diff line change
Expand Up @@ -42,6 +42,9 @@ pub fn host_rpc_code(code: &str) -> i64 {
// must tell the provider's error from Core's by the number alone.
codes::FAILED => -32070,
codes::INVALID_RESULT => -32071,
// -32072 to -32077 are the toolchain provider protocol's.
codes::HOST_REMOTE_DISCONNECTED => -32078,
codes::HOST_PLATFORM_UNAVAILABLE => -32079,
_ => -32603,
}
}
Expand Down Expand Up @@ -86,6 +89,8 @@ pub fn host_failure(code: Option<&str>) -> (&'static str, i64) {
Some(codes::HOST_METHOD_NOT_DECLARED) => codes::HOST_METHOD_NOT_DECLARED,
Some(codes::HOST_TOOL_UNRESOLVED) => codes::HOST_TOOL_UNRESOLVED,
Some(codes::HOST_FOREIGN_CONTRIBUTION) => codes::HOST_FOREIGN_CONTRIBUTION,
Some(codes::HOST_REMOTE_DISCONNECTED) => codes::HOST_REMOTE_DISCONNECTED,
Some(codes::HOST_PLATFORM_UNAVAILABLE) => codes::HOST_PLATFORM_UNAVAILABLE,
// A backend exit is reported to pages, never to the backend itself.
Some(codes::BACKEND_EXITED) => codes::HOST_FAILED,
_ => codes::HOST_FAILED,
Expand Down
4 changes: 4 additions & 0 deletions rust/crates/plugin-rpc/src/host_api.rs
Original file line number Diff line number Diff line change
Expand Up @@ -98,6 +98,10 @@ mod tests {
method("lithe/host/workspace/readFile")["grant"],
"lithe/host/workspace/readFile"
);
assert_eq!(
method("lithe/host/workspace/getHostPaths")["grant"],
"lithe/host/workspace/getHostPaths"
);
assert!(method("lithe/host/subscriptions/dispose")["grant"].is_null());
assert_eq!(method("lithe/host/functions/invoke")["coreServed"], true);
}
Expand Down
22 changes: 22 additions & 0 deletions rust/crates/plugin-rpc/src/host_params.rs
Original file line number Diff line number Diff line change
Expand Up @@ -35,6 +35,7 @@ pub(crate) fn check_host_params(
HostParams::Credential { with_secret: false } => &["key"],
HostParams::Subscription => &["subscriptionID"],
HostParams::Context => &["context"],
HostParams::HostPaths => &["context", "rootIDs"],
HostParams::FunctionProviders => &["functionID"],
HostParams::FunctionInvoke => &[
"pluginID",
Expand Down Expand Up @@ -117,6 +118,27 @@ pub(crate) fn check_host_params(
return Err(invalid(CONTEXT_REFERENCE_MESSAGE));
}
}
HostParams::HostPaths => {
if !params.get("context").is_some_and(is_context_reference) {
return Err(invalid(CONTEXT_REFERENCE_MESSAGE));
}
if let Some(root_ids) = params.get("rootIDs") {
let root_ids = root_ids
.as_array()
.filter(|ids| !ids.is_empty() && ids.len() <= MAX_HOST_PATH_ROOTS);
let mut seen = BTreeSet::new();
if !root_ids.is_some_and(|ids| {
ids.iter().all(|id| {
id.as_str()
.is_some_and(|id| is_credential_key(id) && seen.insert(id))
})
}) {
return Err(invalid(
"`rootIDs` is 1-256 distinct root IDs of 1-128 ASCII letters, digits, '.', '_', ':' or '-'.",
));
}
}
}
HostParams::FunctionProviders => {
if !params
.get("functionID")
Expand Down
9 changes: 9 additions & 0 deletions rust/crates/plugin-rpc/src/host_requests.rs
Original file line number Diff line number Diff line change
Expand Up @@ -40,6 +40,9 @@ pub(crate) enum HostParams {
Subscription,
/// Exactly `{ context }`, a well-formed context reference.
Context,
/// `{ context, rootIDs? }`: a well-formed context reference and at most
/// 256 distinct root IDs (plan API-40).
HostPaths,
/// Snapshot source listing or one bounded chunk from a selected source.
KeymapSnapshot { chunk: bool },
/// `{ context, serverID, method, params?, timeoutMilliseconds? }`: one
Expand Down Expand Up @@ -209,6 +212,12 @@ pub(crate) const HOST_METHODS: &[HostMethod] = &[
permission: None,
params: HostParams::Context,
},
HostMethod {
method: WORKSPACE_GET_HOST_PATHS,
service: HOST_SERVICE_WORKSPACE,
permission: Some("workspace.metadata"),
params: HostParams::HostPaths,
},
HostMethod {
method: "lithe/host/workspace/readDirectory",
service: "workspace",
Expand Down
60 changes: 60 additions & 0 deletions rust/crates/plugin-rpc/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -183,6 +183,15 @@ const WORKSPACE_GET_CONTEXT: &str = "lithe/host/workspace/getContext";

const WORKSPACE_GET_FOLDERS: &str = "lithe/host/workspace/getFolders";

/// Paths of a remote context's root folders on the remote host (plan
/// API-40). `workspace.metadata` admits it as a grant of its own, not as
/// part of the `workspace` service, so a host that does not serve it leaves
/// it out of the session and adding it widened no existing grant.
const WORKSPACE_GET_HOST_PATHS: &str = "lithe/host/workspace/getHostPaths";

/// Most root IDs one `getHostPaths` request may name.
const MAX_HOST_PATH_ROOTS: usize = 256;

/// Backend request listing the plugins that export a function it may call.
pub const FUNCTIONS_PROVIDERS: &str = "lithe/host/functions/providers";

Expand Down Expand Up @@ -291,6 +300,13 @@ pub mod codes {
/// The request names a contribution, such as a language server, that
/// another plugin owns.
pub const HOST_FOREIGN_CONTRIBUTION: &str = "plugin.host.foreignContribution";
/// The connection to the remote workspace host was lost while the
/// operation ran. Whether the operation took effect on the host is
/// unknown; the backend asks again once the context is current.
pub const HOST_REMOTE_DISCONNECTED: &str = "plugin.host.remoteDisconnected";
/// The plugin has no package or managed runtime for the remote host's
/// platform, so the process the request needs cannot run there.
pub const HOST_PLATFORM_UNAVAILABLE: &str = "plugin.host.platformUnavailable";
/// A page's call or a host operation ended because the backend process
/// exited. Reported by hosts to pages, never sent to a backend.
pub const BACKEND_EXITED: &str = "plugin.backend.exited";
Expand Down Expand Up @@ -804,6 +820,50 @@ mod tests {
.is_err());
}

/// `workspace.metadata` admits `getHostPaths` as a method grant of its
/// own, and one request names at most 256 root IDs (plan API-40).
#[test]
fn host_paths_are_a_method_grant_with_bounded_root_ids() {
assert_eq!(
methods_for_permission("workspace.metadata"),
[WORKSPACE_GET_HOST_PATHS]
);
assert!(is_host_grant(WORKSPACE_GET_HOST_PATHS));
let granted = BTreeSet::from([
HOST_SERVICE_WORKSPACE.to_string(),
WORKSPACE_GET_HOST_PATHS.to_string(),
]);
let context = json!({ "contextID": "w-3f2a", "generation": 2 });
let root_ids =
|count: usize| -> Vec<String> { (0..count).map(|i| format!("r{i}")).collect() };
let request = |count| {
host_request(
WORKSPACE_GET_HOST_PATHS,
Some(&json!({ "context": context, "rootIDs": root_ids(count) })),
Some(0),
&granted,
)
};
assert_eq!(
request(MAX_HOST_PATH_ROOTS),
Ok(HostRoute::Forward(HOST_SERVICE_WORKSPACE))
);
assert_eq!(
request(MAX_HOST_PATH_ROOTS + 1).unwrap_err().code,
codes::HOST_INVALID_PARAMS
);
// The method is not part of the `workspace` service's methods, so a
// session granted only the service never learns it exists.
let capabilities =
capabilities_answer(0, &BTreeSet::from([HOST_SERVICE_WORKSPACE.to_string()]));
assert!(!capabilities.to_string().contains(WORKSPACE_GET_HOST_PATHS));
let capabilities = capabilities_answer(0, &granted);
assert!(capabilities["services"][HOST_SERVICE_WORKSPACE]["methods"]
.as_array()
.unwrap()
.contains(&json!(WORKSPACE_GET_HOST_PATHS)));
}

/// A message over the bound is cut at a character boundary, never inside
/// a multi-byte character.
#[test]
Expand Down
10 changes: 10 additions & 0 deletions rust/fixtures/plugins/host-api-v1.json
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,16 @@
"input": { "method": "lithe/host/workspace/readFile", "service": "workspace", "grants": ["workspace"] },
"expected": { "allowed": false, "code": "plugin.host.permissionDenied" }
},
{
"name": "metadata grant alone does not grant host paths",
"input": { "method": "lithe/host/workspace/getHostPaths", "service": "workspace", "grants": ["workspace"] },
"expected": { "allowed": false, "code": "plugin.host.permissionDenied" }
},
{
"name": "the host-paths grant admits host paths",
"input": { "method": "lithe/host/workspace/getHostPaths", "service": "workspace", "grants": ["workspace", "lithe/host/workspace/getHostPaths"] },
"expected": { "allowed": true }
},
{
"name": "a method cannot be attributed to another service",
"input": { "method": "lithe/host/editor/applyEdits", "service": "workspace", "grants": ["workspace"] },
Expand Down
2 changes: 2 additions & 0 deletions shared/plugins/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -155,6 +155,7 @@ SDK 与插件后端在构建时下载,并按锁定的 SHA-256(或 Git commit
| [构建、运行与测试接入](guides/build-tool-integration.md) | 执行提供者示例、工程后端边界,以及声明式构建系统的现状和已发布示例 |
| [编写调试适配器插件](guides/debug-adapter-plugin.md) | 从最小清单到启动方式、先编译再调试的 `executable`,以及调试控制台补全 |
| [插件后端:工作区上下文与宿主服务](guides/plugin-backend-workspace.md) | 免页面启动、工作区上下文、纯后端命令、文档/文件/窗口宿主方法、宿主通知、失败码,以及宿主服务只增不改时后端要遵守的规则 |
| [插件后端:远程工作区](guides/plugin-backend-remote-workspaces.md) | 远程上下文的来源与远端平台、单独获取的远端路径 `getHostPaths`、`path` 与 `hostPath` 的区别、断线与平台失败,以及插件函数中转 |
| [插件自有的原生终端](guides/native-terminals.md) | `terminal/*`:创建、显示、关闭、重开和退出通知(本机工作区) |

收录和上架的规则由 Lithe Store 维护:[发布社区插件](https://github.com/Lithe-IDEA/Lithe-store/blob/main/docs/publishing.md)讲怎么打包、提交收录和发布新版本,[版本追踪与更新](https://github.com/Lithe-IDEA/Lithe-store/blob/main/docs/versioning-and-updates.md)讲 Lithe 怎么选择可安装的版本。
Expand Down Expand Up @@ -375,6 +376,7 @@ The example plugins are maintained in their own repositories. Before copying one
| [Build, run, and test integration](guides/build-tool-integration.md) | Execution provider examples, the project backend boundary, and the current status and published examples of declarative build systems |
| [Writing a debug adapter plugin](guides/debug-adapter-plugin.md) | From a minimal manifest to launch modes, compile-then-debug `executable`, and debug console completion |
| [Plugin backend: workspace contexts and host services](guides/plugin-backend-workspace.md) | Launching without a page, workspace contexts, backend-only commands, document, file, and window host methods, host notifications, failure codes, and the rules a backend follows while host services only grow |
| [Plugin backend: remote workspaces](guides/plugin-backend-remote-workspaces.md) | A remote context's authority and the remote platform, remote paths fetched on their own with `getHostPaths`, how `path` and `hostPath` differ, disconnect and platform failures, and the function relay |
| [Plugin-owned native terminals](guides/native-terminals.md) | `terminal/*`: create, show, close, reopen, and exit notifications (local workspaces) |

Lithe Store maintains the listing rules: [Publishing community plugins](https://github.com/Lithe-IDEA/Lithe-store/blob/main/docs/publishing.md) covers packaging, submitting a listing, and releasing new versions, and [Version tracking and updates](https://github.com/Lithe-IDEA/Lithe-store/blob/main/docs/versioning-and-updates.md) covers how Lithe chooses an installable version.
Expand Down
Loading
Loading