Skip to content

Commit a8feea8

Browse files
authored
Rollup merge of #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 7b7ad4d + 2ebe679 commit a8feea8

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-[0-9a-f]{8}.*\.js$/);
423441
const searchModule = require(path.join(staticFiles, searchJs));
424442
globalThis.nonnull = (x, msg) => {
425443
if (x === null) {

0 commit comments

Comments
 (0)