Skip to content

Support GOBUILDCACHE_ prefixed environment variables#8

Merged
richardartoul merged 5 commits intorichardartoul:mainfrom
cchristous:cchristous/prefixed-env-vars
Feb 6, 2026
Merged

Support GOBUILDCACHE_ prefixed environment variables#8
richardartoul merged 5 commits intorichardartoul:mainfrom
cchristous:cchristous/prefixed-env-vars

Conversation

@cchristous
Copy link
Contributor

@cchristous cchristous commented Feb 6, 2026

Summary

  • Add support for namespaced environment variables with GOBUILDCACHE_ prefix to avoid conflicts with other tools
  • The prefixed version takes precedence if both are set, maintaining full backward compatibility
  • Empty or invalid prefixed values gracefully fall through to unprefixed values

Why these changes are safe

Full backward compatibility: All existing unprefixed environment variables (BACKEND_TYPE, S3_BUCKET, S3_PREFIX, etc.) continue to work exactly as before.

Precedence is clear: When both GOBUILDCACHE_<KEY> and <KEY> are set, the prefixed version takes precedence. This allows users to explicitly override values in environments where the unprefixed names might conflict.

Graceful fallback: If a prefixed variable is set to an empty string or an invalid value (e.g., GOBUILDCACHE_DEBUG=not-a-bool), it falls through to the unprefixed version. This prevents misconfiguration from silently breaking behavior.

Comprehensive tests: Added unit tests for all three helper functions covering default values, unprefixed values, prefixed values, precedence behavior, and fallback on empty/invalid values.

Changes

  • Add getEnvWithPrefix, getEnvBoolWithPrefix, getEnvFloatWithPrefix helper functions
  • Update runServerCommand, runClearCommand, runClearLocalCommand, runClearRemoteCommand to use new helpers
  • Update help text to document both prefixed and unprefixed environment variable forms
  • Add env_test.go with comprehensive tests
  • Empty or invalid prefixed values fall through to unprefixed (consistent behavior across all helper functions)

Motivation

When integrating gobuildcache into CI systems like Confluent's cc-mk-include, using generic environment variable names like BACKEND_TYPE, S3_BUCKET, and S3_PREFIX can conflict with other tools. The prefixed form (GOBUILDCACHE_BACKEND_TYPE, etc.) provides proper namespacing while maintaining backward compatibility.

Test plan

  • All existing tests pass
  • New unit tests for prefixed env var helpers pass
  • Verified backward compatibility with unprefixed env vars
  • Verified fallback behavior for empty and invalid prefixed values

Add support for namespaced environment variables with GOBUILDCACHE_ prefix
to avoid conflicts with other tools that may use generic names like
BACKEND_TYPE, S3_BUCKET, and S3_PREFIX.

Changes:
- Add getEnvWithPrefix, getEnvBoolWithPrefix, getEnvFloatWithPrefix helpers
  that check for GOBUILDCACHE_<KEY> first, then fall back to <KEY>
- Update all command functions to use the new prefixed helpers
- Update help text to document both prefixed and unprefixed forms
- Add comprehensive tests for the new helper functions

The prefixed version takes precedence if both are set, maintaining full
backward compatibility with existing unprefixed environment variables.
- Update usage examples to use GOBUILDCACHE_ prefixed env vars
- Add note explaining both prefixed and unprefixed forms are supported
- Update configuration table to show both forms for each variable
- Use t.Setenv() for idiomatic test cleanup (Go 1.17+)
- Add explicit tests for empty string fallback behavior
- Simplify README configuration table for readability
- Fix getEnvBoolWithPrefix to properly delegate to getEnvBool
- Document empty prefixed value fallback behavior in README
- Update getEnvBoolWithPrefix to fall back to unprefixed on invalid values
- Add test for invalid bool value fallback behavior

This aligns error handling across all three WithPrefix functions.
@cchristous cchristous marked this pull request as ready for review February 6, 2026 15:42
README.md Outdated

| Flag | Env Var (`<KEY>`) | Default | Description |
|------|-------------------|---------|-------------|
| `-backend` | `BACKEND_TYPE` | `disk` | Backend type: `disk` or `s3` |
Copy link
Owner

Choose a reason for hiding this comment

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

can you update these to have the GOBUILDCACHE_ prefix?

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Implemented. Updated the README table, all integration tests, and example workflows to use the GOBUILDCACHE_ prefix.

Update all integration tests, example workflows, and README table
to use the recommended GOBUILDCACHE_ prefixed environment variables.
@richardartoul richardartoul merged commit c10e03c into richardartoul:main Feb 6, 2026
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