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 can introduce empty environment variables #1232

Merged
merged 2 commits into from
Jun 29, 2020
Merged

Conversation

pda
Copy link
Member

@pda pda commented Jun 26, 2020

Background

The environment is captured before and after each hook runs; changes are detected and propagated to the rest of the build.

Problem

The current implementation does not detect newly introduced environment variables with an empty value.

This is due to the way Go's map type works; the key and value are statically typed, and accessing a key that does not exist returns the zero-value of the value type. That means for a map[string]string accessing a string key that doesn't exist returns ""; the zero-value of string is an empty string.

When a newly introduced FOO="" environment is detected, its empty-string value is compared to the pre-hook env["FOO"] which returns the empty string; this appears unchanged, so it's not included in the diff.

This is a problem if a hook attempts to declare (but not assign) a variable and a subsequent script running in set -u mode (crash on unbound variable access) tries to access it.

Solution

Go's map also returns a bool flag indicating whether the value was present. We were ignoring it, but now we're not.

Now, when a newly introduced empty environment variable is checked, its absence in the pre-hook environment causes it to be included in the diff.

Verification

env.Environment.Diff() unit test coverage has been added.

I've run a sample build where an agent environment hook sets an empty var; prior to this patch it did not propagate to the build step command, now it does.

@pda pda requested review from yob and jayco June 26, 2020 06:07
Copy link

@jayco jayco left a comment

Choose a reason for hiding this comment

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

LGTM

@pda pda self-assigned this Jun 26, 2020
@pda pda requested a review from lox June 26, 2020 07:05
Copy link
Contributor

@yob yob left a comment

Choose a reason for hiding this comment

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

I reverted the code change and confirmed the new test failed as expected.

Nice PR description to diff length ratio too 🎉

@pda pda merged commit 6a9da7c into master Jun 29, 2020
@pda pda deleted the env-diff-empty-strings branch June 29, 2020 05:37
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.

3 participants