abpframework/abp · error · Exception

"public class" declaration not found!

Error message

"public class" declaration not found!

What it means

ModuleClassDependcyAdder.GetIndexOfWhereDependsOnWillBeAdded searches the module file text for the literal 'public class' and throws if IndexOf returns -1. Adding a [DependsOn] attribute requires inserting it immediately before the module class declaration; if the class is declared differently (static, internal, partial-first-line, file-scoped namespace reformatted, or file is empty/corrupt), the insertion point cannot be located.

Source

Thrown at framework/src/Volo.Abp.Cli.Core/Volo/Abp/Cli/ProjectModification/ModuleClassDependcyAdder.cs:47

        File.WriteAllText(path, file);
    }

    protected virtual string InsertDependsOnAttribute(string file, string moduleName)
    {
        var indexOfPublicClassDeclaration = GetIndexOfWhereDependsOnWillBeAdded(file);
        var dependsOnAttribute = GetDependsOnAttribute(moduleName);

        return file.Insert(indexOfPublicClassDeclaration, dependsOnAttribute);
    }

    protected virtual int GetIndexOfWhereDependsOnWillBeAdded(string file)
    {
        var indexOfPublicClassDeclaration = file.IndexOf("public class", StringComparison.Ordinal);

        if (indexOfPublicClassDeclaration < 0)
        {
            throw new Exception("\"public class\" declaration not found!");
        }

        return indexOfPublicClassDeclaration;
    }

    protected virtual string GetDependsOnAttribute(string moduleName)
    {
        return "[DependsOn(typeof(" + moduleName + "))]" + Environment.NewLine + "    ";
    }

    protected virtual void ParseModuleNameAndNameSpace(string module, out string nameSpace, out string moduleName)
    {
        var words = module?.Split('.');

        if (words == null || words.Length <= 1)
        {
            nameSpace = null;
            moduleName = module;

View on GitHub (pinned to 7ed43b1931)

Solutions

  1. Open the target module .cs file and ensure it contains a declaration literally starting with 'public class' (e.g. change 'internal class MyModule : AbpModule' to 'public class MyModule : AbpModule').
  2. Avoid 'public partial class' patterns that break the substring match, or add the [DependsOn] attribute manually above the class declaration.
  3. Re-run 'abp add-package <pkg>' once the declaration matches the expected pattern.

Example fix

// before
internal class MyProjectModule : AbpModule { }
// after
[DependsOn(typeof(AbpSomePackageModule))]
public class MyProjectModule : AbpModule { }
Defensive patterns

Strategy: validation

Validate before calling

var moduleSrc = File.ReadAllText(moduleFile);
if (!moduleSrc.Contains("public class", StringComparison.Ordinal))
{
    Console.Error.WriteLine($"{moduleFile} has no 'public class' declaration; add the [DependsOn] manually.");
    return;
}

Type guard

static bool HasPublicClassDeclaration(string source) =>
    source.Contains("public class", StringComparison.Ordinal);

Prevention

When it happens

Trigger: Calling ModuleClassDependcyAdder.Add (triggered by 'abp add-package' / ProjectNugetPackageAdder) on a file whose module class is declared 'internal class', 'public sealed partial class', or where the class declaration was altered so the literal substring 'public class' is absent.

Common situations: A module class declared 'public partial class' where 'public class' is split across the partial keyword; an 'internal' module class; a manually edited or auto-formatted file that changed visibility; a generated file that omits the public modifier.

Related errors


AI-assisted analysis of abpframework/abp@7ed43b1931 (2026-08-13). Data as JSON: /api/errors/cadf7b7234ced3ed. Report an issue: GitHub.