diff --git a/rust/contracts/rust-core-api.md b/rust/contracts/rust-core-api.md index c2bd7f72..48d10a92 100644 --- a/rust/contracts/rust-core-api.md +++ b/rust/contracts/rust-core-api.md @@ -7455,8 +7455,10 @@ resource does not affect code signatures or update deltas. `workspaceRoot:""`, allowing discovery and inspection before a project is open. Global mode requires an empty local pin document, never selects project wrappers, project environments, project settings or project provider statements, and does not require a fictitious workspace -filesystem fact. Additional `workspaceRoots` remain exclusion roots. Normal requests still -require the real project root. Native callers supply registered homes as ordinary +filesystem fact. Global resolution can consume `queries` with `directory:"."`, outer +`context:null`, and explicit `workspace:null, context:null` in each query's params. +Additional `workspaceRoots` remain exclusion roots. Normal requests still require +queries bound to the real project root and directory. Native callers supply registered homes as ordinary `systemInstallations` and the current catalog default as `globalDefault:{home}`; the existing precedence remains fixed consumer choice, project override, global default, then automatic discovery. Clearing the global default exposes live automatic selection without diff --git a/rust/crates/core/src/tests/toolchain_resolution.rs b/rust/crates/core/src/tests/toolchain_resolution.rs index 5c0cf28e..297bb465 100644 --- a/rust/crates/core/src/tests/toolchain_resolution.rs +++ b/rust/crates/core/src/tests/toolchain_resolution.rs @@ -1256,6 +1256,51 @@ fn global_toolchain_discovery_and_defaults_need_no_project_directory() { assert_eq!(candidates["candidates"]["installations"][0]["home"], HOME); } +#[test] +fn global_toolchain_resolution_binds_provider_replies_to_their_scope() { + let scope: Value = serde_json::from_str(include_str!( + "../../../../fixtures/toolchains/global-query-resolution.json" + )) + .unwrap(); + let mut value = input(); + self_provided(&mut value); + for field in ["directory", "global", "workspaceRoot", "context"] { + value[field] = scope[field].clone(); + } + value["filesystem"] = json!([]); + installation(&mut value, HOME, "21.0.4"); + let mut backend = installs_query(&value, &[(HOME, "21.0.4")]); + let workspace_scope = backend["request"]["params"]["workspace"].take(); + let workspace_context = backend["request"]["params"]["context"].take(); + for field in ["context", "workspace"] { + backend["request"]["params"][field] = scope["queryScope"][field].clone(); + } + value["queries"] = json!([backend]); + let selection = &resolve(&value)["outcome"]["selection"]; + assert_eq!(selection["home"], HOME); + assert_eq!(selection["layer"], scope["expectedLayer"]); + assert_eq!(selection["provider"], scope["expectedProvider"]); + + let mut with_context = value.clone(); + with_context["context"] = workspace_context.clone(); + with_context["queries"][0]["request"]["params"]["context"] = workspace_context; + let mut with_workspace = value.clone(); + with_workspace["queries"][0]["request"]["params"]["workspace"] = workspace_scope; + let mut in_workspace = value.clone(); + in_workspace["global"] = json!(false); + in_workspace["workspaceRoot"] = json!(WORKSPACE); + in_workspace["filesystem"] + .as_array_mut() + .unwrap() + .push(fact(WORKSPACE, "directory", false)); + in_workspace["queries"][0]["filesystem"] = in_workspace["filesystem"].clone(); + for rejected in [with_context, with_workspace, in_workspace] { + let result = resolve_command(rejected, validated_manifest_json).unwrap(); + assert_eq!(result["status"], "rejected", "{result}"); + assert_eq!(result["error"]["code"], "plugin.toolchain.invalidParams"); + } +} + #[test] fn global_toolchain_mode_refuses_project_pins_and_ignores_project_executables() { let mut value = input(); diff --git a/rust/crates/toolchain/src/resolution.rs b/rust/crates/toolchain/src/resolution.rs index c1ded77a..d2ed94e5 100644 --- a/rust/crates/toolchain/src/resolution.rs +++ b/rust/crates/toolchain/src/resolution.rs @@ -189,18 +189,29 @@ pub(crate) fn resolve(input: &Value, validate_manifest: ManifestValidator) -> Va if value["platform"] != platform { return Err(reject("queries.platform")); } - if value["request"]["params"]["workspace"]["root"] - .as_str() - .and_then(|path| snapshot.key(path).ok()) - != Some(snapshot.key(&root)?) - { - return Err(reject("queries.workspace")); - } - let requested_directory = value["request"]["params"]["workspace"]["directory"] - .as_str() - .unwrap_or(""); - if requested_directory != directory { - return Err(reject("queries.directory")); + let params = &value["request"]["params"]; + let workspace = ¶ms["workspace"]; + if input["global"] == true { + if !workspace.is_null() { + return Err(reject("queries.workspace")); + } + if !params["context"].is_null() { + return Err(reject("queries.context")); + } + if directory != "." { + return Err(reject("queries.directory")); + } + } else { + if workspace["root"] + .as_str() + .and_then(|path| snapshot.key(path).ok()) + != Some(snapshot.key(&root)?) + { + return Err(reject("queries.workspace")); + } + if workspace["directory"].as_str() != Some(directory) { + return Err(reject("queries.directory")); + } } if value["filesystem"] != input["filesystem"] { return Err(reject("queries.filesystem")); @@ -217,7 +228,7 @@ pub(crate) fn resolve(input: &Value, validate_manifest: ManifestValidator) -> Va if value["currentGeneration"].as_u64() != Some(generation) { return Err(reject("queries.generation")); } - if value["request"]["params"]["context"] != input["context"] { + if params["context"] != input["context"] { return Err(reject("queries.context")); } if !query_providers.insert(( diff --git a/rust/fixtures/toolchains/global-query-resolution.json b/rust/fixtures/toolchains/global-query-resolution.json new file mode 100644 index 00000000..51258068 --- /dev/null +++ b/rust/fixtures/toolchains/global-query-resolution.json @@ -0,0 +1,12 @@ +{ + "directory": ".", + "global": true, + "workspaceRoot": "", + "context": null, + "queryScope": { + "workspace": null, + "context": null + }, + "expectedLayer": "automatic", + "expectedProvider": "dev.lithe.fixture.java-support:installs" +} diff --git a/shared/plugins/contract/v1/toolchains/README.md b/shared/plugins/contract/v1/toolchains/README.md index d6dd4ef7..1ab3b857 100644 --- a/shared/plugins/contract/v1/toolchains/README.md +++ b/shared/plugins/contract/v1/toolchains/README.md @@ -315,9 +315,13 @@ its declared `path` layout. Each query is a complete `decodeReply` input, bound to the resolver's registry, generation/context, root/directory, case policy, roots and filesystem snapshot. -The resolver accepts local workspace queries here; global-only decoder inputs -are not a substitute for the workspace-bound query round. `providerChoices` -records explicit project/global provider or automatic-installation choices; it +Workspace resolution only accepts queries bound to the same workspace and +directory. With `global:true` and `workspaceRoot:""`, the resolver accepts +global-only queries with explicit `workspace:null` and `context:null` in the +request. The resolution has `directory:"."` and `context:null`. Any additional +`workspaceRoots` still exclude installations inside an open workspace. +`providerChoices` records explicit project/global provider or +automatic-installation choices; it does not activate providers or bypass their evidence checks. Its shape is `{project?:"pluginID:providerID",global?:"pluginID:providerID", automatic?:"absolute-installation-home"}`; choices not present in the current