From a0d498adac7a1133e44d20e258a675612d7568a6 Mon Sep 17 00:00:00 2001 From: Emanuele Stoppa Date: Mon, 5 Jul 2021 09:49:56 +0100 Subject: [PATCH] feat(project): avoid validate manifest (#1615) --- internal/codec-js-manifest/index.test.ts | 12 +++++-- .../codec-js-manifest/normalize/fields.ts | 1 + internal/codec-js-manifest/normalize/index.ts | 31 ++++++++++++------- internal/codec-spdx-license/parse.ts | 4 +++ internal/core/common/IntegrationLoader.ts | 6 +++- internal/core/server/commands/init.ts | 8 ++--- internal/core/server/fs/MemoryFileSystem.ts | 12 ++++++- internal/core/server/fs/Resolver.ts | 1 - .../core/server/project/ProjectManager.ts | 1 - internal/core/server/utils/getManifest.ts | 14 ++++++--- internal/project/load.ts | 1 - 11 files changed, 63 insertions(+), 28 deletions(-) diff --git a/internal/codec-js-manifest/index.test.ts b/internal/codec-js-manifest/index.test.ts index 065cbe54a25..f872a38dc3e 100644 --- a/internal/codec-js-manifest/index.test.ts +++ b/internal/codec-js-manifest/index.test.ts @@ -25,7 +25,9 @@ test( DIAGNOSTIC_CATEGORIES.parse, "manifest", ); - t.snapshot(await normalizeManifest(PATH, manifest, [])); + t.snapshot( + await normalizeManifest({path: PATH, consumer: manifest, projects: []}), + ); }, ); @@ -44,7 +46,9 @@ test( DIAGNOSTIC_CATEGORIES.parse, "manifest", ); - t.snapshot(await normalizeManifest(PATH, manifest, [])); + t.snapshot( + await normalizeManifest({path: PATH, consumer: manifest, projects: []}), + ); }, ); @@ -63,6 +67,8 @@ test( DIAGNOSTIC_CATEGORIES.parse, "manifest", ); - t.snapshot(await normalizeManifest(PATH, manifest, [])); + t.snapshot( + await normalizeManifest({path: PATH, consumer: manifest, projects: []}), + ); }, ); diff --git a/internal/codec-js-manifest/normalize/fields.ts b/internal/codec-js-manifest/normalize/fields.ts index 25abd3e8719..bc0009bd19e 100644 --- a/internal/codec-js-manifest/normalize/fields.ts +++ b/internal/codec-js-manifest/normalize/fields.ts @@ -176,6 +176,7 @@ export function normalizeRepoField( } } +// TODO: review this logic export function normalizeExportsField( consumer: Consumer, ): boolean | ManifestExportsField { diff --git a/internal/codec-js-manifest/normalize/index.ts b/internal/codec-js-manifest/normalize/index.ts index 034f0b719eb..532120ef2a9 100644 --- a/internal/codec-js-manifest/normalize/index.ts +++ b/internal/codec-js-manifest/normalize/index.ts @@ -238,7 +238,6 @@ const INCORRECT_DEPENDENCIES_SUFFIXES = [ "dependancies", "dependecies", ]; - function checkDependencyKeyTypo(key: string, prop: Consumer) { for (const depPrefixKey of DEPENDENCIES_KEYS) { // Ignore if the key is a valid dependency key @@ -274,7 +273,7 @@ function normalizeManifestMetadata( metadata: ManifestMetadata; parsedLicense: undefined | SPDXLicenseParseResult; } { - const {consumer, loose} = context; + const {consumer, loose, checkDependenciesAndLicense} = context; const name = normalizeNameField(consumer, loose); const version = normalizeVersion(context); @@ -283,12 +282,14 @@ function normalizeManifestMetadata( } const strName = name === undefined ? undefined : manifestNameToString(name); - - const parsedLicense = normalizeLicense( - consumer, - {name: strName, version}, - context, - ); + let parsedLicense: undefined | SPDXLicenseParseResult = undefined; + if (checkDependenciesAndLicense) { + parsedLicense = normalizeLicense( + consumer, + {name: strName, version}, + context, + ); + } return { parsedLicense, @@ -385,12 +386,18 @@ type NormalizeContext = { loose: boolean; consumer: Consumer; projects: CompilerProject[]; + checkDependenciesAndLicense?: boolean; }; +interface NormalizeManifest { + path: AbsoluteFilePath; + consumer: Consumer; + projects: CompilerProject[]; + checkDependenciesAndLicense?: boolean; +} + export async function normalizeManifest( - path: AbsoluteFilePath, - consumer: Consumer, - projects: CompilerProject[], + {path, consumer, projects, checkDependenciesAndLicense}: NormalizeManifest, ): Promise { const loose = path.hasSegment("node_modules"); @@ -399,7 +406,6 @@ export async function normalizeManifest( for (const [key, prop] of consumer.asMap()) { // Check for typos for dependencies checkDependencyKeyTypo(key, prop); - // Check for other typos const correctKey = TYPO_KEYS.get(key); if (correctKey !== undefined) { @@ -413,6 +419,7 @@ export async function normalizeManifest( loose, consumer, projects, + checkDependenciesAndLicense, }; const {metadata, parsedLicense} = normalizeManifestMetadata(context); diff --git a/internal/codec-spdx-license/parse.ts b/internal/codec-spdx-license/parse.ts index 9ac99fec6ce..bdb7800ae35 100644 --- a/internal/codec-spdx-license/parse.ts +++ b/internal/codec-spdx-license/parse.ts @@ -175,6 +175,10 @@ function isLicenseValid( const {exceptions} = parser.options; if (exceptions !== undefined) { for (const project of exceptions.projects) { + // if the check on dependencies is not enabled, do not do any kind of check + if (!project.config.dependencies.enabled) { + continue; + } const {invalidLicenses} = project.config.dependencies.exceptions; const licenses = invalidLicenses.get(id); if (licenses === undefined) { diff --git a/internal/core/common/IntegrationLoader.ts b/internal/core/common/IntegrationLoader.ts index b26ff55e09d..13152894308 100644 --- a/internal/core/common/IntegrationLoader.ts +++ b/internal/core/common/IntegrationLoader.ts @@ -139,7 +139,11 @@ export default class IntegrationLoader< ); const versionProp = jsonConsumer.get("version"); - const manifest = await normalizeManifest(manifestPath, jsonConsumer, []); + const manifest = await normalizeManifest({ + path: manifestPath, + consumer: jsonConsumer, + projects: [], + }); if (manifest.version === undefined) { throw versionProp.unexpected(() => diff --git a/internal/core/server/commands/init.ts b/internal/core/server/commands/init.ts index c77252abac8..9a0ab379d5a 100644 --- a/internal/core/server/commands/init.ts +++ b/internal/core/server/commands/init.ts @@ -197,11 +197,11 @@ export default createServerCommand({ path: manifestPath, input: await manifestPath.readFileText(), }); - const manifestDefinition = await normalizeManifest( - manifestPath, + const manifestDefinition = await normalizeManifest({ + path: manifestPath, consumer, - [], - ); + projects: [], + }); if (!manifestDefinition) { reporter.error(markup`Couldn't find any manifest at path ${cwd}`); return; diff --git a/internal/core/server/fs/MemoryFileSystem.ts b/internal/core/server/fs/MemoryFileSystem.ts index d4c6a8d5597..847db8add04 100644 --- a/internal/core/server/fs/MemoryFileSystem.ts +++ b/internal/core/server/fs/MemoryFileSystem.ts @@ -703,8 +703,18 @@ export default class MemoryFileSystem { const projects = await this.server.projectManager.getProjectHierarchyFromPath( path, ); + let checkDependenciesAndLicense = false; + const mainProject = await this.server.projectManager.findLoadedProject(path); + if (mainProject) { + checkDependenciesAndLicense = mainProject.config.dependencies.enabled; + } const {consumer: normalizeConsumer, diagnostics: rawDiagnostics} = consumer.capture(); - const manifest = await normalizeManifest(path, normalizeConsumer, projects); + const manifest = await normalizeManifest({ + path, + consumer: normalizeConsumer, + projects, + checkDependenciesAndLicense, + }); // If manifest is undefined then we failed to validate and have diagnostics if (rawDiagnostics.length > 0) { diff --git a/internal/core/server/fs/Resolver.ts b/internal/core/server/fs/Resolver.ts index a803c5bde03..7ab00673b8a 100644 --- a/internal/core/server/fs/Resolver.ts +++ b/internal/core/server/fs/Resolver.ts @@ -294,7 +294,6 @@ function getPreferredMainKey( if (alias !== undefined) { return alias; } - if (manifest.main !== undefined) { return { key: consumer.get("main"), diff --git a/internal/core/server/project/ProjectManager.ts b/internal/core/server/project/ProjectManager.ts index 3baa3b0194e..22cf31fb8f2 100644 --- a/internal/core/server/project/ProjectManager.ts +++ b/internal/core/server/project/ProjectManager.ts @@ -606,7 +606,6 @@ export default class ProjectManager { diagnostics: DiagnosticsProcessor, ) { const name = manifestNameToString(def.manifest.name); - const type = isProjectPackage ? "project package manifest" : "manifest"; this.logger.info( markup`Declaring ${type} ${name} in project #${project.id} in ${def.directory}`, diff --git a/internal/core/server/utils/getManifest.ts b/internal/core/server/utils/getManifest.ts index 51e9c8f520c..a0885f05a04 100644 --- a/internal/core/server/utils/getManifest.ts +++ b/internal/core/server/utils/getManifest.ts @@ -12,12 +12,18 @@ export default async function getManifest( const projects = await server.projectManager.getProjectHierarchyFromPath( manifestPath, ); + let checkDependenciesAndLicense = false; + const mainProject = await server.projectManager.findLoadedProject(cwd); + if (mainProject) { + checkDependenciesAndLicense = mainProject.config.dependencies.enabled; + } if (await manifestPath.exists()) { - manifest = await normalizeManifest( - manifestPath, - json.consumeValue(await manifestPath.readFileTextMeta()), + manifest = await normalizeManifest({ + path: manifestPath, + consumer: json.consumeValue(await manifestPath.readFileTextMeta()), projects, - ); + checkDependenciesAndLicense, + }); } return manifest; } diff --git a/internal/project/load.ts b/internal/project/load.ts index 3ac50f3ce1d..ca867da26c1 100644 --- a/internal/project/load.ts +++ b/internal/project/load.ts @@ -334,7 +334,6 @@ export async function normalizeProjectConfig( if (dependencies.has("enabled")) { config.dependencies.enabled = dependencies.get("enabled").asBoolean(); } - if (dependencies.has("exceptions")) { const exceptions = dependencies.get("exceptions").asMap();