fix(pipeline): the path rule reads the run's indexed files and never guesses

Review 1 of the A stack (A1-1, A1-2, A1-3, B1, M3 and the walk ruling),
to be folded into A1 ("path rule for Protobuf and Make imports" and "an
import path that names no repository file has no target").

The universe is the run's indexed file list (the files discovery kept,
the same list on the full and the incremental routes), built once before
any question and read-only afterwards. The directory walk, the symbolic
link flag and the walk-based listing digest are gone: a copy of a file in
an ignored or skipped directory (node_modules, a .gitignore'd tree) is not
seen, so an import that only such a copy could satisfy names no file and
draws no IMPORTS edge. The semantic manifest keys the universe by the
sorted indexed paths a row's import can name, read from the discovered
list: no disk access on any planning.

A pick that depends on what the index cannot know is a guess, its own
verdict (CBM_INCLUDE_PATH_GUESS), never FILE: main's strategies decide the
IMPORTS edge and the shared layer takes the include as unresolved.
- Protobuf: when several indexed files end with the import path, the one
  beside the importer is only one include root's reading (protoc -I . and
  -I svc read different files).
- Make: a relative include in a makefile below the repository root names
  a file relative to the directory make runs in (GNU make 4.3 does not
  read sub/b.mk for `include b.mk` in sub/a.mk included from the root);
  only a makefile at the root decides.

A Protobuf path protoc refuses as a virtual path (a `.` or `..` segment,
`//`, a backslash) is not read. Absence for a Make path that climbs out
of its directory is read from the path after its leading `../` segments.

Tests: the edge-level test is red on main (an import naming no repository
file drew main's edge to a same-named Go module); a copy under
node_modules is not seen; the beside pick keeps main's edge; the guess
verdicts for Protobuf and Make; the virtual-path refusals; the inventory
key; linear build work.

Signed-off-by: Martin Vogel <martin.vogel.tech@gmail.com>
This commit is contained in:
Martin Vogel
2026-11-19 18:05:14 +01:00
parent 592094f6b2
commit c5137e2f1c
5 changed files with 418 additions and 406 deletions
+147 -245
View File
@@ -23,19 +23,8 @@
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#ifndef _WIN32
#include <dirent.h> /* DT_LNK */
#endif
/* A directory entry that is a symbolic link: compat_fs passes the POSIX
* d_type (DT_LNK is 10 in every POSIX libc) and 0 on Windows. */
#if defined(DT_LNK)
#define IP_DT_LNK DT_LNK
#else
#define IP_DT_LNK 10
#endif
enum { IP_PATH_MAX = 1024 };
enum { IP_PATH_MAX = 1024, IP_UP_LEN = 3 /* "../" */ };
/* ── Rows ────────────────────────────────────────────────────────────── */
@@ -44,16 +33,20 @@ static const char *const IP_MAKE_SUFFIXES[] = {".mk", ".make", NULL};
static const char *const IP_MAKE_PREFIXES[] = {"Makefile", "makefile", "GNUmakefile", NULL};
static const cbm_include_path_row_t IP_ROWS[] = {
/* Protobuf: `import "a/b.proto"` names the one repository file that is
* that path or ends with it, else the file beside the importer. A row
* that names no `.proto` file is not read as a path. */
/* Protobuf: `import "a/b.proto"` names the one indexed file that is that
* path or ends with it. When several do, the file beside the importer is
* only a guess (protoc reads the one under its -I root). A row that names
* no `.proto` file, or a path protoc refuses, is not read as a path. */
{.lang = CBM_LANG_PROTOBUF,
.suffixes = IP_PROTO_SUFFIXES,
.path_needs_suffix = true,
.virtual_path = true,
.base_file_dir = true,
.whole_path_tail = true},
/* Make: `include x.mk` names the file from the makefile's directory or
* the repository root, and only when exactly one of them is a file. */
* the repository root, and only when exactly one of them is a file. Below
* the root that is a guess: GNU make reads a relative include from the
* directory it runs in, which the index does not know. */
{.lang = CBM_LANG_MAKEFILE,
.suffixes = IP_MAKE_SUFFIXES,
.prefixes = IP_MAKE_PREFIXES,
@@ -163,7 +156,7 @@ static bool ip_dirname(char *out, size_t sz, const char *rel) {
return true;
}
/* ── The universe and its listing ────────────────────────────────────── */
/* ── The universe: the run's indexed files ───────────────────────────── */
typedef struct {
const char *path; /* the one path with this tail, or the first of several */
@@ -171,49 +164,18 @@ typedef struct {
} ip_tail_t;
struct cbm_include_universe {
char *root;
CBMArena arena;
CBMHashTable *listed; /* rel -> non-NULL */
CBMHashTable *tails; /* every path suffix on a '/' boundary -> ip_tail_t */
/* the same suffixes in lower case: a path that matches one only when case
* is ignored is not known absent (a case-insensitive disk opens it) */
CBMHashTable *ftails;
const char **paths; /* listed paths, in listing order */
size_t npaths;
size_t cap_paths;
bool did_list;
/* false when an indexed path did not fit the rule's buffers: then no
* uniqueness and no absence is proven */
bool complete;
/* the walk met a symbolic link (not entered): a path under one is not
* known absent */
bool saw_link;
uint64_t visited; /* directory entries read by the listing */
uint64_t tail_steps; /* suffix keys made and looked up */
uint64_t tail_steps; /* suffix keys made while building */
};
cbm_include_universe_t *cbm_include_universe_new(const char *repo_path) {
if (!repo_path || !repo_path[0]) {
return NULL;
}
cbm_include_universe_t *u = cbm_alloc(CBM_MEM_CLASS_OTHER, sizeof(*u));
if (!u) {
return NULL;
}
memset(u, 0, sizeof(*u));
cbm_arena_init(&u->arena);
size_t n = strlen(repo_path);
while (n > 1 && repo_path[n - 1] == '/') {
n--;
}
u->root = cbm_arena_alloc(&u->arena, n + 1);
if (!u->root) {
cbm_include_universe_free(u);
return NULL;
}
memcpy(u->root, repo_path, n);
u->root[n] = '\0';
return u;
}
void cbm_include_universe_free(cbm_include_universe_t *u) {
if (!u) {
return;
@@ -227,33 +189,35 @@ void cbm_include_universe_free(cbm_include_universe_t *u) {
if (u->ftails) {
cbm_ht_free(u->ftails);
}
cbm_free(CBM_MEM_CLASS_OTHER, u->paths);
cbm_arena_destroy(&u->arena);
cbm_free(CBM_MEM_CLASS_OTHER, u);
}
uint64_t cbm_include_universe_visited(const cbm_include_universe_t *u) {
return u ? u->visited : 0;
}
uint64_t cbm_include_universe_tail_steps(const cbm_include_universe_t *u) {
return u ? u->tail_steps : 0;
}
/* "a/B.proto" -> "a/b.proto" (ASCII), in the universe's arena. */
static const char *ip_fold(cbm_include_universe_t *u, const char *p) {
char *f = cbm_arena_strdup(&u->arena, p);
for (char *c = f; c && *c; c++) {
if (*c >= 'A' && *c <= 'Z') {
*c = (char)(*c - 'A' + 'a');
}
/* "a/B.proto" -> "a/b.proto" (ASCII) into out; false when it does not fit. */
static bool ip_fold_into(char *out, size_t sz, const char *p) {
size_t n = strlen(p);
if (n >= sz) {
return false;
}
return f;
for (size_t i = 0; i <= n; i++) {
char c = p[i];
out[i] = (c >= 'A' && c <= 'Z') ? (char)(c - 'A' + 'a') : c;
}
return true;
}
/* One listed path: into the set, the order, and the suffix indexes. */
static bool ip_list_add(cbm_include_universe_t *u, const char *rel) {
const char *folded = ip_fold(u, rel);
/* One indexed path: into the set and the suffix indexes (building only). */
static bool ip_list_add(cbm_include_universe_t *u, const char *path) {
const char *rel = cbm_arena_strdup(&u->arena, path);
char fold[IP_PATH_MAX];
if (!rel || !ip_fold_into(fold, sizeof(fold), rel)) {
return false;
}
const char *folded = cbm_arena_strdup(&u->arena, fold);
if (!folded) {
return false;
}
@@ -262,20 +226,6 @@ static bool ip_list_add(cbm_include_universe_t *u, const char *rel) {
const char *slash = strchr(p, '/');
p = slash ? slash + SKIP_ONE : NULL;
}
if (u->npaths >= u->cap_paths) {
size_t ncap = u->cap_paths ? u->cap_paths * PAIR_LEN : CBM_SZ_256;
const char **np = cbm_alloc(CBM_MEM_CLASS_OTHER, ncap * sizeof(*np));
if (!np) {
return false;
}
if (u->npaths) {
memcpy((void *)np, (const void *)u->paths, u->npaths * sizeof(*np));
}
cbm_free(CBM_MEM_CLASS_OTHER, (void *)u->paths);
u->paths = np;
u->cap_paths = ncap;
}
u->paths[u->npaths++] = rel;
cbm_ht_set(u->listed, rel, (void *)rel);
for (const char *p = rel; p;) {
u->tail_steps++;
@@ -296,145 +246,66 @@ static bool ip_list_add(cbm_include_universe_t *u, const char *rel) {
return true;
}
/* Push dir onto the walk's stack (heap, grown by doubling). */
static bool ip_push_dir(char ***stack, int *top, int *cap, char *dir) {
if (*top >= *cap) {
int ncap = *cap * PAIR_LEN;
char **ns = cbm_alloc(CBM_MEM_CLASS_OTHER, (size_t)ncap * sizeof(*ns));
if (!ns) {
return false;
}
memcpy((void *)ns, (const void *)*stack, (size_t)*top * sizeof(*ns));
cbm_free(CBM_MEM_CLASS_OTHER, (void *)*stack);
*stack = ns;
*cap = ncap;
cbm_include_universe_t *cbm_include_universe_new(const cbm_file_info_t *files, int count) {
cbm_include_universe_t *u = cbm_alloc(CBM_MEM_CLASS_OTHER, sizeof(*u));
if (!u) {
return NULL;
}
(*stack)[(*top)++] = dir;
return true;
}
/* One directory of the walk: its listed files kept, its subdirectories
* pushed; false when the listing cannot be whole. */
static bool ip_list_dir(cbm_include_universe_t *u, const char *dir, char ***stack, int *top,
int *cap) {
char abs[IP_PATH_MAX * PAIR_LEN];
if (snprintf(abs, sizeof(abs), "%s%s%s", u->root, dir[0] ? "/" : "", dir) >= (int)sizeof(abs)) {
return false;
}
cbm_dir_t *d = cbm_opendir(abs);
if (!d) {
return false;
}
bool ok = true;
cbm_dirent_t *ent;
while (ok && (ent = cbm_readdir(d)) != NULL) {
const char *name = ent->name;
if (strcmp(name, ".") == 0 || strcmp(name, "..") == 0 || strcmp(name, ".git") == 0) {
continue;
}
u->visited++;
/* a symbolic link is not entered: it may leave the repository, and
* what it points at inside is listed anyway */
if (ent->d_type == IP_DT_LNK) {
u->saw_link = true;
continue;
}
if (!ent->is_dir && !cbm_include_path_listed_name(name)) {
continue;
}
char *rel = dir[0] ? cbm_arena_sprintf(&u->arena, "%s/%s", dir, name)
: cbm_arena_strdup(&u->arena, name);
if (!rel || strlen(rel) >= IP_PATH_MAX) {
ok = false; /* a path the rule cannot hold: the listing is not whole */
break;
}
if (!ent->is_dir) {
ok = ip_list_add(u, rel);
continue;
}
char child[IP_PATH_MAX * PAIR_LEN];
cbm_path_info_t info;
if (snprintf(child, sizeof(child), "%s/%s", abs, name) >= (int)sizeof(child) ||
cbm_path_info_utf8(child, &info) != CBM_PATH_INFO_OK) {
ok = false;
break;
}
if (info.is_symlink) { /* a junction (Windows) is not entered either */
u->saw_link = true;
} else {
ok = ip_push_dir(stack, top, cap, rel);
}
}
cbm_closedir(d);
return ok;
}
/* The listing: an explicit stack of directories, each read once. */
static void ip_list(cbm_include_universe_t *u) {
u->did_list = true;
memset(u, 0, sizeof(*u));
cbm_arena_init(&u->arena);
u->listed = cbm_ht_create(CBM_SZ_4K);
u->tails = cbm_ht_create(CBM_SZ_4K);
u->ftails = cbm_ht_create(CBM_SZ_4K);
if (!u->listed || !u->tails || !u->ftails) {
return;
cbm_include_universe_free(u);
return NULL;
}
int cap = CBM_SZ_64;
int top = 0;
char **stack = cbm_alloc(CBM_MEM_CLASS_OTHER, (size_t)cap * sizeof(*stack));
bool ok = stack != NULL;
if (ok) {
stack[top++] = cbm_arena_strdup(&u->arena, "");
ok = stack[0] != NULL;
u->complete = true;
for (int i = 0; files && i < count; i++) {
const char *rel = files[i].rel_path;
if (!rel || !rel[0]) {
continue;
}
const char *base = strrchr(rel, '/');
base = base ? base + SKIP_ONE : rel;
if (!cbm_include_path_listed_name(base) || cbm_ht_get(u->listed, rel)) {
continue; /* a name no row's import names, or listed already */
}
if (strlen(rel) >= IP_PATH_MAX) {
u->complete = false; /* a path the rule cannot hold */
continue;
}
if (!ip_list_add(u, rel)) {
cbm_include_universe_free(u);
return NULL;
}
}
while (ok && top > 0) {
const char *dir = stack[--top];
ok = ip_list_dir(u, dir, &stack, &top, &cap);
}
cbm_free(CBM_MEM_CLASS_OTHER, (void *)stack);
u->complete = ok;
char b[CBM_SZ_32];
snprintf(b, sizeof(b), "%llu", (unsigned long long)u->visited);
if (!ok) {
cbm_log_warn("include_path.listing_incomplete", "visited", b, "effect",
"answers_needing_it_abstain");
} else {
cbm_log_info("include_path.listing", "visited", b);
}
}
static bool ip_listing(cbm_include_universe_t *u) {
if (!u->did_list) {
ip_list(u);
}
return u->complete;
return u;
}
enum { IP_EXISTS_NO = 0, IP_EXISTS_YES = 1, IP_EXISTS_UNKNOWN = 2 };
/* A file of the run (a File node), or a listed one; unknown when the listing
* a listed name needs cannot complete. */
static int ip_exists(cbm_include_universe_t *u, const cbm_gbuf_t *graph, const char *project,
/* A file of the run (a File node), or an indexed one; unknown when the
* universe is not whole for a name a row keeps. */
static int ip_exists(const cbm_include_universe_t *u, const cbm_gbuf_t *graph, const char *project,
const char *rel) {
if (graph && project && cbm_pipeline_file_node(graph, project, rel)) {
return IP_EXISTS_YES;
}
if (cbm_ht_get(u->listed, rel)) {
return IP_EXISTS_YES;
}
const char *base = strrchr(rel, '/');
base = base ? base + SKIP_ONE : rel;
if (!cbm_include_path_listed_name(base)) {
return IP_EXISTS_NO; /* not a file of the run, and named like nothing the listing keeps */
}
if (!ip_listing(u)) {
return IP_EXISTS_UNKNOWN;
}
return cbm_ht_get(u->listed, rel) ? IP_EXISTS_YES : IP_EXISTS_NO;
return u->complete || !cbm_include_path_listed_name(base) ? IP_EXISTS_NO : IP_EXISTS_UNKNOWN;
}
/* Of the candidates, the one that names a file; NULL when none does, when
* (exactly one required) several do, or when existence is unknown for a
* candidate that decides the answer. */
static const char *ip_pick(cbm_include_universe_t *u, const cbm_gbuf_t *graph, const char *project,
char cands[][IP_PATH_MAX], int n, bool exactly_one, char *buf,
size_t buf_sz) {
static const char *ip_pick(const cbm_include_universe_t *u, const cbm_gbuf_t *graph,
const char *project, char cands[][IP_PATH_MAX], int n, bool exactly_one,
char *buf, size_t buf_sz) {
const char *hit = NULL;
for (int i = 0; i < n; i++) {
bool dup = false;
@@ -467,15 +338,15 @@ static const char *ip_pick(cbm_include_universe_t *u, const cbm_gbuf_t *graph, c
return buf;
}
/* The one listed file whose repository path is `key` or ends with "/" + key. */
static const char *ip_unique_tail(cbm_include_universe_t *u, const cbm_include_path_row_t *row,
const char *key, char *buf, size_t buf_sz) {
/* The one indexed file whose repository path is `key` or ends with "/" + key. */
static const char *ip_unique_tail(const cbm_include_universe_t *u,
const cbm_include_path_row_t *row, const char *key, char *buf,
size_t buf_sz) {
const char *base = strrchr(key, '/');
base = base ? base + SKIP_ONE : key;
if (!base[0] || !ip_row_names(row, base) || !ip_listing(u)) {
return NULL; /* a name the listing does not keep, or no whole listing */
if (!base[0] || !ip_row_names(row, base) || !u->complete) {
return NULL; /* a name the universe does not keep, or not whole */
}
u->tail_steps++;
const ip_tail_t *e = cbm_ht_get(u->tails, key);
if (!e || e->n != SKIP_ONE || strlen(e->path) >= buf_sz) {
return NULL; /* none, or two files end with it */
@@ -484,22 +355,44 @@ static const char *ip_unique_tail(cbm_include_universe_t *u, const cbm_include_p
return buf;
}
/* Proven that no repository file is `key` or ends with "/" + key: the whole
* listing holds none, none when letter case is ignored, and the walk passed
* no symbolic link a path could hide under. */
static bool ip_absent_tail(cbm_include_universe_t *u, const cbm_include_path_row_t *row,
/* Proven that no indexed file is `key` or ends with "/" + key, also when
* letter case is ignored. */
static bool ip_absent_tail(const cbm_include_universe_t *u, const cbm_include_path_row_t *row,
const char *key) {
const char *base = strrchr(key, '/');
base = base ? base + SKIP_ONE : key;
if (!base[0] || !ip_row_names(row, base) || !ip_listing(u) || u->saw_link) {
char folded[IP_PATH_MAX];
if (!base[0] || !ip_row_names(row, base) || !u->complete ||
!ip_fold_into(folded, sizeof(folded), key)) {
return false;
}
u->tail_steps++;
const char *folded = ip_fold(u, key);
return folded && !cbm_ht_get(u->tails, key) && !cbm_ht_get(u->ftails, folded);
return !cbm_ht_get(u->tails, key) && !cbm_ht_get(u->ftails, folded);
}
cbm_include_path_verdict_t cbm_include_path_resolve(cbm_include_universe_t *u,
/* A path protoc accepts as a virtual path: no `.` or `..` segment, no empty
* segment (`//`), no backslash. */
static bool ip_virtual_ok(const char *p) {
if (strchr(p, '\\')) {
return false;
}
for (const char *s = p; *s;) {
size_t sl = strcspn(s, "/");
if (sl == 0 || (sl == SKIP_ONE && s[0] == '.') ||
(sl == PAIR_LEN && s[0] == '.' && s[1] == '.')) {
return false;
}
s += sl;
if (*s == '/') {
s++;
if (!*s) {
return false; /* a trailing slash: an empty segment */
}
}
}
return true;
}
cbm_include_path_verdict_t cbm_include_path_resolve(const cbm_include_universe_t *u,
const cbm_gbuf_t *graph, const char *project,
const char *rel, CBMLanguage lang,
const CBMImport *imp, char *buf,
@@ -519,11 +412,19 @@ cbm_include_path_verdict_t cbm_include_path_resolve(cbm_include_universe_t *u,
}
}
char dir[IP_PATH_MAX];
if (!u || p[0] == '/' || strpbrk(p, "$`") || !ip_dirname(dir, sizeof(dir), rel)) {
if (!u || p[0] == '/' || strpbrk(p, "$`") || (row->virtual_path && !ip_virtual_ok(p)) ||
!ip_dirname(dir, sizeof(dir), rel)) {
return CBM_INCLUDE_PATH_NONE; /* a path the rule cannot read */
}
/* The tail key: the path after its leading `./` and `../` segments (a
* Make path climbing out of its directory, from any directory make runs
* in, still names a file that ends with the rest). */
const char *kp = p;
while (strncmp(kp, "./", PAIR_LEN) == 0 || strncmp(kp, "../", IP_UP_LEN) == 0) {
kp += kp[1] == '.' ? IP_UP_LEN : PAIR_LEN;
}
char key[IP_PATH_MAX];
bool keyed = ip_join(key, sizeof(key), "", p);
bool keyed = ip_join(key, sizeof(key), "", kp);
if (row->whole_path_tail && keyed && ip_unique_tail(u, row, key, buf, buf_sz)) {
return CBM_INCLUDE_PATH_FILE;
}
@@ -536,11 +437,15 @@ cbm_include_path_verdict_t cbm_include_path_resolve(cbm_include_universe_t *u,
n++;
}
if (ip_pick(u, graph, project, cands, n, row->exactly_one, buf, buf_sz)) {
return CBM_INCLUDE_PATH_FILE;
/* A tail row gets here only when several indexed files end with the
* path: the one beside the importer is an assumption about the include
* root. Below the root, a base is an assumption about the directory
* the tool runs in. Only a makefile at the root decides. */
return row->whole_path_tail || dir[0] ? CBM_INCLUDE_PATH_GUESS : CBM_INCLUDE_PATH_FILE;
}
/* Proof of "no file": no repository file is the path or ends with it, so
* no include root or run directory inside the repository can supply one
* (absence at the bases alone would not do: another directory may). */
/* Proof of "no file": no indexed file is the path or ends with it, so no
* include root or run directory inside the index can supply one (absence
* at the bases alone would not do: another directory may). */
return keyed && ip_absent_tail(u, row, key) ? CBM_INCLUDE_PATH_NO_FILE : CBM_INCLUDE_PATH_NONE;
}
@@ -585,35 +490,33 @@ static int ip_strcmp_ptr(const void *a, const void *b) {
return strcmp(*(const char *const *)a, *(const char *const *)b);
}
int cbm_include_path_listing_digest(const char *repo_path, char out[CBM_SHA256_HEX_LEN + 1]) {
cbm_include_universe_t *u = cbm_include_universe_new(repo_path);
if (!u) {
int cbm_include_path_inventory_digest(const cbm_file_info_t *files, int count,
char out[CBM_SHA256_HEX_LEN + 1]) {
const char **sorted =
count > 0 ? cbm_alloc(CBM_MEM_CLASS_OTHER, (size_t)count * sizeof(*sorted)) : NULL;
if (count > 0 && !sorted) {
return CBM_NOT_FOUND;
}
ip_list(u);
size_t n = 0;
for (int i = 0; i < count; i++) {
const char *rel = files[i].rel_path;
const char *base = rel ? strrchr(rel, '/') : NULL;
base = base ? base + SKIP_ONE : rel;
if (rel && rel[0] && cbm_include_path_listed_name(base)) {
sorted[n++] = rel;
}
}
if (n > SKIP_ONE) {
qsort((void *)sorted, n, sizeof(*sorted), ip_strcmp_ptr);
}
cbm_sha256_ctx sha;
cbm_sha256_init(&sha);
int rc = 0;
if (!u->complete) {
static const char marker[] = "include-listing-incomplete-v1";
cbm_sha256_update(&sha, marker, sizeof(marker) - SKIP_ONE);
} else {
const char **sorted =
u->npaths ? cbm_alloc(CBM_MEM_CLASS_OTHER, u->npaths * sizeof(*sorted)) : NULL;
if (u->npaths && !sorted) {
rc = CBM_NOT_FOUND;
}
for (size_t i = 0; sorted && i < u->npaths; i++) {
sorted[i] = u->paths[i];
}
if (sorted && u->npaths > SKIP_ONE) {
qsort((void *)sorted, u->npaths, sizeof(*sorted), ip_strcmp_ptr);
}
for (size_t i = 0; sorted && i < u->npaths; i++) {
cbm_sha256_update(&sha, sorted[i], strlen(sorted[i]) + SKIP_ONE);
}
cbm_free(CBM_MEM_CLASS_OTHER, (void *)sorted);
static const char version[] = "include-inventory-v1";
cbm_sha256_update(&sha, version, sizeof(version));
for (size_t i = 0; i < n; i++) {
cbm_sha256_update(&sha, sorted[i], strlen(sorted[i]) + SKIP_ONE);
}
cbm_free(CBM_MEM_CLASS_OTHER, (void *)sorted);
uint8_t digest[CBM_SHA256_DIGEST_LEN];
cbm_sha256_final(&sha, digest);
static const char hex[] = "0123456789abcdef";
@@ -622,6 +525,5 @@ int cbm_include_path_listing_digest(const char *repo_path, char out[CBM_SHA256_H
out[i * PAIR_LEN + SKIP_ONE] = hex[digest[i] & 0x0f];
}
out[CBM_SHA256_HEX_LEN] = '\0';
cbm_include_universe_free(u);
return rc;
return 0;
}
+43 -29
View File
@@ -5,23 +5,28 @@
* include_path.h — the path rule: which repository file an import statement
* names, for the languages whose imports name files by path (a row below).
*
* The rule reads the import's text as a path and finds the one repository file
* it names: the bases a row allows (the importing file's directory, the
* The rule reads the import's text as a path and finds the one indexed file it
* names: the bases a row allows (the importing file's directory, the
* repository root), "exactly one" where two bases could both name a file, and
* a whole-path tail (the one listed file whose path is the import's path or
* ends with "/" + it). Existence is read from the run's File nodes and from a
* listing of every file under the repository named like what a row's import
* can name (names only, nothing read; `.git` and symbolic links not entered),
* walked once per run on the first question that needs it. Uniqueness is a
* statement about the repository, not about what the indexer keeps: a file
* in a skipped directory counts. A listing that cannot complete answers none.
* a whole-path tail (the one indexed file whose path is the import's path or
* ends with "/" + it). The universe is the run's indexed file list (the files
* discovery kept, on every route), never a walk of the disk: a copy of a file
* in an ignored or skipped directory (node_modules, a .gitignore'd tree) is
* not seen, so an import only such a copy could satisfy names no file.
*
* A pick that depends on what the index cannot know is a guess, its own
* verdict, never FILE: the file beside a Protobuf importer when several
* indexed files end with the path (protoc reads the one under its -I root),
* and a Make include from a makefile below the root (GNU make reads relative
* paths from the directory it runs in). A guess decides no IMPORTS edge (main
* decides) and resolves no include of the shared layer.
*
* Lifted from the parked include-scope branch (round 5, include_scope.c
* section 2): its universe, bases, exactly-one, tail index and import node;
* the shell and Make evaluated paths and run directories stay there.
* section 2): its bases, exactly-one, tail index and import node; the shell
* and Make evaluated paths and run directories stay there.
*
* Threads: one universe per run. Every question is asked on one thread (the
* import edges and the sequential import map); nothing is read concurrently.
* Threads: a universe is built once, from the file list, before any question,
* and is read-only afterwards: a question writes nothing (sealed at creation).
*/
#include "cbm.h"
@@ -41,6 +46,9 @@ typedef struct {
/* an import whose path does not end in one of `suffixes` is not a path
* the rule reads (Protobuf: a row that names no `.proto` file) */
bool path_needs_suffix;
/* a virtual path (protoc): `.` and `..` segments, `//` and backslashes are
* refused by the tool, so the rule reads no such path */
bool virtual_path;
bool base_file_dir; /* relative to the importing file's directory */
bool base_root; /* relative to the repository root */
bool exactly_one; /* two bases naming two files: none */
@@ -55,28 +63,32 @@ bool cbm_include_path_listed_name(const char *name);
typedef struct cbm_include_universe cbm_include_universe_t;
/* A universe for the repository at repo_path; nothing is walked yet. NULL on
* allocation failure. */
cbm_include_universe_t *cbm_include_universe_new(const char *repo_path);
/* The universe of the run's indexed files (`files`, the discovered list of
* the run on every route): every file whose name a row's import can name.
* Built here, read-only afterwards. NULL on allocation failure. */
cbm_include_universe_t *cbm_include_universe_new(const cbm_file_info_t *files, int count);
void cbm_include_universe_free(cbm_include_universe_t *u);
/* Directory entries the listing read, and tail-index keys made and probed:
* the counted work of growth tests. */
uint64_t cbm_include_universe_visited(const cbm_include_universe_t *u);
/* Tail-index keys made while building: the counted work of growth tests (a
* question makes none). */
uint64_t cbm_include_universe_tail_steps(const cbm_include_universe_t *u);
typedef enum {
CBM_INCLUDE_PATH_NOT_A_PATH = 0, /* no row, or not a path the row reads: main decides */
CBM_INCLUDE_PATH_FILE, /* names one repository file (path in the buffer) */
CBM_INCLUDE_PATH_FILE, /* names one indexed file (path in the buffer) */
CBM_INCLUDE_PATH_NONE, /* a path whose file the rule cannot tell (several, unknown) */
/* proven: no repository file is that path or ends with it (whole listing,
* letter case ignored, no symbolic link passed) */
/* proven: no indexed file is that path or ends with it (letter case
* ignored) */
CBM_INCLUDE_PATH_NO_FILE,
/* a file picked by an assumption the index cannot check (path in the
* buffer): the include root or the directory make runs in. Not FILE: main
* decides the edge, the shared layer takes the include as unresolved. */
CBM_INCLUDE_PATH_GUESS,
} cbm_include_path_verdict_t;
/* The file the import `imp` of the file `rel` names, written into buf.
* `graph`/`project`: the run's File nodes, which count as existing. */
cbm_include_path_verdict_t cbm_include_path_resolve(cbm_include_universe_t *u,
cbm_include_path_verdict_t cbm_include_path_resolve(const cbm_include_universe_t *u,
const cbm_gbuf_t *graph, const char *project,
const char *rel, CBMLanguage lang,
const CBMImport *imp, char *buf, size_t buf_sz);
@@ -84,14 +96,16 @@ cbm_include_path_verdict_t cbm_include_path_resolve(cbm_include_universe_t *u,
/* The IMPORTS target of an import, for resolve_import_strategies: true when
* the path rule decides it, with *out the target node (the file's Module node
* when it has one, else its File node) or NULL: no edge (the named file has no
* node, or no repository file is named; a symbol is never the target of a
* statement that names a file). False: main's strategies decide. */
* node, or no indexed file is named; a symbol is never the target of a
* statement that names a file). False: main's strategies decide (a guess is
* not a decision). */
bool cbm_include_path_decides(const cbm_pipeline_ctx_t *ctx, const char *rel, CBMLanguage lang,
const CBMImport *imp, const cbm_gbuf_node_t **out);
/* The semantic-manifest digest of the listing: every listed path, sorted, or
* a fixed marker when the listing cannot complete. Walks the repository.
* Returns 0, or -1 on allocation failure. */
int cbm_include_path_listing_digest(const char *repo_path, char out[CBM_SHA256_HEX_LEN + 1]);
/* The semantic-manifest key of the universe: every indexed path a row's
* import can name, sorted, hashed. Reads the list only (no disk). Returns 0,
* or -1 on allocation failure. */
int cbm_include_path_inventory_digest(const cbm_file_info_t *files, int count,
char out[CBM_SHA256_HEX_LEN + 1]);
#endif /* CBM_INCLUDE_PATH_H */
+2 -3
View File
@@ -4561,9 +4561,8 @@ static int cbm_pipeline_run_staged(cbm_pipeline_t *p) {
* when no usable configs are found — non-TS projects pay nothing. */
path_aliases =
cbm_load_path_aliases_excluded(p->repo_path, p->excluded_dirs, p->excluded_count);
/* The path rule's universe (include_path.h): nothing is walked until an
* import of a path row asks. */
include_universe = cbm_include_universe_new(p->repo_path);
/* The path rule's universe (include_path.h): the run's indexed files. */
include_universe = cbm_include_universe_new(files, file_count);
/* Build shared context for pass functions */
cbm_pipeline_ctx_t ctx = {
+8 -7
View File
@@ -570,17 +570,18 @@ int cbm_pipeline_build_semantic_manifest(const cbm_pipeline_t *p, const char *pr
rc = semantic_manifest_add_digest(&builder, project, CBM_SEMANTIC_INPUT_PROJECT_CONFIG,
project_config_digest, 0, 0);
}
/* The path rule's listing (include_path.h), only where a file of a path
* row is discovered: its imports read files no manifest row tracks (a
* pruned or ignored `.proto`), and a changed set of those names, anywhere,
* must route to a full index. */
/* The path rule's universe (include_path.h), only where a file of a path
* row is discovered: a changed set of the indexed names a row's import
* can name (one added, removed or renamed) changes which file an import
* names in files that did not change, so it routes to a full index. A key
* of the discovered list: nothing is walked. */
bool path_row_file = false;
for (int i = 0; i < file_count && !path_row_file; i++) {
path_row_file = cbm_include_path_row(files[i].language) != NULL;
}
if (rc == 0 && path_row_file) {
char listing_digest[CBM_SHA256_HEX_LEN + 1];
rc = cbm_include_path_listing_digest(repo_path, listing_digest);
rc = cbm_include_path_inventory_digest(files, file_count, listing_digest);
if (rc == 0) {
rc = semantic_manifest_add_digest(&builder, project, CBM_SEMANTIC_INPUT_INCLUDE_LISTING,
listing_digest, 0, 0);
@@ -3859,7 +3860,7 @@ static int run_closure_delta(cbm_pipeline_t *p, const char *db_path, const char
cbm_log_error("delta.err", "phase", "stored_namespaces");
goto out;
}
include_universe = cbm_include_universe_new(cbm_pipeline_repo_path(p));
include_universe = cbm_include_universe_new(files, file_count);
cbm_pipeline_ctx_t ctx = {
.include_universe = include_universe,
.project_name = project,
@@ -4620,7 +4621,7 @@ int cbm_pipeline_run_incremental(cbm_pipeline_t *p, const char *db_path, cbm_fil
cbm_path_alias_collection_t *path_aliases =
cbm_load_path_aliases_excluded(cbm_pipeline_repo_path(p), excluded_dirs, excluded_count);
cbm_include_universe_t *include_universe = cbm_include_universe_new(cbm_pipeline_repo_path(p));
cbm_include_universe_t *include_universe = cbm_include_universe_new(files, file_count);
cbm_pipeline_ctx_t ctx = {
.include_universe = include_universe,
+218 -122
View File
@@ -1,20 +1,28 @@
/*
* test_include_path.c -- the path rule (src/pipeline/include_path.h): which
* repository file a Protobuf or Make import names, read from a real tree.
* indexed file a Protobuf or Make import names.
*/
#include "../src/foundation/compat.h"
#include "test_framework.h"
#include "test_helpers.h"
#include "../src/pipeline/include_path.h"
#include "../src/foundation/compat_fs.h"
#include "pipeline/pipeline.h"
#include "sqlite3.h"
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <unistd.h>
static char *ip_tree(void) {
char *dir = th_mktempdir("cbm_ip");
return dir ? strdup(dir) : NULL;
/* A universe of the given indexed paths (NULL-terminated). */
static cbm_include_universe_t *ip_universe(const char *const *paths) {
cbm_file_info_t files[64];
int n = 0;
for (; paths[n] && n < 64; n++) {
memset(&files[n], 0, sizeof(files[n]));
files[n].rel_path = (char *)paths[n];
}
return cbm_include_universe_new(files, n);
}
/* The verdict for an import `path` of the file `rel`, the file in *out. */
@@ -28,15 +36,11 @@ static cbm_include_path_verdict_t ip_ask(cbm_include_universe_t *u, CBMLanguage
return cbm_include_path_resolve(u, NULL, NULL, rel, lang, &imp, out, out_sz);
}
TEST(include_path_proto_tail_beside_and_ambiguity) {
char *root = ip_tree();
ASSERT_NOT_NULL(root);
th_write_file(TH_PATH(root, "api/a.proto"), "syntax = \"proto3\";\n");
th_write_file(TH_PATH(root, "x/common.proto"), "syntax = \"proto3\";\n");
th_write_file(TH_PATH(root, "y/z/shared.proto"), "syntax = \"proto3\";\n");
th_write_file(TH_PATH(root, "w/shared.proto"), "syntax = \"proto3\";\n");
th_write_file(TH_PATH(root, "api/shared.proto"), "syntax = \"proto3\";\n");
cbm_include_universe_t *u = cbm_include_universe_new(root);
TEST(include_path_proto_tail_guess_and_ambiguity) {
static const char *const files[] = {"api/a.proto", "x/common.proto", "y/z/shared.proto",
"w/shared.proto", "api/shared.proto", "main.go",
NULL};
cbm_include_universe_t *u = ip_universe(files);
ASSERT_NOT_NULL(u);
char got[1024];
@@ -51,24 +55,35 @@ TEST(include_path_proto_tail_beside_and_ambiguity) {
CBM_INCLUDE_PATH_FILE);
ASSERT_STR_EQ(got, "y/z/shared.proto");
/* three files end with the tail: the one beside the importer */
/* three files end with the tail: the one beside the importer is a guess
* (protoc -I . and -I api read different files), never FILE */
ASSERT_EQ(ip_ask(u, CBM_LANG_PROTOBUF, "api/a.proto", "shared.proto", got, sizeof(got)),
CBM_INCLUDE_PATH_FILE);
CBM_INCLUDE_PATH_GUESS);
ASSERT_STR_EQ(got, "api/shared.proto");
/* ... and none from a directory without one */
ASSERT_EQ(ip_ask(u, CBM_LANG_PROTOBUF, "x/common.proto", "shared.proto", got, sizeof(got)),
CBM_INCLUDE_PATH_NONE);
/* no such file (proven); a path the rule cannot read; leaving the repository */
/* no such file (proven); only a case variant exists (a case-insensitive
* disk opens it, so not proven); absolute */
ASSERT_EQ(ip_ask(u, CBM_LANG_PROTOBUF, "api/a.proto", "nope/none.proto", got, sizeof(got)),
CBM_INCLUDE_PATH_NO_FILE);
/* only a case variant exists: a case-insensitive disk opens it, so not proven */
ASSERT_EQ(ip_ask(u, CBM_LANG_PROTOBUF, "api/a.proto", "X/Common.proto", got, sizeof(got)),
CBM_INCLUDE_PATH_NONE);
ASSERT_EQ(ip_ask(u, CBM_LANG_PROTOBUF, "api/a.proto", "/abs/common.proto", got, sizeof(got)),
CBM_INCLUDE_PATH_NONE);
/* paths protoc refuses ("Backslashes, consecutive slashes, \".\", or \"..\"
* are not allowed in the virtual path"): not read, never normalised */
ASSERT_EQ(ip_ask(u, CBM_LANG_PROTOBUF, "a.proto", "../common.proto", got, sizeof(got)),
CBM_INCLUDE_PATH_NONE);
ASSERT_EQ(ip_ask(u, CBM_LANG_PROTOBUF, "api/a.proto", "./x/common.proto", got, sizeof(got)),
CBM_INCLUDE_PATH_NONE);
ASSERT_EQ(ip_ask(u, CBM_LANG_PROTOBUF, "api/a.proto", "x//common.proto", got, sizeof(got)),
CBM_INCLUDE_PATH_NONE);
ASSERT_EQ(ip_ask(u, CBM_LANG_PROTOBUF, "api/a.proto", "x\\common.proto", got, sizeof(got)),
CBM_INCLUDE_PATH_NONE);
ASSERT_EQ(ip_ask(u, CBM_LANG_PROTOBUF, "api/a.proto", "x/./common.proto", got, sizeof(got)),
CBM_INCLUDE_PATH_NONE);
/* a row that names no .proto file is not read as a path */
ASSERT_EQ(ip_ask(u, CBM_LANG_PROTOBUF, "api/a.proto", "x.common", got, sizeof(got)),
@@ -77,116 +92,117 @@ TEST(include_path_proto_tail_beside_and_ambiguity) {
ASSERT_EQ(ip_ask(u, CBM_LANG_GO, "api/a.go", "x/common.proto", got, sizeof(got)),
CBM_INCLUDE_PATH_NOT_A_PATH);
cbm_include_universe_free(u);
th_rmtree(root);
free(root);
PASS();
}
TEST(include_path_make_exactly_one_base) {
char *root = ip_tree();
ASSERT_NOT_NULL(root);
th_write_file(TH_PATH(root, "Makefile"), "include rules.mk\n");
th_write_file(TH_PATH(root, "sub/Makefile"), "include local.mk\n");
th_write_file(TH_PATH(root, "sub/local.mk"), "X = 1\n");
th_write_file(TH_PATH(root, "rules.mk"), "Y = 1\n");
th_write_file(TH_PATH(root, "both.mk"), "Z = 1\n");
th_write_file(TH_PATH(root, "sub/both.mk"), "Z = 2\n");
th_write_file(TH_PATH(root, "other/elsewhere.mk"), "W = 1\n");
cbm_include_universe_t *u = cbm_include_universe_new(root);
/* GNU make reads a relative include from the directory it runs in (then -I
* dirs): a root makefile decides, a makefile below the root only guesses
* (GNU make 4.3: `include b.mk` in sub/a.mk, included from the root, does not
* read sub/b.mk). */
TEST(include_path_make_root_decides_below_is_guess) {
static const char *const files[] = {"Makefile", "sub/Makefile", "sub/local.mk",
"rules.mk", "both.mk", "sub/both.mk",
"other/elsewhere.mk", "sub/a.mk", "sub/b.mk",
NULL};
cbm_include_universe_t *u = ip_universe(files);
ASSERT_NOT_NULL(u);
char got[1024];
/* only beside the makefile; only at the root */
ASSERT_EQ(ip_ask(u, CBM_LANG_MAKEFILE, "sub/Makefile", "local.mk", got, sizeof(got)),
CBM_INCLUDE_PATH_FILE);
ASSERT_STR_EQ(got, "sub/local.mk");
ASSERT_EQ(ip_ask(u, CBM_LANG_MAKEFILE, "sub/Makefile", "rules.mk", got, sizeof(got)),
/* the root makefile: its own directory is the root */
ASSERT_EQ(ip_ask(u, CBM_LANG_MAKEFILE, "Makefile", "rules.mk", got, sizeof(got)),
CBM_INCLUDE_PATH_FILE);
ASSERT_STR_EQ(got, "rules.mk");
/* both: which one make reads depends on where it runs */
ASSERT_EQ(ip_ask(u, CBM_LANG_MAKEFILE, "Makefile", "sub/local.mk", got, sizeof(got)),
CBM_INCLUDE_PATH_FILE);
ASSERT_STR_EQ(got, "sub/local.mk");
/* below the root: beside it, or at the root, depends on where make runs */
ASSERT_EQ(ip_ask(u, CBM_LANG_MAKEFILE, "sub/Makefile", "local.mk", got, sizeof(got)),
CBM_INCLUDE_PATH_GUESS);
ASSERT_EQ(ip_ask(u, CBM_LANG_MAKEFILE, "sub/Makefile", "rules.mk", got, sizeof(got)),
CBM_INCLUDE_PATH_GUESS);
ASSERT_EQ(ip_ask(u, CBM_LANG_MAKEFILE, "sub/a.mk", "b.mk", got, sizeof(got)),
CBM_INCLUDE_PATH_GUESS);
/* both: none */
ASSERT_EQ(ip_ask(u, CBM_LANG_MAKEFILE, "sub/Makefile", "both.mk", got, sizeof(got)),
CBM_INCLUDE_PATH_NONE);
/* none (proven); a name with a variable */
/* none (proven); climbing out of its directory, from any directory make
* runs in it still names a file ending in the rest (none); a name with a
* variable */
ASSERT_EQ(ip_ask(u, CBM_LANG_MAKEFILE, "Makefile", "missing.mk", got, sizeof(got)),
CBM_INCLUDE_PATH_NO_FILE);
/* not at either base, but elsewhere in the repository (make may run
* there): not proven absent */
ASSERT_EQ(ip_ask(u, CBM_LANG_MAKEFILE, "sub/x/Makefile", "../../gone.mk", got, sizeof(got)),
CBM_INCLUDE_PATH_NO_FILE);
/* not at either base, but elsewhere (make may run there): not proven absent */
ASSERT_EQ(ip_ask(u, CBM_LANG_MAKEFILE, "sub/Makefile", "elsewhere.mk", got, sizeof(got)),
CBM_INCLUDE_PATH_NONE);
ASSERT_EQ(ip_ask(u, CBM_LANG_MAKEFILE, "Makefile", "$(TOP)/rules.mk", got, sizeof(got)),
CBM_INCLUDE_PATH_NONE);
cbm_include_universe_free(u);
th_rmtree(root);
free(root);
PASS();
}
/* `.git` and a symbolic link to a directory are not entered; a file under
* either is no answer. */
TEST(include_path_listing_skips_git_and_links) {
char *root = ip_tree();
ASSERT_NOT_NULL(root);
th_write_file(TH_PATH(root, "a.proto"), "");
th_write_file(TH_PATH(root, ".git/hidden/g.proto"), "");
th_write_file(TH_PATH(root, "real/l.proto"), "");
char target[1024];
snprintf(target, sizeof(target), "%s/real", root);
ASSERT_EQ(symlink(target, TH_PATH(root, "linked")), 0);
cbm_include_universe_t *u = cbm_include_universe_new(root);
/* The universe is the indexed list: a file the run did not index (an ignored
* or skipped directory) is not seen, whatever is on the disk. */
TEST(include_path_universe_is_the_indexed_files) {
static const char *const files[] = {"a.proto", "common/t.proto", "real/l.proto", NULL};
cbm_include_universe_t *u = ip_universe(files);
ASSERT_NOT_NULL(u);
char got[1024];
ASSERT_EQ(ip_ask(u, CBM_LANG_PROTOBUF, "a.proto", "hidden/g.proto", got, sizeof(got)),
CBM_INCLUDE_PATH_NONE);
ASSERT_EQ(ip_ask(u, CBM_LANG_PROTOBUF, "a.proto", "linked/l.proto", got, sizeof(got)),
CBM_INCLUDE_PATH_NONE);
/* the file itself, where it really is, is one answer (the link adds none) */
/* node_modules/pkg/common/t.proto exists on a disk, not in the index: the
* indexed one is the only file the tail names */
ASSERT_EQ(ip_ask(u, CBM_LANG_PROTOBUF, "a.proto", "common/t.proto", got, sizeof(got)),
CBM_INCLUDE_PATH_FILE);
ASSERT_STR_EQ(got, "common/t.proto");
/* only under an ignored directory: no indexed file, proven */
ASSERT_EQ(ip_ask(u, CBM_LANG_PROTOBUF, "a.proto", "only.proto", got, sizeof(got)),
CBM_INCLUDE_PATH_NO_FILE);
ASSERT_EQ(ip_ask(u, CBM_LANG_PROTOBUF, "a.proto", "l.proto", got, sizeof(got)),
CBM_INCLUDE_PATH_FILE);
ASSERT_STR_EQ(got, "real/l.proto");
cbm_include_universe_free(u);
th_rmtree(root);
free(root);
PASS();
}
/* The manifest digest follows the set of listed names, not their contents
* nor files the rows do not name. */
TEST(include_path_listing_digest_follows_names) {
char *root = ip_tree();
ASSERT_NOT_NULL(root);
th_write_file(TH_PATH(root, "a.proto"), "one");
/* The manifest key follows the set of indexed names a row keeps: not their
* order, nor files no row names. */
TEST(include_path_inventory_digest_follows_indexed_names) {
cbm_file_info_t f[4];
memset(f, 0, sizeof(f));
char d0[CBM_SHA256_HEX_LEN + 1];
char d1[CBM_SHA256_HEX_LEN + 1];
char d2[CBM_SHA256_HEX_LEN + 1];
char d3[CBM_SHA256_HEX_LEN + 1];
ASSERT_EQ(cbm_include_path_listing_digest(root, d0), 0);
th_write_file(TH_PATH(root, "a.proto"), "two");
th_write_file(TH_PATH(root, "main.go"), "package main\n");
ASSERT_EQ(cbm_include_path_listing_digest(root, d1), 0);
th_write_file(TH_PATH(root, "vendor/pruned/b.proto"), "");
ASSERT_EQ(cbm_include_path_listing_digest(root, d2), 0);
th_write_file(TH_PATH(root, "build/rules.mk"), "");
ASSERT_EQ(cbm_include_path_listing_digest(root, d3), 0);
f[0].rel_path = "a.proto";
f[1].rel_path = "main.go";
ASSERT_EQ(cbm_include_path_inventory_digest(f, 1, d0), 0);
ASSERT_EQ(cbm_include_path_inventory_digest(f, 2, d1), 0);
f[2].rel_path = "b/rules.mk";
ASSERT_EQ(cbm_include_path_inventory_digest(f, 3, d2), 0);
f[0].rel_path = "b/rules.mk";
f[2].rel_path = "a.proto";
ASSERT_EQ(cbm_include_path_inventory_digest(f, 3, d3), 0);
ASSERT_STR_EQ(d1, d0);
ASSERT_STR_NEQ(d2, d1);
ASSERT_STR_NEQ(d3, d2);
th_rmtree(root);
free(root);
ASSERT_STR_EQ(d3, d2);
PASS();
}
/* Counted work of the listing and the tail index: n files at 1x, 4x, 16x. */
static bool ip_work(int n, uint64_t *visited, uint64_t *steps) {
char *root = ip_tree();
if (!root) {
/* Counted work of building the tail index: n files at 1x, 4x, 16x (a
* question makes no work the universe keeps). */
static bool ip_work(int n, uint64_t *steps) {
cbm_file_info_t *files = calloc((size_t)n * 2, sizeof(*files));
char (*names)[64] = calloc((size_t)n * 2, sizeof(*names));
if (!files || !names) {
free(files);
free(names);
return false;
}
for (int i = 0; i < n; i++) {
char rel[64];
snprintf(rel, sizeof(rel), "d%d/e%d/f%d.proto", i % 10, i % 7, i);
th_write_file(TH_PATH(root, rel), "");
snprintf(rel, sizeof(rel), "d%d/src%d.go", i % 10, i);
th_write_file(TH_PATH(root, rel), "");
snprintf(names[2 * i], 64, "d%d/e%d/f%d.proto", i % 10, i % 7, i);
snprintf(names[2 * i + 1], 64, "d%d/src%d.go", i % 10, i);
files[2 * i].rel_path = names[2 * i];
files[2 * i + 1].rel_path = names[2 * i + 1];
}
cbm_include_universe_t *u = cbm_include_universe_new(root);
cbm_include_universe_t *u = cbm_include_universe_new(files, n * 2);
char got[1024];
bool ok = u != NULL;
for (int i = 0; ok && i < n; i++) {
@@ -195,40 +211,32 @@ static bool ip_work(int n, uint64_t *visited, uint64_t *steps) {
ok = ip_ask(u, CBM_LANG_PROTOBUF, "top.proto", want, got, sizeof(got)) ==
CBM_INCLUDE_PATH_FILE;
}
*visited = cbm_include_universe_visited(u);
*steps = cbm_include_universe_tail_steps(u);
cbm_include_universe_free(u);
th_rmtree(root);
free(root);
free(files);
free(names);
return ok;
}
TEST(include_path_listing_grows_linearly) {
uint64_t v1, s1, v4, s4, v16, s16;
ASSERT_TRUE(ip_work(50, &v1, &s1));
ASSERT_TRUE(ip_work(200, &v4, &s4));
ASSERT_TRUE(ip_work(800, &v16, &s16));
/* entries read and index steps: at most 4.5 times per 4 times */
ASSERT_TRUE(v4 * 2 <= v1 * 9 && v16 * 2 <= v4 * 9);
TEST(include_path_universe_grows_linearly) {
uint64_t s1, s4, s16;
ASSERT_TRUE(ip_work(50, &s1));
ASSERT_TRUE(ip_work(200, &s4));
ASSERT_TRUE(ip_work(800, &s16));
/* index steps: at most 4.5 times per 4 times */
ASSERT_TRUE(s4 * 2 <= s1 * 9 && s16 * 2 <= s4 * 9);
ASSERT_GT(v16, v4);
ASSERT_GT(s16, s4);
PASS();
}
/* The IMPORTS decision: a statement that names a file is decided by the path
* rule when it names one repository file (its node, or no edge without one)
* or provably none (no edge: a symbol of the name is never its target); with
* several candidate files main's strategies decide. */
* rule when it names one indexed file (its node) or provably none (no edge: a
* symbol of the name is never its target); with several candidate files, or
* a guess, main's strategies decide. */
TEST(include_path_names_no_file_decides_no_target) {
char *root = ip_tree();
ASSERT_NOT_NULL(root);
th_write_file(TH_PATH(root, "a.proto"), "");
th_write_file(TH_PATH(root, "x/common.proto"), "");
th_write_file(TH_PATH(root, "y/dup.proto"), "");
th_write_file(TH_PATH(root, "z/dup.proto"), "");
th_write_file(TH_PATH(root, "skipped/only.proto"), "");
th_write_file(TH_PATH(root, "Makefile"), "");
cbm_gbuf_t *gb = cbm_gbuf_new("p", root);
static const char *const files[] = {"a.proto", "x/common.proto", "y/dup.proto",
"z/dup.proto", "Makefile", NULL};
cbm_gbuf_t *gb = cbm_gbuf_new("p", "/tmp/p");
char *file_qn = cbm_pipeline_fqn_compute("p", "x/common.proto", "__file__");
ASSERT_GT(
cbm_gbuf_upsert_node(gb, "File", "common.proto", file_qn, "x/common.proto", 1, 1, "{}"), 0);
@@ -236,41 +244,129 @@ TEST(include_path_names_no_file_decides_no_target) {
memset(&ctx, 0, sizeof(ctx));
ctx.gbuf = gb;
ctx.project_name = "p";
ctx.include_universe = cbm_include_universe_new(root);
ctx.include_universe = ip_universe(files);
CBMImport imp;
memset(&imp, 0, sizeof(imp));
const cbm_gbuf_node_t *out = NULL;
imp.module_path = "google/protobuf/empty.proto"; /* no repository file ends with it */
imp.module_path = "google/protobuf/empty.proto"; /* no indexed file ends with it */
ASSERT_TRUE(cbm_include_path_decides(&ctx, "a.proto", CBM_LANG_PROTOBUF, &imp, &out));
ASSERT_NULL(out);
imp.module_path = "skipped/only.proto"; /* one file, without a node */
imp.module_path = "skipped/only.proto"; /* only in a directory the run did not index */
ASSERT_TRUE(cbm_include_path_decides(&ctx, "a.proto", CBM_LANG_PROTOBUF, &imp, &out));
ASSERT_NULL(out);
imp.module_path = "missing.mk";
ASSERT_TRUE(cbm_include_path_decides(&ctx, "Makefile", CBM_LANG_MAKEFILE, &imp, &out));
ASSERT_NULL(out);
/* partners: the named file's node; several files: main decides */
/* partners: the named file's node; several files: main decides; a guess:
* main decides */
imp.module_path = "x/common.proto";
ASSERT_TRUE(cbm_include_path_decides(&ctx, "a.proto", CBM_LANG_PROTOBUF, &imp, &out));
ASSERT_NOT_NULL(out);
ASSERT_STR_EQ(out->qualified_name, file_qn);
imp.module_path = "dup.proto";
ASSERT_FALSE(cbm_include_path_decides(&ctx, "a.proto", CBM_LANG_PROTOBUF, &imp, &out));
ASSERT_FALSE(cbm_include_path_decides(&ctx, "y/b.proto", CBM_LANG_PROTOBUF, &imp, &out));
cbm_include_universe_free(ctx.include_universe);
free(file_qn);
cbm_gbuf_free(gb);
th_rmtree(root);
free(root);
PASS();
}
/* ── IMPORTS edges of a real index ───────────────────────────────────────── */
/* How many IMPORTS edges go from the node whose QN ends in `.src` to the node
* whose QN ends in `.tgt`. */
static int ip_imports(const char *db, const char *src, const char *tgt) {
sqlite3 *h = NULL;
if (sqlite3_open_v2(db, &h, SQLITE_OPEN_READONLY, NULL) != SQLITE_OK) {
sqlite3_close(h);
return -1;
}
sqlite3_stmt *st = NULL;
int n = -1;
if (sqlite3_prepare_v2(h,
"SELECT count(*) FROM edges e JOIN nodes s ON s.id = e.source_id "
"JOIN nodes t ON t.id = e.target_id WHERE e.type = 'IMPORTS' "
"AND s.qualified_name LIKE '%.' || ?1 AND t.qualified_name LIKE '%.' || ?2",
-1, &st, NULL) == SQLITE_OK) {
sqlite3_bind_text(st, 1, src, -1, SQLITE_TRANSIENT);
sqlite3_bind_text(st, 2, tgt, -1, SQLITE_TRANSIENT);
if (sqlite3_step(st) == SQLITE_ROW) {
n = sqlite3_column_int(st, 0);
}
}
sqlite3_finalize(st);
sqlite3_close(h);
return n;
}
/* Red on main: an import that names no repository file got main's edge to a
* same-named symbol (here the Go module mvccpb/kv); it has none now, and the
* import of a real file keeps its edge. A copy under node_modules (not
* indexed) is not seen: alone it names no file, beside an indexed copy the
* indexed one is the only file. Several files and a guess keep main's edge. */
TEST(include_path_edges_name_only_indexed_files) {
char *tmp = th_mktempdir("cbm_ip_edges");
ASSERT_NOT_NULL(tmp);
char dir[512];
snprintf(dir, sizeof(dir), "%s/repo", tmp);
ASSERT_EQ(th_write_file(TH_PATH(dir, "api/rpc.proto"),
"syntax = \"proto3\";\npackage api;\nimport \"etcd/api/mvccpb/kv.proto\";\n"
"import \"kvs/real.proto\";\nimport \"only.proto\";\n"
"import \"common/t.proto\";\nmessage R { int32 a = 1; }\n"),
0);
ASSERT_EQ(th_write_file(TH_PATH(dir, "kvs/real.proto"),
"syntax = \"proto3\";\npackage kvs;\nmessage Real { int32 a = 1; }\n"),
0);
ASSERT_EQ(th_write_file(TH_PATH(dir, "common/t.proto"),
"syntax = \"proto3\";\npackage common;\nmessage T { int32 a = 1; }\n"),
0);
ASSERT_EQ(th_write_file(TH_PATH(dir, "mvccpb/kv.go"),
"package mvccpb\n\ntype KeyValue struct{}\n"),
0);
ASSERT_EQ(th_write_file(TH_PATH(dir, "node_modules/pkg/only.proto"),
"syntax = \"proto3\";\npackage only;\nmessage O { int32 a = 1; }\n"),
0);
ASSERT_EQ(th_write_file(TH_PATH(dir, "node_modules/pkg/common/t.proto"),
"syntax = \"proto3\";\npackage common;\nmessage T { int32 b = 1; }\n"),
0);
/* B1: two files end with common.proto; protoc -I . reads the root one, -I
* svc the other: main's edge (to the root file) stays, never svc/'s */
ASSERT_EQ(th_write_file(TH_PATH(dir, "status.proto"),
"syntax = \"proto3\";\nmessage Status { int32 root_field = 1; }\n"),
0);
ASSERT_EQ(th_write_file(TH_PATH(dir, "svc/status.proto"),
"syntax = \"proto3\";\nmessage Status { int32 svc_field = 1; }\n"),
0);
ASSERT_EQ(th_write_file(TH_PATH(dir, "svc/api.proto"),
"syntax = \"proto3\";\npackage api;\nimport \"status.proto\";\n"
"message Req { Status s = 1; }\n"),
0);
char db[512];
snprintf(db, sizeof(db), "%s/ip.db", tmp);
cbm_pipeline_t *p = cbm_pipeline_new(dir, db, CBM_MODE_FULL);
ASSERT_NOT_NULL(p);
ASSERT_EQ(cbm_pipeline_run(p), 0);
cbm_pipeline_free(p);
/* main drew api/rpc.proto -> mvccpb.kv for the etcd path */
ASSERT_EQ(ip_imports(db, "api.rpc.proto.__file__", "mvccpb.kv"), 0);
ASSERT_EQ(ip_imports(db, "api.rpc.proto.__file__", "kvs.real"), 1);
ASSERT_EQ(ip_imports(db, "api.rpc.proto.__file__", "common.t"), 1);
ASSERT_EQ(ip_imports(db, "api.rpc.proto.__file__", "pkg.only"), 0);
ASSERT_EQ(ip_imports(db, "api.rpc.proto.__file__", "pkg.common.t"), 0);
ASSERT_EQ(ip_imports(db, "svc.api.proto.__file__", "svc.status"), 0);
th_cleanup(tmp);
PASS();
}
SUITE(include_path) {
RUN_TEST(include_path_names_no_file_decides_no_target);
RUN_TEST(include_path_proto_tail_beside_and_ambiguity);
RUN_TEST(include_path_make_exactly_one_base);
RUN_TEST(include_path_listing_skips_git_and_links);
RUN_TEST(include_path_listing_digest_follows_names);
RUN_TEST(include_path_listing_grows_linearly);
RUN_TEST(include_path_proto_tail_guess_and_ambiguity);
RUN_TEST(include_path_make_root_decides_below_is_guess);
RUN_TEST(include_path_universe_is_the_indexed_files);
RUN_TEST(include_path_inventory_digest_follows_indexed_names);
RUN_TEST(include_path_universe_grows_linearly);
RUN_TEST(include_path_edges_name_only_indexed_files);
}