Skip to content

Commit 4c33f26

Browse files
committed
Fix StringIndexOutOfBoundsException with watch patterns. Fixes #9610
1 parent bea2c14 commit 4c33f26

File tree

2 files changed

+21
-6
lines changed

2 files changed

+21
-6
lines changed

grails-core/src/main/groovy/grails/boot/GrailsApp.groovy

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -150,7 +150,7 @@ class GrailsApp extends SpringApplication {
150150
configureDirectoryWatcher(directoryWatcher, dir.absolutePath)
151151
}
152152

153-
def locationLength = location.length() + 1
153+
def locationLength = baseDirPath.length() + 1
154154

155155
for(GrailsPlugin plugin in pluginManager.allPlugins) {
156156
def watchedResourcePatterns = plugin.getWatchedResourcePatterns()
@@ -175,12 +175,12 @@ class GrailsApp extends SpringApplication {
175175
}
176176
first = false
177177
if(wp.file) {
178-
def resolvedPath = new File(watchBase, wp.file.path.substring(locationLength))
178+
def resolvedPath = new File(watchBase, wp.file.canonicalPath.substring(locationLength))
179179
directoryWatcher.addWatchFile(resolvedPath)
180180
}
181181
else if(wp.directory && wp.extension) {
182182

183-
def resolvedPath = new File(watchBase, wp.directory.path.substring(locationLength))
183+
def resolvedPath = new File(watchBase, wp.directory.canonicalPath.substring(locationLength))
184184
directoryWatcher.addWatchDirectory(resolvedPath, wp.extension)
185185
}
186186
}

grails-core/src/main/groovy/org/grails/plugins/support/WatchPatternParser.java

Lines changed: 18 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,9 @@
1515
*/
1616
package org.grails.plugins.support;
1717

18+
import grails.io.ResourceUtils;
19+
import grails.util.BuildSettings;
20+
import grails.util.GrailsStringUtils;
1821
import org.springframework.util.StringUtils;
1922

2023
import java.io.File;
@@ -37,12 +40,24 @@ public List<WatchPattern> getWatchPatterns(List<String> patterns) {
3740
for (String pattern : patterns) {
3841
WatchPattern watchPattern = new WatchPattern();
3942
watchPattern.setPattern(pattern);
40-
if (pattern.startsWith("file:")) {
41-
pattern = pattern.substring(5);
43+
boolean isClasspath = false;
44+
if (pattern.startsWith(ResourceUtils.FILE_URL_PREFIX)) {
45+
pattern = pattern.substring(ResourceUtils.FILE_URL_PREFIX.length());
46+
}
47+
else if (pattern.startsWith(ResourceUtils.CLASSPATH_URL_PREFIX)) {
48+
pattern = pattern.substring(ResourceUtils.CLASSPATH_URL_PREFIX.length());
49+
isClasspath = true;
4250
}
4351

4452
if (pattern.contains(WILD_CARD)) {
45-
watchPattern.setDirectory(new File(pattern.substring(0, pattern.indexOf(WILD_CARD))));
53+
String dirPath = pattern.substring(0, pattern.indexOf(WILD_CARD));
54+
if(!GrailsStringUtils.isBlank(dirPath)) {
55+
watchPattern.setDirectory(new File(dirPath));
56+
}
57+
else if(isClasspath && BuildSettings.BASE_DIR != null) {
58+
watchPattern.setDirectory(new File(BuildSettings.BASE_DIR, "src/main/resources"));
59+
}
60+
4661
setExtension(pattern, watchPattern);
4762
watchPatterns.add(watchPattern);
4863
}

0 commit comments

Comments
 (0)