Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Feat: Determine if the project.json is for nuget or NX #5421

Open
wants to merge 1 commit into
base: main
Choose a base branch
from
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
31 changes: 30 additions & 1 deletion src/lib/plugins/get-deps-from-plugin.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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');

Expand All @@ -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,
Expand Down Expand Up @@ -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;
}