diff --git a/Cargo.lock b/Cargo.lock index a069b149791..327f5e8f601 100644 --- a/Cargo.lock +++ b/Cargo.lock @@ -10524,6 +10524,18 @@ version = "0.2.3" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "fc04a4c58212d57930a24bf47d3fa87485264a3a054e9c10e042eb373573ad3c" +[[package]] +name = "managed_resources" +version = "0.1.0" +dependencies = [ + "anyhow", + "futures 0.3.34", + "gpui", + "serde", + "serde_json", + "thiserror 2.0.17", +] + [[package]] name = "manatee" version = "0.8.0-alpha.5" @@ -11285,6 +11297,7 @@ dependencies = [ "futures 0.3.34", "http_client", "log", + "managed_resources", "paths", "semver", "serde", diff --git a/Cargo.toml b/Cargo.toml index 5b1f5473e6d..cdb40996b51 100644 --- a/Cargo.toml +++ b/Cargo.toml @@ -141,6 +141,7 @@ members = [ "crates/lsp", "crates/lsp_command_selector", "crates/lsp_locations", + "crates/managed_resources", "crates/markdown", "crates/markdown_preview", "crates/menu", @@ -415,6 +416,7 @@ lmstudio = { path = "crates/lmstudio" } lsp = { path = "crates/lsp" } lsp_command_selector = { path = "crates/lsp_command_selector" } lsp_locations = { path = "crates/lsp_locations" } +managed_resources = { path = "crates/managed_resources" } markdown = { path = "crates/markdown" } markdown_preview = { path = "crates/markdown_preview" } menu = { path = "crates/menu" } diff --git a/crates/managed_resources/Cargo.toml b/crates/managed_resources/Cargo.toml new file mode 100644 index 00000000000..6a6bdec2957 --- /dev/null +++ b/crates/managed_resources/Cargo.toml @@ -0,0 +1,24 @@ +[package] +name = "managed_resources" +version = "0.1.0" +edition.workspace = true +publish.workspace = true +license = "GPL-3.0-or-later" + +[lints] +workspace = true + +[lib] +path = "src/managed_resources.rs" +doctest = false + +[dependencies] +anyhow.workspace = true +futures.workspace = true +gpui.workspace = true +serde = { workspace = true, features = ["derive"] } +thiserror.workspace = true + +[dev-dependencies] +gpui = { workspace = true, features = ["test-support"] } +serde_json.workspace = true diff --git a/crates/managed_resources/LICENSE-GPL b/crates/managed_resources/LICENSE-GPL new file mode 120000 index 00000000000..89e542f750c --- /dev/null +++ b/crates/managed_resources/LICENSE-GPL @@ -0,0 +1 @@ +../../LICENSE-GPL \ No newline at end of file diff --git a/crates/managed_resources/src/managed_resources.rs b/crates/managed_resources/src/managed_resources.rs new file mode 100644 index 00000000000..bb699fa44b8 --- /dev/null +++ b/crates/managed_resources/src/managed_resources.rs @@ -0,0 +1,942 @@ +use futures::{FutureExt, future::Shared}; +use gpui::{App, AsyncApp, Context, Task, WeakEntity}; +use serde::{Deserialize, Serialize}; +use std::{ + any::{Any, TypeId}, + collections::{BTreeMap, HashMap}, + future::Future, + rc::Rc, + sync::Arc, +}; + +pub trait ManagedResource: Sized + Send + Sync + 'static { + type Key: 'static; + type Context: ?Sized + 'static; + type Command: ManagedResourceCommand; + + /// Must be stable across restarts and unique to this resource type and key. + /// Include configuration that changes what is authorized; context is not part of the cache key. + fn id(key: &Self::Key) -> ResourceId; + + fn obtain( + key: &Self::Key, + context: &Self::Context, + ) -> impl Future>; +} + +pub trait ManagedResourceCommand: Sized { + type Output; + + fn run(self, resource: Arc) -> impl Future; +} + +#[derive(Clone, Debug, PartialEq, Eq, Hash, Serialize, Deserialize)] +pub struct ResourceId { + pub kind: String, + pub key: String, +} + +#[derive(Clone, Copy, Debug, PartialEq, Eq, Serialize, Deserialize)] +#[serde(rename_all = "snake_case")] +pub enum ResourcePermission { + Allowed, + Denied, +} + +#[derive(Clone, Debug, Default, PartialEq, Eq, Serialize, Deserialize)] +#[serde(transparent)] +pub struct ResourcePermissions { + entries: BTreeMap>, +} + +impl ResourcePermissions { + fn permission(&self, id: &ResourceId) -> Option { + self.entries.get(&id.kind)?.get(&id.key).copied() + } + + fn set_permission(&mut self, id: ResourceId, permission: ResourcePermission) { + self.entries + .entry(id.kind) + .or_default() + .insert(id.key, permission); + } +} + +#[derive(Clone, Debug, thiserror::Error)] +pub enum ResourceAccessError { + #[error("access to the managed resource was denied")] + Denied, + #[error("failed to obtain the managed resource: {0}")] + Obtain(#[source] Arc), + #[error("the managed resource identity is already in use by another resource type")] + ConflictingResourceType, + #[error("the managed resource store was dropped")] + StoreDropped, +} + +type RequestApproval = dyn Fn(ResourceId, &mut App) -> Task; +type ResourceTask = Shared, ResourceAccessError>>>; + +pub struct ManagedResourceStore { + permissions: ResourcePermissions, + resources: HashMap, + request_approval: Rc, +} + +struct ResourceEntry { + resource_type: TypeId, + value: Option>, + pending: Option, + permission_revision: u64, +} + +impl ManagedResourceStore { + pub fn new( + permissions: ResourcePermissions, + request_approval: impl Fn(ResourceId, &mut App) -> Task + 'static, + ) -> Self { + Self { + permissions, + resources: HashMap::new(), + request_approval: Rc::new(request_approval), + } + } + + pub fn permissions(&self) -> ResourcePermissions { + self.permissions.clone() + } + + pub fn permission(&self, id: &ResourceId) -> Option { + self.permissions.permission(id) + } + + pub fn set_permission( + &mut self, + id: ResourceId, + permission: ResourcePermission, + cx: &mut Context, + ) { + if let Some(resource) = self.resources.get_mut(&id) { + resource.permission_revision = resource.permission_revision.wrapping_add(1); + } + self.permissions.set_permission(id, permission); + cx.notify(); + } + + pub fn clear_permission(&mut self, id: &ResourceId, cx: &mut Context) { + if let Some(resource) = self.resources.get_mut(id) { + resource.permission_revision = resource.permission_revision.wrapping_add(1); + } + if let Some(permissions) = self.permissions.entries.get_mut(&id.kind) { + permissions.remove(&id.key); + if permissions.is_empty() { + self.permissions.entries.remove(&id.kind); + } + } + cx.notify(); + } + + /// Approval and acquisition are shared; callbacks may run concurrently. + /// Dropping the returned task cancels only that callback, not shared acquisition. + /// Permission changes supersede a pending approval and are checked before each + /// callback, but cannot revoke resource handles already handed out. + pub fn with_managed_resource( + &mut self, + key: Resource::Key, + context: Rc, + cx: &mut Context, + operation: impl AsyncFnOnce(Arc) -> Ret + 'static, + ) -> Task> { + let id = Resource::id(&key); + if self.permission(&id) == Some(ResourcePermission::Denied) { + return Task::ready(Err(ResourceAccessError::Denied)); + } + let entry = self + .resources + .entry(id.clone()) + .or_insert_with(|| ResourceEntry { + resource_type: TypeId::of::(), + value: None, + pending: None, + permission_revision: 0, + }); + if entry.resource_type != TypeId::of::() { + return Task::ready(Err(ResourceAccessError::ConflictingResourceType)); + } + + let resource = if let Some(pending) = &entry.pending { + pending.clone() + } else { + let value = entry.value.clone(); + let pending = cx + .spawn({ + let id = id.clone(); + async move |this, cx| { + let result = async { + Self::ensure_permission(&this, &id, cx).await?; + if let Some(value) = value { + return Ok(value); + } + Resource::obtain(&key, &context) + .await + .map(|resource| Arc::new(resource) as Arc) + .map_err(|error| ResourceAccessError::Obtain(Arc::new(error))) + } + .await; + this.update(cx, |this, _| { + if let Some(entry) = this.resources.get_mut(&id) { + entry.pending = None; + if let Ok(value) = &result { + entry.value = Some(value.clone()); + } + } + }) + .map_err(|_| ResourceAccessError::StoreDropped)?; + result + } + }) + .shared(); + entry.pending = Some(pending.clone()); + pending + }; + + cx.spawn(async move |this, cx| { + let resource = resource.await?; + this.read_with(cx, |this, _| Self::check_permission(&this.permissions, &id)) + .map_err(|_| ResourceAccessError::StoreDropped)??; + let resource = resource + .downcast::() + .map_err(|_| ResourceAccessError::ConflictingResourceType)?; + Ok(operation(resource).await) + }) + } + + async fn ensure_permission( + this: &WeakEntity, + id: &ResourceId, + cx: &mut AsyncApp, + ) -> Result<(), ResourceAccessError> { + let approval = this + .read_with(cx, |this, _| match this.permission(id) { + Some(ResourcePermission::Allowed) => Ok(None), + Some(ResourcePermission::Denied) => Err(ResourceAccessError::Denied), + None => Ok(Some(( + this.resources + .get(id) + .map(|resource| resource.permission_revision), + this.request_approval.clone(), + ))), + }) + .map_err(|_| ResourceAccessError::StoreDropped)??; + if let Some((revision, request_approval)) = approval { + let permission = cx.update(|cx| request_approval(id.clone(), cx)).await; + this.update(cx, |this, cx| { + if this + .resources + .get(id) + .map(|resource| resource.permission_revision) + == revision + { + this.set_permission(id.clone(), permission, cx); + } + }) + .map_err(|_| ResourceAccessError::StoreDropped)?; + // Observers may revoke permission while the update flushes notifications. + this.read_with(cx, |this, _| Self::check_permission(&this.permissions, id)) + .map_err(|_| ResourceAccessError::StoreDropped)??; + } + Ok(()) + } + + fn check_permission( + permissions: &ResourcePermissions, + id: &ResourceId, + ) -> Result<(), ResourceAccessError> { + match permissions.permission(id) { + Some(ResourcePermission::Allowed) => Ok(()), + Some(ResourcePermission::Denied) | None => Err(ResourceAccessError::Denied), + } + } +} + +#[cfg(test)] +mod tests { + use super::*; + use futures::{channel::oneshot, poll}; + use gpui::{AppContext as _, Entity, TestAppContext}; + use std::{ + cell::Cell, + sync::atomic::{AtomicUsize, Ordering::SeqCst}, + }; + + type TestResource = Counter<0>; + type OtherResource = Counter<1>; + + fn deny_approval(_: ResourceId, _: &mut App) -> Task { + Task::ready(ResourcePermission::Denied) + } + + fn new_store(cx: &mut TestAppContext) -> Entity { + cx.new(|_| ManagedResourceStore::new(ResourcePermissions::default(), deny_approval)) + } + + fn access( + store: &Entity, + key: Resource::Key, + context: &Rc, + cx: &mut TestAppContext, + operation: impl AsyncFnOnce(Arc) -> Ret + 'static, + ) -> Task> { + store.update(cx, |store, cx| { + store.with_managed_resource(key, context.clone(), cx, operation) + }) + } + + fn pending_approval_store( + cx: &mut TestAppContext, + ) -> ( + Entity, + oneshot::Sender, + ) { + let (response, receive_response) = oneshot::channel(); + let receive_response = Cell::new(Some(receive_response)); + let store = cx.new(|_| { + ManagedResourceStore::new(ResourcePermissions::default(), move |id, cx| { + assert_eq!(id, TestResource::id(&"first")); + let receive_response = receive_response + .take() + .expect("concurrent access must share the approval decision"); + cx.spawn(async move |_| { + receive_response.await.expect("approval response cancelled") + }) + }) + }); + (store, response) + } + + struct Counter { + value: AtomicUsize, + } + + #[derive(Default)] + struct TestContext { + obtain_count: Cell, + fail_next_obtain: Cell, + obtain_ready: Cell>>, + } + + #[derive(Debug, PartialEq, Eq, thiserror::Error)] + enum TestError { + #[error("acquisition failed")] + Obtain, + #[error("command failed")] + Command, + #[error("acquisition cancelled")] + Cancelled, + } + + impl ManagedResource for Counter { + type Key = &'static str; + type Context = TestContext; + type Command = TestCommand; + + fn id(key: &Self::Key) -> ResourceId { + ResourceId { + kind: format!("counter-{KIND}"), + key: key.to_string(), + } + } + + async fn obtain(key: &Self::Key, context: &Self::Context) -> anyhow::Result { + context.obtain_count.set(context.obtain_count.get() + 1); + if let Some(obtain_ready) = context.obtain_ready.take() { + obtain_ready.await.map_err(|_| TestError::Cancelled)?; + } + if context.fail_next_obtain.replace(false) { + return Err(TestError::Obtain.into()); + } + Ok(Self { + value: AtomicUsize::new(key.len()), + }) + } + } + + enum TestCommand { + Add(usize), + Fail, + } + + impl ManagedResourceCommand> for TestCommand { + type Output = Result; + + async fn run(self, resource: Arc>) -> Self::Output { + match self { + Self::Add(value) => Ok(resource.value.fetch_add(value, SeqCst) + value), + Self::Fail => Err(TestError::Command), + } + } + } + + #[gpui::test] + async fn permissions_gate_acquisition_and_cached_access(cx: &mut TestAppContext) { + let approval_count = Rc::new(Cell::new(0)); + let store = cx.new(|_| { + ManagedResourceStore::new(ResourcePermissions::default(), { + let approval_count = approval_count.clone(); + move |_, _| { + approval_count.set(approval_count.get() + 1); + Task::ready(ResourcePermission::Denied) + } + }) + }); + let context = Rc::new(TestContext::default()); + let id = TestResource::id(&"first"); + + for _ in 0..2 { + let result = access::(&store, "first", &context, cx, async |_| { + panic!("an unapproved resource must not reach the callback") + }) + .await; + assert!(matches!(result, Err(ResourceAccessError::Denied))); + } + assert_eq!( + store.read_with(cx, |store, _| store.permission(&id)), + Some(ResourcePermission::Denied) + ); + assert_eq!(approval_count.get(), 1); + assert_eq!(context.obtain_count.get(), 0); + + store.update(cx, |store, cx| { + store.set_permission(id.clone(), ResourcePermission::Allowed, cx) + }); + let retained = access::(&store, "first", &context, cx, async |resource| { + assert_eq!(TestCommand::Add(7).run(resource.clone()).await, Ok(12)); + resource + }) + .await + .expect("resource allowed"); + + store.update(cx, |store, cx| { + store.set_permission(id.clone(), ResourcePermission::Denied, cx) + }); + let result = access::(&store, "first", &context, cx, async |_| { + panic!("a cached resource must not bypass denial") + }) + .await; + assert!(matches!(result, Err(ResourceAccessError::Denied))); + assert_eq!(retained.value.load(SeqCst), 12); + + store.update(cx, |store, cx| store.clear_permission(&id, cx)); + let result = access::(&store, "first", &context, cx, async |_| { + panic!("a cached resource must not bypass missing approval") + }) + .await; + assert!(matches!(result, Err(ResourceAccessError::Denied))); + assert_eq!(approval_count.get(), 2); + + store.update(cx, |store, cx| { + store.set_permission(id, ResourcePermission::Allowed, cx) + }); + let cached = + access::(&store, "first", &context, cx, async |resource| resource) + .await + .expect("resource allowed again"); + assert!(Arc::ptr_eq(&cached, &retained)); + let result = cx + .background_executor + .spawn(async move { TestCommand::Add(3).run(cached).await }) + .await; + assert_eq!(result, Ok(15)); + assert_eq!(retained.value.load(SeqCst), 15); + assert_eq!(context.obtain_count.get(), 1); + assert_eq!(approval_count.get(), 2); + } + + #[gpui::test] + async fn permissions_round_trip_without_sharing_live_resources(cx: &mut TestAppContext) { + let store = new_store(cx); + let context = Rc::new(TestContext::default()); + store.update(cx, |store, cx| { + store.set_permission(TestResource::id(&"first"), ResourcePermission::Allowed, cx); + store.set_permission(TestResource::id(&"second"), ResourcePermission::Denied, cx); + store.set_permission(OtherResource::id(&"first"), ResourcePermission::Denied, cx); + }); + let original = + access::(&store, "first", &context, cx, async |resource| resource) + .await + .expect("resource allowed"); + assert_eq!(TestCommand::Add(7).run(original.clone()).await, Ok(12)); + + let permissions = store.read_with(cx, |store, _| store.permissions()); + let serialized = serde_json::to_value(&permissions).expect("serializable permissions"); + assert_eq!( + serialized, + serde_json::json!({ + "counter-0": { "first": "allowed", "second": "denied" }, + "counter-1": { "first": "denied" }, + }) + ); + let restored = cx.new(|_| { + ManagedResourceStore::new( + serde_json::from_value(serialized).expect("restorable permissions"), + deny_approval, + ) + }); + assert_eq!( + restored.read_with(cx, |store, _| store.permissions()), + permissions + ); + let resource = + access::(&restored, "first", &context, cx, async |resource| resource) + .await + .expect("restored permission"); + assert!(!Arc::ptr_eq(&original, &resource)); + assert_eq!(TestCommand::Add(11).run(resource).await, Ok(16)); + assert_eq!(context.obtain_count.get(), 2); + + for key in ["second", "third"] { + let result = access::(&restored, key, &context, cx, async |_| { + panic!("approval must not extend to another key") + }) + .await; + assert!(matches!(result, Err(ResourceAccessError::Denied))); + } + let result = access::(&restored, "first", &context, cx, async |_| { + panic!("approval must not extend to another resource kind") + }) + .await; + assert!(matches!(result, Err(ResourceAccessError::Denied))); + assert_eq!(context.obtain_count.get(), 2); + + restored.update(cx, |store, cx| { + store.set_permission(OtherResource::id(&"first"), ResourcePermission::Allowed, cx) + }); + let value = + access::(&restored, "first", &context, cx, async |resource| { + resource.value.load(SeqCst) + }) + .await + .expect("other resource allowed"); + assert_eq!(value, 5); + assert_eq!(context.obtain_count.get(), 3); + } + + #[gpui::test] + async fn acquisition_failures_are_shared_and_retryable(cx: &mut TestAppContext) { + let store = new_store(cx); + let context = Rc::new(TestContext::default()); + context.fail_next_obtain.set(true); + let (ready, wait) = oneshot::channel(); + context.obtain_ready.set(Some(wait)); + store.update(cx, |store, cx| { + store.set_permission(TestResource::id(&"first"), ResourcePermission::Allowed, cx) + }); + let first = access::(&store, "first", &context, cx, async |_| { + panic!("failed acquisition must not reach the callback") + }); + let second = access::(&store, "first", &context, cx, async |_| { + panic!("failed acquisition must not reach another callback") + }); + cx.run_until_parked(); + assert_eq!(context.obtain_count.get(), 1); + ready.send(()).expect("acquisition pending"); + let Err(ResourceAccessError::Obtain(first_error)) = first.await else { + panic!("expected acquisition failure"); + }; + let Err(ResourceAccessError::Obtain(second_error)) = second.await else { + panic!("expected shared acquisition failure"); + }; + assert!(Arc::ptr_eq(&first_error, &second_error)); + assert_eq!( + first_error.downcast_ref::(), + Some(&TestError::Obtain) + ); + + let value = access::(&store, "first", &context, cx, async |resource| { + TestCommand::Add(4).run(resource).await + }) + .await + .expect("acquisition retried"); + assert_eq!(value, Ok(9)); + let result = access::(&store, "first", &context, cx, async |resource| { + TestCommand::Fail.run(resource).await + }) + .await + .expect("resource cached"); + assert_eq!(result, Err(TestError::Command)); + assert_eq!(context.obtain_count.get(), 2); + } + + #[gpui::test] + async fn concurrent_access_shares_acquisition_without_serializing_callbacks( + cx: &mut TestAppContext, + ) { + let store = new_store(cx); + let context = Rc::new(TestContext::default()); + let (obtain_ready, obtain_wait) = oneshot::channel(); + context.obtain_ready.set(Some(obtain_wait)); + let (command_ready, command_wait) = oneshot::channel(); + let command_started = Rc::new(Cell::new(false)); + store.update(cx, |store, cx| { + store.set_permission(TestResource::id(&"first"), ResourcePermission::Allowed, cx); + store.set_permission( + TestResource::id(&"different"), + ResourcePermission::Allowed, + cx, + ); + }); + + let mut first = access::(&store, "first", &context, cx, { + let command_started = command_started.clone(); + async move |resource| { + command_started.set(true); + command_wait.await.expect("command released"); + resource + } + }); + let mut second = + access::(&store, "first", &context, cx, async |resource| resource); + cx.run_until_parked(); + assert!(poll!(&mut first).is_pending()); + assert!(poll!(&mut second).is_pending()); + assert_eq!(context.obtain_count.get(), 1); + assert!(!command_started.get()); + + let different = + access::(&store, "different", &context, cx, async |resource| { + resource.value.load(SeqCst) + }) + .await + .expect("other resource independent"); + assert_eq!(different, 9); + + obtain_ready.send(()).expect("acquisition pending"); + cx.run_until_parked(); + assert!(command_started.get()); + assert!(poll!(&mut first).is_pending()); + let second = second + .await + .expect("second callback does not wait for first"); + assert_eq!(TestCommand::Add(7).run(second.clone()).await, Ok(12)); + command_ready.send(()).expect("first callback pending"); + let first = first.await.expect("first callback completed"); + assert!(Arc::ptr_eq(&first, &second)); + assert_eq!(first.value.load(SeqCst), 12); + assert_eq!(context.obtain_count.get(), 2); + } + + #[gpui::test] + async fn revocation_blocks_new_callbacks_without_cancelling_running_callbacks( + cx: &mut TestAppContext, + ) { + let store = new_store(cx); + let context = Rc::new(TestContext::default()); + let id = TestResource::id(&"first"); + store.update(cx, |store, cx| { + store.set_permission(id.clone(), ResourcePermission::Allowed, cx) + }); + let (command_ready, command_wait) = oneshot::channel(); + let started = Rc::new(Cell::new(false)); + let running = access::(&store, "first", &context, cx, { + let started = started.clone(); + async move |resource| { + started.set(true); + command_wait.await.expect("command released"); + TestCommand::Add(2).run(resource).await + } + }); + cx.run_until_parked(); + assert!(started.get()); + let waiting = access::(&store, "first", &context, cx, async |_| { + panic!("a callback not yet started must observe revocation") + }); + store.update(cx, |store, cx| { + store.set_permission(id, ResourcePermission::Denied, cx) + }); + command_ready.send(()).expect("command pending"); + assert_eq!(running.await.expect("running callback allowed"), Ok(7)); + assert!(matches!(waiting.await, Err(ResourceAccessError::Denied))); + } + + #[gpui::test] + async fn revocation_during_acquisition_prevents_callbacks(cx: &mut TestAppContext) { + for permission in [Some(ResourcePermission::Denied), None] { + let store = new_store(cx); + let context = Rc::new(TestContext::default()); + let id = TestResource::id(&"first"); + store.update(cx, |store, cx| { + store.set_permission(id.clone(), ResourcePermission::Allowed, cx) + }); + let (obtain_ready, obtain_wait) = oneshot::channel(); + context.obtain_ready.set(Some(obtain_wait)); + let first = access::(&store, "first", &context, cx, async |_| { + panic!("revocation during acquisition must prevent the callback") + }); + let second = access::(&store, "first", &context, cx, async |_| { + panic!("revocation must also prevent concurrent callbacks") + }); + cx.run_until_parked(); + assert_eq!(context.obtain_count.get(), 1); + store.update(cx, |store, cx| match permission { + Some(permission) => store.set_permission(id.clone(), permission, cx), + None => store.clear_permission(&id, cx), + }); + obtain_ready.send(()).expect("acquisition pending"); + assert!(matches!(first.await, Err(ResourceAccessError::Denied))); + assert!(matches!(second.await, Err(ResourceAccessError::Denied))); + + store.update(cx, |store, cx| { + store.set_permission(id, ResourcePermission::Allowed, cx) + }); + let value = + access::(&store, "first", &context, cx, async |resource| { + resource.value.load(SeqCst) + }) + .await + .expect("completed acquisition cached"); + assert_eq!(value, 5); + assert_eq!(context.obtain_count.get(), 1); + } + } + + #[gpui::test] + async fn cancelled_callback_does_not_cancel_shared_acquisition(cx: &mut TestAppContext) { + let store = new_store(cx); + let context = Rc::new(TestContext::default()); + store.update(cx, |store, cx| { + store.set_permission(TestResource::id(&"first"), ResourcePermission::Allowed, cx) + }); + let (obtain_ready, obtain_wait) = oneshot::channel(); + context.obtain_ready.set(Some(obtain_wait)); + let cancelled = access::(&store, "first", &context, cx, async |_| { + panic!("cancelled callback must not run") + }); + cx.run_until_parked(); + assert_eq!(context.obtain_count.get(), 1); + drop(cancelled); + cx.run_until_parked(); + obtain_ready + .send(()) + .expect("shared acquisition still pending"); + cx.run_until_parked(); + let value = access::(&store, "first", &context, cx, async |resource| { + resource.value.load(SeqCst) + }) + .await + .expect("completed acquisition cached without waiters"); + assert_eq!(value, 5); + assert_eq!(context.obtain_count.get(), 1); + } + + #[gpui::test] + async fn conflicting_resource_types_cannot_reuse_an_identity(cx: &mut TestAppContext) { + struct ConflictingResource; + + impl ManagedResource for ConflictingResource { + type Key = (); + type Context = (); + type Command = Self; + + fn id(_: &()) -> ResourceId { + TestResource::id(&"first") + } + + async fn obtain(_: &(), _: &()) -> anyhow::Result { + panic!("a conflicting identity must not be acquired") + } + } + + impl ManagedResourceCommand for ConflictingResource { + type Output = (); + + async fn run(self, _: Arc) {} + } + + let store = new_store(cx); + let context = Rc::new(TestContext::default()); + store.update(cx, |store, cx| { + store.set_permission(TestResource::id(&"first"), ResourcePermission::Allowed, cx) + }); + let first = access::(&store, "first", &context, cx, async |_| ()); + let result = access::(&store, (), &Rc::new(()), cx, async |_| { + panic!("a conflicting resource must not reach the callback") + }) + .await; + assert!(matches!( + result, + Err(ResourceAccessError::ConflictingResourceType) + )); + first.await.expect("original resource allowed"); + } + + #[gpui::test] + async fn approval_is_awaited_and_shared_before_acquisition(cx: &mut TestAppContext) { + for permission in [ResourcePermission::Allowed, ResourcePermission::Denied] { + let (store, approve) = pending_approval_store(cx); + let context = Rc::new(TestContext::default()); + let mut first = + access::(&store, "first", &context, cx, async |resource| resource); + let mut second = + access::(&store, "first", &context, cx, async |resource| resource); + cx.run_until_parked(); + assert!(poll!(&mut first).is_pending()); + assert!(poll!(&mut second).is_pending()); + assert_eq!(context.obtain_count.get(), 0); + assert_eq!( + store.read_with(cx, |store, _| store.permission(&TestResource::id(&"first"))), + None + ); + + store.update(cx, |store, cx| { + store.set_permission( + TestResource::id(&"different"), + ResourcePermission::Allowed, + cx, + ) + }); + let different = + access::(&store, "different", &context, cx, async |resource| { + resource.value.load(SeqCst) + }) + .await + .expect("other resource independent of approval"); + assert_eq!(different, 9); + + approve.send(permission).expect("approval pending"); + match permission { + ResourcePermission::Allowed => { + let first = first.await.expect("approval granted"); + let second = second.await.expect("approval shared"); + assert!(Arc::ptr_eq(&first, &second)); + assert_eq!(first.value.load(SeqCst), 5); + assert_eq!(context.obtain_count.get(), 2); + } + ResourcePermission::Denied => { + assert!(matches!(first.await, Err(ResourceAccessError::Denied))); + assert!(matches!(second.await, Err(ResourceAccessError::Denied))); + assert_eq!(context.obtain_count.get(), 1); + } + } + assert_eq!( + store.read_with(cx, |store, _| store.permission(&TestResource::id(&"first"))), + Some(permission) + ); + } + } + + #[gpui::test] + async fn permission_changes_supersede_pending_approval(cx: &mut TestAppContext) { + for response in [ResourcePermission::Allowed, ResourcePermission::Denied] { + for permission in [ + Some(ResourcePermission::Allowed), + Some(ResourcePermission::Denied), + None, + ] { + let (store, approve) = pending_approval_store(cx); + let context = Rc::new(TestContext::default()); + let id = TestResource::id(&"first"); + let pending = + access::(&store, "first", &context, cx, async |resource| { + resource.value.load(SeqCst) + }); + cx.run_until_parked(); + store.update(cx, |store, cx| match permission { + Some(permission) => store.set_permission(id.clone(), permission, cx), + None => store.clear_permission(&id, cx), + }); + approve.send(response).expect("approval pending"); + if permission == Some(ResourcePermission::Allowed) { + assert_eq!(pending.await.expect("explicit approval wins"), 5); + assert_eq!(context.obtain_count.get(), 1); + } else { + assert!(matches!(pending.await, Err(ResourceAccessError::Denied))); + assert_eq!(context.obtain_count.get(), 0); + } + assert_eq!( + store.read_with(cx, |store, _| store.permission(&id)), + permission + ); + } + } + } + + #[gpui::test] + async fn permission_observers_can_revoke_before_acquisition(cx: &mut TestAppContext) { + let (store, approve) = pending_approval_store(cx); + let context = Rc::new(TestContext::default()); + let observed = Rc::new(Cell::new(None)); + let _subscription = cx.update(|cx| { + cx.observe(&store, { + let observed = observed.clone(); + move |store, cx| { + let id = TestResource::id(&"first"); + let permission = store.read(cx).permission(&id); + observed.set(permission); + if permission == Some(ResourcePermission::Allowed) { + store.update(cx, |store, cx| { + store.set_permission(id, ResourcePermission::Denied, cx); + }); + } + } + }) + }); + let pending = access::(&store, "first", &context, cx, async |_| { + panic!("observer revoked access") + }); + cx.run_until_parked(); + approve + .send(ResourcePermission::Allowed) + .expect("approval pending"); + assert!(matches!(pending.await, Err(ResourceAccessError::Denied))); + assert_eq!(context.obtain_count.get(), 0); + assert_eq!(observed.get(), Some(ResourcePermission::Denied)); + + store.update(cx, |store, cx| { + store.clear_permission(&TestResource::id(&"first"), cx) + }); + assert_eq!(observed.get(), None); + } + + #[gpui::test] + async fn cancelled_callback_does_not_cancel_shared_approval(cx: &mut TestAppContext) { + let (store, approve) = pending_approval_store(cx); + let context = Rc::new(TestContext::default()); + let cancelled = access::(&store, "first", &context, cx, async |_| { + panic!("cancelled callback must not run") + }); + cx.run_until_parked(); + drop(cancelled); + cx.run_until_parked(); + let pending = access::(&store, "first", &context, cx, async |resource| { + resource.value.load(SeqCst) + }); + cx.run_until_parked(); + assert_eq!(context.obtain_count.get(), 0); + approve + .send(ResourcePermission::Allowed) + .expect("approval still pending"); + assert_eq!(pending.await.expect("shared approval granted"), 5); + assert_eq!(context.obtain_count.get(), 1); + } + + #[gpui::test] + async fn dropping_store_prevents_pending_callbacks(cx: &mut TestAppContext) { + let (store, approve) = pending_approval_store(cx); + let weak_store = store.downgrade(); + let context = Rc::new(TestContext::default()); + let pending = access::(&store, "first", &context, cx, async |_| { + panic!("a dropped store must not start a callback") + }); + cx.run_until_parked(); + drop(store); + assert!(weak_store.upgrade().is_none()); + approve + .send(ResourcePermission::Allowed) + .expect("approval pending"); + assert!(matches!( + pending.await, + Err(ResourceAccessError::StoreDropped) + )); + assert_eq!(context.obtain_count.get(), 0); + } +} diff --git a/crates/node_runtime/Cargo.toml b/crates/node_runtime/Cargo.toml index 2f7b29ae291..a9692edec8e 100644 --- a/crates/node_runtime/Cargo.toml +++ b/crates/node_runtime/Cargo.toml @@ -24,6 +24,7 @@ chrono = { workspace = true, default-features = true } futures.workspace = true http_client.workspace = true log.workspace = true +managed_resources.workspace = true paths.workspace = true semver.workspace = true serde.workspace = true diff --git a/crates/node_runtime/src/node_runtime.rs b/crates/node_runtime/src/node_runtime.rs index d9de0165cc9..faa20b29b47 100644 --- a/crates/node_runtime/src/node_runtime.rs +++ b/crates/node_runtime/src/node_runtime.rs @@ -5,6 +5,7 @@ use chrono::{DateTime, Utc}; use futures::{AsyncReadExt, FutureExt as _, channel::oneshot, future::Shared}; use http_client::{Host, HttpClient, Url}; use log::Level; +use managed_resources::{ManagedResource, ManagedResourceCommand, ResourceId}; use semver::{Version, VersionReq}; use serde::Deserialize; use smol::io::BufReader; @@ -43,6 +44,50 @@ pub struct NpmCommand { pub current_dir: PathBuf, } +pub struct Npm { + runtime: NodeRuntime, +} + +impl ManagedResource for Npm { + type Key = (); + type Context = NodeRuntime; + type Command = InstallNpmPackages; + + fn id(_: &()) -> ResourceId { + ResourceId { + kind: "npm".into(), + key: "default".into(), + } + } + + async fn obtain(_: &(), runtime: &NodeRuntime) -> Result { + runtime.run_npm_subcommand(None, "--version", &[]).await?; + Ok(Self { + runtime: runtime.clone(), + }) + } +} + +pub struct InstallNpmPackages { + pub directory: PathBuf, + pub packages: Vec<(String, String)>, +} + +impl ManagedResourceCommand for InstallNpmPackages { + type Output = Result<()>; + + async fn run(self, npm: Arc) -> Self::Output { + let packages = self + .packages + .iter() + .map(|(name, version)| (name.as_str(), version.as_str())) + .collect::>(); + npm.runtime + .npm_install_packages(&self.directory, &packages) + .await + } +} + pub enum VersionStrategy<'a> { /// Install if current version doesn't match pinned version Pin(&'a Version), diff --git a/crates/node_runtime/tests/system_node.rs b/crates/node_runtime/tests/system_node.rs index 758e14226f2..0cc2e5c86df 100644 --- a/crates/node_runtime/tests/system_node.rs +++ b/crates/node_runtime/tests/system_node.rs @@ -7,7 +7,10 @@ #![cfg(unix)] use anyhow::Result; -use node_runtime::{NodeBinaryOptions, NodeDiscoveryError, NodeRuntime, SystemNode}; +use managed_resources::{ManagedResource, ManagedResourceCommand}; +use node_runtime::{ + InstallNpmPackages, NodeBinaryOptions, NodeDiscoveryError, NodeRuntime, Npm, SystemNode, +}; use semver::Version; use std::{ env, fs, @@ -66,6 +69,87 @@ fn configured_runtime_keeps_node_available_when_npm_is_missing() -> Result<()> { ) } +#[test] +fn managed_npm_obtains_and_installs_packages() -> Result<()> { + if env::var_os(CHILD).is_some() { + let directory = env::current_dir()?; + paths::set_custom_data_dir(&directory.join("data").to_string_lossy()); + return smol::block_on(async { + let npm_path = directory.join("npm"); + let runtime = NodeRuntime::new( + Arc::new(http_client::BlockedHttpClient), + None, + watch::channel(Some(NodeBinaryOptions { + use_paths: Some((directory.join("node"), npm_path.clone())), + ..Default::default() + })) + .1, + ); + assert!(Npm::obtain(&(), &runtime).await.is_err()); + + fs::write( + &npm_path, + "#!/bin/sh\nprintf '%s\\n' \"$@\" > \"$0.args\"\npwd -P > \"$0.cwd\"\n", + )?; + fs::set_permissions(&npm_path, fs::Permissions::from_mode(0o755))?; + let npm = Arc::new(Npm::obtain(&(), &runtime).await?); + let arguments = fs::read_to_string(directory.join("npm.args"))?; + assert!(arguments.lines().any(|argument| argument == "--version")); + + let installation_directory = directory.join("language server"); + fs::create_dir(&installation_directory)?; + let install = || InstallNpmPackages { + directory: installation_directory.clone(), + packages: vec![ + ("@example/server".into(), "1.2.3".into()), + ("another-package".into(), "4.5.6".into()), + ], + }; + install().run(npm.clone()).await?; + let arguments = fs::read_to_string(directory.join("npm.args"))?; + let arguments = arguments.lines().collect::>(); + assert!( + arguments.starts_with(&[ + "--prefix", + installation_directory.to_str().expect("UTF-8 test path"), + "install", + ]), + "{arguments:?}" + ); + assert!( + arguments.windows(2).any( + |arguments| arguments == ["@example/server@1.2.3", "another-package@4.5.6"] + ), + "{arguments:?}" + ); + assert_eq!( + fs::read_to_string(directory.join("npm.cwd"))?.trim(), + fs::canonicalize(&installation_directory)?.to_string_lossy() + ); + + fs::write( + &npm_path, + "#!/bin/sh\necho 'installation failed' >&2\nexit 7\n", + )?; + let error = install() + .run(npm) + .await + .expect_err("npm failure propagates"); + assert!(error.to_string().contains("installation failed")); + assert!(Npm::obtain(&(), &runtime).await.is_err()); + Ok(()) + }); + } + + let directory = tempfile::tempdir()?; + fake_node(directory.path(), "printf 'v22.0.0\\n'")?; + run_child_test( + "managed_npm_obtains_and_installs_packages", + directory.path(), + directory.path(), + ) +} + #[test] fn discovers_node_without_npm_and_passes_the_search_path_to_the_probe() -> Result<()> { let directory = tempfile::tempdir()?;