diff --git a/src/librustdoc/html/static/js/source-script.js b/src/librustdoc/html/static/js/source-script.js
index 1e9bfa5cc8959..a6a0b09ef31fe 100644
--- a/src/librustdoc/html/static/js/source-script.js
+++ b/src/librustdoc/html/static/js/source-script.js
@@ -33,7 +33,7 @@ function createDirEntry(elem, parent, fullPath, hasFoundFile) {
folders.className = "folders";
if (elem.dirs) {
for (const dir of elem.dirs) {
- if (createDirEntry(dir, folders, fullPath, hasFoundFile)) {
+ if (createDirEntry(dir, folders, fullPath, false)) {
dirEntry.open = true;
hasFoundFile = true;
}
diff --git a/src/test/rustdoc-gui/sidebar-source-code.goml b/src/test/rustdoc-gui/sidebar-source-code.goml
index 86df478fa1dd2..e882080c7daba 100644
--- a/src/test/rustdoc-gui/sidebar-source-code.goml
+++ b/src/test/rustdoc-gui/sidebar-source-code.goml
@@ -16,15 +16,27 @@ click: (10, 10)
wait-for: "html:not(.expanded)"
assert: "nav.sidebar"
+// Checking that only the path to the current file is "open".
+goto: file://|DOC_PATH|/src/lib2/another_folder/sub_mod/mod.rs.html
+// First we expand the sidebar again.
+click: (10, 10)
+// We wait for the sidebar to be expanded.
+wait-for-css: (".source-sidebar-expanded nav.sidebar", {"width": "300px"})
+assert: "//*[@class='dir-entry' and @open]/*[text()='lib2']"
+assert: "//*[@class='dir-entry' and @open]/*[text()='another_folder']"
+assert: "//*[@class='dir-entry' and @open]/*[text()='sub_mod']"
+// Only "another_folder" should be "open" in "lib2".
+assert: "//*[@class='dir-entry' and not(@open)]/*[text()='another_mod']"
+// All other trees should be collapsed.
+assert-count: ("//*[@id='source-sidebar']/details[not(text()='lib2') and not(@open)]", 5)
+
// We now switch to mobile mode.
size: (600, 600)
-// We check that the sidebar has the expected width (0).
-assert-css: ("nav.sidebar", {"width": "0px"})
-// We expand the sidebar.
-click: "#sidebar-toggle"
-assert-css: (".source-sidebar-expanded nav.sidebar", {"width": "600px"})
+wait-for-css: (".source-sidebar-expanded nav.sidebar", {"width": "600px"})
// We collapse the sidebar.
click: (10, 10)
+// We check that the sidebar has the expected width (0).
+assert-css: ("nav.sidebar", {"width": "0px"})
// We ensure that the class has been removed.
assert-false: ".source-sidebar-expanded"
assert: "nav.sidebar"
diff --git a/src/test/rustdoc-gui/src/lib2/another_folder/mod.rs b/src/test/rustdoc-gui/src/lib2/another_folder/mod.rs
new file mode 100644
index 0000000000000..ec9a20859243d
--- /dev/null
+++ b/src/test/rustdoc-gui/src/lib2/another_folder/mod.rs
@@ -0,0 +1,3 @@
+pub fn another_fn() {}
+
+pub mod sub_mod;
diff --git a/src/test/rustdoc-gui/src/lib2/another_folder/sub_mod/mod.rs b/src/test/rustdoc-gui/src/lib2/another_folder/sub_mod/mod.rs
new file mode 100644
index 0000000000000..f16722cf35b7b
--- /dev/null
+++ b/src/test/rustdoc-gui/src/lib2/another_folder/sub_mod/mod.rs
@@ -0,0 +1 @@
+pub fn subsubsub() {}
diff --git a/src/test/rustdoc-gui/src/lib2/another_mod/mod.rs b/src/test/rustdoc-gui/src/lib2/another_mod/mod.rs
new file mode 100644
index 0000000000000..9a4f007a2f0ca
--- /dev/null
+++ b/src/test/rustdoc-gui/src/lib2/another_mod/mod.rs
@@ -0,0 +1 @@
+pub fn tadam() {}
diff --git a/src/test/rustdoc-gui/src/lib2/lib.rs b/src/test/rustdoc-gui/src/lib2/lib.rs
index d06b46f952d0e..4546449e10291 100644
--- a/src/test/rustdoc-gui/src/lib2/lib.rs
+++ b/src/test/rustdoc-gui/src/lib2/lib.rs
@@ -2,6 +2,9 @@
#![feature(doc_cfg)]
+pub mod another_folder;
+pub mod another_mod;
+
pub mod module {
pub mod sub_module {
pub mod sub_sub_module {