Skip to content

Commit 93d362d

Browse files
committed
feat(pkgmap): resolve Package.swift manifests for cross-package IMPORTS
Add parse_package_swift to pass_pkgmap.c, a hand-rolled literal pattern-extractor (same tier as parse_cargo_toml/parse_package_json) that maps SwiftPM target/product names to their conventional Sources/<name> directory, letting a bare `import Foo` resolve across package boundaries exactly like package.json/Cargo.toml already do. Extracts, via literal-only matching: - .target(name: "Foo", ...) -> "Foo" -> Sources/Foo - .library(name: "Foo", targets: ["Bar"]) -> "Foo" -> Sources/Bar .package(url:)/.package(path:) dependency declarations and target-to-target dependency references are deliberately NOT used to mint entries, mirroring how package.json/Cargo.toml only self-register a manifest's own identity -- a dependency resolves because the providing package's own Package.swift registers itself when the repo-wide manifest walk reaches it. Because Package.swift is executable Swift, any name that is not a bare string literal (a variable, a concatenation) is skipped rather than guessed, so no edge is minted from an expression that couldn't be confidently evaluated. Adds "Package.swift" to cbm_pkgmap_try_parse's dispatch table and is_pkgmap_manifest_basename. Tests: direct parser-level cases for local path dependencies, remote package identities, products, targets, target-name dependencies, and two fail-closed ambiguous-name cases, plus an end-to-end pipeline test (two SwiftPM packages under one root) proving a real cross-package IMPORTS edge, mirroring repro_issue408.c's JS-workspace proof. Part of DeusData#551 (item 1); item 2 (Swift semantic tier for cross-package CALLS) is out of scope here per the maintainer's authorization. Signed-off-by: gsdali <51393997+gsdali@users.noreply.github.com>
1 parent dc7178c commit 93d362d

2 files changed

Lines changed: 475 additions & 2 deletions

File tree

‎src/pipeline/pass_pkgmap.c‎

Lines changed: 221 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@
33
*
44
* Scans discovered files for manifest files (package.json, go.mod, Cargo.toml,
55
* pyproject.toml, composer.json, pubspec.yaml, pom.xml, build.gradle, mix.exs,
6-
* *.gemspec) and builds a hash table mapping bare package specifiers to resolved
6+
* *.gemspec, Package.swift) and builds a hash table mapping bare package specifiers to resolved
77
* module QNs. This enables IMPORTS edges for non-relative imports like
88
* "@myorg/pkg", "github.com/foo/bar", "use my_crate::foo".
99
*
@@ -665,6 +665,220 @@ static void parse_gemspec(const char *source, int source_len, const char *rel_pa
665665
}
666666
}
667667

668+
/* Swift: Package.swift — SwiftPM manifest.
669+
*
670+
* Package.swift is executable Swift, not declarative, so this is a
671+
* hand-rolled literal pattern-extractor (same spirit as parse_cargo_toml),
672+
* not a Swift evaluator: it locates `.target(...)` / `.library(...)` call
673+
* expressions by literal prefix and pulls only their quoted-string-literal
674+
* `name:` / `targets:` arguments. Anything computed or concatenated is
675+
* skipped, not guessed — fail-closed, like every other parser here.
676+
*
677+
* `.package(url:)` / `.package(path:)` dependencies, and target-to-target
678+
* dependency references, are NOT used to mint entries: mirroring
679+
* package.json/Cargo.toml, only a manifest's OWN products/targets are
680+
* self-registered — a dependency resolves because the DEPENDENCY's own
681+
* Package.swift registers itself when the repo-wide manifest walk
682+
* (cbm_pkgmap_scan_repo) reaches it, exactly like a JS workspace sibling's
683+
* package.json (see repro_issue408.c). */
684+
685+
/* Find the matching ')' for the '(' at `open`, scanning forward to `end`.
686+
* Tracks string-literal spans (with backslash-escape handling) so a ')' or
687+
* '(' inside a quoted argument value never perturbs the depth count.
688+
* Returns NULL for an unbalanced/unterminated call — the caller treats
689+
* that span as unparseable and skips it (fail-closed). */
690+
static const char *swift_match_paren(const char *open, const char *end) {
691+
int depth = 0;
692+
bool in_str = false;
693+
for (const char *p = open; p < end; p++) {
694+
if (in_str) {
695+
if (*p == '\\' && p + SKIP_ONE < end) {
696+
p++;
697+
continue;
698+
}
699+
if (*p == '"') {
700+
in_str = false;
701+
}
702+
continue;
703+
}
704+
if (*p == '"') {
705+
in_str = true;
706+
} else if (*p == '(') {
707+
depth++;
708+
} else if (*p == ')') {
709+
depth--;
710+
if (depth == 0) {
711+
return p;
712+
}
713+
}
714+
}
715+
return NULL;
716+
}
717+
718+
/* Extract a bare double-quoted string-literal value at `p` (after skipping
719+
* leading whitespace), requiring the closing quote be immediately followed
720+
* by a comma or `end` — rejects `name: "Foo" + suffix`-style concatenation,
721+
* which a plain quote-scan alone cannot tell apart from a true literal.
722+
* Returns heap string, or NULL (fail-closed). */
723+
static char *swift_quoted_literal(const char *p, const char *end) {
724+
while (p < end && (*p == ' ' || *p == '\t')) {
725+
p++;
726+
}
727+
if (p >= end || *p != '"') {
728+
return NULL;
729+
}
730+
p++;
731+
const char *start = p;
732+
while (p < end && *p != '"' && *p != '\n') {
733+
p++;
734+
}
735+
if (p >= end || *p != '"') {
736+
return NULL;
737+
}
738+
char *value = cbm_strndup(start, (size_t)(p - start));
739+
p++; /* past closing quote */
740+
while (p < end && (*p == ' ' || *p == '\t')) {
741+
p++;
742+
}
743+
if (p < end && (*p == ',' || *p == ')')) {
744+
return value;
745+
}
746+
free(value);
747+
return NULL;
748+
}
749+
750+
/* Search [start, end) for `needle`, then extract the bare quoted-literal
751+
* argument immediately following it via swift_quoted_literal. Bounded to
752+
* `end` so a match can never leak past the enclosing call's own argument
753+
* list into a later, unrelated declaration. Returns heap string or NULL. */
754+
static char *swift_extract_after(const char *start, const char *end, const char *needle) {
755+
size_t nlen = strlen(needle);
756+
if (nlen == 0 || start + nlen > end) {
757+
return NULL;
758+
}
759+
for (const char *p = start; p + nlen <= end; p++) {
760+
if (memcmp(p, needle, nlen) == 0) {
761+
return swift_quoted_literal(p + nlen, end);
762+
}
763+
}
764+
return NULL;
765+
}
766+
767+
/* Search [start, end) for `needle: [...]` and extract the FIRST array
768+
* element as a string literal (representative target of a product's
769+
* `targets:` list, mirroring import_candidate_symbol's first-member
770+
* convention elsewhere in this file). NULL (fail-closed) when the array is
771+
* absent, empty, or its first element is not a bare literal. */
772+
static char *swift_first_array_literal(const char *start, const char *end, const char *needle) {
773+
size_t nlen = strlen(needle);
774+
if (nlen == 0 || start + nlen > end) {
775+
return NULL;
776+
}
777+
for (const char *p = start; p + nlen <= end; p++) {
778+
if (memcmp(p, needle, nlen) != 0) {
779+
continue;
780+
}
781+
const char *q = p + nlen;
782+
while (q < end && (*q == ' ' || *q == '\t')) {
783+
q++;
784+
}
785+
if (q >= end || *q != '[') {
786+
return NULL;
787+
}
788+
q++; /* past '[' */
789+
while (q < end && (*q == ' ' || *q == '\t' || *q == '\n')) {
790+
q++;
791+
}
792+
if (q >= end || *q != '"') {
793+
return NULL; /* first element isn't a bare literal */
794+
}
795+
return extract_quoted(q, end);
796+
}
797+
return NULL;
798+
}
799+
800+
/* Register `entry_name` → the conventional `Sources/<target_name>` dir
801+
* (fixed-convention, same approach parse_cargo_toml takes for `src/lib`).
802+
* Both arguments are borrowed; caller retains ownership. */
803+
static void swift_register_target(const char *rel_path, const char *entry_name,
804+
const char *target_name, cbm_pkg_entries_t *entries) {
805+
if (!entry_name || !entry_name[0] || !target_name || !target_name[0]) {
806+
return;
807+
}
808+
char suffix[PKGMAP_PATH_BUF];
809+
snprintf(suffix, sizeof(suffix), "Sources/%s", target_name);
810+
char *entry = build_entry_path(rel_path, suffix);
811+
if (entry) {
812+
pkg_entries_push(entries, strdup(entry_name), entry);
813+
}
814+
}
815+
816+
/* Scan for `.target(name: "Foo", ...)` declarations. Non-overlapping: the
817+
* scan resumes AFTER each matched close paren, so a
818+
* `dependencies: [.target(name: "Bar")]` reference nested inside Foo's own
819+
* argument list is never re-visited as a second top-level target. */
820+
static void swift_scan_targets(const char *source, const char *end, const char *rel_path,
821+
cbm_pkg_entries_t *entries) {
822+
static const char prefix[] = ".target(";
823+
const char *cursor = source;
824+
while (cursor < end) {
825+
const char *hit = strstr(cursor, prefix);
826+
if (!hit || hit >= end) {
827+
return;
828+
}
829+
const char *open = hit + strlen(prefix) - SKIP_ONE;
830+
const char *close = swift_match_paren(open, end);
831+
if (!close) {
832+
return; /* unbalanced — nothing further here is trustworthy */
833+
}
834+
char *name = swift_extract_after(open, close, "name:");
835+
if (name) {
836+
swift_register_target(rel_path, name, name, entries);
837+
free(name);
838+
}
839+
cursor = close + SKIP_ONE;
840+
}
841+
}
842+
843+
/* Scan for `.library(name: "Foo", targets: ["Bar", ...])` product
844+
* declarations. Registers the PRODUCT name against the first listed
845+
* target's conventional source directory; a product with a missing,
846+
* non-literal, or empty `targets:` array is skipped, not guessed. */
847+
static void swift_scan_products(const char *source, const char *end, const char *rel_path,
848+
cbm_pkg_entries_t *entries) {
849+
static const char prefix[] = ".library(";
850+
const char *cursor = source;
851+
while (cursor < end) {
852+
const char *hit = strstr(cursor, prefix);
853+
if (!hit || hit >= end) {
854+
return;
855+
}
856+
const char *open = hit + strlen(prefix) - SKIP_ONE;
857+
const char *close = swift_match_paren(open, end);
858+
if (!close) {
859+
return;
860+
}
861+
char *product_name = swift_extract_after(open, close, "name:");
862+
if (product_name) {
863+
char *target_name = swift_first_array_literal(open, close, "targets:");
864+
if (target_name) {
865+
swift_register_target(rel_path, product_name, target_name, entries);
866+
free(target_name);
867+
}
868+
free(product_name);
869+
}
870+
cursor = close + SKIP_ONE;
871+
}
872+
}
873+
874+
/* SwiftPM: Package.swift — library products + targets → Sources/<name> */
875+
static void parse_package_swift(const char *source, int source_len, const char *rel_path,
876+
cbm_pkg_entries_t *entries) {
877+
const char *end = source + source_len;
878+
swift_scan_targets(source, end, rel_path, entries);
879+
swift_scan_products(source, end, rel_path, entries);
880+
}
881+
668882
/* ── Public: manifest detection + parsing ──────────────────────── */
669883

670884
bool cbm_pkgmap_try_parse(const char *basename, const char *rel_path, const char *source,
@@ -713,6 +927,10 @@ bool cbm_pkgmap_try_parse(const char *basename, const char *rel_path, const char
713927
parse_gemspec(source, source_len, rel_path, entries);
714928
return true;
715929
}
930+
if (strcmp(basename, "Package.swift") == 0) {
931+
parse_package_swift(source, source_len, rel_path, entries);
932+
return true;
933+
}
716934
return false;
717935
}
718936

@@ -773,7 +991,8 @@ static bool is_pkgmap_manifest_basename(const char *basename) {
773991
strcmp(basename, "Cargo.toml") == 0 || strcmp(basename, "pyproject.toml") == 0 ||
774992
strcmp(basename, "composer.json") == 0 || strcmp(basename, "pubspec.yaml") == 0 ||
775993
strcmp(basename, "pom.xml") == 0 || strcmp(basename, "build.gradle") == 0 ||
776-
strcmp(basename, "build.gradle.kts") == 0 || strcmp(basename, "mix.exs") == 0) {
994+
strcmp(basename, "build.gradle.kts") == 0 || strcmp(basename, "mix.exs") == 0 ||
995+
strcmp(basename, "Package.swift") == 0) {
777996
return true;
778997
}
779998
return ends_with(basename, ".gemspec");

0 commit comments

Comments
 (0)