Skip to content

Commit ef48399

Browse files
committed
fix: crash due to config packages key with missing value
- Treat empty 'packages:' entry as empty array to prevent NPE. Ensures consistent behavior with when no 'package:' entry or 'package: []' is specified. - Log and return null instead of System.exit(1) on 'load', since System.exit(1) is already done in 'initialize' Added tests to verify: - Loading config with 'packages' key having no value. - Error message for scalar 'packages' value.
1 parent 011a645 commit ef48399

File tree

2 files changed

+23
-1
lines changed

2 files changed

+23
-1
lines changed

agent/src/main/java/com/appland/appmap/config/AppMapConfig.java

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -131,9 +131,13 @@ static AppMapConfig load(Path configFile, boolean mustExist) {
131131
ObjectMapper mapper = new ObjectMapper(new YAMLFactory());
132132
try {
133133
singleton = mapper.readValue(inputStream, AppMapConfig.class);
134+
if (singleton.packages == null) {
135+
logger.error("AppMap: missing value for the 'packages' entry in appmap.yml");
136+
return null;
137+
}
134138
} catch (IOException e) {
135139
logger.error("AppMap: encountered syntax error in appmap.yml {}", e.getMessage());
136-
System.exit(1);
140+
return null;
137141
}
138142
singleton.configFile = configFile;
139143
logger.debug("config: {}", singleton);

agent/src/test/java/com/appland/appmap/config/AppMapConfigTest.java

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -100,6 +100,24 @@ public void hasAppmapDir() throws Exception {
100100
AppMapConfig.load(configFile, true);
101101
assertEquals("/appmap", AppMapConfig.get().getAppmapDir());
102102
}
103+
104+
@Test
105+
public void loadPackagesKeyWithMissingValue() throws Exception {
106+
Path configFile = tmpdir.resolve("appmap.yml");
107+
final String contents = "name: test\npackages:\npath: xyz";
108+
Files.write(configFile, contents.getBytes());
109+
String actualErr = tapSystemErr(() -> AppMapConfig.load(configFile, false));
110+
assertTrue(actualErr.contains("AppMap: missing value for the 'packages'"));
111+
}
112+
113+
@Test
114+
public void loadPackagesKeyWithScalarValue() throws Exception {
115+
Path configFile = Files.createTempFile("appmap", ".yml");
116+
final String contents = "name:q test\npackages: abc\n";
117+
Files.write(configFile, contents.getBytes());
118+
String actualErr = tapSystemErr(() -> AppMapConfig.load(configFile, false));
119+
assertTrue(actualErr.contains("AppMap: encountered syntax error in appmap.yml"));
120+
}
103121
}
104122

105123

0 commit comments

Comments
 (0)