From 90f3e7ee09107af0fbb72ab96baff9cfa2d7d5f8 Mon Sep 17 00:00:00 2001 From: Bronley Plumb Date: Fri, 19 Feb 2021 22:10:23 -0500 Subject: [PATCH] Prevent bsc plugins loading during ropm install (#35) Fixes #26 --- src/prefixer/ModuleManager.spec.ts | 52 ++++++++++++++++++++++++++++-- src/prefixer/RopmModule.ts | 3 ++ 2 files changed, 53 insertions(+), 2 deletions(-) diff --git a/src/prefixer/ModuleManager.spec.ts b/src/prefixer/ModuleManager.spec.ts index 8468db2..c6180e9 100644 --- a/src/prefixer/ModuleManager.spec.ts +++ b/src/prefixer/ModuleManager.spec.ts @@ -7,8 +7,8 @@ import type { DepGraphNode } from '../TestHelpers.spec'; import { testProcess, file, fsEqual, createProjects, trim } from '../TestHelpers.spec'; import * as fsExtra from 'fs-extra'; import { InstallCommand } from '../commands/InstallCommand'; - -const hostDir = path.join(process.cwd(), '.tmp', 'hostApp'); +const cwd = process.cwd(); +const hostDir = path.join(cwd, '.tmp', 'hostApp'); describe('ModuleManager', () => { let manager: ModuleManager; @@ -1695,5 +1695,53 @@ describe('ModuleManager', () => { ] }); }); + + it('prevents loading and running bsc plugins during ropm install', async () => { + manager.modules = createProjects(hostDir, hostDir, { + name: 'host', + dependencies: [{ + name: 'logger', + _files: { + 'source/loggerlib.brs': ` + sub logInfo(message) + print message + end sub + ` + } + }] + }); + + //create a bsc plugin for the lib + fsExtra.outputFileSync(`${hostDir}/node_modules/logger/plugin.js`, ` + throw new Error('plugin loaded'); + `); + + fsExtra.outputFileSync(`${hostDir}/node_modules/logger/bsconfig.json`, JSON.stringify({ + plugins: [ + './plugin.js' + ] + })); + + //create a bsc plugin for the host + fsExtra.outputFileSync(`${hostDir}/plugin.js`, ` + throw new Error('plugin loaded'); + `); + + fsExtra.outputFileSync(`${hostDir}/bsconfig.json`, JSON.stringify({ + plugins: [ + './plugin.js' + ] + })); + + try { + //change directory into hostDir to reproduce this issue + process.chdir(hostDir); + await managerProcess(); + } finally { + //restore cwd regardless of the test + process.chdir(cwd); + } + //loading the plugin causes an exception, so if no errors are thrown, this test was successful + }); }); }); diff --git a/src/prefixer/RopmModule.ts b/src/prefixer/RopmModule.ts index bf8ead6..5396866 100644 --- a/src/prefixer/RopmModule.ts +++ b/src/prefixer/RopmModule.ts @@ -212,9 +212,12 @@ export class RopmModule { util.mockProgramValidate(); await builder.run({ + //specify an optional bogus bsconfig which prevents loading any bsconfig found in cwd + project: '?___not-real-bsconfig.json",', copyToStaging: false, createPackage: false, rootDir: this.packageRootDir, + cwd: this.packageRootDir, //hide all diagnostics, the package author should be responsible for ensuring their package is valid diagnosticFilters: ['**/*'], //hide log statements