Skip to content

Commit 259db16

Browse files
tmonestudioclaude
authored andcommitted
fix(discover): keep JVM packages named like build output dirs (DeusData#2661)
Below src/<set>/{java,kotlin,scala,groovy}/ every directory is a package, so a built-in skip name there (target, tmp, vendor, deploy, env, dist...) is a package name, not build output. Discovery and the inventory filter now keep such directories; the root itself, its siblings and the safety core (.git, worktrees) stay skipped. Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com> Signed-off-by: Leonardo trindade miranda <tmonestudio@gmail.com>
1 parent 10ab613 commit 259db16

4 files changed

Lines changed: 113 additions & 3 deletions

File tree

‎src/discover/discover.c‎

Lines changed: 54 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -346,6 +346,56 @@ static bool resolve_global_excludes_path(char *out, size_t out_sz) {
346346
return false;
347347
}
348348

349+
/* JVM source-set language roots: src/<set>/<lang>/ in Maven and Gradle layouts. */
350+
static const char *JVM_SOURCE_LANG_DIRS[] = {"java", "kotlin", "scala", "groovy", NULL};
351+
352+
static bool component_equals(const char *start, size_t len, const char *word) {
353+
return strlen(word) == len && strncmp(start, word, len) == 0;
354+
}
355+
356+
/* True when rel_path names a directory strictly below a JVM source root
357+
* ("src/main/java/com/acme/target", "app/src/test/kotlin/x/tmp"). There every
358+
* directory is a package, so a built-in skip name such as "target", "tmp" or
359+
* "vendor" is an ordinary package name, not build output (#2661). The root
360+
* itself and directories beside it ("src/main/target") are not packages. */
361+
bool cbm_discover_path_in_jvm_package(const char *rel_path) {
362+
if (!rel_path) {
363+
return false;
364+
}
365+
enum { JVM_MAX_COMPONENTS = 256 };
366+
const char *starts[JVM_MAX_COMPONENTS];
367+
size_t lens[JVM_MAX_COMPONENTS];
368+
size_t count = 0;
369+
const char *p = rel_path;
370+
while (*p && count < JVM_MAX_COMPONENTS) {
371+
while (*p == '/' || *p == '\\') {
372+
p++;
373+
}
374+
if (!*p) {
375+
break;
376+
}
377+
const char *start = p;
378+
while (*p && *p != '/' && *p != '\\') {
379+
p++;
380+
}
381+
starts[count] = start;
382+
lens[count] = (size_t)(p - start);
383+
count++;
384+
}
385+
/* The language root sits at i + 2; the directory must be below it. */
386+
for (size_t i = 0; i + 3 < count; i++) {
387+
if (!component_equals(starts[i], lens[i], "src")) {
388+
continue;
389+
}
390+
for (int l = 0; JVM_SOURCE_LANG_DIRS[l]; l++) {
391+
if (component_equals(starts[i + 2], lens[i + 2], JVM_SOURCE_LANG_DIRS[l])) {
392+
return true;
393+
}
394+
}
395+
}
396+
return false;
397+
}
398+
349399
/* True when rel_path equals a suffix rule or ends with "/" + the rule. */
350400
bool cbm_discover_path_has_skip_suffix(const char *rel_path) {
351401
if (!rel_path) {
@@ -803,8 +853,10 @@ static dir_verdict_t directory_verdict(const char *entry_name, const char *rel_p
803853
const gitignore_link_t *ignore_chain,
804854
const cbm_gitignore_t *global_gi,
805855
const cbm_gitignore_t *cbmignore) {
806-
if (cbm_should_skip_dir(entry_name, opts ? opts->mode : CBM_MODE_FULL) ||
807-
cbm_discover_path_has_skip_suffix(rel_path)) {
856+
bool builtin_skip = cbm_should_skip_dir(entry_name, opts ? opts->mode : CBM_MODE_FULL) &&
857+
(cbm_discover_is_safety_core_directory(entry_name) ||
858+
!cbm_discover_path_in_jvm_package(rel_path));
859+
if (builtin_skip || cbm_discover_path_has_skip_suffix(rel_path)) {
808860
/* #500: a .cbmignore negation (e.g. "!obj/") whose rule is the last
809861
* match for this dir un-skips a built-in skip-list dir — except the
810862
* non-negatable safety core. Fall through so .gitignore/global/local

‎src/discover/discover_policy_internal.h‎

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5,5 +5,6 @@
55
/* Pure legacy policy predicates; callers provide live terminated strings. */
66
bool cbm_discover_path_has_skip_suffix(const char *relative_path);
77
bool cbm_discover_is_safety_core_directory(const char *name);
8+
bool cbm_discover_path_in_jvm_package(const char *relative_path);
89

910
#endif

‎src/discover/inventory_filter.c‎

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -102,7 +102,9 @@ static bool cif_directory_reason(cif_context *c, size_t parent, cbm_inventory_pa
102102
if (!cif_poll(c)) {
103103
return false;
104104
}
105-
bool builtin = cbm_should_skip_dir(name, CBM_MODE_FULL);
105+
bool builtin = cbm_should_skip_dir(name, CBM_MODE_FULL) &&
106+
(cbm_discover_is_safety_core_directory(name) ||
107+
!cbm_discover_path_in_jvm_package((const char *)path.data));
106108
bool suffix = !builtin && cbm_discover_path_has_skip_suffix((const char *)path.data);
107109
bool safety =
108110
(builtin || suffix) && c->owner->cbm_program && cbm_discover_is_safety_core_directory(name);

‎tests/test_discover.c‎

Lines changed: 55 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@
66
#include "test_framework.h"
77
#include "test_helpers.h"
88
#include "discover/discover.h"
9+
#include "discover/discover_policy_internal.h"
910
#include "foundation/platform.h"
1011

1112
typedef struct {
@@ -1328,6 +1329,58 @@ TEST(discover_cbmignore_negates_always_skip_dir) {
13281329
PASS();
13291330
}
13301331

1332+
/* ── JVM packages named like build output (issue #2661) ─────────── */
1333+
1334+
/* Below src/<set>/{java,kotlin,scala,groovy}/ every directory is a package, so
1335+
* "target", "tmp", "vendor" there are package names, not build output. */
1336+
TEST(jvm_package_path_predicate_issue2661) {
1337+
ASSERT_TRUE(cbm_discover_path_in_jvm_package("src/main/java/com/acme/target"));
1338+
ASSERT_TRUE(cbm_discover_path_in_jvm_package("app/src/test/kotlin/x/tmp"));
1339+
ASSERT_TRUE(cbm_discover_path_in_jvm_package("src/main/scala/vendor"));
1340+
/* The language root itself and its siblings are not packages. */
1341+
ASSERT_FALSE(cbm_discover_path_in_jvm_package("src/main/java"));
1342+
ASSERT_FALSE(cbm_discover_path_in_jvm_package("src/main/target"));
1343+
ASSERT_FALSE(cbm_discover_path_in_jvm_package("target"));
1344+
ASSERT_FALSE(cbm_discover_path_in_jvm_package("module/target/classes"));
1345+
ASSERT_FALSE(cbm_discover_path_in_jvm_package("src/java/main/tmp"));
1346+
ASSERT_FALSE(cbm_discover_path_in_jvm_package(NULL));
1347+
PASS();
1348+
}
1349+
1350+
TEST(discover_keeps_jvm_packages_named_like_build_dirs_issue2661) {
1351+
char *base = th_mktempdir("cbm_disc_jvm_pkg");
1352+
ASSERT(base != NULL);
1353+
1354+
th_write_file(TH_PATH(base, "src/main/java/com/acme/target/Foo.java"),
1355+
"package com.acme.target;\nclass Foo {}\n");
1356+
th_write_file(TH_PATH(base, "src/test/java/com/acme/tmp/T.java"),
1357+
"package com.acme.tmp;\nclass T {}\n");
1358+
th_write_file(TH_PATH(base, "src/main/kotlin/com/acme/vendor/V.kt"),
1359+
"package com.acme.vendor\nclass V\n");
1360+
/* Real build output stays skipped. */
1361+
th_write_file(TH_PATH(base, "target/classes/Gen.java"), "class Gen {}\n");
1362+
th_write_file(TH_PATH(base, "src/main/target/Out.java"), "class Out {}\n");
1363+
/* The safety core is never un-skipped, even below a package root. */
1364+
th_write_file(TH_PATH(base, "src/main/java/com/.git/H.java"), "class H {}\n");
1365+
1366+
cbm_discover_opts_t opts = {0};
1367+
cbm_file_info_t *files = NULL;
1368+
int count = 0;
1369+
int rc = cbm_discover(base, &opts, &files, &count);
1370+
ASSERT_EQ(rc, 0);
1371+
ASSERT_TRUE(discover_has_rel_path(files, count, "src/main/java/com/acme/target/Foo.java"));
1372+
ASSERT_TRUE(discover_has_rel_path(files, count, "src/test/java/com/acme/tmp/T.java"));
1373+
ASSERT_TRUE(discover_has_rel_path(files, count, "src/main/kotlin/com/acme/vendor/V.kt"));
1374+
ASSERT_FALSE(discover_has_rel_path(files, count, "target/classes/Gen.java"));
1375+
ASSERT_FALSE(discover_has_rel_path(files, count, "src/main/target/Out.java"));
1376+
ASSERT_FALSE(discover_has_rel_path(files, count, "src/main/java/com/.git/H.java"));
1377+
ASSERT_EQ(count, 3);
1378+
1379+
cbm_discover_free(files, count);
1380+
th_cleanup(base);
1381+
PASS();
1382+
}
1383+
13311384
/* ── Laravel compiled Blade view cache (issue #1735) ─────────────── */
13321385

13331386
/* storage/framework/views/ holds Laravel's compiled Blade templates: generated
@@ -2381,6 +2434,8 @@ SUITE(discover) {
23812434
RUN_TEST(skip_venv);
23822435
RUN_TEST(skip_dist);
23832436
RUN_TEST(skip_target);
2437+
RUN_TEST(jvm_package_path_predicate_issue2661);
2438+
RUN_TEST(discover_keeps_jvm_packages_named_like_build_dirs_issue2661);
23842439
RUN_TEST(skip_vendor);
23852440
RUN_TEST(skip_vendored);
23862441
RUN_TEST(skip_terraform);

0 commit comments

Comments
 (0)