question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

Add a way to detect if a file exists.

See original GitHub issue

Is your feature request related to a problem? Please describe. We want to make a rule that detects if a file exists in the filesystem, because it would be related to a functionality itself (for example, if we detect a tsconfig.json named file, we can infer that the project uses Typescript)

Describe the solution you’d like One solution would be using the “applies_to_file_regex” field, but if the rule hasn’t patterns to match, it will generate one match for each file that exists with this name o regex name. For example, a rule like this:

{ "name": "Typescript", "id": "TEST0001", "description": "Typescript", "tags":[ "Test.Product.Typescript" ], "severity": "critical", "applies_to_file_regex": [ "tsconfig.json" ] }

will generate one match in the result like this: { "ruleId": "TEST0001", "ruleName": "Typescript", "ruleDescription": "Typescript", "tags": [ "Test.Product.Typescript" ], "severity": "Critical", "pattern": "", "confidence": "", "type": "", "language": "json", "fileName": "./tsconfig.json", "sample": "", "excerpt": "", "startLocationLine": 0, "startLocationColumn": 0, "endLocationLine": 0, "endLocationColumn": 0 }

In order to generalize the behaviour, it would work with “applies_to” field too, and will generate one match for each file of the types setted in appplies_to field. For example, a rule without patterns that “applies_to”: “json”, will generate one match for each json file detected.

We are open to other solutions of course.

Thanks a lot! regards.

Issue Analytics

  • State:closed
  • Created 2 years ago
  • Reactions:1
  • Comments:8 (6 by maintainers)

github_iconTop GitHub Comments

1reaction
ismadirolascommented, Jan 10, 2022

Hello @gfs , We can cover our use case with this solution, we tested it and it works. Thanks for the quick solution!

0reactions
gfscommented, Jan 7, 2022

Good to hear we have already covered one case.

For this second empty file case, have you tried using ^$ as the regex?

I tested this and while the regex is valid we had a separate check that was skipping empty files. #413 will change this to allow rules to be checked against empty files.

Here is a sample rule, again using metric, that will match empty files named application.yml.

[{
  "name": "Test Rule",
  "id": "ID0001",
  "description": "Test Rule",
  "tags":[ "Metric.Application.Yaml.File" ],
  "severity": "critical",
  "applies_to_file_regex": [ "application.yml" ],
  "patterns": [
    {
      "pattern": "^$",
      "type": "regex",
      "scopes": [ "all" ],
      "modifiers": [],
      "confidence": "high"
    }
  ]
}]
Read more comments on GitHub >

github_iconTop Results From Across the Web

How to Check if a File Exists in Python with isFile() and ...
How to Check if a File Exists Using the Path.is_file() Method in Python ... The is_file() method checks if a file exists. It...
Read more >
How do I check if a file exists in Java?
I would recommend using isFile() instead of exists() . Most of the time you are looking to check if the path points to...
Read more >
Validate PowerShell to Check if a File Exists (Examples)
The first way is the Test-Path cmdlet, specifically designed to determine whether a path or file exists. When using this cmdlet to test...
Read more >
How to Check If a File Exists in Python
1) Using os.path.exists() function to check if a file exists ... To check if a file exists, you pass the file path to...
Read more >
How To Check If A File Exists In C#
The File.Exists method checks if a file exists in C# at a specified location. The File class is defined in the System.IO namespace....
Read more >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found