Skip to content

Commit

Permalink
Do not infer for strict mode.
Browse files Browse the repository at this point in the history
  • Loading branch information
nathanhammond committed Apr 11, 2023
1 parent c51cee8 commit 90b302c
Show file tree
Hide file tree
Showing 2 changed files with 26 additions and 6 deletions.
25 changes: 19 additions & 6 deletions cli/internal/taskhash/taskhash.go
Expand Up @@ -380,17 +380,30 @@ func (th *Tracker) CalculateTaskHash(packageTask *nodes.PackageTask, dependencyS
}

var keyMatchers []string
framework := inference.InferFramework(packageTask.Pkg)
if framework != nil && framework.EnvMatcher != "" {
// log auto detected framework and env prefix
logger.Debug(fmt.Sprintf("auto detected framework for %s", packageTask.PackageName), "framework", framework.Slug, "env_prefix", framework.EnvMatcher)
keyMatchers = append(keyMatchers, framework.EnvMatcher)
var framework *inference.Framework
envVarContainingExcludePrefix := ""

// In strict environment variable mode we don't do framework env var inference.
// You must instead:
// - Specify all environment variables that you want available in your build environment.
// - Specify whether you want those considered for the hash or merely passed through.
//
// This allows advanced users to improve both cache hit ratio _and_ correctness with
// the tradeoff of increased configuration verbosity.
if packageTask.EnvMode != util.Strict {
envVarContainingExcludePrefix = "TURBO_CI_VENDOR_ENV_KEY"
framework := inference.InferFramework(packageTask.Pkg)
if framework != nil && framework.EnvMatcher != "" {
// log auto detected framework and env prefix
logger.Debug(fmt.Sprintf("auto detected framework for %s", packageTask.PackageName), "framework", framework.Slug, "env_prefix", framework.EnvMatcher)
keyMatchers = append(keyMatchers, framework.EnvMatcher)
}
}

envVars, err := env.GetHashableEnvVars(
packageTask.TaskDefinition.EnvVarDependencies,
keyMatchers,
"TURBO_CI_VENDOR_ENV_KEY",
envVarContainingExcludePrefix,
)
if err != nil {
return "", err
Expand Down
7 changes: 7 additions & 0 deletions docs/pages/repo/docs/core-concepts/caching.mdx
Expand Up @@ -261,6 +261,13 @@ To alter the cache for _all_ tasks, you can declare environment variables in the

### Automatic environment variable inclusion

<Callout type="warning">
Turborepo only does automatic environment variable inclusion when the environment variable mode is set to `loose` or when using the default environment variable mode of `infer` without specifying `passthroughEnv`.

In `strict` environment variable mode you must specify every environment variable you want available in your execution environment via `env` or `globalEnv` if you want them considered for the hash and `passthroughEnv` or `globalPassthroughEnv` if not.

</Callout>

To help ensure correct caching across environments, Turborepo automatically infers and includes public environment variables when calculating cache keys for apps built with detected frameworks. You can safely omit framework-specific public environment variables from `turbo.json`:

```diff filename="turbo.json"
Expand Down

0 comments on commit 90b302c

Please sign in to comment.