Skip to content

Commit 7d6ec67

Browse files
authored
Rollup merge of rust-lang#145359 - GuillaumeGomez:correctly-pick-search.js, r=lolbinarycat
Fix bug where `rustdoc-js` tester would not pick the right `search.js` file if there is more than one It happened to me quite a few times recently when I worked on the search index: 1. I make a change in search.js 2. I run `rustdoc-js` tests 3. nothing changes So my solution was to simply remove the folder, but it's really suboptimal. With this PR, it now picks the most recently modified file. cc ``@lolbinarycat``
2 parents 812f0a3 + 826ebde commit 7d6ec67

File tree

1 file changed

+19
-1
lines changed

1 file changed

+19
-1
lines changed

src/tools/rustdoc-js/tester.js

Lines changed: 19 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -409,6 +409,24 @@ async function runChecks(testFile, doSearch, parseQuery, getCorrections) {
409409
return res;
410410
}
411411

412+
function mostRecentMatch(staticFiles, regex) {
413+
const matchingEntries = fs.readdirSync(staticFiles)
414+
.filter(f => f.match(regex))
415+
.map(f => {
416+
const stats = fs.statSync(path.join(staticFiles, f));
417+
return {
418+
path: f,
419+
time: stats.mtimeMs,
420+
};
421+
});
422+
if (matchingEntries.length === 0) {
423+
throw "No static file matching regex";
424+
}
425+
// We sort entries in descending order.
426+
matchingEntries.sort((a, b) => b.time - a.time);
427+
return matchingEntries[0].path;
428+
}
429+
412430
/**
413431
* Load searchNNN.js and search-indexNNN.js.
414432
*
@@ -452,7 +470,7 @@ function loadSearchJS(doc_folder, resource_suffix) {
452470
};
453471

454472
const staticFiles = path.join(doc_folder, "static.files");
455-
const searchJs = fs.readdirSync(staticFiles).find(f => f.match(/search.*\.js$/));
473+
const searchJs = mostRecentMatch(staticFiles, /search-[0-9a-f]{8}.*\.js$/);
456474
const searchModule = require(path.join(staticFiles, searchJs));
457475
searchModule.initSearch(searchIndex.searchIndex);
458476
const docSearch = searchModule.docSearch;

0 commit comments

Comments
 (0)