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 27a7c267784b CAMEL-26943: camel-core - dev mode: the fixed save after 
a failed reload no longer fails with a duplicate route id (#29886)
27a7c267784b is described below

commit 27a7c267784bdec31cc98fe2d01c6106a928fb79
Author: Claus Ibsen <[email protected]>
AuthorDate: Fri Nov 13 23:44:08 2026 +0100

    CAMEL-26943: camel-core - dev mode: the fixed save after a failed reload no 
longer fails with a duplicate route id (#29886)
    
    ## What
    
    CAMEL-26943. In dev mode a failed reload restores the routes that ran 
before. The failed file goes back from the content that last loaded, which is 
kept in memory (CAMEL-24899, CAMEL-26444). With `camel run hello.camel.yaml 
--dev`, the next save of the fixed file then failed:
    
    ```
    WARN  Reload failed due to: Unknown node id: lgo. The previous routes were 
restored (1 route(s) running, 1 from the last loaded content of the changed 
file); the changed file loads on its next save
    WARN  Reload failed and the previous routes could not be restored due to: 
Failed to start route: hello because: duplicate route id detected hello. ...
    ```
    
    Every later save failed the same way, until a restart.
    
    The version from memory was made with `ResourceHelper.fromBytes(location, 
content)`, so its URI was made from the location: `file:hello.camel.yaml`. The 
file on disk has the URI `file:/.../hello.camel.yaml`. A reload uses the URI to 
tell which running routes came from the saved file. It did not see that the 
routes from memory came from it, so it loaded both.
    
    In a folder with a space in its name, the URI could not be made from the 
location at all (`Illegal character in path`). The next save failed on that 
before any route was removed, so the version from memory kept running until a 
restart.
    
    ## Fix
    
    The version from memory has the URI of the file it stands for. It keeps the 
scheme `mem`, the location and the content. With an absolute folder (the tests 
so far, and Camel Main), the URI made from the location was already the URI of 
the file, so nothing changes there.
    
    ## Tests
    
    - `RouteReloadRollbackTest` (camel-yaml-dsl, with the other restore tests): 
the only route file is saved broken, then fixed. The fixed version runs, as the 
only route. This is tested in a folder under `target/` given as a relative 
path, and in a folder with a space in its name. Both fail without the fix: one 
on a duplicate route id, the other on an illegal character in the path.
    - The reload tests of camel-core (26), camel-main (9), camel-xml-io-dsl (3) 
and camel-yaml-dsl (27) pass.
    - `camel run hello.camel.yaml --dev`, with the camel-support jar of this PR 
in the camel-launcher: break the file (`lgo`) and the previous version is 
restored; fix it and the fixed version runs (`Routes reloaded summary (total:1 
started:1 changed:1)`). The camel-launcher without this PR fails as above.
    
    No backport: the restore from memory came in 4.23.0 (CAMEL-24860, 
CAMEL-24899).
    
    Co-Authored-By: Claude Opus 5.5 <[email protected]>
---
 .../camel/support/RouteWatcherReloadStrategy.java  | 29 +++++++++++++++++-
 .../camel/dsl/yaml/RouteReloadRollbackTest.java    | 35 ++++++++++++++++++++++
 2 files changed, 63 insertions(+), 1 deletion(-)

diff --git 
a/core/camel-support/src/main/java/org/apache/camel/support/RouteWatcherReloadStrategy.java
 
b/core/camel-support/src/main/java/org/apache/camel/support/RouteWatcherReloadStrategy.java
index 397b58124d2e..e9e812e889bd 100644
--- 
a/core/camel-support/src/main/java/org/apache/camel/support/RouteWatcherReloadStrategy.java
+++ 
b/core/camel-support/src/main/java/org/apache/camel/support/RouteWatcherReloadStrategy.java
@@ -16,6 +16,7 @@
  */
 package org.apache.camel.support;
 
+import java.io.ByteArrayInputStream;
 import java.io.File;
 import java.io.InputStream;
 import java.net.URI;
@@ -869,7 +870,7 @@ public class RouteWatcherReloadStrategy extends 
FileWatcherResourceReloadStrateg
             for (Resource rs : failed) {
                 byte[] content = rs != null ? lastGoodContentOf(rs) : null;
                 if (content != null && !equalResourceLocation(restore, rs)) {
-                    restore.add(ResourceHelper.fromBytes(rs.getLocation(), 
content));
+                    restore.add(lastLoaded(rs, content));
                     fromMemory++;
                 }
             }
@@ -901,6 +902,32 @@ public class RouteWatcherReloadStrategy extends 
FileWatcherResourceReloadStrateg
         }
     }
 
+    /**
+     * The content a file last loaded with, kept in memory, as the resource 
that stands for the file in a restore. It
+     * has the URI of the file, so the next reload knows it is the file that 
is saved, and loads the saved file in its
+     * place. A URI made from the location is not the URI of the file: the 
location of a file in the current folder is
+     * relative (camel run), and a location with a space in it is no URI at 
all (CAMEL-26943).
+     */
+    private static Resource lastLoaded(Resource file, byte[] content) {
+        URI uri = file.getURI();
+        return new ResourceSupport("mem", file.getLocation()) {
+            @Override
+            public boolean exists() {
+                return true;
+            }
+
+            @Override
+            public URI getURI() {
+                return uri;
+            }
+
+            @Override
+            public InputStream getInputStream() {
+                return new ByteArrayInputStream(content);
+            }
+        };
+    }
+
     /**
      * Adds the sources of a route to the sources to load again: the file it 
was loaded from and, for a route from a
      * templated route, the file of the templated route. The route template of 
a templated route is either in a file,
diff --git 
a/dsl/camel-yaml-dsl/camel-yaml-dsl/src/test/java/org/apache/camel/dsl/yaml/RouteReloadRollbackTest.java
 
b/dsl/camel-yaml-dsl/camel-yaml-dsl/src/test/java/org/apache/camel/dsl/yaml/RouteReloadRollbackTest.java
index e0bbe2469516..35780e23e926 100644
--- 
a/dsl/camel-yaml-dsl/camel-yaml-dsl/src/test/java/org/apache/camel/dsl/yaml/RouteReloadRollbackTest.java
+++ 
b/dsl/camel-yaml-dsl/camel-yaml-dsl/src/test/java/org/apache/camel/dsl/yaml/RouteReloadRollbackTest.java
@@ -344,6 +344,41 @@ class RouteReloadRollbackTest extends YamlTestSupport {
         }
     }
 
+    @Test
+    void theFixedSaveAfterABrokenOneLoadsInARelativeFolder() throws Exception {
+        // camel run watches the current folder, so the files have relative 
locations (file:only.camel.yaml)
+        assertTheFixedSaveLoads(Files.createTempDirectory(Path.of("target"), 
"camel-reload-relative"));
+    }
+
+    @Test
+    void theFixedSaveAfterABrokenOneLoadsInAFolderWithASpaceInItsName() throws 
Exception {
+        // a location with a space in it is not a URI as it is
+        assertTheFixedSaveLoads(Files.createTempDirectory("camel reload"));
+    }
+
+    // CAMEL-26943: the version that goes back from memory after a broken save 
is the same file as the one saved next,
+    // so the fixed save loads in its place, and not next to it with the same 
route id
+    private static void assertTheFixedSaveLoads(Path solo) throws Exception {
+        Path only = Files.writeString(solo.resolve("only.camel.yaml"), 
route("only", "mock:only"));
+        RouteWatcherReloadStrategy strategy = new 
RouteWatcherReloadStrategy(solo.toString());
+        strategy.setPattern("*.yaml");
+        strategy.setScheduler(false);
+        CamelContext ctx = startWithRoutes(strategy, false, only);
+        try {
+            Files.writeString(only, brokenRoute("only"));
+            assertThatThrownBy(() -> reload(strategy, ctx, 
only)).hasMessageContaining("pollEnrich");
+            assertRunsTo(ctx, "direct:only", "mock:only");
+
+            Files.writeString(only, route("only", "mock:fixed"));
+            reload(strategy, ctx, only);
+            assertThat(ctx.getRoutes()).hasSize(1);
+            assertRunsTo(ctx, "direct:only", "mock:fixed");
+        } finally {
+            ctx.stop();
+            deleteRecursively(solo);
+        }
+    }
+
     @Test
     void 
theFirstBrokenSaveUnderSrcMainResourcesGoesBackToTheVersionLoadedFromTargetClasses()
 throws Exception {
         // the Camel Main defaults: the routes load from the classpath, 
target/classes, and the reload watches

Reply via email to