Skip to content

Commit 2c29c16

Browse files
authored
Merge pull request #290 from getappmap/fix/detect-packages-in-multi-repos
fix: detect packages in multi-project repositories
2 parents 155da18 + c85a803 commit 2c29c16

File tree

6 files changed

+55
-25
lines changed

6 files changed

+55
-25
lines changed

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

Lines changed: 48 additions & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -20,6 +20,7 @@
2020
import java.util.HashSet;
2121
import java.util.List;
2222
import java.util.Set;
23+
import java.util.concurrent.atomic.AtomicBoolean;
2324
import java.util.function.Consumer;
2425

2526
import org.tinylog.Logger;
@@ -224,31 +225,29 @@ public static String getDefault(String directory) throws IOException {
224225
pw.println("# https://appmap.io/docs/reference/appmap-java.html#configuration");
225226
pw.format("name: %s\n", CLI.projectName(new File(directory)));
226227

227-
// For now, this only works in this type of standardize repo structure.
228-
Path javaDir = Paths.get(directory).resolve("src/main/java");
229-
if (Files.isDirectory(javaDir)) {
230-
int pkgStart = javaDir.getNameCount();
231-
// Collect package names in src/main/java
232-
Set<Path> packages = new HashSet<>();
233-
Files.walkFileTree(javaDir, new SimpleFileVisitor<Path>() {
234-
@Override
235-
public FileVisitResult visitFile(Path file, BasicFileAttributes attrs) {
236-
if (file.getFileName().toString().endsWith(".java")) {
237-
int pkgEnd = file.getParent().getNameCount();
238-
if (pkgStart == pkgEnd) {
239-
// We're in the the unnamed package, ignore
240-
return FileVisitResult.CONTINUE;
241-
}
242-
243-
Path packagePath = file.getParent().subpath(pkgStart, pkgEnd);
244-
if (packagePath.getNameCount() > 0) {
245-
packages.add(packagePath);
246-
}
247-
}
248-
return FileVisitResult.CONTINUE;
228+
// Set to collect packages from all relevant src/main/java directories
229+
Set<Path> packages = new HashSet<>();
230+
AtomicBoolean srcMainJavaDirExists = new AtomicBoolean(false);
231+
232+
// Traverse the root directory to find all src/main/java directories
233+
Files.walkFileTree(Paths.get(directory), new SimpleFileVisitor<Path>() {
234+
@Override
235+
public FileVisitResult preVisitDirectory(Path dir, BasicFileAttributes attrs) {
236+
if (dir.endsWith("src/main/java")) {
237+
srcMainJavaDirExists.set(true);
238+
collectPackages(dir, packages);
249239
}
250-
});
240+
return FileVisitResult.CONTINUE;
241+
}
251242

243+
@Override
244+
public FileVisitResult visitFileFailed(Path file, IOException io)
245+
{
246+
return FileVisitResult.SKIP_SUBTREE;
247+
}
248+
});
249+
250+
if (srcMainJavaDirExists.get()) {
252251
pw.print("\n"
253252
+ "# Your project contains the directory src/main/java. AppMap has\n"
254253
+ "# auto-detected the following Java packages in this directory:\n"
@@ -288,6 +287,32 @@ public void accept(Path packagePath) {
288287
return sw.toString();
289288
}
290289

290+
private static void collectPackages(Path javaDir, Set<Path> packages) {
291+
int pkgStart = javaDir.getNameCount();
292+
try {
293+
Files.walkFileTree(javaDir, new SimpleFileVisitor<Path>() {
294+
@Override
295+
public FileVisitResult visitFile(Path file, BasicFileAttributes attrs) {
296+
if (file.getFileName().toString().endsWith(".java")) {
297+
int pkgEnd = file.getParent().getNameCount();
298+
if (pkgStart == pkgEnd) {
299+
// We're in the unnamed package, ignore
300+
return FileVisitResult.CONTINUE;
301+
}
302+
303+
Path packagePath = file.getParent().subpath(pkgStart, pkgEnd);
304+
if (packagePath.getNameCount() > 0) {
305+
packages.add(packagePath);
306+
}
307+
}
308+
return FileVisitResult.CONTINUE;
309+
}
310+
});
311+
} catch (IOException e) {
312+
e.printStackTrace();
313+
}
314+
}
315+
291316
public static TaggedLogger configureLogging() {
292317
// tinylog freezes its configuration after the first call to any of its
293318
// methods other than those in Configuration. So, get everything ready

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

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -37,7 +37,7 @@ public void loadBadDirectory() throws Exception {
3737
public void createDefault() throws IOException {
3838
// Just verify that the file gets created when it should. The contents
3939
// get verified elsewhere.
40-
Path configFile = Paths.get(System.getProperty("java.io.tmpdir"), "appmap.yml");
40+
Path configFile = tmpdir.resolve("appmap.yml");
4141
Files.deleteIfExists(configFile);
4242
AppMapConfig.load(configFile, false);
4343
assertTrue(Files.exists(configFile));
@@ -55,7 +55,7 @@ public void preservesExisting() throws IOException {
5555

5656
@Test
5757
public void requiresExisting() throws Exception {
58-
Path configFile = Paths.get(System.getProperty("java.io.tmpdir"), "appmap.yml");
58+
Path configFile = tmpdir.resolve("appmap.yml");
5959
Files.deleteIfExists(configFile);
6060

6161
String actualErr = tapSystemErr(() -> AppMapConfig.load(configFile, true));

agent/test/agent_cli/agent_cli.bats

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -29,6 +29,8 @@ setup_file() {
2929
assert_success
3030
assert_json_contains '.configuration.contents' 'path: pkg1'
3131
assert_json_contains '.configuration.contents' 'path: pkg2'
32+
assert_json_contains '.configuration.contents' 'path: com.example.subprojpkg1'
33+
assert_json_contains '.configuration.contents' 'path: com.example.subprojpkg2'
3234
}
3335

3436
@test "appmap agent init empty project" {
Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
// intentionally blank
Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
// intentionally blank

agent/test/intellij/.gitignore

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
appmap-intellij-plugin

0 commit comments

Comments
 (0)