feat: ignore glob in sourcemap commands (#36366)

This commit is contained in:
David Newell
2025-08-15 12:45:30 +01:00
committed by GitHub
parent cdf70250c4
commit 4f24048dbe
7 changed files with 69 additions and 10 deletions

24
cli/Cargo.lock generated
View File

@@ -194,6 +194,16 @@ dependencies = [
"generic-array",
]
[[package]]
name = "bstr"
version = "1.12.0"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "234113d19d0d7d613b40e86fb654acf958910802bcceab913a4f9e7cda03b1a4"
dependencies = [
"memchr",
"serde",
]
[[package]]
name = "bumpalo"
version = "3.19.0"
@@ -719,6 +729,19 @@ version = "0.31.1"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "07e28edb80900c19c28f1072f2e8aeca7fa06b23cd4169cefe1af5aa3260783f"
[[package]]
name = "globset"
version = "0.4.16"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "54a1028dfc5f5df5da8a56a73e6c153c9a9708ec57232470703592a3f18e49f5"
dependencies = [
"aho-corasick",
"bstr",
"log",
"regex-automata 0.4.9",
"regex-syntax 0.8.5",
]
[[package]]
name = "h2"
version = "0.3.27"
@@ -1480,6 +1503,7 @@ dependencies = [
"clap",
"crossterm 0.28.1",
"dirs",
"globset",
"inquire",
"magic_string",
"miette",

View File

@@ -24,6 +24,7 @@ dirs = "6.0.0"
inquire = "0.7.5"
posthog-symbol-data = "0.1.0"
walkdir = "2.5.0"
globset = "0.4"
ratatui = "0.29.0"
crossterm = "0.28.1"
tui-textarea = "0.7.0"

View File

@@ -45,6 +45,10 @@ pub enum SourcemapCommand {
/// The directory containing the bundled chunks
#[arg(short, long)]
directory: PathBuf,
/// One or more directory glob patterns to ignore
#[arg(short, long)]
ignore: Vec<String>,
},
/// Upload the bundled chunks to PostHog
Upload {
@@ -52,6 +56,10 @@ pub enum SourcemapCommand {
#[arg(short, long)]
directory: PathBuf,
/// One or more directory glob patterns to ignore
#[arg(short, long)]
ignore: Vec<String>,
/// The project name associated with the uploaded chunks. Required to have the uploaded chunks associated with
/// a specific release, auto-discovered from git information on disk if not provided.
#[arg(long)]
@@ -99,11 +107,12 @@ impl Cli {
login::login()?;
}
Commands::Sourcemap { cmd } => match cmd {
SourcemapCommand::Inject { directory } => {
sourcemap::inject::inject(directory)?;
SourcemapCommand::Inject { directory, ignore } => {
sourcemap::inject::inject(directory, ignore)?;
}
SourcemapCommand::Upload {
directory,
ignore,
project,
version,
delete_after,
@@ -111,6 +120,7 @@ impl Cli {
sourcemap::upload::upload(
command.host,
directory,
ignore,
project.clone(),
version.clone(),
*delete_after,

View File

@@ -5,7 +5,7 @@ use uuid;
use crate::utils::{posthog::capture_command_invoked, sourcemaps::read_pairs};
pub fn inject(directory: &Path) -> Result<()> {
pub fn inject(directory: &Path, ignore_globs: &[String]) -> Result<()> {
let capture_handle = capture_command_invoked("sourcemap_inject", None::<&str>);
let directory = directory.canonicalize().map_err(|e| {
anyhow!(
@@ -15,7 +15,7 @@ pub fn inject(directory: &Path) -> Result<()> {
)
})?;
info!("Processing directory: {}", directory.display());
let mut pairs = read_pairs(&directory)?;
let mut pairs = read_pairs(&directory, ignore_globs)?;
if pairs.is_empty() {
bail!("No source files found");
}

View File

@@ -47,6 +47,7 @@ struct BulkUploadFinishRequest {
pub fn upload(
host: Option<String>,
directory: &PathBuf,
ignore_globs: &[String],
project: Option<String>,
version: Option<String>,
delete_after: bool,
@@ -61,7 +62,7 @@ pub fn upload(
host, token.env_id
);
let pairs = read_pairs(directory)?;
let pairs = read_pairs(directory, ignore_globs)?;
let sourcemap_paths = pairs
.iter()
.map(|pair| pair.sourcemap.path.clone())

View File

@@ -1,5 +1,6 @@
use anyhow::{anyhow, bail, Ok, Result};
use core::str;
use globset::{Glob, GlobSetBuilder};
use magic_string::{GenerateDecodedMapOptions, MagicString};
use posthog_symbol_data::{write_symbol_data, SourceAndMap};
use serde::{Deserialize, Serialize};
@@ -152,16 +153,28 @@ impl SourcePair {
}
}
pub fn read_pairs(directory: &PathBuf) -> Result<Vec<SourcePair>> {
pub fn read_pairs(directory: &PathBuf, ignore_globs: &[String]) -> Result<Vec<SourcePair>> {
// Make sure the directory exists
if !directory.exists() {
bail!("Directory does not exist");
}
let mut builder = GlobSetBuilder::new();
for glob in ignore_globs {
builder.add(Glob::new(&glob)?);
}
let set: globset::GlobSet = builder.build()?;
let mut pairs = Vec::new();
for entry in WalkDir::new(directory).into_iter().filter_map(|e| e.ok()) {
let entry_path = entry.path().canonicalize()?;
if is_javascript_file(&entry_path) {
if set.is_match(&entry_path) {
info!(
"Skipping because it matches an ignored glob: {}",
entry_path.display()
);
} else if is_javascript_file(&entry_path) {
info!("Processing file: {}", entry_path.display());
let source = SourceFile::load(&entry_path)?;
let sourcemap_path = get_sourcemap_path(&source)?;

View File

@@ -19,14 +19,24 @@ fn assert_file_eq(base_path: &Path, path: &str, actual: &str) {
#[test]
fn test_search() {
let pairs = read_pairs(&get_case_path("search")).expect("Failed to read pairs");
let pairs = read_pairs(&get_case_path("search"), &Vec::new()).expect("Failed to read pairs");
assert_eq!(pairs.len(), 2);
}
#[test]
fn test_ignore() {
let pairs = read_pairs(&get_case_path(""), &Vec::new()).expect("Failed to read pairs");
assert_eq!(pairs.len(), 4);
let pairs = read_pairs(&get_case_path(""), &vec!["**/search/**".to_string()])
.expect("Failed to read pairs");
assert_eq!(pairs.len(), 2);
}
#[test]
fn test_pair_inject() {
let case_path = get_case_path("inject");
let mut pairs = read_pairs(&case_path).expect("Failed to read pairs");
let mut pairs = read_pairs(&case_path, &Vec::new()).expect("Failed to read pairs");
assert_eq!(pairs.len(), 1);
let current_pair = pairs.first_mut().expect("Failed to get first pair");
let chunk_id = "00000-00000-00000";
@@ -49,7 +59,7 @@ fn test_pair_inject() {
#[test]
fn test_index_inject() {
let case_path = get_case_path("index_map");
let mut pairs = read_pairs(&case_path).expect("Failed to read pairs");
let mut pairs = read_pairs(&case_path, &Vec::new()).expect("Failed to read pairs");
let current_pair = pairs.first_mut().expect("Failed to get first pair");
let chunk_id = "00000-00000-00000";
current_pair