Skip to content

feat: ignore files listed in .mdbookignore during build#3040

Open
jhult wants to merge 4 commits into
rust-lang:masterfrom
jhult:feat/mdbookignore
Open

feat: ignore files listed in .mdbookignore during build#3040
jhult wants to merge 4 commits into
rust-lang:masterfrom
jhult:feat/mdbookignore

Conversation

@jhult

@jhult jhult commented Feb 25, 2026

Copy link
Copy Markdown
Contributor

Rebase/update of #1908 on top of master.

@rustbot rustbot added the S-waiting-on-review Status: waiting on a review label Feb 25, 2026
@jhult

jhult commented Mar 2, 2026

Copy link
Copy Markdown
Contributor Author

@ehuss, I'd appreciate a review when you get a chance.

@jhult jhult force-pushed the feat/mdbookignore branch from cd3130f to a42c81d Compare March 3, 2026 04:13
@rustbot

This comment has been minimized.

@jhult

jhult commented Mar 8, 2026

Copy link
Copy Markdown
Contributor Author

Other related PRs: #2247 and #2991.

@jhult jhult force-pushed the feat/mdbookignore branch from a42c81d to 6059e1b Compare March 16, 2026 19:25
@rustbot

This comment has been minimized.

@jhult jhult force-pushed the feat/mdbookignore branch 2 times, most recently from 553101f to 4bbff36 Compare March 22, 2026 21:46
@rustbot

This comment has been minimized.

jhult added 4 commits April 21, 2026 19:24
Load .mdbookignore file from src directory and use it to filter files copied during the HTML build process.
Add a new function that uses the  crate to filter files during copy operations, supporting gitignore-style patterns.

build: update Cargo.lock for ignore and pathdiff dependency
@jhult jhult force-pushed the feat/mdbookignore branch from 4bbff36 to 9620641 Compare April 22, 2026 00:26
@rustbot

rustbot commented Apr 22, 2026

Copy link
Copy Markdown
Collaborator

This PR was rebased onto a different master commit. Here's a range-diff highlighting what actually changed.

Rebasing is a normal part of keeping PRs up to date, so no action is needed—this note is just to help reviewers.

@jhult

jhult commented May 18, 2026

Copy link
Copy Markdown
Contributor Author

@ehuss - any thoughts on this?

Comment on lines +451 to +456
builder.add_line(None, "/.mdbookignore")?;
builder.add_line(None, "/.gitignore")?;
builder.add_line(None, "/.git/")?;
builder.add_line(None, "/book.toml")?;
builder.add_line(None, "/theme/")?;
builder.add_line(None, "*.md")?;

@ehuss ehuss Jul 2, 2026

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I'm not sure why all of these extra ignores were added. For example, this would break a book that has images or other content inside a directory called theme. Can this be trimmed down to just *.md and .mdbookignore?

View changes since the review

Comment thread tests/testsuite/build.rs
// Directories listed in .mdbookignore should not be copied.
assert!(!test.dir.join("book/ignored_dir").exists());
// Non-ignored files should be copied.
assert!(test.dir.join("book/included.json").exists());

@ehuss ehuss Jul 2, 2026

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

There's some files in the book that this test isn't checking. For example, file.json, custom.css, etc. Can we make sure everything gets checked?

View changes since the review

let mdbook_ignore = src_dir.join(".mdbookignore");
if mdbook_ignore.exists() {
if let Some(err) = builder.add(mdbook_ignore) {
warn!("Unable to load '.mdbookignore' file: {}", err);

@ehuss ehuss Jul 2, 2026

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Why would this be a warning and not an error?

View changes since the review

}

copy_files_except_ext(&entry, &target_file_path, true, avoid_dir, ext_blacklist)?;
copy_files_except_ignored(&entry, &target_file_path, true, avoid_dir, ignore)?;

@ehuss ehuss Jul 2, 2026

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The way this handles relative paths seems to break root-anchored patterns like /subdir/foo.txt.

When descending, from is now the subdirectory (e.g., src/subdir/), but ignore was built with the original root (e.g., src). In the recursive invocation, pathdiff::diff_paths(&entry, from) computes the path relative to the subdirectory, not relative to the gitignore root. So a root-anchored pattern like /subdir/file.txt will never match file.txt once inside subdir/.

I think maybe the fix is to always compute relative paths against ignore.path() (the gitignore's own root), not against from.

Also, can you add some tests that include this scenario?

View changes since the review


if let Some(ignore) = ignore {
let relative_path =
pathdiff::diff_paths(&entry, from).unwrap_or_else(|| entry.to_path_buf());

@ehuss ehuss Jul 2, 2026

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Can you say more why pathdiff is needed? Would it be possible to drop the pathdiff dependency by using entry.strip_prefix(from)? Entries should always be under from, so I'm not sure what scenario this is needed.

View changes since the review

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

S-waiting-on-review Status: waiting on a review

Projects

None yet

Development

Successfully merging this pull request may close these issues.

3 participants