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

fix: fix local builds with plugins and build settings from the ui #4929

Merged
merged 3 commits into from
Aug 17, 2022

Conversation

danez
Copy link
Contributor

@danez danez commented Aug 12, 2022

Summary

This is a long story. I was debugging yesterday for hours.

Problem

The initial problem was that I tried deploying a nextjs site from the cli and it did not work. The setup is as following:

  • project cloned from nextra a nextjs documentation application
  • No local config (netlify.toml)
  • in the netlify UI I configured github connection, build command and publish directory.
  • netlify link done

When I tried deploying this site, the nextjs-plugin would be installed automatically, but during the build at first the onPreBuild would run for the nextjs-plugin. Next the onBuild for the nextjs-plugin woudl run and this fails because the build command was not triggered at all.

Investigation

After hours of trying to understand @netlify/build and it's connection to the cli and to @netlify/config I found out what is happening. Let me try to summarize:

  1. The cli build command is triggered
  2. In the cli the base-command calls @netlify/config for the config and gets the correct config back (including ui settings)
  3. @netlify/build is called with the config supplied from the cli
  4. resolveInitialConfig is called in @netlify/build which respects the passed in config from the cli. all fine so far
  5. The onPreBuild event is triggered for the build plugin and the plugin makes changes to the config.
  6. After the plugin is finished, @netlify/build computes a config changeset called configMutation. This details exactly what the plugin changed in the config, in order to be able to print the changes to the output but also to be always able to reapply these changes again to a config object.
  7. We now call @netlify/config to get a fresh copy of the config and with the configMutations applied. Because we didn't supplied siteId in the first place this now failed to grab anything from the API which is site related. So at this point we basically revert the config to some default values (no build command, publish directory = root directory, etc.) and this is the reason that my build command is never run.

By adding the siteId to the input Options it all starts working again, and this is now inline with buildbot, which also supplies the siteId.

Alamy_A14A69-c-29b8790

@danez danez requested review from lukasholzer and a team August 12, 2022 09:40
@@ -20,6 +22,7 @@ const netlifyBuildPromise = import('@netlify/build')
*/
const getBuildOptions = ({ cachedConfig, options: { context, cwd, debug, dry, json, offline, silent }, token }) => ({
cachedConfig,
siteId: cachedConfig.siteInfo.id,
Copy link
Contributor Author

Choose a reason for hiding this comment

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

This, and this only is the fix, the rest is for the tests.

@github-actions github-actions bot added the type: bug code to address defects in shipped code label Aug 12, 2022
@github-actions
Copy link

github-actions bot commented Aug 12, 2022

📊 Benchmark results

Comparing with f1d1d91

Package size: 222 MB

(no change)

^  222 MB  222 MB  228 MB                                                                                 
│   ┌──┐    ┌──┐    ┌──┐   222 MB  222 MB  222 MB  222 MB  222 MB  222 MB  222 MB  222 MB  222 MB  222 MB 
│   |  |    |  |    |  |    ┌──┐    ┌──┐    ┌──┐    ┌──┐    ┌──┐    ┌──┐    ┌──┐    ┌──┐    ┌──┐    ┌──┐  
│   |  |    |  |    |  |    |  |    |  |    |  |    |  |    |  |    |  |    |  |    |  |    |  |    |▒▒|  
│   |  |    |  |    |  |    |  |    |  |    |  |    |  |    |  |    |  |    |  |    |  |    |  |    |▒▒|  
│   |  |    |  |    |  |    |  |    |  |    |  |    |  |    |  |    |  |    |  |    |  |    |  |    |▒▒|  
│   |  |    |  |    |  |    |  |    |  |    |  |    |  |    |  |    |  |    |  |    |  |    |  |    |▒▒|  
│   |  |    |  |    |  |    |  |    |  |    |  |    |  |    |  |    |  |    |  |    |  |    |  |    |▒▒|  
│   |  |    |  |    |  |    |  |    |  |    |  |    |  |    |  |    |  |    |  |    |  |    |  |    |▒▒|  
│   |  |    |  |    |  |    |  |    |  |    |  |    |  |    |  |    |  |    |  |    |  |    |  |    |▒▒|  
│   |  |    |  |    |  |    |  |    |  |    |  |    |  |    |  |    |  |    |  |    |  |    |  |    |▒▒|  
│   |  |    |  |    |  |    |  |    |  |    |  |    |  |    |  |    |  |    |  |    |  |    |  |    |▒▒|  
│   |  |    |  |    |  |    |  |    |  |    |  |    |  |    |  |    |  |    |  |    |  |    |  |    |▒▒|  
│   |  |    |  |    |  |    |  |    |  |    |  |    |  |    |  |    |  |    |  |    |  |    |  |    |▒▒|  
│   |  |    |  |    |  |    |  |    |  |    |  |    |  |    |  |    |  |    |  |    |  |    |  |    |▒▒|  
│   |  |    |  |    |  |    |  |    |  |    |  |    |  |    |  |    |  |    |  |    |  |    |  |    |▒▒|  
│   |  |    |  |    |  |    |  |    |  |    |  |    |  |    |  |    |  |    |  |    |  |    |  |    |▒▒|  
│   |  |    |  |    |  |    |  |    |  |    |  |    |  |    |  |    |  |    |  |    |  |    |  |    |▒▒|  
│   |  |    |  |    |  |    |  |    |  |    |  |    |  |    |  |    |  |    |  |    |  |    |  |    |▒▒|  
│   |  |    |  |    |  |    |  |    |  |    |  |    |  |    |  |    |  |    |  |    |  |    |  |    |▒▒|  
│   |  |    |  |    |  |    |  |    |  |    |  |    |  |    |  |    |  |    |  |    |  |    |  |    |▒▒|  
│   |  |    |  |    |  |    |  |    |  |    |  |    |  |    |  |    |  |    |  |    |  |    |  |    |▒▒|  
└───┴──┴────┴──┴────┴──┴────┴──┴────┴──┴────┴──┴────┴──┴────┴──┴────┴──┴────┴──┴────┴──┴────┴──┴────┴──┴──>
    T-12    T-11    T-10    T-9     T-8     T-7     T-6     T-5     T-4     T-3     T-2     T-1      T    
Legend

host: apiUrl.host,
}
options = { ...options, testOpts }
}
Copy link
Contributor Author

Choose a reason for hiding this comment

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

This is not nice,.
The host could be supplied by setting apiHost but the scheme cannot be changed besides the testOpts.

I'm also not sure if NETLIFY_API_URL is only for tests or if that is meant to be used by users?

Should I add apiScheme to @netlify/build? Or is it okay this way?

@@ -9,7 +9,6 @@ const { withSiteBuilder } = require('./utils/site-builder')

const defaultEnvs = {
NETLIFY_AUTH_TOKEN: 'fake-token',
NETLIFY_SITE_ID: 'site_id',
Copy link
Contributor Author

@danez danez Aug 12, 2022

Choose a reason for hiding this comment

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

I opted to not set it for every test here, because it does not really reflect real world scenarios.
If NETLIFY_SITE_ID is set this bug I'm fixing here never surfaces, because @netlify/build and @netlify/config both also read it.
But most users never set it probably.

I instead opted to add a new method to the site-builder withStateFile, which mimics netlify link

@danez danez self-assigned this Aug 12, 2022
Copy link
Contributor

@ericapisani ericapisani left a comment

Choose a reason for hiding this comment

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

Quite the journey to get this bug sorted. Thanks for the detailed summary and the fix, this LGTM 🚀

@danez danez added the automerge Add to Kodiak auto merge queue label Aug 17, 2022
@kodiakhq kodiakhq bot merged commit 56cfac8 into main Aug 17, 2022
@kodiakhq kodiakhq bot deleted the fix-local-build-with-plugins branch August 17, 2022 15:23
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
automerge Add to Kodiak auto merge queue type: bug code to address defects in shipped code
Projects
None yet
Development

Successfully merging this pull request may close these issues.

2 participants