RicoSuter/NSwag · error · InvalidOperationException

More than one project was found in directory

Error message

More than one project was found in directory '{path}'. Specify one using its file name.

What it means

When the Project argument resolves to a directory containing more than one *.csproj file, FindProject refuses to guess and throws this error, asking the caller to name the exact project file. This ambiguity check exists because NSwag can only generate a spec from a single executable project.

Solutions

  1. Specify the exact project file: `/project:src/MyApi/MyApi.csproj` instead of a directory.
  2. Change the working directory to the folder containing only the desired project and run without /project.
  3. Move helper/test csproj files into their own directories if you want directory-based resolution to work.
  4. In an nswag.json config, set the `project` property to the full csproj path.

Example fix

// before
nswag aspnetcore2openapi /project:src
// after
nswag aspnetcore2openapi /project:src/MyApi/MyApi.csproj
Defensive patterns

Strategy: validation

Validate before calling

var csprojs = Directory.GetFiles(dir, "*.csproj");
if (csprojs.Length > 1)
    Console.WriteLine($"Ambiguous directory {dir}: pass one of {string.Join(", ", csprojs)} explicitly.");

Try / catch

try
{
    await command.RunAsync(processor, host);
}
catch (InvalidOperationException ex) when (ex.Message.Contains("More than one project was found"))
{
    // retry with the exact csproj file path
}

Prevention

When it happens

Trigger: Calling `nswag aspnetcore2openapi /project:someDir` (or running in the current directory) where `Directory.GetFiles(path, "*.csproj")` returns 2+ files — e.g. a folder with both the API project and a test/helper csproj.

Common situations: Solution folders that keep main and test projects in the same directory; legacy non-SDK-style layouts with multiple csproj in one folder; monorepo checkouts where the working directory aggregates several projects.

Related errors


AI-assisted analysis of RicoSuter/NSwag@63daf8fcc3 (2026-09-14). Data as JSON: /api/errors/a6b9e82166b19ddf. Report an issue: GitHub.

Appendix: source

Thrown at src/NSwag.Commands/Commands/Generation/AspNetCore/ProjectMetadata.cs:66

        {
            if (path == null)
            {
                path = Directory.GetCurrentDirectory();
            }
            else if (!Directory.Exists(path))
            {
                // It's not a directory. Treat the input path as the project file.
                return path;
            }

            var projectFiles = Directory.GetFiles(path, "*.csproj");
            if (projectFiles.Length == 0)
            {
                throw new InvalidOperationException($"No project (.csproj) file could be found under directory {path}.");
            }
            else if (projectFiles.Length > 1)
            {
                throw new InvalidOperationException($"More than one project was found in directory '{path}'. Specify one using its file name.");
            }

            return projectFiles[0];
        }

        public static async Task<ProjectMetadata> GetProjectMetadata(
            string file,
            string buildExtensionsDir,
            string framework,
            string configuration,
            string runtime,
            bool noBuild,
            string outputPath,
            IConsoleHost console = null)
        {
            Debug.Assert(!string.IsNullOrEmpty(file), "file is null or empty.");

            var args = CreateMsBuildArguments(file, framework, configuration, runtime, noBuild, outputPath);

View on GitHub (pinned to 63daf8fcc3)