Repository: probe-rs/probe-rs
Pull request #3289 (2025-05-07 01:25:59)

target-gen: support external definitions again
With the recent reorganization of target definitions, the ability to specify an external target definition was lost.

Add new versions of various session initialization functions that support taking an external registry, and use them as part of `target-gen`.


Patch:
diff --git a/changelog/added-session-attach-with-registry.md b/changelog/added-session-attach-with-registry.md
new file mode 100644
index 00000000..19b0f6d4
--- /dev/null
+++ b/changelog/added-session-attach-with-registry.md
@@ -0,0 +1,1 @@
+Added API calls to automatically attach a Session with a provided Registry
diff --git a/probe-rs/src/session.rs b/probe-rs/src/session.rs
index 89b566ba..ee6469f7 100644
--- a/probe-rs/src/session.rs
+++ b/probe-rs/src/session.rs
@@ -435,12 +435,8 @@ impl Session {
         Ok(session)
     }
 
-    /// Automatically creates a session with the first connected probe found.
-    #[tracing::instrument(skip(target))]
-    pub fn auto_attach(
-        target: impl Into<TargetSelector>,
-        session_config: SessionConfig,
-    ) -> Result<Session, Error> {
+    /// Automatically open a probe with the given session config.
+    fn auto_probe(session_config: &SessionConfig) -> Result<Probe, Error> {
         // Get a list of all available debug probes.
         let lister = Lister::new();
 
@@ -462,9 +458,33 @@ impl Session {
         if let Some(protocol) = session_config.protocol {
             probe.select_protocol(protocol)?;
         }
+        Ok(probe)
+    }
+
+    /// Automatically creates a session with the first connected probe found.
+    #[tracing::instrument(skip(target))]
+    pub fn auto_attach(
+        target: impl Into<TargetSelector>,
+        session_config: SessionConfig,
+    ) -> Result<Session, Error> {
+        // Attach to a chip.
+        Self::auto_probe(&session_config)?.attach(target, session_config.permissions)
+    }
 
+    /// Automatically creates a session with the first connected probe found
+    /// using the registry that was provided.
+    #[tracing::instrument(skip(target, registry))]
+    pub fn auto_attach_with_registry(
+        target: impl Into<TargetSelector>,
+        session_config: SessionConfig,
+        registry: &Registry,
+    ) -> Result<Session, Error> {
         // Attach to a chip.
-        probe.attach(target, session_config.permissions)
+        Self::auto_probe(&session_config)?.attach_with_registry(
+            target,
+            session_config.permissions,
+            registry,
+        )
     }
 
     /// Lists the available cores with their number and their type.


Test patch:
diff --git a/target-gen/src/commands/test.rs b/target-gen/src/commands/test.rs
index 0710bba4..cf7d46e0 100644
--- a/target-gen/src/commands/test.rs
+++ b/target-gen/src/commands/test.rs
@@ -98,7 +98,8 @@ pub fn cmd_test(
     };
 
     // We need to get the chip name so that special startup procedure can be used. (matched on name)
-    let mut session = probe_rs::Session::auto_attach(target_name, session_config)?;
+    let mut session =
+        probe_rs::Session::auto_attach_with_registry(target_name, session_config, &registry)?;
 
     // Register callback to update the progress.
     let t = Rc::new(RefCell::new(Instant::now()));