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

Hooks executed twice with serverless-offline #38

Open
bboure opened this issue Oct 9, 2021 · 2 comments
Open

Hooks executed twice with serverless-offline #38

bboure opened this issue Oct 9, 2021 · 2 comments

Comments

@bboure
Copy link

bboure commented Oct 9, 2021

Hi there!

It seems like this plugin when used with serverless-offline executes some Serverless lifecycle hooks twice. Specifically, this line. I am not familiar with that method, but I think it is the programmatic equivalent of doing sls export-env, which will (re)execute all hooks.

I came across this issue because I also use lift. When executing sls offline start, I get an error that is thrown here, which probably indicates that the initialize hook is executed twice.

If I comment this line, the problem goes away.

I think that initOfflineHook() should probably call the internal functions (["collect", "resolve", "apply", "write") programmatically instead. It will not only fix this issue but also avoid useless double code execution.

As a side note, I tried to solve this issue for me by setting enableOffline to false. It didn't work because the command (export-env) is executed anyway and the check is done later.

Thanks!

@arabold
Copy link
Owner

arabold commented Oct 9, 2021

Hi @bboure , thanks for the report. I haven't used lift myself, so there might be some interdependency that causes this problem. But I just ran both sls offline and sls offline start in a simple local test project and couldn't reproduce the hooks being invoked multiple times. The order of function calls is as follows:

  1. initOfflineHook -- This triggers the rest of the hooks
  2. collectEnvVars -- Collect the environment variables from the Serverless configuration
  3. _loadConfig
  4. resolveEnvVars -- Resolves the environment variables by looking up the referenced AWS resources
  5. _loadConfig
  6. setEnvVars -- Set the environment variables on the Serverless instance
  7. _loadConfig
  8. writeEnvVars -- Writes the environment variables to the output file (only if not running in offline mode)
  9. _loadConfig

the _loadConfig is used excessively which could be optimized, but it doesn't cause any issues. Every other hook function is invoked only exactly once. writeEnvVars is called always, but it doesn't update the local .env file in case sls offline is run.

The line in question is necessary to trigger the other hooks. By removing it, you're essentially disabling the serverless-export-env plugi when serverless-offline is run - the same as setting enableOffline: false. In that case, none of the functions above will do anything as they check the internal isEnabled state.

Are you able to setup a minimal test project that reproduces the behavior you're seeing?

@bboure
Copy link
Author

bboure commented Oct 10, 2021

Thanks for the feedback @arabold

What runs twice is hoks from other plugins, such as initialize.

I just created a demo repo that reproduces this issue.

If you run sls offline start on that repo, you will see that the initialize hook is executed twice.
I included a mock plugin to demonstrate that 👇

image

If you uncomment the serverless-lift plugin in servertless.yml, you will see that it throws the aforementioned error.

image.

After some more investigation, it seems like the hook is executed here.

I am now wondering if this is really an issue from this plugin or maybe Lift that should support more hook calls; or even a serverless framework issue that should not call init twice, when pluginManager.run() is called programmatically?

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

No branches or pull requests

2 participants