quarkusio/quarkus · error · CodestartStructureException

File already exists: + targetPath.toString()

Error message

File already exists: + targetPath.toString()

What it means

SmartPackageFileStrategyHandler throws CodestartStructureException when the Java package file (e.g. a class moved/renamed by package change) it is about to write already exists at targetDirectory.resolve(modifiedRelativePath). The handler computes a modified relative path based on package data, then refuses to overwrite an existing file. This prevents silent clobbering of existing sources during package refactor codestarts.

Source

Thrown at independent-projects/tools/codestarts/src/main/java/io/quarkus/devtools/codestarts/core/strategy/SmartPackageFileStrategyHandler.java:59

        // TODO this is a temporary workaround, we need change how this work to support having multiple processors for one file
        if (modifiedRelativePath.contains("/native-test/")) {
            final boolean isMaven = CodestartData.getBuildtool(data).filter(b -> Objects.equals(b, "maven")).isPresent();
            if (isMaven) {
                modifiedRelativePath = modifiedRelativePath.replace("/native-test/", "/test/");
            }
        }

        if (modifiedRelativePath.contains("/integrationTest/")) {
            final boolean isMaven = CodestartData.getBuildtool(data).filter(b -> Objects.equals(b, "maven")).isPresent();
            if (isMaven) {
                modifiedRelativePath = modifiedRelativePath.replace("/integrationTest/", "/test/");
            }
        }

        final Path targetPath = targetDirectory.resolve(modifiedRelativePath);

        if (Files.exists(targetPath)) {
            throw new CodestartStructureException(
                    "File already exists: " + targetPath.toString());
        }

        createDirectories(targetPath);
        writeFile(targetPath, content);
    }

    static String refactorPackage(String content, String packageName) {
        return content.replaceAll(REPLACE_REGEX, "$1 " + packageName);
    }
}

View on GitHub (pinned to e1c734241f)

Solutions

  1. Remove or rename the existing file at the target path before re-applying the codestart
  2. Run the codestart against a clean checkout so modified relative paths do not already exist
  3. Choose a different target package name so the computed path is unoccupied

Example fix

// before
src/main/java/com/example/app/MyService.java already exists; re-running package codestart -> throws

// after
git checkout -- .   // or delete target file, then re-apply codestart
Defensive patterns

Strategy: validation

Validate before calling

Path targetPath = targetDirectory.resolve(modifiedRelativePath);
if (Files.exists(targetPath)) {
    throw new IllegalStateException("Package rewrite would overwrite: " + targetPath);
}

Try / catch

try {
    handler.process(targetDir, relativePath, codestartFiles, data);
} catch (CodestartStructureException e) {
    if (e.getMessage().startsWith("File already exists")) {
        // compare existing file with generated content before overwriting manually
    } else throw e;
}

Prevention

When it happens

Trigger: Applying a codestart that maps sources into a new package when a file already exists at the modified relative path (e.g. re-running after a previous application, or user code already occupies that package path).

Common situations: Changing the quarkus package name (e.g. via codestart data) twice; a project where the target package/class already exists from prior scaffolding or manual code.

Related errors


AI-assisted analysis of quarkusio/quarkus@e1c734241f (2026-09-05). Data as JSON: /api/errors/f84eaeb1592ff6b9. Report an issue: GitHub.