Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Compile pluginsLocalPaths array for bundle exclusion #7931

Open
wants to merge 1 commit into
base: main
Choose a base branch
from

Conversation

fredericbarthelet
Copy link
Contributor

Closes: #7908

@codecov-commenter
Copy link

codecov-commenter commented Jul 11, 2020

Codecov Report

Merging #7931 into master will decrease coverage by 0.02%.
The diff coverage is 57.14%.

Impacted file tree graph

@@            Coverage Diff             @@
##           master    #7931      +/-   ##
==========================================
- Coverage   88.30%   88.27%   -0.03%     
==========================================
  Files         244      245       +1     
  Lines        9082     9095      +13     
==========================================
+ Hits         8020     8029       +9     
- Misses       1062     1066       +4     
Impacted Files Coverage Δ
lib/plugins/aws/info/getStackInfo.js 87.75% <57.14%> (-0.62%) ⬇️
lib/plugins/aws/utils/resolveCfImportValue.js 57.14% <57.14%> (ø)

Continue to review full report at Codecov.

Legend - Click here to learn more
Δ = absolute <relative> (impact), ø = not affected, ? = missing data
Powered by Codecov. Last update 3ffa549...9016181. Read the comment docs.

@fredericbarthelet fredericbarthelet force-pushed the exclude-plugin-local-path-from-bundle branch 3 times, most recently from 7b75030 to a834eb7 Compare July 11, 2020 15:52
@fredericbarthelet fredericbarthelet force-pushed the exclude-plugin-local-path-from-bundle branch from a834eb7 to 9016181 Compare July 11, 2020 15:57
Copy link
Contributor

@medikoo medikoo left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Great thanks @fredericbarthelet for taking that. Solution looks very promising!

Still, please see my comment I believe we need to imply more intelligent handling to plugins referenced with relative paths.

// add local service plugins Paths
const pluginsLocalPaths = this.serverless.pluginManager
.getLocalPluginsPaths(this.serverless.service.plugins)
.map(pluginsLocalPath => `${pluginsLocalPath}/**`);
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This collection may contain both directory and file paths. e.g. when local plugin referenced as ./plugin, may actually point ./plugin.js. Therefore treating all paths as directories won't work well.

It's hard to have 100% bulletproof solution for that, but I think what could work best is:

  • Agree that getLocalPluginsPaths may return paths to directories, and files. To make distinction easy, let's ensure that all directory paths are postfixed with / (or system path separator - we need to be careful with Windows here)
  • In getLocalPluginsPaths return paths as follows
    • If there's a custom local path, return it with postfixed / (as it's clearly a directory path)
    • Resolve final paths to plugins referenced with relative paths (starting with ./), as it's done here:
      return require(cjsResolve(servicePath, pluginPath).realPath);
      . If path as input into configuration, points a directory (in which main plugin module lives) then return it postfixed with /, if path points a plugin file, then return it with fully resolved file extension.

Having above ensure glob rules are applied differently to directories and files

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Thank you for your review @medikoo

I understand your proposal, however IMHO it won't also be 100% bulletproof.

Consider the following plugin files :

|_ serverlessProjectRoot
    |_ serverless.yml
    |_ pluginDirectory
        |_ mySuperPlugin.js
        |_ utils.js

Consider as well the following configuration for this projet serverless service file :

plugins: ['./pluginDirectory/mySuperPlugin']

Using your proposal, we would in fact identify that mySuperPlugin should be resolved to ./pluginDirectory/mySuperPlugin.js instead of ./pluginDirectory/mySuperPlugin/ and therefore exclude this file rather than pluginDireectory/mySuperPlugin/** glob.
However, if this file locally requires utils.js, it will not be identified as a file to be excluded and will be shipped with lambda package. Correct ?

Achieving the same result, we could simply generate 2 exclusion blobs from the getLocalPluginsPaths method call : for each item of this list, one exact file resolution blob ${item}.js and one directory resultion blob ${item}/**. This would not require module resolution from the packageService.

With this solution and the same architecture from above, the exclusion list will include pluginDirectory/mySuperPlugin.js and pluginDirectory/mySuperPlugin/**, resulting in the same quantity of excluded files.

WDYT ?

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

However, if this file locally requires utils.js, it will not be identified as a file to be excluded and will be shipped with lambda package. Correct ?

Yes, agreed, it's exactly the reason I mentioned it's hard to have 100% bulletproof solution :)

for each item of this list, one exact file resolution blob ${item}.js and one directory resultion blob ${item}/**

This may work for most of the cases, still is immune to one flaw. Imagine user having both foo.js and foo folder which is expected to land in lambda. With above we will not package foo folder, and that will be clearly a bug on our side.

Of course it's an edge case, but one we can avoid by resolving the main plugin module path, and reasoning against that. It'll be nice to not open door for false positives.

@medikoo medikoo modified the milestone: v2 Aug 24, 2020
@medikoo medikoo added this to the v3-ready milestone Feb 26, 2021
@medikoo medikoo removed this from the v3-ready milestone Jun 7, 2021
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

Successfully merging this pull request may close these issues.

Content of local plugins referenced by relative path, are not excluded for lambda bundles
4 participants