Skip to content

Commit

Permalink
fix : fix a bug in the upstream CommonJSModuleLoader (janus-idp#600)
Browse files Browse the repository at this point in the history
* fix : fix a bug in the upstream CommonJSLoader

that prevented laoding modules from embedded `node_modules` folders
of private packages located in the plugin `node_modules` folder.

Signed-off-by: David Festal <dfestal@redhat.com>

* Add a comment, as requested in review.

Signed-off-by: David Festal <dfestal@redhat.com>

---------

Signed-off-by: David Festal <dfestal@redhat.com>
  • Loading branch information
davidfestal committed Oct 16, 2023
1 parent 80376b4 commit 0961437
Show file tree
Hide file tree
Showing 3 changed files with 74 additions and 1 deletion.
5 changes: 5 additions & 0 deletions .changeset/thirty-bags-kiss.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
---
'backend': patch
---

Fix a bug in the upstream CommonJSLoader, which prevented laoding modules from embedded node_modules folders of private packages located in the plugin node_modules folder.
14 changes: 13 additions & 1 deletion packages/backend/src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -50,6 +50,13 @@ import {
} from '@backstage/backend-plugin-manager';
import { DefaultEventBroker } from '@backstage/plugin-events-backend';

// TODO(davidfestal): The following import is a temporary workaround for a bug
// in the upstream @backstage/backend-plugin-manager package.
//
// It should be removed as soon as the upstream package is fixed and released.
// see https://github.com/janus-idp/backstage-showcase/pull/600
import { CommonJSModuleLoader } from './loader/CommonJSModuleLoader';

function makeCreateEnv(config: Config, pluginProvider: BackendPluginProvider) {
const root = getRootLogger();
const reader = UrlReaders.default({ logger: root, config });
Expand Down Expand Up @@ -161,7 +168,12 @@ async function main() {
argv: process.argv,
logger,
});
const pluginManager = await PluginManager.fromConfig(config, logger);
const pluginManager = await PluginManager.fromConfig(
config,
logger,
undefined,
new CommonJSModuleLoader(logger),
);
const createEnv = makeCreateEnv(config, pluginManager);

const appEnv = useHotMemoize(module, () => createEnv('app'));
Expand Down
56 changes: 56 additions & 0 deletions packages/backend/src/loader/CommonJSModuleLoader.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,56 @@
/*
* Copyright 2023 The Backstage Authors
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/

import { ModuleLoader } from '@backstage/backend-plugin-manager';

import path from 'path';
import { Logger } from 'winston';

export class CommonJSModuleLoader implements ModuleLoader {
constructor(public readonly logger: Logger) {}

async bootstrap(
backstageRoot: string,
dynamicPluginsPaths: string[],
): Promise<void> {
const backstageRootNodeModulesPath = `${backstageRoot}/node_modules`;
const dynamicNodeModulesPaths = [
...dynamicPluginsPaths.map(p => path.resolve(p, 'node_modules')),
];
const Module = require('module');
const oldNodeModulePaths = Module._nodeModulePaths;
Module._nodeModulePaths = (from: string): string[] => {
const result: string[] = oldNodeModulePaths(from);
if (!dynamicPluginsPaths.some(p => from.startsWith(p))) {
return result;
}
const filtered = result.filter(nodeModulePath => {
return (
nodeModulePath === backstageRootNodeModulesPath ||
dynamicNodeModulesPaths.some(p => nodeModulePath.startsWith(p))
);
});
this.logger.debug(
`Overriding node_modules search path for dynamic plugin ${from} to: ${filtered}`,
);
return filtered;
};
}

async load(packagePath: string): Promise<any> {
return await import(/* webpackIgnore: true */ packagePath);
}
}

0 comments on commit 0961437

Please sign in to comment.