Skip to content

Commit 40631ba

Browse files
Only Run npm-package-json-lint When package.json is Present (#2280)
* Link to a PR rather than an issue in changelog * Prefer list.append(x) to list += [x] The former is slightly faster as it generally doesn't create a new list. * Don't warn that REPOSITORY linters are missing Some REPOSITORY linters are intentionally omitted from flavors to keep their size small and performance fast. Hence, their absence from a flavor isn't considered an error when FAIL_IF_MISSING_LINTER_IN_FLAVOR is true. Therefore, omit them from the list of missing linters displayed to the user to avoid confusion. * Simplify check_active_linters_match_flavor Reduce indentation by inverting test, and prefer the more Pythonic test not list to len(list) == 0 for simplicity. Remove some comments rendered unnecessary via this more direct expression of our intent. * Correct capitalization of Docker in a warning * Only lint package.json when present (#2279) npm-package-json-lint only lints Node.js package.json files, so only run it when package.json is present. npm-package-json-lint is correctly omitted from most flavors. Many non-Node.js projects contain other JSON files, so this change prevents false positives when FAIL_IF_MISSING_LINTER_IN_FLAVOR is true.
1 parent 33fe169 commit 40631ba

File tree

3 files changed

+29
-35
lines changed

3 files changed

+29
-35
lines changed

CHANGELOG.md

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -16,7 +16,9 @@ Note: Can be used with `oxsecurity/megalinter@beta` in your GitHub Action mega-l
1616
- Change name of config file for powershell formatter to avoid collision with powershell linter config
1717
- Enhance find SARIF json in stdout output
1818
- Pass --show-context, --show-suggestions, and --no-must-find-files to CSpell for friendlier UX
19-
by @Kurt-von-Laven in [#2271](https://github.com/oxsecurity/megalinter/issues/2271).
19+
by @Kurt-von-Laven in [#2275](https://github.com/oxsecurity/megalinter/pull/2275).
20+
- Only run npm-package-json-lint when package.json is present by @Kurt-von-Laven in
21+
[#2280](https://github.com/oxsecurity/megalinter/pull/2280).
2022

2123
- Documentation
2224
- Configure jsonschema documentation formatting (see [Descriptor schema](https://megalinter.io/latest/json-schemas/descriptor.html), [Configuration schema](https://megalinter.io/latest/json-schemas/configuration.html)), by @echoix in [#2270](https://github.com/oxsecurity/megalinter/pull/2270)

megalinter/descriptors/json.megalinter-descriptor.yml

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -233,6 +233,8 @@ linters:
233233
- salesforce
234234
file_names_regex:
235235
- "package\\.json"
236+
active_only_if_file_found:
237+
- "package.json"
236238
cli_lint_mode: project
237239
cli_executable: npmPkgJsonLint
238240
cli_config_arg_name: --configFile

megalinter/flavor_factory.py

Lines changed: 24 additions & 34 deletions
Original file line numberDiff line numberDiff line change
@@ -84,31 +84,31 @@ def check_active_linters_match_flavor(active_linters):
8484
missing_linters = []
8585
for active_linter in active_linters:
8686
if active_linter.name not in flavor_linters:
87-
missing_linters += [active_linter.name]
8887
active_linter.is_active = False
89-
# Manage cases where linters are missing in flavor
90-
if len(missing_linters) > 0:
91-
# Do not warn/stop if missing linters are repository ones (mostly OX.security related)
92-
if not are_all_repository_linters(missing_linters):
93-
missing_linters_str = ",".join(missing_linters)
94-
logging.warning(
95-
f"MegaLinter flavor [{flavor}] does not contain linters {missing_linters_str}.\n"
96-
"As they are not available in this docker image, they will not be processed\n"
97-
"To solve this problem, please either: \n"
98-
f"- use default flavor {ML_REPO}\n"
99-
"- add ignored linters in DISABLE or DISABLE_LINTERS variables in your .mega-linter.yml config file "
100-
"located in your root directory\n"
101-
"- ignore this message by setting config variable FLAVOR_SUGGESTIONS to false"
102-
)
103-
# Stop the process if user wanted so in case of missing linters
104-
if config.get("FAIL_IF_MISSING_LINTER_IN_FLAVOR", "") == "true":
105-
logging.error(
106-
'Missing linter and FAIL_IF_MISSING_LINTER_IN_FLAVOR has been set to "true": Stop run'
107-
)
108-
sys.exit(84)
109-
return False
110-
# All good !
111-
return True
88+
# Ignore linters that shouldn't trigger failure when missing.
89+
if not active_linter.name.startswith("REPOSITORY"):
90+
missing_linters.append(active_linter.name)
91+
92+
if not missing_linters:
93+
return True
94+
95+
missing_linters_str = ",".join(missing_linters)
96+
logging.warning(
97+
f"MegaLinter flavor [{flavor}] does not contain linters {missing_linters_str}.\n"
98+
"As they are not available in this Docker image, they will not be processed\n"
99+
"To solve this problem, please either: \n"
100+
f"- use default flavor {ML_REPO}\n"
101+
"- add ignored linters in DISABLE or DISABLE_LINTERS variables in your .mega-linter.yml config file "
102+
"located in your root directory\n"
103+
"- ignore this message by setting config variable FLAVOR_SUGGESTIONS to false"
104+
)
105+
# Stop the process if user wanted so in case of missing linters
106+
if config.get("FAIL_IF_MISSING_LINTER_IN_FLAVOR", "") == "true":
107+
logging.error(
108+
'Missing linter and FAIL_IF_MISSING_LINTER_IN_FLAVOR has been set to "true": Stop run'
109+
)
110+
sys.exit(84)
111+
return False
112112

113113

114114
# Compare active linters with available flavors to make suggestions to improve CI performances
@@ -147,13 +147,3 @@ def get_megalinter_flavor_suggestions(active_linters):
147147
)
148148
new_flavor_linters_names = map(lambda linter: linter.name, new_flavor_linters)
149149
return ["new", new_flavor_linters_names]
150-
151-
152-
def are_all_repository_linters(linter_names: list[str]) -> bool:
153-
if len(linter_names) == 0:
154-
return False
155-
result = True
156-
for linter_name in linter_names:
157-
if not linter_name.startswith("REPOSITORY"):
158-
result = False
159-
return result

0 commit comments

Comments
 (0)