Skip to content

Fix mapnik initialization and plugin discovery - #113

Merged
trasch merged 3 commits into
mainfrom
fix/config-initialization
Nov 2, 2026
Merged

trasch merged 3 commits into
mainfrom
fix/config-initialization

Conversation

@trasch

@trasch trasch commented Nov 2, 2026 •

Copy link
Copy Markdown
Contributor

Summary

MapnikConfig.initialize reported success for paths that registered nothing, cached failures forever, and silently ignored custom paths once the lazy default had run. The Linux plugin discovery also never actually compared versions. This PR fixes all of it.

Initialization

initialize succeeded for nonexistent paths

register_fonts("/nonexistent", true) returns 0 without throwing, and register_datasources does not throw for an empty directory either, so mapnik_init returned true:

try MapnikConfig.initialize(plugins: "/nonexistent/input", fonts: "/nonexistent/fonts")
// returned successfully

let map = try Mapnik(xml: xml)
// Loading style failed: ... no datasource plugin directories have been successfully registered

mapnik_init now checks that each non-empty path exists, is a directory, and actually registers something (at least one .input file; register_fonts returning true).

A failed init poisoned the process permanently

The failure was cached as .failure, so the lazy ensureInitialized path could never retry with the platform defaults. Failures are no longer cached:

1 initialize(bad paths) threw: plugin path is not a directory: '/nonexistent/input'
2 Mapnik(xml:) works after failed init          <- previously failed forever

Custom paths after the lazy default were silently ignored

3 initialize(custom) after init threw: mapnik was already initialized with plugins
  '/usr/lib/.../mapnik/4.2/input' and fonts '/usr/share/fonts'; initialization must
  happen before the first map is created
4 same paths re-init ok

Same paths stay a cheap no-op; different paths now surface the ordering mistake.

Plugin discovery

versionComponents used Int("4.2"), which is nil, so every candidate produced an empty version list and the "highest version wins" logic silently degraded to a lexicographic tiebreak:

4.10 sorted below 4.2     (wrong)
10.0 sorted below 3.1     (wrong)

The version component is now split on . and compared numerically. compareVersionedPaths and highestVersionedDirectory are internal so they can be tested directly, and the cached path moved out of a lazy static let into the lock-protected state with an injectable provider.

pkg-config deadlock

standardError was an undrained Pipe(). A pkg-config that writes more than the pipe buffer to stderr would block forever while readDataToEndOfFile() waited on stdout — hanging the first Mapnik(xml:) in the process. stderr now goes to /dev/null and the pipe is closed in a defer.

Testing

  • PluginDiscoveryTests: version parsing, numeric comparison (4.10 > 4.2, 10.0 > 3.1), unversioned paths, and picking the newest directory from a synthetic layout.
  • MapnikConfigInitializationTests: the process-wide scenarios run in child processes (bad paths throw, a failure does not poison the lazy init, different paths after success throw).
  • Full suite: 135 tests, with only the two pre-existing saveXML(explicitDefaults:) failures remaining — fixed in Honour all render options and fix metatile geometry #111.

Breaking changes

  • initialize throws for a nonexistent/empty plugin or font path instead of silently succeeding.
  • initialize throws when called with different paths after a successful initialization.
  • defaultPluginPath/defaultFontPath are computed properties (same values).

Closes #11
Closes #12
Closes #38
Closes #39
Closes #40

Initialization
- mapnik_init now fails when a plugin or font path does not exist, is not
  a directory, or registers nothing (no *.input files / no usable fonts).
  Previously register_fonts("/nonexistent") returned 0 without throwing,
  so initialize() reported success for paths that registered nothing.
- A failed initialize() is no longer cached. Caching the failure let one
  bad explicit path poison the process: every later Mapnik(xml:) failed
  and the lazy default initialization could never retry.
- Calling initialize() with different paths after a successful
  initialization now throws instead of silently returning, so an
  accidental "too late" call is visible rather than ignored.
- defaultPluginPath/defaultFontPath are computed properties.

Plugin discovery
- versionComponents now splits the version component on ".", so "4.2"
  parses as [4, 2]. It previously used Int("4.2"), which is nil, so every
  candidate produced [] and the "highest version wins" logic silently
  degraded to a lexicographic comparison: 4.10 sorted below 4.2 and 10.0
  below 3.1.
- compareVersionedPaths handles unversioned paths explicitly and is
  internal so it can be tested.
- highestVersionedDirectory is internal for the same reason.
- The cached Linux plugin path moved from a lazy `static let` (which ran
  pkg-config during global initialization and could never be reset) into
  the lock-protected State, with an injectable provider for tests.

pkg-config probing
- stderr goes to /dev/null instead of an undrained Pipe, and the pipe is
  closed in a defer. A chatty pkg-config could previously fill the stderr
  pipe buffer and deadlock the child, hanging the first map creation.

Tests
- PluginDiscoveryTests covers version parsing, numeric comparison
  (4.10 > 4.2, 10.0 > 3.1), unversioned paths, and picking the newest
  plugin directory from a synthetic layout.
- MapnikConfigInitializationTests runs the process-wide initialization
  scenarios in child processes: bad paths throw, a failure does not poison
  the lazy default init, and different paths after success throw.

Fixes #11
Fixes #12
Fixes #38
Fixes #39
Fixes #40
The child-process initialization tests assumed argv[0] is the runnable test
binary, which is true on Linux but not on macOS: there argv[0] is the
.xctest bundle directory and the executable lives in Contents/MacOS/<name>.
The helper now handles both layouts, returns nil (skip) when neither
matches, and the resolution is unit-tested for the bundle layout so it is
covered on Linux CI too.
@trasch trasch self-assigned this Nov 2, 2026
@trasch trasch added the bug Something isn't working label Nov 2, 2026
@trasch
trasch merged commit 0aa1da6 into main Nov 2, 2026
2 checks passed
@trasch
trasch deleted the fix/config-initialization branch November 2, 2026 20:21
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment