Skip to content

Commit 1411b8c

Browse files
Refactor: Improve ignore pattern handling for sourcemaps
This change updates the ignore pattern matching logic to correctly handle patterns relative to the current working directory. It also adds new integration tests to verify this behavior for both `sourcemaps inject` and `sourcemaps upload` commands. Co-authored-by: daniel.szoke <[email protected]>
1 parent 15dccc3 commit 1411b8c

File tree

17 files changed

+135
-11
lines changed

17 files changed

+135
-11
lines changed

src/commands/sourcemaps/inject.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -35,7 +35,7 @@ pub fn make_command(command: Command) -> Command {
3535
.short('i')
3636
.value_name("IGNORE")
3737
.action(ArgAction::Append)
38-
.help("Ignores all files and folders matching the given glob"),
38+
.help("Ignores all files and folders matching the given glob. Patterns are relative to the current directory, or absolute if starting with `/`."),
3939
)
4040
.arg(
4141
Arg::new("ignore_file")

src/commands/sourcemaps/upload.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -161,7 +161,7 @@ pub fn make_command(command: Command) -> Command {
161161
.short('i')
162162
.value_name("IGNORE")
163163
.action(ArgAction::Append)
164-
.help("Ignores all files and folders matching the given glob"),
164+
.help("Ignores all files and folders matching the given glob. Patterns are relative to the current directory, or absolute if starting with `/`."),
165165
)
166166
.arg(
167167
Arg::new("ignore_file")

src/utils/file_search.rs

Lines changed: 39 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,12 @@
11
use std::collections::BTreeSet;
2+
use std::env;
23
use std::fs;
34
use std::io::Read as _;
45
use std::path::PathBuf;
56

67
use anyhow::Result;
78
use console::style;
8-
use ignore::overrides::OverrideBuilder;
9+
use glob::Pattern;
910
use ignore::types::TypesBuilder;
1011
use ignore::WalkBuilder;
1112
use log::{info, warn};
@@ -126,12 +127,43 @@ impl ReleaseFileSearch {
126127
builder.add_ignore(ignore_file);
127128
}
128129

129-
if !&self.ignores.is_empty() {
130-
let mut override_builder = OverrideBuilder::new(&self.path);
131-
for ignore in &self.ignores {
132-
override_builder.add(ignore)?;
133-
}
134-
builder.overrides(override_builder.build()?);
130+
// Compile ignore patterns relative to CWD (not relative to search path)
131+
let cwd = env::current_dir()?;
132+
let ignore_patterns: Vec<Pattern> = self
133+
.ignores
134+
.iter()
135+
.filter_map(|pattern| {
136+
// Patterns starting with ! are negations (handled in upload.rs/inject.rs)
137+
// Remove the ! prefix for glob pattern compilation
138+
let pattern_str = pattern.strip_prefix('!').unwrap_or(pattern);
139+
match Pattern::new(pattern_str) {
140+
Ok(p) => Some(p),
141+
Err(e) => {
142+
warn!("Invalid ignore pattern '{}': {}", pattern_str, e);
143+
None
144+
}
145+
}
146+
})
147+
.collect();
148+
149+
// Use filter_entry to match patterns relative to CWD
150+
if !ignore_patterns.is_empty() {
151+
builder.filter_entry(move |entry| {
152+
let entry_path = entry.path();
153+
154+
// Try to make the path relative to CWD, or use absolute path if that fails
155+
let check_path = entry_path.strip_prefix(&cwd).unwrap_or(entry_path);
156+
157+
// Check if any pattern matches - if so, ignore (return false)
158+
for pattern in &ignore_patterns {
159+
if pattern.matches_path(check_path) {
160+
return false;
161+
}
162+
}
163+
164+
// No patterns matched, keep this entry
165+
true
166+
});
135167
}
136168

137169
for result in builder.build() {

tests/integration/_cases/sourcemaps/sourcemaps-inject-help.trycmd

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -15,7 +15,8 @@ Arguments:
1515

1616
Options:
1717
-i, --ignore <IGNORE>
18-
Ignores all files and folders matching the given glob
18+
Ignores all files and folders matching the given glob. Patterns are relative to the
19+
current directory, or absolute if starting with `/`.
1920

2021
-o, --org <ORG>
2122
The organization ID or slug.
Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,23 @@
1+
```
2+
$ sentry-cli sourcemaps inject path_a path_b --ignore "**/path_a/test/**" --dry-run
3+
? success
4+
> Searching path_a
5+
> Found 2 files
6+
> Searching path_b
7+
> Found 4 files
8+
> Analyzing 6 sources
9+
> Analyzing completed in [..]
10+
> Injecting debug ids
11+
12+
Source Map Debug ID Injection Report
13+
Modified: The following source files have been modified to have debug ids
14+
[..]-[..]-[..]-[..]-[..] - path_a/keep/file3.js
15+
[..]-[..]-[..]-[..]-[..] - path_b/keep/file4.js
16+
[..]-[..]-[..]-[..]-[..] - path_b/test/file2.js
17+
Modified: The following sourcemap files have been modified to have debug ids
18+
[..]-[..]-[..]-[..]-[..] - path_a/keep/file3.js.map
19+
[..]-[..]-[..]-[..]-[..] - path_b/keep/file4.js.map
20+
[..]-[..]-[..]-[..]-[..] - path_b/test/file2.js.map
21+
22+
23+
```

tests/integration/_cases/sourcemaps/sourcemaps-upload-help.trycmd

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -67,7 +67,8 @@ Options:
6767
--strip-common-prefix
6868
Similar to --strip-prefix but strips the most common prefix on all sources references.
6969
-i, --ignore <IGNORE>
70-
Ignores all files and folders matching the given glob
70+
Ignores all files and folders matching the given glob. Patterns are relative to the
71+
current directory, or absolute if starting with `/`.
7172
-I, --ignore-file <IGNORE_FILE>
7273
Ignore all files and folders specified in the given ignore file, e.g. .gitignore.
7374
--bundle <BUNDLE>
Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,17 @@
1+
```
2+
$ sentry-cli sourcemaps upload path_a path_b --ignore "**/path_a/test/**" --url-prefix "~/" --validate
3+
? failed
4+
> Found 2 files
5+
> Found 4 files
6+
> Analyzing 6 sources
7+
> Analyzing completed in [..]
8+
> Rewriting sources
9+
> Rewriting completed in [..]
10+
> Adding source map references
11+
> Validating sources
12+
error: Cannot upload: You must either specify a release or have debug ids injected into your sources
13+
14+
Add --log-level=[info|debug] or export SENTRY_LOG_LEVEL=[info|debug] to see more output.
15+
Please attach the full debug log to all bug reports.
16+
17+
```
Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,2 @@
1+
// This file should be kept
2+
console.log("path_a/keep/file3.js");

tests/integration/_fixtures/ignore_test/path_a/keep/file3.js.map

Lines changed: 1 addition & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.
Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,2 @@
1+
// This file should be ignored
2+
console.log("path_a/test/file1.js");

0 commit comments

Comments
 (0)