Skip to content

Commit

Permalink
fix: plugin module resolution works unexpectedly (#3089)
Browse files Browse the repository at this point in the history
In theory, plugin module resolution should work based on where the plugin file is located. Previously it was pulling dependencies from (I think?) the CLI's node_modules. I'm not actually totally sure, but I know it wasn't resolving them based on the actual plugin file location.

This change creates a `require` to invoke the plugin, very similar to the way we do it for preflight execution. This change on it's own broke the plugin e2e tests (because that folder did not have a node_modules with `@cdktf/provider-aws` in it), so I also moved them to `examples/tests/valid/plugins`, that way they can use the node_modules from the valid tests.

## Checklist

- [x] Title matches [Winglang's style guide](https://docs.winglang.io/contributing/pull_requests#how-are-pull-request-titles-formatted)
- [x] Description explains motivation and solution
- [ ] Tests added (always)
- [ ] Docs updated (only required for features)
- [ ] Added `pr/e2e-full` label if this feature requires end-to-end testing

*By submitting this pull request, I confirm that my contribution is made under the terms of the [Monada Contribution License](https://docs.winglang.io/terms-and-policies/contribution-license.html)*.
  • Loading branch information
MarkMcCulloh authored Jun 26, 2023
1 parent f8ad560 commit 95d4163
Show file tree
Hide file tree
Showing 9 changed files with 76 additions and 198 deletions.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
10 changes: 9 additions & 1 deletion libs/wingsdk/src/core/plugin-manager.ts
Original file line number Diff line number Diff line change
Expand Up @@ -61,8 +61,16 @@ export class PluginManager {

const pluginDir = dirname(pluginAbsolutePath);

const modulePaths = module.paths ?? [__dirname];

const requireResolve = (path: string) =>
require.resolve(path, { paths: [...modulePaths, pluginDir] });
// eslint-disable-next-line @typescript-eslint/no-require-imports
const pluginRequire = (path: string) => require(requireResolve(path));
pluginRequire.resolve = requireResolve;

const context = vm.createContext({
require,
require: pluginRequire,
console,
exports: hooks,
process,
Expand Down
Loading

0 comments on commit 95d4163

Please sign in to comment.