64
64
import javax .annotation .Nullable ;
65
65
import lombok .SneakyThrows ;
66
66
import lombok .extern .slf4j .Slf4j ;
67
+ import org .apache .commons .lang3 .SystemUtils ;
67
68
import org .jetbrains .annotations .NotNull ;
68
69
69
70
@ Slf4j
@@ -416,7 +417,7 @@ private Map<String, ProtoFile> loadFilesWithLocations() {
416
417
files .filter (p -> !Files .isDirectory (p ) && p .toString ().endsWith (".proto" ))
417
418
.forEach (path -> {
418
419
// relative path will be used as "import" statement
419
- String relativePath = baseLocation .relativize (path ).toString ();
420
+ String relativePath = removeBackSlashes ( baseLocation .relativize (path ).toString () );
420
421
var protoFileElement = ProtoParser .Companion .parse (
421
422
Location .get (baseLocation .toString (), relativePath ),
422
423
readFileAsString (path )
@@ -426,6 +427,27 @@ private Map<String, ProtoFile> loadFilesWithLocations() {
426
427
}
427
428
return filesByLocations ;
428
429
}
430
+
431
+ /**
432
+ * Replaces backslashes in the given file path with forward slashes if the operating system is Windows.
433
+ *
434
+ * <p>This method is designed to standardize file paths by converting Windows-style backslashes (`\`)
435
+ * to Linux/Unix-style forward slashes (`/`) when the application is running on a Windows OS.
436
+ * On other operating systems, the input path is returned unchanged.</p>
437
+ *
438
+ * <p>This is needed because imports in Protobuf use forward slashes (`/`)
439
+ * which causes a conflict with Windows paths. For example,`language/language.proto`
440
+ * would be converted to `language\language.proto` in Windows causing a resolution exception</p>
441
+ *
442
+ * @param path the file path to standardize; must not be {@code null}.
443
+ * @return the standardized file path with forward slashes if running on Windows, or the original path otherwise.
444
+ */
445
+ private @ NotNull String removeBackSlashes (@ NotNull final String path ) {
446
+ if (SystemUtils .IS_OS_WINDOWS ) {
447
+ return path .replace ("\\ " , "/" );
448
+ }
449
+ return path ;
450
+ }
429
451
}
430
452
431
453
}
0 commit comments