Skip to content

Commit 9967d12

Browse files
committed
Index external entities after local entities
The navigation indexing code skips all further nodes after it has already indexed a node with a given identifier [1]. This can result in an odd interaction when there is an external link which is resolved to a path that the bundle serves itself (or more colloquially, a catalog has an external link to itself). Since external entities are indexed before local entities, they were always be preferred over local entities, resulting in local entities mistakenly being dropped from the navigator altogether. The resulting navigator is incorrect and missing nodes, due to external entities not having hierarchy information. This issue was introduced when support for external links in the navigator was introduced. This commit updates `ConvertActionConverter` to always index local entities first. If any external entities clash with local entities, they will be resolved as the local entity (including its hierarchy) in the navigator. Fixes rdar://163018922. [1]: https://github.com/swiftlang/swift-docc/blob/b27288dd99b0e2715ed1a2d5720cd0f23118c030/Sources/SwiftDocC/Indexing/Navigator/NavigatorIndex.swift#L711-L713 [2]: 65aaf92
1 parent d616aa5 commit 9967d12

File tree

2 files changed

+188
-11
lines changed

2 files changed

+188
-11
lines changed

Sources/SwiftDocC/Infrastructure/ConvertActionConverter.swift

Lines changed: 22 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -100,16 +100,7 @@ package enum ConvertActionConverter {
100100

101101
let resultsSyncQueue = DispatchQueue(label: "Convert Serial Queue", qos: .unspecified, attributes: [])
102102
let resultsGroup = DispatchGroup()
103-
104-
// Consume external links and add them into the sidebar.
105-
for externalLink in context.externalCache {
106-
// Here we're associating the external node with the **current** bundle's bundle ID.
107-
// This is needed because nodes are only considered children if the parent and child's bundle ID match.
108-
// Otherwise, the node will be considered as a separate root node and displayed separately.
109-
let externalRenderNode = ExternalRenderNode(externalEntity: externalLink.value, bundleIdentifier: context.inputs.id)
110-
try outputConsumer.consume(externalRenderNode: externalRenderNode)
111-
}
112-
103+
113104
let renderSignpostHandle = signposter.beginInterval("Render", id: signposter.makeSignpostID(), "Render \(context.knownPages.count) pages")
114105

115106
var conversionProblems: [Problem] = context.knownPages.concurrentPerform { identifier, results in
@@ -172,7 +163,27 @@ package enum ConvertActionConverter {
172163
signposter.endInterval("Render", renderSignpostHandle)
173164

174165
guard !Task.isCancelled else { return [] }
175-
166+
167+
// Consumes all external links and adds them into the sidebar.
168+
// This consumes all external links referenced across all content, and indexes them so they're available for reference in the navigator.
169+
// This is not ideal as it means that links outside of the Topics section can impact the content of the navigator.
170+
// TODO: It would be more correct to only index external links which have been curated as part of the Topics section.
171+
//
172+
// This has to run after all local nodes have been indexed because we're associating the external node with the **current** bundle's bundle ID,
173+
// which makes it possible for there be clashes between local and external render nodes.
174+
// When there are duplicate nodes, only the first one will be indexed,
175+
// so in order to prefer local entities whenever there are any clashes, we have to index external nodes second.
176+
// TODO: External render nodes should be associated with the correct bundle identifier.
177+
try signposter.withIntervalSignpost("Index external links", id: signposter.makeSignpostID()) {
178+
for externalLink in context.externalCache {
179+
// Here we're associating the external node with the **current** bundle's bundle ID.
180+
// This is needed because nodes are only considered children if the parent and child's bundle ID match.
181+
// Otherwise, the node will be considered as a separate root node and displayed separately.
182+
let externalRenderNode = ExternalRenderNode(externalEntity: externalLink.value, bundleIdentifier: context.inputs.id)
183+
try outputConsumer.consume(externalRenderNode: externalRenderNode)
184+
}
185+
}
186+
176187
// Write various metadata
177188
if emitDigest {
178189
signposter.withIntervalSignpost("Emit digest", id: signposter.makeSignpostID()) {

Tests/CommandLineTests/ConvertActionTests.swift

Lines changed: 166 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3186,6 +3186,160 @@ class ConvertActionTests: XCTestCase {
31863186
"/images/unit-test/image-name~dark@2x.png"
31873187
])
31883188
}
3189+
3190+
func testExternalLinksInContentDontAffectNavigatorIndex() async throws {
3191+
let temporaryFolder = try createTemporaryDirectory()
3192+
3193+
let resolver: OutOfProcessReferenceResolver
3194+
do {
3195+
let executableLocation = temporaryFolder.appendingPathComponent("link-resolver-executable")
3196+
3197+
let externalBundleID = "com.external.test"
3198+
let externalSummary = LinkDestinationSummary(
3199+
kind: .class,
3200+
language: .swift,
3201+
relativePresentationURL: URL(string: "/documentation/TestBundle/SampleClass")!,
3202+
referenceURL: URL(string: "doc://\(externalBundleID)/documentation/TestBundle/SampleClass")!,
3203+
title: "SampleClass",
3204+
abstract: [.text("External abstract")],
3205+
availableLanguages: [.swift],
3206+
references: [],
3207+
variants: []
3208+
)
3209+
let encodedResponse = try String(decoding: JSONEncoder().encode(OutOfProcessReferenceResolver.ResponseV2.resolved(externalSummary)), as: UTF8.self)
3210+
3211+
try """
3212+
#!/bin/bash
3213+
echo '{"bundleIdentifier":"\(externalBundleID)","capabilities":0}' # Write this resolver's bundle identifier
3214+
read # Wait for docc to send a symbol USR
3215+
echo '\(encodedResponse)' # Respond with the resolved link summary
3216+
""".write(to: executableLocation, atomically: true, encoding: .utf8)
3217+
3218+
// `0o0700` is `-rwx------` (read, write, & execute only for owner)
3219+
try FileManager.default.setAttributes([.posixPermissions: 0o0700], ofItemAtPath: executableLocation.path)
3220+
XCTAssert(FileManager.default.isExecutableFile(atPath: executableLocation.path))
3221+
3222+
resolver = try OutOfProcessReferenceResolver(processLocation: executableLocation, errorOutputHandler: { _ in })
3223+
}
3224+
3225+
let catalog = Folder(name: "unit-test.docc", content: [
3226+
InfoPlist(displayName: "TestBundle", identifier: "com.test.example"),
3227+
TextFile(name: "Article.md", utf8Content: """
3228+
# Article
3229+
3230+
This is an internal article with an external link <doc://com.external.test/documentation/TestBundle/SampleClass> which clashes with the curated local link.
3231+
3232+
External links in content should not affect the navigator.
3233+
3234+
## Topics
3235+
3236+
- ``SampleClass``
3237+
"""),
3238+
TextFile(name: "SampleClass.md", utf8Content: """
3239+
# ``SampleClass``
3240+
3241+
This extends the documentation for this symbol.
3242+
3243+
## Topics
3244+
3245+
- <doc:ChildArticleA>
3246+
- <doc:ChildArticleB>
3247+
"""),
3248+
TextFile(name: "ChildArticleA.md", utf8Content: """
3249+
# ChildArticleA
3250+
3251+
A child article.
3252+
"""),
3253+
TextFile(name: "ChildArticleB.md", utf8Content: """
3254+
# ChildArticleB
3255+
3256+
A child article.
3257+
"""),
3258+
// Symbol graph with a class that matches an external link path
3259+
JSONFile(name: "TestBundle.symbols.json", content: makeSymbolGraph(moduleName: "TestBundle", symbols: [
3260+
makeSymbol(id: "some-symbol-id", language: .swift, kind: .class, pathComponents: ["SampleClass"])
3261+
])),
3262+
])
3263+
3264+
let catalogURL = try catalog.write(inside: temporaryFolder)
3265+
let targetURL = temporaryFolder.appendingPathComponent("Output.doccarchive", isDirectory: true)
3266+
3267+
let action = try ConvertAction(
3268+
documentationBundleURL: catalogURL,
3269+
outOfProcessResolver: resolver,
3270+
analyze: false,
3271+
targetDirectory: targetURL,
3272+
htmlTemplateDirectory: nil,
3273+
emitDigest: false,
3274+
currentPlatforms: nil,
3275+
fileManager: FileManager.default,
3276+
temporaryDirectory: createTemporaryDirectory()
3277+
)
3278+
3279+
let result = try await action.perform(logHandle: .none)
3280+
3281+
XCTAssertEqual(result.outputs, [targetURL])
3282+
3283+
let renderIndexURL = targetURL.appendingPathComponent("index", isDirectory: true)
3284+
.appendingPathComponent("index.json", isDirectory: false)
3285+
3286+
XCTAssertEqual(
3287+
try RenderIndex.fromURL(renderIndexURL),
3288+
try RenderIndex.fromString(
3289+
"""
3290+
{
3291+
"includedArchiveIdentifiers": [
3292+
"com.test.example"
3293+
],
3294+
"interfaceLanguages": {
3295+
"swift": [
3296+
{
3297+
"children": [
3298+
{
3299+
"title": "Articles",
3300+
"type": "groupMarker"
3301+
},
3302+
{
3303+
"children": [
3304+
{
3305+
"children": [
3306+
{
3307+
"path": "/documentation/testbundle/childarticlea",
3308+
"title": "ChildArticleA",
3309+
"type": "article"
3310+
},
3311+
{
3312+
"path": "/documentation/testbundle/childarticleb",
3313+
"title": "ChildArticleB",
3314+
"type": "article"
3315+
}
3316+
],
3317+
"path": "/documentation/testbundle/sampleclass",
3318+
"title": "SampleClass",
3319+
"type": "class"
3320+
}
3321+
],
3322+
"path": "/documentation/testbundle/article",
3323+
"title": "Article",
3324+
"type": "symbol"
3325+
}
3326+
],
3327+
"path": "/documentation/testbundle",
3328+
"title": "TestBundle",
3329+
"type": "module"
3330+
}
3331+
]
3332+
},
3333+
"schemaVersion": {
3334+
"major": 0,
3335+
"minor": 1,
3336+
"patch": 2
3337+
}
3338+
}
3339+
"""
3340+
)
3341+
)
3342+
}
31893343

31903344
#endif
31913345
}
@@ -3280,3 +3434,15 @@ private extension ConvertAction {
32803434
return try await perform(logHandle: &logHandle)
32813435
}
32823436
}
3437+
3438+
extension RenderIndex {
3439+
static func fromString(_ string: String) throws -> RenderIndex {
3440+
let decoder = JSONDecoder()
3441+
return try decoder.decode(RenderIndex.self, from: Data(string.utf8))
3442+
}
3443+
3444+
static func fromURL(_ url: URL) throws -> RenderIndex? {
3445+
let data = try Data(contentsOf: url)
3446+
return try JSONDecoder().decode(RenderIndex.self, from: data)
3447+
}
3448+
}

0 commit comments

Comments
 (0)