This is an automated email from the ASF dual-hosted git repository.

davsclaus pushed a commit to branch main
in repository https://gitbox.apache.org/repos/asf/camel.git


The following commit(s) were added to refs/heads/main by this push:
     new 1f73b6ff3ca4 CAMEL-26912: camel-jbang - camel export fails for two 
files that would be the same file of the project (#29815)
1f73b6ff3ca4 is described below

commit 1f73b6ff3ca424f8c55a4f2543c214a21f9a2d99
Author: Claus Ibsen <[email protected]>
AuthorDate: Fri Nov 13 22:02:50 2026 +0100

    CAMEL-26912: camel-jbang - camel export fails for two files that would be 
the same file of the project (#29815)
    
    ## Summary
    
    Item 5 of [CAMEL-26912](https://issues.apache.org/jira/browse/CAMEL-26912): 
**two files with the same name in different directories collapsed into one file 
of the exported project, and one route was silently lost.**
    
    The export copies the route files flat into `src/main/resources/camel` (and 
resources, Kamelets, Java sources and so on into their own directory), by file 
name. `camel export x/route.camel.yaml y/route.camel.yaml` wrote one 
`route.camel.yaml`: the one copied last, and said "Project export successful!".
    
    ### Fix
    
    The export now fails when two different files would be the same file of the 
project, and names them:
    
    ```
    ERROR: Cannot export /work/x/route.camel.yaml and /work/y/route.camel.yaml 
as both would be src/main/resources/camel/route.camel.yaml in the exported 
project: rename one of them
    ```
    
    - Checked where each file is copied (`Exporter.copySourceFiles`): routes, 
resources, Kamelets, JKube fragments, and Java sources (by their package).
    - The same file given twice (such as `x/route.camel.yaml` and 
`x/../x/route.camel.yaml`) is copied once, as before.
    - `ExportException`: the failure of an export that `ProjectExporter` 
reports with its message and exit code 1, without a stack trace (an exception 
the CLI does not know is exit 70 with a stack trace). The export directory is 
not touched: the project is only copied to it at the end.
    
    Keeping both files under their directories (`camel/x/route.camel.yaml`) 
would need the routes include pattern of each runtime (Camel Main, Spring Boot, 
Quarkus) to load subdirectories, so this PR fails clearly instead.
    
    Co-Authored-By: Claude Opus 5.5 <[email protected]>
---
 .../dsl/jbang/run/export/ExportException.java      | 30 +++++++++++
 .../camel/dsl/jbang/run/export/Exporter.java       | 26 ++++++++++
 .../dsl/jbang/run/export/ProjectExporter.java      |  3 ++
 .../dsl/jbang/run/export/ProjectExporterTest.java  | 58 +++++++++++++++++++++-
 4 files changed, 115 insertions(+), 2 deletions(-)

diff --git 
a/dsl/camel-jbang/camel-jbang-run/src/main/java/org/apache/camel/dsl/jbang/run/export/ExportException.java
 
b/dsl/camel-jbang/camel-jbang-run/src/main/java/org/apache/camel/dsl/jbang/run/export/ExportException.java
new file mode 100644
index 000000000000..67d4358e3b1e
--- /dev/null
+++ 
b/dsl/camel-jbang/camel-jbang-run/src/main/java/org/apache/camel/dsl/jbang/run/export/ExportException.java
@@ -0,0 +1,30 @@
+/*
+ * Licensed to the Apache Software Foundation (ASF) under one or more
+ * contributor license agreements.  See the NOTICE file distributed with
+ * this work for additional information regarding copyright ownership.
+ * The ASF licenses this file to You under the Apache License, Version 2.0
+ * (the "License"); you may not use this file except in compliance with
+ * the License.  You may obtain a copy of the License at
+ *
+ *      http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ */
+package org.apache.camel.dsl.jbang.run.export;
+
+/**
+ * The files to export cannot be exported as they are, such as two files that 
would be the same file of the project.
+ * {@link ProjectExporter} prints the message and fails the export with exit 
code 1, without a stack trace.
+ *
+ * @since 4.24
+ */
+public class ExportException extends RuntimeException {
+
+    public ExportException(String message) {
+        super(message);
+    }
+}
diff --git 
a/dsl/camel-jbang/camel-jbang-run/src/main/java/org/apache/camel/dsl/jbang/run/export/Exporter.java
 
b/dsl/camel-jbang/camel-jbang-run/src/main/java/org/apache/camel/dsl/jbang/run/export/Exporter.java
index d3f306985a3d..75139fcb4bad 100644
--- 
a/dsl/camel-jbang/camel-jbang-run/src/main/java/org/apache/camel/dsl/jbang/run/export/Exporter.java
+++ 
b/dsl/camel-jbang/camel-jbang-run/src/main/java/org/apache/camel/dsl/jbang/run/export/Exporter.java
@@ -676,6 +676,8 @@ abstract class Exporter {
         // expression): they stay at the resources root, where the reference 
resolves, instead of camel-groovy where
         // they would be compiled as scripts and be out of reach of the 
reference (CAMEL-24853)
         Set<String> resourceReferenced = 
resourceReferencedFiles(prop.getProperty("camel.main.routesIncludePattern"));
+        // the file of the project that each file of the export is copied to: 
two files must not end up as one
+        Map<Path, Path> copied = new HashMap<>();
 
         for (String k : SETTINGS_PROP_SOURCE_KEYS) {
             String files;
@@ -766,6 +768,7 @@ abstract class Exporter {
                     }
                     if (!java) {
                         if (kamelet) {
+                            claim(copied, out, source);
                             ExportHelper.safeCopy(source, out, true);
                             if (request.symbolicLink) {
                                 deferredSymlinks.put(out, source);
@@ -777,12 +780,16 @@ abstract class Exporter {
                                     .replace(".jkube.yaml", ".yaml")
                                     .replace(".jkube.yml", ".yml");
                             out = 
srcCamelResourcesDir.getParent().getParent().resolve("jkube").resolve(name);
+                            claim(copied, out, source);
                             Files.createDirectories(out.getParent());
                             ExportHelper.safeCopy(source, out, true);
                             if (request.symbolicLink) {
                                 deferredSymlinks.put(out, source);
                             }
                         } else {
+                            if (!Files.isDirectory(source)) {
+                                claim(copied, out, source);
+                            }
                             Files.createDirectories(out.getParent());
                             ExportHelper.safeCopy(getClass().getClassLoader(), 
scheme, source, out, true);
                             if (request.symbolicLink) {
@@ -814,6 +821,7 @@ abstract class Exporter {
                                 }
                             }
                         }
+                        claim(copied, out, source);
                         if (request.javaLiveReload) {
                             ExportHelper.safeCopy(source, out, true);
                             if (request.symbolicLink) {
@@ -877,6 +885,24 @@ abstract class Exporter {
         return false;
     }
 
+    /**
+     * Claims the file of the project that a file of the export is copied to: 
two files with the same name in different
+     * directories (x/route.camel.yaml and y/route.camel.yaml) would be the 
same file of the project, and one of them
+     * would be lost (CAMEL-26912). The same file given twice is copied once.
+     */
+    private void claim(Map<Path, Path> copied, Path out, Path source) {
+        Path from = source.toAbsolutePath().normalize();
+        Path target = out.toAbsolutePath().normalize();
+        Path previous = copied.putIfAbsent(target, from);
+        if (previous != null && !previous.equals(from)) {
+            Path build = buildDir().toAbsolutePath().normalize();
+            Path name = target.startsWith(build) ? build.relativize(target) : 
target;
+            throw new ExportException(
+                    "Cannot export " + previous + " and " + from + " as both 
would be "
+                                      + name.toString().replace('\\', '/') + " 
in the exported project: rename one of them");
+        }
+    }
+
     protected void adjustJavaSourceFileLine(String line, OutputStream fos) 
throws Exception {
         // noop
     }
diff --git 
a/dsl/camel-jbang/camel-jbang-run/src/main/java/org/apache/camel/dsl/jbang/run/export/ProjectExporter.java
 
b/dsl/camel-jbang/camel-jbang-run/src/main/java/org/apache/camel/dsl/jbang/run/export/ProjectExporter.java
index c3b65f1127e8..17ec601ece7b 100644
--- 
a/dsl/camel-jbang/camel-jbang-run/src/main/java/org/apache/camel/dsl/jbang/run/export/ProjectExporter.java
+++ 
b/dsl/camel-jbang/camel-jbang-run/src/main/java/org/apache/camel/dsl/jbang/run/export/ProjectExporter.java
@@ -188,6 +188,9 @@ public class ProjectExporter {
         // directory, and an export without files reuses the run data that the 
last run or export left there
         try (WorkDirLock lock = WorkDirLock.acquire(copy.workDir, printer())) {
             return exporter.export();
+        } catch (ExportException e) {
+            printer().printErr(e.getMessage());
+            return 1;
         }
     }
 
diff --git 
a/dsl/camel-jbang/camel-jbang-run/src/test/java/org/apache/camel/dsl/jbang/run/export/ProjectExporterTest.java
 
b/dsl/camel-jbang/camel-jbang-run/src/test/java/org/apache/camel/dsl/jbang/run/export/ProjectExporterTest.java
index ed70367d4496..796f798f2e9a 100644
--- 
a/dsl/camel-jbang/camel-jbang-run/src/test/java/org/apache/camel/dsl/jbang/run/export/ProjectExporterTest.java
+++ 
b/dsl/camel-jbang/camel-jbang-run/src/test/java/org/apache/camel/dsl/jbang/run/export/ProjectExporterTest.java
@@ -30,6 +30,7 @@ import java.util.concurrent.Executors;
 import java.util.concurrent.Future;
 import java.util.jar.JarEntry;
 import java.util.jar.JarOutputStream;
+import java.util.stream.Collectors;
 import java.util.stream.Stream;
 
 import javax.xml.xpath.XPathFactory;
@@ -391,6 +392,55 @@ class ProjectExporterTest {
         assertThat(exportDir.resolve("pom.xml")).exists();
     }
 
+    @Test
+    void failsForTwoFilesWithTheSameNameInDifferentDirectories() throws 
Exception {
+        Path x = writeRoute(Files.createDirectories(dir.resolve("x")), 
"route", "x");
+        Path y = writeRoute(Files.createDirectories(dir.resolve("y")), 
"route", "y");
+        Path exportDir = dir.resolve("export");
+
+        StringPrinter printer = new StringPrinter();
+        int exit = new ProjectExporter(new 
ExportContext().withPrinter(printer).withDependencyResolver(routes(x, y)))
+                .export(request(x, exportDir));
+
+        // one of the routes would be lost
+        assertThat(exit).isEqualTo(1);
+        assertThat(printer.getOutput()).contains(x.toString(), y.toString(),
+                "src/main/resources/camel/route.camel.yaml", "rename one of 
them");
+        assertThat(exportDir.resolve("pom.xml")).doesNotExist();
+    }
+
+    @Test
+    void copiesTheSameFileGivenTwiceOnce() throws Exception {
+        Path x = writeRoute(Files.createDirectories(dir.resolve("x")), 
"route", "x");
+        Path exportDir = dir.resolve("export");
+
+        StringPrinter printer = new StringPrinter();
+        int exit = new ProjectExporter(
+                new ExportContext().withPrinter(printer)
+                        .withDependencyResolver(routes(x, 
dir.resolve("x/../x/route.camel.yaml"))))
+                .export(request(x, exportDir));
+
+        assertThat(exit).as(printer.getOutput()).isZero();
+        
assertThat(exportDir.resolve("src/main/resources/camel/route.camel.yaml")).hasSameTextualContentAs(x);
+    }
+
+    /**
+     * A resolver whose run loads the given route files.
+     */
+    private static DependencyResolver routes(Path... files) {
+        return r -> {
+            Path settings = r.workDir.resolve(ExportHelper.RUN_SETTINGS_FILE);
+            Files.createDirectories(settings.getParent());
+            String pattern = Stream.of(files).map(f -> "file:" + 
f).collect(Collectors.joining(","));
+            Files.writeString(settings, """
+                    camel.main.routesIncludePattern=%s
+                    dependency=camel:timer
+                    dependency=camel:log
+                    """.formatted(pattern));
+            return 0;
+        };
+    }
+
     @Test
     void requiresRuntime() throws Exception {
         ExportRequest request = new ExportRequest();
@@ -484,13 +534,17 @@ class ProjectExporterTest {
     }
 
     private Path writeRoute(String name) throws Exception {
-        Path route = dir.resolve(name + ".camel.yaml");
+        return writeRoute(dir, name, name);
+    }
+
+    private static Path writeRoute(Path directory, String name, String timer) 
throws Exception {
+        Path route = directory.resolve(name + ".camel.yaml");
         Files.writeString(route, """
                 - from:
                     uri: timer:%s
                     steps:
                       - to: log:%s
-                """.formatted(name, name));
+                """.formatted(timer, timer));
         return route;
     }
 

Reply via email to