Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
11 changes: 9 additions & 2 deletions crates/ignore/src/gitignore.rs
Original file line number Diff line number Diff line change
Expand Up @@ -702,14 +702,14 @@ fn parse_excludes_file(data: &[u8]) -> Option<PathBuf> {
Regex::builder()
.configure(Regex::config().utf8_empty(false))
.syntax(syntax::Config::new().utf8(false))
.build(r#"(?im-u)^\s*excludesfile\s*=\s*"?\s*(\S+?)\s*"?\s*$"#)
.build(r#"(?im-u)^\s*excludesfile\s*=\s*(?:"(\S(?:[^"\r\n]*\S)?)"|(\S+))\s*$"#)
.unwrap()
});
// We don't care about amortizing allocs here I think. This should only
// be called ~once per traversal or so? (Although it's not guaranteed...)
let mut caps = re.create_captures();
re.captures(data, &mut caps);
let span = caps.get_group(1)?;
let span = caps.get_group(1).or_else(|| caps.get_group(2))?;
let candidate = &data[span];
std::str::from_utf8(candidate).ok().map(|s| PathBuf::from(expand_tilde(s)))
}
Expand Down Expand Up @@ -879,6 +879,13 @@ mod tests {
);
}

#[test]
fn parse_excludes_file_with_spaces() {
let data = bytes("[core]\nexcludesFile = \"~/foo bar/ignore\"");
let got = super::parse_excludes_file(&data).unwrap();
assert_eq!(path_string(got), super::expand_tilde("~/foo bar/ignore"));
}

#[test]
fn parse_excludes_file5() {
let data = bytes("[core]\nexcludesFile = \" \"~/foo/bar \" \"");
Expand Down
5 changes: 3 additions & 2 deletions crates/oxide/src/scanner/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -781,8 +781,8 @@ fn create_walker(sources: &Sources) -> Option<WalkBuilder> {
// Scan hidden files / directories
builder.hidden(false);

// Don't respect global gitignore files
builder.git_global(false);
// Respect global gitignore files configured in git (e.g. `core.excludesFile`)
builder.git_global(true);
Comment thread
greptile-apps[bot] marked this conversation as resolved.

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.

P1 Global ignores use wrong directory

Global ignore patterns containing / are matched from the process working directory, not the scanned repository. If the CLI scans /projects/app from another directory, a global rule src/ignored.html does not exclude /projects/app/src/ignored.html. The scanner still reads that file and generates CSS from it, even though Git ignores it.

Build the global matcher relative to each repository root rather than the process working directory.


// By default, allow .gitignore files to be used regardless of whether or not
// a .git directory is present. This is an optimization for when projects
Expand Down Expand Up @@ -822,6 +822,7 @@ fn create_walker(sources: &Sources) -> Option<WalkBuilder> {
for parent in first_root?.ancestors() {
if parent.join(".git").exists() {
builder.require_git(true);
builder.current_dir(parent);
break;
}
}
Expand Down
61 changes: 60 additions & 1 deletion crates/oxide/tests/scanner.rs
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ mod scanner {
use std::process::Command;
use std::thread::sleep;
use std::time::Duration;
use std::{fs, path};
use std::{env, fs, path};

use tailwindcss_oxide::*;
use tempfile::tempdir;
Expand Down Expand Up @@ -2923,6 +2923,65 @@ mod scanner {
);
}

#[test]
fn respects_git_core_excludes_file() {
if env::var_os("TAILWIND_OXIDE_CORE_EXCLUDES_CHILD").is_none() {
let status = Command::new(env::current_exe().unwrap())
.args(["--exact", "scanner::respects_git_core_excludes_file"])
.env("TAILWIND_OXIDE_CORE_EXCLUDES_CHILD", "1")
.status()
.unwrap();
assert!(status.success());
return;
}

let dir = tempdir().unwrap().into_path();

let global_ignore = dir.join("global ignore");
fs::write(&global_ignore, "ignored-by-global.html\nsrc/ignored-relative.html\n").unwrap();

let global_gitconfig = dir.join(".gitconfig");
fs::write(
&global_gitconfig,
format!(
"[core]\n excludesFile = \"{}\"\n",
global_ignore.display()
),
)
.unwrap();

let repo_dir = dir.join("repo");
fs::create_dir_all(&repo_dir).unwrap();
_ = Command::new("git").arg("init").current_dir(&repo_dir).output();

create_files_in(
&repo_dir,
&[
("src/index.html", "content-['index.html']"),
(
"src/ignored-by-global.html",
"content-['ignored-by-global.html']",
),
(
"src/ignored-relative.html",
"content-['ignored-relative.html']",
),
],
);

let sources = vec![public_source_entry_from_pattern(
repo_dir.clone(),
"@source '**/*'",
)];

env::set_var("GIT_CONFIG_GLOBAL", &global_gitconfig);

let mut scanner = Scanner::new(sources);
let candidates = scanner.scan();

assert_eq!(candidates, vec!["content-['index.html']"]);
}

#[test]
fn test_explicitly_ignore_explicitly_allowed_files() {
// Create a temporary working directory
Expand Down