← Back to Pull requests

[SwiftExtract] Preserve source file path for deferred (out-of-order) extensions #924

Closed
unknown closed this pull request without merging on Sep 25, 2026 09:00 UTC
View on GitHub
unknown unknown commented on Sep 24, 2026 18:47 UTC
Author

Problem

jextract's symbol table builder processes source files in a single pass, in
whatever order FileManager enumerates the input directory. If an
extension Outer.Inner { ... } is visited before the file that declares
Outer (or Inner), its extended type can't be resolved yet, so it's queued
in unresolvedExtensions and retried in finalize().

When finalize() resolves a deferred extension, it currently hardcodes the
source file path as "FIXME_MISSING_FILEPATH.swift"
(SwiftParsedModuleSymbolTableBuilder.swift, with a // FIXME comment
already marking this).

Because the JNI thunk printer groups generated thunks by source file
(writeSwiftThunkSources), everything with this fake path lands in a single
FIXME_MISSING_FILEPATH+SwiftJava.swift. The JExtractSwiftPlugin only
declares the real per-source-file +SwiftJava.swift outputs to SwiftPM, so
this file is never compiled. The generated Java bindings are correct (the
Java class exists with all its methods, including native declarations), but
the corresponding Swift/JNI implementations are silently missing from the
built library — this fails at runtime with UnsatisfiedLinkError /
"No implementation found for ..." instead of at build time.

This is order-dependent, so it's easy to hit by accident: on a case-insensitive
APFS volume, FileManager's directory enumeration order is filename-hash
based, not alphabetical, so whether an extension's file is read before or
after its extended type's declaring file depends on unrelated filenames
elsewhere in the module. For example, extension Outer.Inner { struct X {} }
in Feature.swift and enum Outer { enum Inner {} } in Core.swift work
fine as long as Core.swift happens to be enumerated first — flip that
order (as can happen simply by adding or renaming unrelated files in the
same directory) and X's JNI thunks silently disappear from the build.

Fix

1. SwiftParsedModuleSymbolTableBuilder.unresolvedExtensions now stores the
sourceFilePath alongside each deferred ExtensionDeclSyntax, and
finalize() passes that real path through to tryHandle(extension:sourceFilePath:)
instead of the placeholder string. Deferred extensions are now attributed
to their actual source file like any other declaration, so their JNI thunks
land in the correct, plugin-declared output file and actually get compiled.

2. collectAllFiles now sorts the files gathered from each input path before
returning them. Enumeration order was previously whatever the filesystem
handed back (non-deterministic across machines/filesystems for the reasons
above); sorting makes extraction output reproducible. This alone doesn't
fix the bug (many still-valid orderings would still defer extensions
declared in files before their type's declaring file), but it removes
filesystem/OS as a source of flakiness on top of fix (1).

Testing

Added deferredExtensionKeepsSourceFilePath to
Tests/SwiftExtractTests/SwiftSymbolTableTests.swift: it feeds the symbol
table an extension file before the file declaring the extended type (mirroring
the real bug), then asserts the nested type's sourceFilePath matches the
extension's actual file rather than the fixed fallback.

Ran the full SwiftExtractTests suite (swift test --filter SwiftExtractTests):
91 tests, 11 suites, all passing.

Verified end-to-end against a real multi-target Swift package with a
JExtractSwiftPlugin-driven Android build: before the fix, HomeFeed's
JNI thunks (and everything else in the affected files) were generated into
FIXME_MISSING_FILEPATH+SwiftJava.swift and silently dropped from the build;
after the fix they're written to their real per-file output and the resulting
.so exports the expected JNI symbols.

Diff: 480 lines (+360 / -120) across 6 files Reviewer: ktoso Review duration: 852m
All Vouch review confidence checks passed
Residual risk 0.19 = Change Risk (0.47) × (1 − Review Confidence (0.60)). Review depth and reviewer attention state are adequate.
Model 1 · Change Risk (XGBoost)
Score: 0.47 · 480 lines diff across 6 files
0.47
Model 2 · Review Depth (DistilBERT)
Depth Score: 0.17 · 1 substantive comment(s)
0.17
Model 3 · Review Timing & Attention
Session adequacy: 100% · Reviewer familiarity: 35%
60%
Bedrock Claude 3 · Risk Narration
(disconnected)
Groq
Groq Risk Narration
AWS Bedrock is currently disconnected. Generate high-confidence PR triage narration on-demand using Groq.
Model 1 · Change Risk Attribution
SHAP Weights
Diff Size diff_size
0.369
Shallow Review Depth shallow_review_depth
0.289
Real PR Conversation Timeline
1 comments
"Review active on #924"
general weight: 0.35
Reviewers
Assignees
Unassigned
Labels
risk: low confidence: 60.5% reviewed
Vouch Risk Breakdown
AI-powered review risk analysis
Change Risk
Change Risk · Model 1 (XGBoost)
Predicted defect probability (0.00–1.00) based on diff churn, modified sensitive files, and author risk history.
Score: 47.3% (MEDIUM) · Higher = larger blast radius
47.3%
Review Confidence
Review Confidence · Models 2 & 3
Scrutiny score (0.00–1.00) combining DistilBERT semantic review depth with reviewer familiarity and session dwell time.
Score: 60.5% · Lower = rushed or shallow review
60.5%
Residual Risk
Residual Risk · Core Equation
Residual = Change × (1 − Confidence)
Unmitigated risk surviving review. ≥ 0.65 triggers an automated re-queue to protect production.
Score: 18.7% · Safe to merge
18.7%