stride3d/stride · error · ArgumentException

ShaderMixinSource must have a name

Error message

ShaderMixinSource must have a name

What it means

After resolving the ShaderMixinSource to compile, EffectCompilerBase.Compile requires its Name to be non-empty, because the name is used for cache keys, module naming and generated shader lookup. An anonymous mixin cannot be addressed, so an ArgumentException is thrown.

Solutions

  1. Set the Name property of the ShaderMixinSource to the shader class name before calling Compile.
  2. Populate Name from the primary ShaderClassSource.ClassName when wrapping class sources.
  3. Add an assertion/debug check on mixin.Name in your effect-building code.

Example fix

// before
var mixin = new ShaderMixinSource();
mixin.Mixins.Add(new ShaderClassSource("MyShader"));
// after
var mixin = new ShaderMixinSource { Name = "MyShader" };
mixin.Mixins.Add(new ShaderClassSource("MyShader"));
Defensive patterns

Strategy: validation

Validate before calling

if (mixin == null || string.IsNullOrEmpty(mixin.Name)) throw new InvalidOperationException("ShaderMixinSource must be named before Compile");

Type guard

bool HasName(ShaderMixinSource m) => !string.IsNullOrEmpty(m?.Name);

Try / catch

try { compiler.Compile(mixin, parameters); } catch (ArgumentException ex) when (ex.Message == "ShaderMixinSource must have a name") { /* set mixin.Name and retry */ }

Prevention

When it happens

Trigger: Calling Compile with a ShaderMixinSource whose Name property was never set (or set to null/empty string), typically when building mixins manually.

Common situations: Hand-constructed ShaderMixinSource objects in effect codegen or unit tests; object initializers that forget Name; deserialization losing the Name field.

Understand the failure class

Background: "must not be empty", "cannot be empty" — required-field validation errors across open-source libraries — this error's family across 41 libraries.

Related errors


AI-assisted analysis of stride3d/stride@96fad776d2 (2026-09-14). Data as JSON: /api/errors/a756906ee17bf35b. Report an issue: GitHub.

Appendix: source

Thrown at sources/shaders/Stride.Shaders.Effects/Compiler/EffectCompilerBase.cs:64

            }
            else
            {
                mixinToCompile = shaderSource as ShaderMixinSource;
                var shaderClassSource = shaderSource as ShaderClassCode;

                if (shaderClassSource != null)
                {
                    mixinToCompile = new ShaderMixinSource { Name = shaderClassSource.ClassName };
                    mixinToCompile.Mixins.Add(shaderClassSource);
                }

                if (mixinToCompile == null)
                {
                    throw new ArgumentException("Unsupported ShaderSource type [{0}]. Supporting only ShaderMixinSource/sdfx, ShaderClassSource", "shaderSource");
                }
                if (string.IsNullOrEmpty(mixinToCompile.Name))
                {
                    throw new ArgumentException("ShaderMixinSource must have a name", "shaderSource");
                }
            }

            // Compile the whole mixin tree
            var compilerResults = new CompilerResults { Module = $"EffectCompile [{mixinToCompile.Name}]" };
            var effectInputHash = ShaderMixinObjectId.Compute(mixinToCompile, compilerParameters.EffectParameters);
            var bytecode = Compile(mixinToCompile, effectParameters: compilerParameters.EffectParameters, compilerParameters, effectInputHash);

            // Since bytecode.Result is a struct, we check if any of its member has been set to know if it's valid
            if (bytecode.Result.CompilationLog is not null || bytecode.Task is not null)
            {
                bytecode.Result.CompilationLog?.CopyTo(compilerResults);
                compilerResults.Bytecode = bytecode;
                compilerResults.SourceParameters = new CompilerParameters(compilerParameters);
            }
            return compilerResults;
        }

View on GitHub (pinned to 96fad776d2)