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
- Remove or rename the existing file at the target path before re-applying the codestart
- Run the codestart against a clean checkout so modified relative paths do not already exist
- 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
- Apply package-changing codestarts only once per project
- Run package rewrites on a clean git tree so changes are reviewable/revertable
- Pick unique package names to avoid pre-existing class collisions
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
- Target file already exists: + targetPath.toString()
- Target file already exists: + targetPath.toString()
- Multiple files found for path with executable FileStrategy:
- Multiple files found for path with 'fail-on-duplicate' FileS
- Multiple files found for path with 'fail-on-duplicate' FileS
AI-assisted analysis of quarkusio/quarkus@e1c734241f (2026-09-05).
Data as JSON: /api/errors/f84eaeb1592ff6b9.
Report an issue: GitHub.