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: set Blobs context on the process #6284

Merged
merged 7 commits into from
Jan 11, 2024
Merged

fix: set Blobs context on the process #6284

merged 7 commits into from
Jan 11, 2024

Conversation

eduardoboucas
Copy link
Member

Summary

To make Blobs available in Netlify Dev, we're currently relying on the functions bootstrap layer to receive the data in the event and populate the context environment variable. This follows what happens in production.

However, there are situations where a framework's own dev server will handle requests instead of the CLI server, and the Blobs context will not be available there because requests aren't flowing through our bootstrap layer.

When deployed, that framework code will be translated to a Netlify Function, where the context will be available, so the fact that it's not available in the CLI is an inconsistency.

With this PR, CLI takes over the responsibility of setting the Blobs context, which it does by setting the environment variable on the parent process, which means framework servers will get access to it.

To maximise parity with production, we hide the context from any functions in the Lambda compatibility mode (aka a v1 function), where the context is not available in production.

The only situation where we will have inconsistencies is if a framework generates a v1 function, since the context will now be available locally but not in production. There's not much we can do to detect this situation, though.

@eduardoboucas eduardoboucas requested a review from a team as a code owner January 5, 2024 10:59
Copy link

github-actions bot commented Jan 5, 2024

📊 Benchmark results

Comparing with 42c8805

  • Dependency count: 1,295 (no change)
  • Package size: 309 MB (no change)
  • Number of ts-expect-error directives: 1,182 (no change)

@@ -8,6 +9,8 @@ import { getPathInProject } from '../settings.js'

let hasPrintedLocalBlobsNotice = false

export const BLOBS_CONTEXT_VARIABLE = 'NETLIFY_BLOBS_CONTEXT'
Copy link
Member Author

Choose a reason for hiding this comment

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

Ideally @netlify/blobs should export this.

Copy link
Contributor

@Skn0tt Skn0tt left a comment

Choose a reason for hiding this comment

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

Code looks good. Could we add a test for this? Here's an example for how the framework server test could look: https://github.com/netlify/cli/pull/6226/files#diff-c119c8fa97b1f6adc793b45e9b5cd627da0ea828513cfbf8444eba89c2312839

// be available to the Lambda. This would be inconsistent with production
// where only V2 functions get the context injected. To fix it, unset the
// context variable before invoking the function.
[BLOBS_CONTEXT_VARIABLE]: undefined,
Copy link
Contributor

Choose a reason for hiding this comment

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

Looking at the implementation of this, it seems like it mutates process.env:

https://github.com/ashiina/lambda-local/blob/95fb3dfd18ec51014d3cc5d9f405e21a5d784a50/src/lambdalocal.ts#L250-L252

This leads to weird side-effects, like the env variable vanishing upon the first V1 Function execution. In this specific instance, it's probably alright because the framework child process was started earlier, and because V2 Functions don't rely on this env variable. But might be worth adding a comment.

@eduardoboucas
Copy link
Member Author

Code looks good. Could we add a test for this? Here's an example for how the framework server test could look: https://github.com/netlify/cli/pull/6226/files#diff-c119c8fa97b1f6adc793b45e9b5cd627da0ea828513cfbf8444eba89c2312839

Great call. Can I do it as a follow-up once your PR is merged so that I can make it part of your test? Integration tests are a bit expensive so reusing one here would be beneficial, I think.

@Skn0tt
Copy link
Contributor

Skn0tt commented Jan 8, 2024

Sounds good, yeah!

@eduardoboucas
Copy link
Member Author

@Skn0tt Added the test in ddadb62.

@Skn0tt
Copy link
Contributor

Skn0tt commented Jan 8, 2024

Nice! Going to approve, what's your thoughts on #6284 (comment)?

Skn0tt
Skn0tt previously approved these changes Jan 8, 2024
@Skn0tt Skn0tt enabled auto-merge (squash) January 11, 2024 13:22
@Skn0tt Skn0tt merged commit ecd4ad3 into main Jan 11, 2024
37 checks passed
@Skn0tt Skn0tt deleted the fix/blobs-context branch January 11, 2024 13:36
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

2 participants