From 03177610a8afb32f8e6e8c7de8d3bc8a011e21d0 Mon Sep 17 00:00:00 2001 From: Patrik Oldsberg Date: Fri, 29 Jan 2021 16:58:18 +0100 Subject: [PATCH 1/5] cli: add fix for module resolution in backend bundle config --- packages/cli/src/lib/bundler/config.ts | 31 +++++++++++++++++++++++++- 1 file changed, 30 insertions(+), 1 deletion(-) diff --git a/packages/cli/src/lib/bundler/config.ts b/packages/cli/src/lib/bundler/config.ts index c53a8329f5..6fbcc65c9a 100644 --- a/packages/cli/src/lib/bundler/config.ts +++ b/packages/cli/src/lib/bundler/config.ts @@ -219,7 +219,7 @@ export async function createBackendConfig( } : {}), externals: [ - nodeExternals({ + nodeExternalsWithResolve({ modulesDir: paths.rootNodeModules, additionalModuleDirs: moduleDirs, allowlist: ['webpack/hot/poll?100', ...localPackageNames], @@ -296,3 +296,32 @@ export async function createBackendConfig( ], }; } + +// This makes the module resolution happen from the context of each non-external module, rather +// than the main entrypoint. This fixes a bug where dependencies would be resolved from the backend +// package rather than each individual backend package and plugin. +// +// TODO(Rugvip): Feature suggestion/contribute this to webpack-externals +function nodeExternalsWithResolve( + options: Parameters[0], +) { + let currentContext: string; + const externals = nodeExternals({ + ...options, + importType(request) { + const resolved = require.resolve(request, { + paths: [currentContext], + }); + return `commonjs ${resolved}`; + }, + }); + + return ( + context: string, + request: string, + callback: webpack.ExternalsFunctionCallback, + ) => { + currentContext = context; + return externals(context, request, callback); + }; +} From bdbeaf605106cb8e2f2082950bb4a2d23ed1b2d7 Mon Sep 17 00:00:00 2001 From: Patrik Oldsberg Date: Fri, 29 Jan 2021 16:58:48 +0100 Subject: [PATCH 2/5] config-loader: validate each schema upfront to provide a more precise error message --- packages/config-loader/src/lib/schema/compile.ts | 8 ++++++++ 1 file changed, 8 insertions(+) diff --git a/packages/config-loader/src/lib/schema/compile.ts b/packages/config-loader/src/lib/schema/compile.ts index 2607d51f80..e340d775a1 100644 --- a/packages/config-loader/src/lib/schema/compile.ts +++ b/packages/config-loader/src/lib/schema/compile.ts @@ -69,6 +69,14 @@ export function compileConfigSchemas( }, }); + for (const schema of schemas) { + try { + ajv.compile(schema.value); + } catch (error) { + throw new Error(`Schema at ${schema.path} is invalid, ${error}`); + } + } + const merged = mergeAllOf( { allOf: schemas.map(_ => _.value) }, { From 9f2b9c2020a55bd103ef7ca2106d61bc9f9d04a1 Mon Sep 17 00:00:00 2001 From: Patrik Oldsberg Date: Fri, 29 Jan 2021 16:59:19 +0100 Subject: [PATCH 3/5] app-backend: re-throw config loading errors with instructions for how to fix --- plugins/app-backend/src/lib/config.ts | 20 ++++++++++++++------ 1 file changed, 14 insertions(+), 6 deletions(-) diff --git a/plugins/app-backend/src/lib/config.ts b/plugins/app-backend/src/lib/config.ts index 99590c1e2d..0607956894 100644 --- a/plugins/app-backend/src/lib/config.ts +++ b/plugins/app-backend/src/lib/config.ts @@ -84,13 +84,21 @@ export async function readConfigs(options: ReadOptions): Promise { const schemaPath = resolvePath(appDistDir, '.config-schema.json'); if (await fs.pathExists(schemaPath)) { const serializedSchema = await fs.readJson(schemaPath); - const schema = await loadConfigSchema({ serialized: serializedSchema }); - const frontendConfigs = await schema.process( - [{ data: config.get() as JsonObject, context: 'app' }], - { visibility: ['frontend'] }, - ); - appConfigs.push(...frontendConfigs); + try { + const schema = await loadConfigSchema({ serialized: serializedSchema }); + + const frontendConfigs = await schema.process( + [{ data: config.get() as JsonObject, context: 'app' }], + { visibility: ['frontend'] }, + ); + appConfigs.push(...frontendConfigs); + } catch (error) { + throw new Error( + 'Invalid schema embedded in the app bundle, to fix this issue you need ' + + `to correct the schema and then rebuild the app bundle. ${error}`, + ); + } } return appConfigs; From e9aab60c75aeb3db7b304c6bc17f45ceaf1ddd5a Mon Sep 17 00:00:00 2001 From: Patrik Oldsberg Date: Fri, 29 Jan 2021 17:03:02 +0100 Subject: [PATCH 4/5] added changesets --- .changeset/loud-walls-collect.md | 5 +++++ .changeset/neat-brooms-allow.md | 5 +++++ .changeset/nice-bottles-battle.md | 5 +++++ 3 files changed, 15 insertions(+) create mode 100644 .changeset/loud-walls-collect.md create mode 100644 .changeset/neat-brooms-allow.md create mode 100644 .changeset/nice-bottles-battle.md diff --git a/.changeset/loud-walls-collect.md b/.changeset/loud-walls-collect.md new file mode 100644 index 0000000000..78356eef38 --- /dev/null +++ b/.changeset/loud-walls-collect.md @@ -0,0 +1,5 @@ +--- +'@backstage/cli': patch +--- + +Fixed module resolution of external libraries during backend development. Modules used to be resolved relative to the backend entrypoint, but are now resolved relative to each individual module. diff --git a/.changeset/neat-brooms-allow.md b/.changeset/neat-brooms-allow.md new file mode 100644 index 0000000000..6ea45e99ba --- /dev/null +++ b/.changeset/neat-brooms-allow.md @@ -0,0 +1,5 @@ +--- +'@backstage/config-loader': patch +--- + +Each piece of the configuration schema is now validate upfront, in order to produce more informative errors. diff --git a/.changeset/nice-bottles-battle.md b/.changeset/nice-bottles-battle.md new file mode 100644 index 0000000000..a688241caa --- /dev/null +++ b/.changeset/nice-bottles-battle.md @@ -0,0 +1,5 @@ +--- +'@backstage/plugin-app-backend': patch +--- + +Failures to load the frontend configuration schema now throws an error that includes more context and instructions for how to fix the issue. From d7f30a800db6e0dcee5de1a08b9ed88428dfe573 Mon Sep 17 00:00:00 2001 From: Patrik Oldsberg Date: Fri, 29 Jan 2021 17:24:14 +0100 Subject: [PATCH 5/5] Update .changeset/neat-brooms-allow.md Co-authored-by: Himanshu Mishra --- .changeset/neat-brooms-allow.md | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/.changeset/neat-brooms-allow.md b/.changeset/neat-brooms-allow.md index 6ea45e99ba..a4c69b0945 100644 --- a/.changeset/neat-brooms-allow.md +++ b/.changeset/neat-brooms-allow.md @@ -2,4 +2,4 @@ '@backstage/config-loader': patch --- -Each piece of the configuration schema is now validate upfront, in order to produce more informative errors. +Each piece of the configuration schema is now validated upfront, in order to produce more informative errors.