{"record":{"id":"f84eaeb1592ff6b9","repo":"quarkusio/quarkus","slug":"file-already-exists-targetpath-tostring","errorCode":null,"errorMessage":"File already exists: + targetPath.toString()","messagePattern":"File already exists: \\+ targetPath\\.toString\\(\\)","errorType":"exception","errorClass":"CodestartStructureException","httpStatus":null,"severity":"error","filePath":"independent-projects/tools/codestarts/src/main/java/io/quarkus/devtools/codestarts/core/strategy/SmartPackageFileStrategyHandler.java","lineNumber":59,"sourceCode":"        // TODO this is a temporary workaround, we need change how this work to support having multiple processors for one file\n        if (modifiedRelativePath.contains(\"/native-test/\")) {\n            final boolean isMaven = CodestartData.getBuildtool(data).filter(b -> Objects.equals(b, \"maven\")).isPresent();\n            if (isMaven) {\n                modifiedRelativePath = modifiedRelativePath.replace(\"/native-test/\", \"/test/\");\n            }\n        }\n\n        if (modifiedRelativePath.contains(\"/integrationTest/\")) {\n            final boolean isMaven = CodestartData.getBuildtool(data).filter(b -> Objects.equals(b, \"maven\")).isPresent();\n            if (isMaven) {\n                modifiedRelativePath = modifiedRelativePath.replace(\"/integrationTest/\", \"/test/\");\n            }\n        }\n\n        final Path targetPath = targetDirectory.resolve(modifiedRelativePath);\n\n        if (Files.exists(targetPath)) {\n            throw new CodestartStructureException(\n                    \"File already exists: \" + targetPath.toString());\n        }\n\n        createDirectories(targetPath);\n        writeFile(targetPath, content);\n    }\n\n    static String refactorPackage(String content, String packageName) {\n        return content.replaceAll(REPLACE_REGEX, \"$1 \" + packageName);\n    }\n}\n","sourceCodeStart":41,"sourceCodeEnd":71,"githubUrl":"https://github.com/quarkusio/quarkus/blob/e1c734241f34c7919086ceb4c9262b4a58f6de44/independent-projects/tools/codestarts/src/main/java/io/quarkus/devtools/codestarts/core/strategy/SmartPackageFileStrategyHandler.java#L41-L71","documentation":"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.","triggerScenarios":"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).","commonSituations":"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.","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"],"exampleFix":"// before\nsrc/main/java/com/example/app/MyService.java already exists; re-running package codestart -> throws\n\n// after\ngit checkout -- .   // or delete target file, then re-apply codestart","handlingStrategy":"validation","validationCode":"Path targetPath = targetDirectory.resolve(modifiedRelativePath);\nif (Files.exists(targetPath)) {\n    throw new IllegalStateException(\"Package rewrite would overwrite: \" + targetPath);\n}","typeGuard":null,"tryCatchPattern":"try {\n    handler.process(targetDir, relativePath, codestartFiles, data);\n} catch (CodestartStructureException e) {\n    if (e.getMessage().startsWith(\"File already exists\")) {\n        // compare existing file with generated content before overwriting manually\n    } else throw e;\n}","preventionTips":["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"],"tags":["codestarts","file-conflict","package"],"backgroundTag":"target-file-already-exists","analyzedSha":"e1c734241f34c7919086ceb4c9262b4a58f6de44","analyzedAt":"2026-09-05T17:01:29.979Z","contentChangedAt":"2026-09-05T17:01:29.979Z","schemaVersion":2},"datasetVersion":"2026-09-12T22:17:10.623Z"}