Skip to content

Commit 77d6ee0

Browse files
Fix bug where rustdoc-js tester would not pick the right search.js file if there is more than one
1 parent 8365fcb commit 77d6ee0

File tree

1 file changed

+20
-2
lines changed

1 file changed

+20
-2
lines changed

src/tools/rustdoc-js/tester.js

Lines changed: 20 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -405,6 +405,24 @@ async function runChecks(testFile, doSearch, parseQuery) {
405405
return res;
406406
}
407407

408+
function mostRecentMatch(staticFiles, regex) {
409+
const matchingEntries = fs.readdirSync(staticFiles)
410+
.filter(f => f.match(regex))
411+
.map(f => {
412+
const stats = fs.statSync(path.join(staticFiles, f));
413+
return {
414+
path: f,
415+
time: stats.mtimeMs,
416+
};
417+
});
418+
if (matchingEntries.length === 0) {
419+
throw "No static file matching regex";
420+
}
421+
// We sort entries in descending order.
422+
matchingEntries.sort((a, b) => b.time - a.time);
423+
return matchingEntries[0].path;
424+
}
425+
408426
/**
409427
* Load searchNNN.js and search-indexNNN.js.
410428
*
@@ -417,9 +435,9 @@ async function runChecks(testFile, doSearch, parseQuery) {
417435
*/
418436
async function loadSearchJS(doc_folder, resource_suffix) {
419437
const staticFiles = path.join(doc_folder, "static.files");
420-
const stringdexJs = fs.readdirSync(staticFiles).find(f => f.match(/stringdex.*\.js$/));
438+
const stringdexJs = mostRecentMatch(staticFiles, /stringdex.*\.js$/);
421439
const stringdexModule = require(path.join(staticFiles, stringdexJs));
422-
const searchJs = fs.readdirSync(staticFiles).find(f => f.match(/search.*\.js$/));
440+
const searchJs = mostRecentMatch(staticFiles, /search.*\.js$/);
423441
const searchModule = require(path.join(staticFiles, searchJs));
424442
globalThis.nonnull = (x, msg) => {
425443
if (x === null) {

0 commit comments

Comments
 (0)