Skip to content

Commit

Permalink
Fix PluginLibrary::library_path on macOS
Browse files Browse the repository at this point in the history
This supersedes #13;
  • Loading branch information
robbert-vdh committed Mar 24, 2023
1 parent 189c440 commit af3af43
Show file tree
Hide file tree
Showing 4 changed files with 18 additions and 14 deletions.
1 change: 1 addition & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@ Versioning](https://semver.org/spec/v2.0.0.html).

### Fixed

- Fixed running tests out of process on macOS.
- The path passed to `clap_entry::init()` now points to the bundle on macOS,
rather than the DSO.
- The `--verbosity` option's value now propagated to child processes when tests
Expand Down
25 changes: 14 additions & 11 deletions src/plugin/library.rs
Original file line number Diff line number Diff line change
Expand Up @@ -24,9 +24,9 @@ use crate::util::{self, unsafe_clap_call};
/// all plugins exposed by the library and to initialize plugins.
#[derive(Debug)]
pub struct PluginLibrary {
/// The path to this plugin library. On macOS, this points to the bundle's root instead of the
/// library contained within the bundle.
library_path: PathBuf,
/// The path to this plugin. On macOS, this points to the bundle's root instead of the library
/// contained within the bundle.
plugin_path: PathBuf,
/// The plugin's library. Its entry point has already been initialized, and it will
/// autoamtically be deinitialized when this object gets dropped.
library: libloading::Library,
Expand Down Expand Up @@ -121,23 +121,26 @@ impl PluginLibrary {
.context("Path contains null bytes")?;

// NOTE: Apple says you can dlopen() bundles. This is a lie.
#[cfg(not(target_os = "macos"))]
let library = load(&path)?;
#[cfg(target_os = "macos")]
let path = {
let library = {
use core_foundation::bundle::CFBundle;
use core_foundation::url::CFURL;

let bundle =
CFBundle::new(CFURL::from_path(path, true).context("Could not create CFURL")?)
CFBundle::new(CFURL::from_path(&path, true).context("Could not create CFURL")?)
.context("Could not open bundle")?;
let executable = bundle
.executable_url()
.context("Could not get executable URL within bundle")?;

executable
let library_path = executable
.to_path()
.context("Could not convert bundle executable path")?
.context("Could not convert bundle executable path")?;

load(&library_path)?
};
let library = load(&path)?;

// The entry point needs to be initialized before it can be used. It will be deinitialized
// when the `Plugin` object is dropped.
Expand All @@ -147,13 +150,13 @@ impl PluginLibrary {
}

Ok(PluginLibrary {
library_path: path,
plugin_path: path,
library,
})
}

pub fn library_path(&self) -> &Path {
&self.library_path
pub fn plugin_path(&self) -> &Path {
&self.plugin_path
}

/// Get the metadata for all plugins stored in this plugin library. Most plugin libraries
Expand Down
2 changes: 1 addition & 1 deletion src/tests/plugin.rs
Original file line number Diff line number Diff line change
Expand Up @@ -138,7 +138,7 @@ impl<'a> TestCase<'a> for PluginTestCase {
.unwrap()
.get_name(),
)
.arg(library.library_path())
.arg(library.plugin_path())
.arg(plugin_id)
.arg(test_name);
}
Expand Down
4 changes: 2 additions & 2 deletions src/tests/plugin/descriptor.rs
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@ pub fn test_consistency(library: &PluginLibrary, plugin_id: &str) -> Result<Test
let metadata = library.metadata().with_context(|| {
format!(
"Could not fetch plugin metadata for '{}'",
library.library_path().display()
library.plugin_path().display()
)
})?;
let factory_descriptor = metadata
Expand Down Expand Up @@ -99,7 +99,7 @@ fn plugin_features(library: &PluginLibrary, plugin_id: &str) -> Result<Vec<Strin
.with_context(|| {
format!(
"Could not fetch plugin metadata for '{}'",
library.library_path().display()
library.plugin_path().display()
)
})
.and_then(|metadata| {
Expand Down

0 comments on commit af3af43

Please sign in to comment.