{"record":{"id":"71e080dadc0f088d","repo":"dagger/dagger","slug":"failed-to-evaluate-cache-argument-s-w-71e080","errorCode":null,"errorMessage":"failed to evaluate @cache argument %s: %w","messagePattern":"failed to evaluate @cache argument (.+?): %w","errorType":"exception","errorClass":null,"httpStatus":null,"severity":"error","filePath":"core/sdk/dang/v2/helpers.go","lineNumber":735,"sourceCode":"\t\tcase \"cache\":\n\t\t\tsel, err := cacheDirectiveSelector(ctx, env, directive)\n\t\t\tif err != nil {\n\t\t\t\treturn nil, err\n\t\t\t}\n\t\t\tsels = append(sels, sel)\n\t\t}\n\t}\n\treturn sels, nil\n}\n\n// cacheDirectiveSelector converts a @cache directive into a withCachePolicy selector.\nfunc cacheDirectiveSelector(ctx context.Context, env dang.ValueScope, directive *dang.DirectiveApplication) (dagql.Selector, error) {\n\tvar policy core.FunctionCachePolicy\n\tvar ttl string\n\tfor _, arg := range directive.Args {\n\t\tval, err := evalDirectiveArg(ctx, env, arg.Value)\n\t\tif err != nil {\n\t\t\treturn dagql.Selector{}, fmt.Errorf(\"failed to evaluate @cache argument %s: %w\", arg.Key, err)\n\t\t}\n\t\tswitch arg.Key {\n\t\tcase \"policy\":\n\t\t\tif s, ok := val.(string); ok {\n\t\t\t\tpolicy = core.FunctionCachePolicy(s)\n\t\t\t}\n\t\tcase \"ttl\":\n\t\t\tif s, ok := val.(string); ok {\n\t\t\t\tttl = s\n\t\t\t}\n\t\t}\n\t}\n\tif policy == \"\" {\n\t\tpolicy = core.FunctionCachePolicyDefault\n\t}\n\targs := []dagql.NamedInput{\n\t\t{Name: \"policy\", Value: policy},\n\t}","sourceCodeStart":717,"sourceCodeEnd":753,"githubUrl":"https://github.com/dagger/dagger/blob/82ba2681dbe30d3547a1dc50ea495900ab5b6047/core/sdk/dang/v2/helpers.go#L717-L753","documentation":"This error wraps any failure that occurs while evaluating an argument passed to the @cache directive in a Dang function definition. The Dang SDK evaluates @cache directive arguments (policy, ttl) at function-creation time via evalDirectiveArg; if that evaluation fails (e.g. the expression errors, references undefined names, or produces an invalid value), the failure is wrapped with the offending argument key so the developer knows which @cache argument was bad.","triggerScenarios":"Declaring a function with @cache(policy: ..., ttl: ...) where the argument expression fails to evaluate — e.g. referencing an undefined variable, calling a function that errors, or a literal that cannot be resolved in the current ValueScope.","commonSituations":"Typo in a variable name inside a @cache argument; using a non-literal expression that depends on runtime values not available when the module is loaded; copying an example with ttl/policy expressions that reference helpers not imported in scope.","solutions":["Fix the underlying evaluation error reported by the wrapped %w cause — it names the real failure","Simplify @cache arguments to plain literals, e.g. @cache(policy: \"per-call\", ttl: \"5m\")","Check that any identifiers used in the @cache argument are defined and imported in the module scope","Verify the argument key is one the directive supports (policy, ttl)"],"exampleFix":"// before\n@cache(policy: cachePolicy, ttl: ttlFromEnv)\nfunc build(): Container { ... }\n// after\n@cache(policy: \"per-call\", ttl: \"5m\")\nfunc build(): Container { ... }","handlingStrategy":"validation","validationCode":"// Prefer literal arguments in @cache so evaluation cannot fail:\n// @cache(policy: \"per-call\", ttl: \"5m\")\nfunc validateCacheArgs(policy any, ttl any) error {\n\tif policy != nil {\n\t\tif _, ok := policy.(string); !ok {\n\t\t\treturn fmt.Errorf(\"@cache policy must be a string, got %T\", policy)\n\t\t}\n\t}\n\tif ttl != nil {\n\t\tif _, ok := ttl.(string); !ok {\n\t\t\treturn fmt.Errorf(\"@cache ttl must be a string, got %T\", ttl)\n\t\t}\n\t}\n\treturn nil\n}","typeGuard":"func isString(v any) bool { _, ok := v.(string); return ok }","tryCatchPattern":"sel, err := cacheDirectiveSelector(ctx, env, directive)\nif err != nil {\n\tvar evalErr *evalError\n\tif errors.As(err, &evalErr) {\n\t\t// fall back to default cache policy or skip function creation\n\t}\n\treturn fmt.Errorf(\"cache directive on function: %w\", err)\n}","preventionTips":["Use plain string literals for @cache policy and ttl","Keep directive arguments free of runtime-dependent expressions","Verify identifiers used in directive arguments exist in module scope"],"tags":["dang","directive","cache","evaluation"],"backgroundTag":"directive-argument-evaluation-failed","analyzedSha":"82ba2681dbe30d3547a1dc50ea495900ab5b6047","analyzedAt":"2026-09-05T07:21:37.930Z","contentChangedAt":"2026-09-05T07:21:37.930Z","schemaVersion":2},"datasetVersion":"2026-09-12T12:17:11.808Z"}