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
43 changes: 9 additions & 34 deletions rust/crates/build/src/invocation.rs
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@ use super::manifest::{
use super::BuildManifest;
use lithe_plugin_model::executables::ordered_indices;
use lithe_plugin_model::{
reported_version, validate_minimum_version, version_components, PluginRejection, Validation,
validate_minimum_version, version_components, PluginRejection, Validation,
};
use serde::Deserialize;
use serde_json::{json, Value};
Expand Down Expand Up @@ -67,48 +67,27 @@ fn candidates(
}).collect()
}

/// Arguments that make a host-managed runtime print its version and exit.
/// Core owns them so hosts use the same probe, for build runtimes and for
/// plugin launch runtimes alike. Hosts capture stderr together with stdout
/// before checking the declared minimum. A tool definition probes the
/// runtimes it resolves itself, so other IDs have none.
pub(crate) fn host_runtime_version_arguments(id: &str) -> Option<&'static [&'static str]> {
match id {
"go" => Some(&["version"]),
"node" => Some(&["--version"]),
_ => None,
}
}

fn runtime(
runtime: &RuntimeDependency,
root: &str,
platform: Platform,
manifest: &impl BuildManifest,
settings: &BTreeMap<String, Value>,
) -> Validation<Value> {
let version = if runtime.is_host_managed() {
host_runtime_version_arguments(&runtime.id)
.map(|arguments| json!({ "arguments": arguments }))
} else {
runtime
.version
.as_ref()
.map(|probe| json!({ "arguments": probe.arguments }))
};
let version = runtime
.version
.as_ref()
.map(|probe| json!({ "arguments": probe.arguments }));
// An implicit reference is the toolchain candidate of the tool
// definition with the same ID; resolving it checks `minimumVersion`.
let executables = if runtime.names_tool_definition() {
vec![json!({ "kind": "toolchain", "toolchainID": runtime.id, "root": root })]
} else if runtime.is_host_managed() {
vec![json!({"kind":"runtime","runtime":runtime.id})]
} else {
candidates(&runtime.executables, root, platform, manifest, settings)?
};
Ok(json!({
"id": runtime.id,
"displayName": runtime.display_name,
"hostManaged": runtime.is_host_managed(),
"pathVariable":format!("LITHE_{}_PATH",runtime.id.to_ascii_uppercase().replace('-',"_")),
"minimumVersion": runtime.minimum_version,
"executables": executables,
Expand Down Expand Up @@ -202,14 +181,10 @@ pub(crate) fn check_version(
),
));
}
let found = if dependency.is_host_managed() {
reported_version(&request.output)
} else {
dependency
.version
.as_ref()
.and_then(|probe: &VersionProbe| probe.version(&request.output))
};
let found = dependency
.version
.as_ref()
.and_then(|probe: &VersionProbe| probe.version(&request.output));
(found, dependency.minimum_version.as_deref())
}
};
Expand Down
2 changes: 1 addition & 1 deletion rust/crates/build/src/manifest.rs
Original file line number Diff line number Diff line change
Expand Up @@ -142,7 +142,7 @@ pub struct DeclarationContext<'a> {
pub declared_settings: &'a lithe_plugin_model::settings::DeclaredSettings,
/// Language IDs the plugin declares a language server for.
pub language_servers: &'a BTreeSet<String>,
/// Whether tool names are host runtimes or tool-definition IDs.
/// The tool-definition IDs in `requiresTools` that a process may name.
pub runtime_tools: lithe_plugin_model::process::RuntimeTools<'a>,
}

Expand Down
74 changes: 16 additions & 58 deletions rust/crates/build/src/manifest/invocation.rs
Original file line number Diff line number Diff line change
Expand Up @@ -21,20 +21,12 @@ pub(crate) struct Invocation {
pub(crate) minimum_version: Option<String>,
}

/// Runtime tools the host discovers and manages itself for build-system
/// runtime dependencies: it exports each as `LITHE_<TOOL>_PATH` and puts its
/// directory on the process's `PATH`. Server processes never use this list;
/// they name tool definitions from `requiresTools`.
pub(crate) const KNOWN_RUNTIME_TOOLS: &[&str] = &["go", "node"];

pub(crate) use lithe_plugin_model::executables::{ExecutableCandidate, ExecutableSource, Platform};

/// A runtime the tool needs. Runtimes the host already manages (`node`,
/// `go`) take only an ID and an optional minimum version and follow the
/// user's runtime settings. Any other runtime either describes how to find
/// and probe it, or lists no executables and names the tool definition with
/// its ID, which finds the executable and checks `minimumVersion`. A
/// plugin is never limited to a host list.
/// A runtime the tool needs. It either describes how to find and probe the
/// executable, or lists no executables and names the tool definition with
/// its ID, which finds the executable and checks `minimumVersion`. Core
/// knows no runtime by name; every runtime comes from a manifest.
#[derive(Debug, Clone, Deserialize, Serialize)]
#[serde(rename_all = "camelCase", deny_unknown_fields)]
pub(crate) struct RuntimeDependency {
Expand All @@ -50,31 +42,24 @@ pub(crate) struct RuntimeDependency {
}

impl RuntimeDependency {
/// True when the host discovers this runtime with its own settings.
pub(crate) fn is_host_managed(&self) -> bool {
KNOWN_RUNTIME_TOOLS.contains(&self.id.as_str())
}

/// True when the runtime is the tool definition with the same ID: it
/// lists no executables and the host does not manage it. Plans give it
/// a single toolchain candidate for that ID.
/// lists no executables. Plans give it a single toolchain candidate for
/// that ID.
pub(crate) fn names_tool_definition(&self) -> bool {
self.executables.is_empty() && !self.is_host_managed()
self.executables.is_empty()
}

/// True when the tool definition the runtime resolves through checks
/// `minimumVersion`, so the manifest needs no version probe: a runtime
/// the host does not manage that is an implicit reference or has exactly
/// one toolchain candidate.
/// `minimumVersion`, so the manifest needs no version probe: the runtime
/// is an implicit reference or has exactly one toolchain candidate.
pub(crate) fn checked_by_tool_definition(&self) -> bool {
!self.is_host_managed()
&& matches!(
self.executables.as_slice(),
[] | [ExecutableCandidate {
source: ExecutableSource::Toolchain { .. },
..
}]
)
matches!(
self.executables.as_slice(),
[] | [ExecutableCandidate {
source: ExecutableSource::Toolchain { .. },
..
}]
)
}
}

Expand Down Expand Up @@ -137,33 +122,6 @@ impl BuildSystemDeclaration {
if let Some(minimum) = &runtime.minimum_version {
validate_minimum_version(minimum, &format!("{field}.minimumVersion"))?;
}
if runtime.is_host_managed() {
// The host finds these with the user's own runtime settings;
// a plugin must not route around that choice.
let toolchain = runtime.executables.len() == 1
&& matches!(
runtime.executables[0].source,
ExecutableSource::Toolchain { .. }
);
if (!runtime.executables.is_empty() && !toolchain) || runtime.version.is_some() {
return Err(invalid(
field,
format!(
"{} is discovered by the host; declare only its minimumVersion.",
runtime.id
),
));
}
if toolchain {
check_candidate(
&mut runtime.executables[0],
&format!("{field}.executables[0]"),
&invocation.environment_inputs,
&BTreeSet::new(),
)?;
}
continue;
}
if runtime.names_tool_definition() {
// The tool definition finds the executable and checks
// `minimumVersion`; a probe here would have nothing to run.
Expand Down
158 changes: 63 additions & 95 deletions rust/crates/core/src/tests/build_runtime_versions.rs
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
//! Host runtimes keep Core's version probe; any other runtime names the tool
//! definition with the same ID, which checks the version itself.
//! A runtime without executables names the tool definition with the same ID,
//! which finds the executable and checks the version itself. Core knows no
//! runtime by name: `go` and `node` follow the same rules as any other ID.

use crate::build::{build_check_tool_version_command, build_invocation_plan_command};
use lithe_plugin_manifest::DeclarativePluginManifest as Manifest;
Expand Down Expand Up @@ -31,126 +32,93 @@ fn check(manifest: Value, runtime: &str, output: &str) -> Value {
.expect("version request is valid")
}

/// A published manifest that names a runtime without executables keeps
/// validating; the plan resolves it through the tool definition with that
/// ID. `go` and `node` get no host candidate and no Core probe.
#[test]
fn host_runtime_probe_arguments_are_portable_across_platforms() {
fn runtime_without_executables_becomes_a_toolchain_candidate() {
for platform in ["macos", "linux", "windows"] {
let mut manifest = manifest();
manifest["buildSystems"][0]["invocation"]["runtimeDependencies"] = json!([
{ "id": "node", "minimumVersion": "20" },
{ "id": "go", "minimumVersion": "1.22" }
]);
let result = plan(manifest, platform);
assert_eq!(result["status"], "ok", "{platform}: {result}");
let runtimes = &result["invocation"]["runtimeDependencies"];
for (index, arguments) in [json!(["--version"]), json!(["version"])]
.into_iter()
.enumerate()
{
assert_eq!(runtimes[index]["hostManaged"], true, "{platform}");
let id = if index == 0 { "node" } else { "go" };
assert_eq!(
runtimes[index]["executables"],
json!([{"kind":"runtime","runtime":id}]),
"{platform}"
);
for (id, minimum, path_variable) in [
("vm", "8", "LITHE_VM_PATH"),
("go", "1.22", "LITHE_GO_PATH"),
("node", "20", "LITHE_NODE_PATH"),
] {
let mut manifest = manifest();
manifest["buildSystems"][0]["invocation"]["runtimeDependencies"] =
json!([{ "id": id, "minimumVersion": minimum }]);
let result = plan(manifest, platform);
assert_eq!(result["status"], "ok", "{platform} {id}: {result}");
assert_eq!(
runtimes[index]["pathVariable"],
format!("LITHE_{}_PATH", id.to_ascii_uppercase())
result["invocation"]["runtimeDependencies"][0],
json!({
"id": id,
"displayName": null,
"pathVariable": path_variable,
"executables": [
{ "kind": "toolchain", "toolchainID": id, "root": "." }
],
"version": null,
"minimumVersion": minimum
}),
"{platform} {id}"
);
// The build tool's own probe stays independent of its runtimes.
assert_eq!(
runtimes[index]["version"]["arguments"], arguments,
"{platform}"
result["invocation"]["version"]["arguments"],
json!(["--version"])
);
}
// The build tool's own probe stays independent of its runtimes.
assert_eq!(
result["invocation"]["version"]["arguments"],
json!(["--version"])
);
}
}

/// A published manifest that names `vm` without executables keeps
/// validating; the plan resolves it through the tool definition `vm`.
#[test]
fn runtime_without_executables_becomes_a_toolchain_candidate() {
for platform in ["macos", "linux", "windows"] {
fn runtime_named_by_tool_definition_has_no_core_probe() {
for (id, output) in [
("vm", "21.0.4\n"),
("go", "go version go1.22.3 darwin/arm64\n"),
("node", "v20.11.0\n"),
] {
let mut manifest = manifest();
manifest["buildSystems"][0]["invocation"]["runtimeDependencies"] =
json!([{ "id": "vm", "minimumVersion": "8" }]);
let result = plan(manifest, platform);
assert_eq!(result["status"], "ok", "{platform}: {result}");
json!([{ "id": id, "minimumVersion": "8" }]);
let result = check(manifest, id, output);
assert_eq!(
result["invocation"]["runtimeDependencies"][0],
result,
json!({
"id": "vm",
"displayName": null,
"hostManaged": false,
"pathVariable":"LITHE_VM_PATH",
"executables": [
{ "kind": "toolchain", "toolchainID": "vm", "root": "." }
],
"version": null,
"minimumVersion": "8"
"status": "rejected",
"error": {
"code": "plugin.build.unavailable",
"message": format!(
"Cargo declares no version probe for {id}; its tool definition checks the version."
)
}
}),
"{platform}"
"{id}"
);
}
}

/// A runtime the plugin describes itself reads its version with its own
/// pattern, whatever its ID.
#[test]
fn runtime_named_by_tool_definition_has_no_core_probe() {
let mut manifest = manifest();
manifest["buildSystems"][0]["invocation"]["runtimeDependencies"] =
json!([{ "id": "vm", "minimumVersion": "8" }]);
let result = check(manifest, "vm", "21.0.4\n");
assert_eq!(
result,
json!({
"status": "rejected",
"error": {
"code": "plugin.build.unavailable",
"message": "Cargo declares no version probe for vm; its tool definition checks the version."
}
})
);
}

#[test]
fn host_runtime_output_reads_the_first_dotted_number() {
for (runtime, minimum, output, satisfied, version) in [
("node", "20", "v20.11.0\n", true, "20.11.0"),
(
"node",
"20",
"warning: custom build\nv18.20.4\n",
false,
"18.20.4",
),
(
"go",
"1.22",
"go version go1.22.3 darwin/arm64\n",
true,
"1.22.3",
),
(
"go",
"1.22",
"go version go1.21.9 linux/amd64\n",
false,
"1.21.9",
),
fn described_runtime_reads_its_own_probe() {
for (output, satisfied, version) in [
("go version go1.22.3 darwin/arm64\n", true, "1.22.3"),
("go version go1.21.9 linux/amd64\n", false, "1.21.9"),
] {
let mut manifest = manifest();
manifest["buildSystems"][0]["invocation"]["runtimeDependencies"] =
json!([{ "id": runtime, "minimumVersion": minimum }]);
let result = check(manifest, runtime, output);
manifest["buildSystems"][0]["invocation"]["runtimeDependencies"] = json!([{
"id": "go",
"minimumVersion": "1.22",
"executables": [{ "kind": "command", "command": "go" }],
"version": { "arguments": ["version"], "pattern": "go(\\d+\\.\\d+(?:\\.\\d+)?)" }
}]);
let result = check(manifest, "go", output);
assert_eq!(result["status"], "ok", "{result}");
assert_eq!(
result["version"],
json!({ "satisfied": satisfied, "version": version }),
"{runtime} {output}"
"{output}"
);
}
}
Expand Down
2 changes: 1 addition & 1 deletion rust/crates/debug/src/adapters.rs
Original file line number Diff line number Diff line change
Expand Up @@ -204,7 +204,7 @@ pub struct DeclarationContext<'a> {
pub declared_settings: &'a DeclaredSettings,
/// Language IDs of the plugin's `languageServers[]`.
pub language_servers: &'a BTreeSet<String>,
/// Whether tool names are host runtimes or tool-definition IDs.
/// The tool-definition IDs in `requiresTools` that a process may name.
pub runtime_tools: lithe_plugin_model::process::RuntimeTools<'a>,
}

Expand Down
Loading
Loading