Skip to content

Commit 5b0cfe7

Browse files
mjambons-santillan
andauthored
Add two howtos related to the migration to Semgrepignore v2 (#2012)
* Add two howtos related to the migration to Semgrepignore v2 * Update docs/kb/semgrep-code/deprecated-include-directive.md Co-authored-by: s-santillan <[email protected]> * Update Semgrepignore v2 specification to reflect upcoming change * Delete migration guide for ':include' which no longer applies * Update docs/kb/semgrep-code/semgrepignore-ignored.md Co-authored-by: s-santillan <[email protected]> * Update docs/semgrepignore-v2-reference.md Co-authored-by: s-santillan <[email protected]> --------- Co-authored-by: s-santillan <[email protected]>
1 parent 0197d24 commit 5b0cfe7

File tree

2 files changed

+68
-10
lines changed

2 files changed

+68
-10
lines changed
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,51 @@
1+
---
2+
tags:
3+
- Semgrep CLI
4+
- Semgrepignore
5+
description: Why am I getting findings in files that should be ignored?
6+
---
7+
8+
# Why am I getting findings in files that should be ignored?
9+
10+
If you don't have already a `.semgrepignore` file, refer to our
11+
[guide on how to exclude files from Semgrep
12+
scans](/docs/ignoring-files-folders-code). Otherwise, if
13+
you already have a `.semgrepignore` file, read on.
14+
15+
Starting with Semgrep CE 1.112.0, the Semgrepignore specification has
16+
changed slightly to better align with Git and Gitignore and to offer
17+
more flexibility.
18+
The new specification is referred to as
19+
[Semgrepignore v2](/docs/semgrepignore-v2-reference).
20+
If you're getting findings in files that should have been
21+
ignored according to your `.semgrepignore` file, check the
22+
following:
23+
24+
1. If you're using Git, check that the `.semgrepignore` file is at the
25+
root of the Git project or at least is within the project.
26+
`.semgrepignore` files can be placed in any folder in the project
27+
and follow the same specification as `.gitignore` files,
28+
which they extend.
29+
2. If you're not using Git, check that the `.semgrepignore` file
30+
is in the folder passed on the `semgrep scan` command line.
31+
For example, if the command is `semgrep scan foo/`, you must move
32+
the `.semgrepignore` file from the current folder
33+
to `foo/.semgrepignore`.
34+
35+
To ensure you're using Semgrepignore v2, pass the flag
36+
`--semgrepignore-v2` to `semgrep scan` or to `semgrep
37+
ci`. To use the legacy Semgrepignore v1 implementation, use
38+
`--no-semgrepignore-v2`. These options are for troubleshooting the
39+
migration from v1 to v2. These flags will be removed when v2 becomes
40+
only implementation available.
41+
42+
## Best practices
43+
44+
* When scanning a whole project, run `semgrep` from the project root.
45+
* Place a `.semgrepignore` file at the project root.
46+
* Optionally, place `.semgrepignore` files in subfolders so as to keep the
47+
exclusion patterns simple and to allow moving these subfolders
48+
around without having to edit the file exclusion patterns.
49+
* Refer to the [Gitignore
50+
specification](https://git-scm.com/docs/gitignore)
51+
for the precise syntax and usage of `.semgrepignore` files.

docs/semgrepignore-v2-reference.md

+17-10
Original file line numberDiff line numberDiff line change
@@ -153,8 +153,9 @@ testsuite/
153153

154154
## Semgrepignore pattern syntax
155155

156-
In Semgrepignore v2, the pattern syntax conforms strictly to the
157-
[Gitignore pattern syntax](https://git-scm.com/docs/gitignore#_pattern_format).
156+
In Semgrepignore v2, the pattern syntax conforms to the
157+
[Gitignore pattern
158+
syntax](https://git-scm.com/docs/gitignore#_pattern_format).
158159
They are glob patterns which support `*` and `**` with their usual
159160
meanings. For example, pattern `**/tmp/*.js` matches paths `tmp/foo.js` and
160161
`src/tmp/bar.js`.
@@ -165,15 +166,21 @@ the pattern or any of its subfolders). For
165166
example, `/a` and `a/b` are anchored patterns but not `a/`. Please
166167
consult the Gitignore documentation for details.
167168

168-
In Semgrepignore v1, the following exceptions to the Gitignore
169+
As a deviation from the Gitignore syntax, Semgrepignore syntax supports
170+
`:include` directives. `:include` followed by an unquoted file path
171+
relative to the path of folder of the source `.semgrepignore` file
172+
(the current folder in v1) inserts patterns from that file.
173+
A common use case is to insert the line `:include .gitignore` at the
174+
beginning of a `.semgrepignore` file so as to avoid duplicating the
175+
Gitignore patterns. Included files may not contain include
176+
directives.
177+
178+
## Legacy Semgrepignore v1
179+
180+
In Semgrepignore v1, the following exceptions to the v2
169181
specification apply:
170182

171183
* unsupported: pattern negation with `!`
172184
* unsupported: character ranges such as `[a-z]`
173-
* extension:
174-
`:include` followed by an unquoted file path relative to the path of
175-
folder of the source `.semgrepignore` file (the current folder in v1)
176-
will insert patterns read from that file. A common use case was
177-
`:include .gitignore` but it has become unnecessary in v2 where
178-
`.gitignore` files are consulted automatically.
179-
Include directives are deprecated in v2.
185+
* only one `.semgrepignore` file is supported and it must be in the
186+
current folder

0 commit comments

Comments
 (0)