diff --git a/lib/services/extensibility-service.ts b/lib/services/extensibility-service.ts index 9c2884c534..6c569469d0 100644 --- a/lib/services/extensibility-service.ts +++ b/lib/services/extensibility-service.ts @@ -508,11 +508,25 @@ export class ExtensibilityService implements IExtensibilityService { extensionName: string, ): Promise { this.$logger.trace(`Asserting extension ${extensionName} is installed.`); - const installedExtensions = this.$fs.readDirectory( - path.join(this.pathToExtensions, constants.NODE_MODULES_FOLDER_NAME), + const nodeModulesPath = path.join( + this.pathToExtensions, + constants.NODE_MODULES_FOLDER_NAME, ); + const installedExtensions = this.$fs.readDirectory(nodeModulesPath); + + let isInstalled: boolean; + if (extensionName.startsWith("@") && extensionName.includes("/")) { + const [scope, pkgName] = extensionName.split("/"); + isInstalled = + installedExtensions.indexOf(scope) !== -1 && + this.$fs + .readDirectory(path.join(nodeModulesPath, scope)) + .indexOf(pkgName) !== -1; + } else { + isInstalled = installedExtensions.indexOf(extensionName) !== -1; + } - if (installedExtensions.indexOf(extensionName) === -1) { + if (!isInstalled) { this.$logger.trace( `Extension ${extensionName} is not installed, starting installation.`, ); diff --git a/test/extension-manifests.ts b/test/extension-manifests.ts index 6f5f5ce1db..cfacb3c733 100644 --- a/test/extension-manifests.ts +++ b/test/extension-manifests.ts @@ -1023,4 +1023,51 @@ describe("extension manifests", () => { assert.deepEqual(capture.loadedModules, ["defaults-default"]); }); }); + + describe("scoped extensions", () => { + it("loads an installed scoped extension lazily without reinstalling", async () => { + const extensionName = "@nsm/scoped-ext"; + const contractsPath = require.resolve("../lib/contracts"); + writeExtension( + extensionName, + { commands: { "nsmscoped|run": "./dist/run.js" } }, + { + "main.js": mainModule("scoped-main"), + "dist/run.js": `const { defineCommand } = require(${JSON.stringify(contractsPath)}); + global.__nsmCapture.loadedModules.push("scoped-run"); + module.exports = defineCommand({ + name: "nsmscoped|run", + params: "any", + async run(ctx) { + global.__nsmCapture.executed.push({ marker: "scoped-run", args: ctx.args }); + }, + });`, + }, + ); + + let installCalled = false; + testInjector.register("packageManager", { + install: async () => { + installCalled = true; + throw new Error("should not reinstall an installed scoped extension"); + }, + }); + + const extensibilityService = resolveService(testInjector); + const data = await extensibilityService.loadExtension(extensionName); + + assert.isFalse(installCalled); + assert.equal(data.extensionName, extensionName); + assert.deepEqual(capture.loadedModules, []); + + const command = testInjector.resolveCommand("nsmscoped|run"); + assert.isOk(command); + assert.deepEqual(capture.loadedModules, ["scoped-run"]); + + await command.execute(["ok"]); + assert.deepEqual(capture.executed, [ + { marker: "scoped-run", args: ["ok"] }, + ]); + }); + }); }); diff --git a/test/services/extensibility-service.ts b/test/services/extensibility-service.ts index a6b1970ee5..15315bd0f8 100644 --- a/test/services/extensibility-service.ts +++ b/test/services/extensibility-service.ts @@ -409,6 +409,96 @@ describe("extensibilityService", () => { assert.isTrue(isNpmInstallCalled); }); + it("loads already-installed scoped extensions without re-running npm install", async () => { + const testInjector = getTestInjector(); + const fs: IFileSystem = testInjector.resolve("fs"); + const extensionNames = ["@scope/extension1", "extension2"]; + fs.exists = (): boolean => true; + fs.readDirectory = (dir: string): string[] => { + if (path.basename(dir) === constants.NODE_MODULES_FOLDER_NAME) { + return ["@scope", "extension2"]; + } + if (path.basename(dir) === "@scope") { + return ["extension1"]; + } + return []; + }; + + mockFsReadJson(testInjector, extensionNames); + + let isNpmInstallCalled = false; + const npm: INodePackageManager = testInjector.resolve("npm"); + npm.install = async (): Promise => { + isNpmInstallCalled = true; + return {}; + }; + + const expectedResults: IExtensionData[] = _.map( + extensionNames, + (extensionName) => ({ + extensionName, + version: "1.0.0", + pathToExtension: getExpectedInstallationPathForExtension( + testInjector, + extensionName, + ), + docs: undefined as any, + }), + ); + + const extensibilityService: IExtensibilityService = + testInjector.resolve(ExtensibilityService); + const actualResult = await Promise.all( + extensibilityService.loadExtensions(), + ); + assert.deepStrictEqual(actualResult, expectedResults); + assert.isFalse(isNpmInstallCalled); + }); + + it("installs a missing scoped extension when its scope directory exists without the package", async () => { + const testInjector = getTestInjector(); + const fs: IFileSystem = testInjector.resolve("fs"); + const extensionName = "@scope/missing-ext"; + fs.exists = (pathToCheck: string): boolean => + pathToCheck !== path.resolve(extensionName); + fs.readDirectory = (dir: string): string[] => { + if (path.basename(dir) === constants.NODE_MODULES_FOLDER_NAME) { + return ["@scope"]; + } + if (path.basename(dir) === "@scope") { + return ["other-ext"]; + } + return []; + }; + + mockFsReadJson(testInjector, [extensionName]); + + const installedPackages: string[] = []; + const npm: INodePackageManager = testInjector.resolve("npm"); + npm.install = async (packageName: string): Promise => { + installedPackages.push(packageName); + return { name: packageName }; + }; + + const extensibilityService: IExtensibilityService = + testInjector.resolve(ExtensibilityService); + const actualResult = await Promise.all( + extensibilityService.loadExtensions(), + ); + assert.deepStrictEqual(installedPackages, [extensionName]); + assert.deepStrictEqual(actualResult, [ + { + extensionName, + version: "1.0.0", + pathToExtension: getExpectedInstallationPathForExtension( + testInjector, + extensionName, + ), + docs: undefined as any, + }, + ]); + }); + it("rejects only promises for extensions that cannot be loaded", async () => { const testInjector = getTestInjector(); const fs: IFileSystem = testInjector.resolve("fs");