diff --git a/packages/controller/src/Controller.ts b/packages/controller/src/Controller.ts index bcbdd007..f24469df 100644 --- a/packages/controller/src/Controller.ts +++ b/packages/controller/src/Controller.ts @@ -644,8 +644,7 @@ export default class Controller { } packageName = pluginInfo.npmPackage ?? pluginInfo.name; - const pluginPackageJsonPath = require.resolve(path.posix.join(pluginInfo.requirePath, "package.json")); - const pluginPackageJson = JSON.parse(await fs.readFile(pluginPackageJsonPath, "utf8")); + const pluginPackageJson = JSON.parse(await fs.readFile(pluginInfo.packagePath, "utf8")); if (pluginInfo.version !== pluginPackageJson.version) { this.config.restartRequired = true; return true; diff --git a/packages/host/src/Host.ts b/packages/host/src/Host.ts index c97ca6fc..7cae0ce6 100644 --- a/packages/host/src/Host.ts +++ b/packages/host/src/Host.ts @@ -876,8 +876,7 @@ export default class Host extends lib.Link { } packageName = pluginInfo.npmPackage ?? pluginInfo.name; - const pluginPackageJsonPath = require.resolve(path.posix.join(pluginInfo.requirePath, "package.json")); - const pluginPackageJson = JSON.parse(await fs.readFile(pluginPackageJsonPath, "utf8")); + const pluginPackageJson = JSON.parse(await fs.readFile(pluginInfo.packagePath, "utf8")); if (pluginInfo.version !== pluginPackageJson.version) { this.config.restartRequired = true; return true; diff --git a/packages/host/src/patch.ts b/packages/host/src/patch.ts index 639ce65f..c94f5ce1 100644 --- a/packages/host/src/patch.ts +++ b/packages/host/src/patch.ts @@ -91,8 +91,7 @@ export class SaveModule { } static async fromPlugin(plugin: BaseInstancePlugin) { - let pluginPackagePath = require.resolve(path.posix.join(plugin.info.requirePath, "package.json")); - let moduleDirectory = path.join(path.dirname(pluginPackagePath), "module"); + let moduleDirectory = path.join(path.dirname(plugin.info.packagePath), "module"); try { await fs.access(moduleDirectory); } catch (err: any) { diff --git a/packages/lib/src/plugin.ts b/packages/lib/src/plugin.ts index 94942f07..456cde2a 100644 --- a/packages/lib/src/plugin.ts +++ b/packages/lib/src/plugin.ts @@ -46,6 +46,10 @@ export type PluginNodeEnvInfo = PluginDeclaration & { * server in order for the web interface to be able to load the plugin. */ webStaticPath: string; + /** + * Absolute path to the package.json file for the plugin. + */ + packagePath: string; requirePath: string; version: string; manifest: any; diff --git a/packages/lib/src/plugin_loader.ts b/packages/lib/src/plugin_loader.ts index 90a1fc72..31ec7a03 100644 --- a/packages/lib/src/plugin_loader.ts +++ b/packages/lib/src/plugin_loader.ts @@ -26,11 +26,15 @@ export async function loadPluginInfos(pluginList: Map) { for (let [pluginName, pluginPath] of pluginList) { let pluginInfo: libPlugin.PluginNodeEnvInfo; let pluginPackage: { name?: string, version: string, main?: string, private?: boolean }; + let packagePath; - // Check if plugin path exists, otherwise remove it + // Check if plugin has a package.json file, otherwise remove it try { - require.resolve(pluginPath); - } catch { + const absolute = path.isAbsolute(pluginPath); + const packageImport = path.posix.join(pluginPath, "package.json"); + packagePath = require.resolve(packageImport); + await fs.access(packagePath, fs.constants.F_OK); + } catch (err) { let errMsg = `Plugin path ${pluginPath} does not exist`; try { await fs.access(pluginPath, fs.constants.F_OK); @@ -43,7 +47,7 @@ export async function loadPluginInfos(pluginList: Map) { try { pluginInfo = require(pluginPath).plugin; - pluginPackage = require(path.posix.join(pluginPath, "package.json")); + pluginPackage = require(packagePath); } catch (err: any) { if (err.code === "InstallationError") { throw err; @@ -69,12 +73,8 @@ export async function loadPluginInfos(pluginList: Map) { continue; } - pluginInfo.webStaticPath = path.join( - path.dirname( - require.resolve(path.posix.join(pluginPath, "package.json")) - ), - "dist", "web", "static", - ); + pluginInfo.webStaticPath = path.join(path.dirname(packagePath), "dist", "web", "static"); + pluginInfo.packagePath = packagePath; pluginInfo.requirePath = pluginPath; pluginInfo.version = pluginPackage.version; pluginInfo.npmPackage = !pluginPackage.private && pluginPath === pluginPackage.name ? pluginPath : undefined; diff --git a/test/controller/Controller.js b/test/controller/Controller.js index 35e6db9c..736d4cbe 100644 --- a/test/controller/Controller.js +++ b/test/controller/Controller.js @@ -241,7 +241,7 @@ describe("controller/src/Controller", function() { controller.config.set("global_chat.load_plugin", true); controller.pluginInfos[0] = { name: "global_chat", - requirePath: path.dirname(require.resolve("@clusterio/controller/package.json")), + packagePath: require.resolve("@clusterio/controller/package.json"), version: controllerVersion, }; }); diff --git a/test/host.js b/test/host.js index 4308c189..ad97bd83 100644 --- a/test/host.js +++ b/test/host.js @@ -123,7 +123,7 @@ describe("Host testing", function() { // This can be used more generally, but i did not want to interfere with handleSyncUserListsEvent const pluginInfos = [{ name: "global_chat", - requirePath: path.dirname(require.resolve("@clusterio/host/package.json")), + packagePath: require.resolve("@clusterio/host/package.json"), version: hostVersion, }]; const hostConfig = new lib.HostConfig("host", { "host.version": hostVersion }); diff --git a/test/lib/plugin_loader.js b/test/lib/plugin_loader.js index 02476e96..58281988 100644 --- a/test/lib/plugin_loader.js +++ b/test/lib/plugin_loader.js @@ -47,6 +47,7 @@ describe("lib/plugin_loader", function() { name: "test", version: "0.0.1", npmPackage: undefined, + packagePath: path.resolve(path.join(testPlugin, "package.json")), requirePath: path.resolve(testPlugin), webStaticPath: path.resolve(path.join(testPlugin, "dist", "web", "static")), }]