From de626c0d807aa3686a5cc542729d986518577554 Mon Sep 17 00:00:00 2001 From: jabaran Date: Tue, 13 Aug 2024 21:46:14 -0400 Subject: [PATCH] Feat: Determine if the project.json is for nuget or NX The goal was to improve the experience when using `--all-projects` There may be a better place to put a check like this where an additional read doesn't happen; however, this was the least impactful location I found to make this type of change. If it was done after the This should allow more use of --all-projects when repositories also have `project.json` file from using [NX/js](https://nx.dev/) In addition to the extra file read, this solution also creates an additional array `targetFiles_temp` to minimize overall code changes. In general, if there is a preferred way to approach this instead, I'd be happy to hear that feedback. --- src/lib/plugins/get-deps-from-plugin.ts | 31 ++++++++++++++++++++++++- 1 file changed, 30 insertions(+), 1 deletion(-) diff --git a/src/lib/plugins/get-deps-from-plugin.ts b/src/lib/plugins/get-deps-from-plugin.ts index 13c4039a27..5f24d6e769 100644 --- a/src/lib/plugins/get-deps-from-plugin.ts +++ b/src/lib/plugins/get-deps-from-plugin.ts @@ -21,6 +21,8 @@ import { convertSingleResultToMultiCustom } from './convert-single-splugin-res-t import { convertMultiResultToMultiCustom } from './convert-multi-plugin-res-to-multi-custom'; import { processYarnWorkspaces } from './nodejs-plugin/yarn-workspaces-parser'; import { ScannedProject } from '@snyk/cli-interface/legacy/common'; +import { SUPPORTED_MANIFEST_FILES } from '../package-managers'; +import * as fs from 'fs'; const debug = debugModule('snyk-test'); @@ -46,13 +48,17 @@ export async function getDepsFromPlugin( const levelsDeep = options.detectionDepth; const ignore = options.exclude ? options.exclude.split(',') : []; - const { files: targetFiles, allFilesFound } = await find({ + const { files: targetFiles_temp, allFilesFound } = await find({ path: root, ignore, filter: multiProjectProcessors[scanType].files, featureFlags, levelsDeep, }); + + //Remove files found which do meet schema requirements + const targetFiles = targetFiles_temp.filter(isValidPackageFileSchema); + debug( `auto detect manifest files, found ${targetFiles.length}`, targetFiles, @@ -155,3 +161,26 @@ export function warnSomeGradleManifestsNotScanned( } return null; } + + +// Evaluate the if the file conforms with expected schema when other popular frameworks conflict with naming convention. +// If it is found to be the expected schema return true; othwewise, false +function isValidPackageFileSchema(filePath:string) : boolean { + let isValid = true; + if( pathLib.basename(filePath) == SUPPORTED_MANIFEST_FILES.PROJECT_JSON ) { + const fileContent = fs.readFileSync(filePath, 'utf-8'); + const data = JSON.parse(fileContent); + + // Define the keys specific to NuGet and Nx + const nugetKeys = ['dependencies', 'frameworks']; + const nxKeys = ['targets', 'architect', 'tasksRunnerOptions']; + + // Check for NuGet-specific keys and ensure Nx-specific keys are not present + const hasNugetKeys = nugetKeys.some(key => key in data); + const hasNxKeys = nxKeys.some(key => key in data); + + isValid = hasNugetKeys && !hasNxKeys; + debug( `auto detected manifest files, found ${filePath}.\n\tNuget Keys: ${hasNugetKeys}\n\tNX Keys: ${hasNxKeys}\n\tIs Valid Nuget: ${isValid}`); + } + return isValid; +} \ No newline at end of file