1212 */
1313#include "pipeline/pipeline.h"
1414#include "pipeline/pipeline_internal.h"
15+ #include "discover/discover.h"
1516#include "foundation/compat.h"
1617#include "foundation/constants.h"
1718#include "foundation/hash_table.h"
2223
2324#include <yyjson/yyjson.h>
2425
26+ #include <dirent.h>
2527#include <stdbool.h>
2628#include <stdio.h>
2729#include <stdlib.h>
2830#include <string.h>
31+ #include <sys/stat.h>
2932
3033/* Read an entire file into a malloc'd buffer. Returns NULL on failure. */
3134static char * pkgmap_read_file (const char * path , int * out_len ) {
@@ -747,6 +750,100 @@ CBMHashTable *cbm_pkgmap_build(cbm_pkg_entries_t *worker_entries, int worker_cou
747750 return map ;
748751}
749752
753+ /* Returns true if basename is a package manifest we know how to parse.
754+ * Used by the filesystem walker; cbm_pkgmap_try_parse is the source of
755+ * truth for which basenames produce entries. */
756+ static bool is_pkgmap_manifest_basename (const char * basename ) {
757+ if (!basename ) {
758+ return false;
759+ }
760+ if (strcmp (basename , "package.json" ) == 0 || strcmp (basename , "go.mod" ) == 0 ||
761+ strcmp (basename , "Cargo.toml" ) == 0 || strcmp (basename , "pyproject.toml" ) == 0 ||
762+ strcmp (basename , "composer.json" ) == 0 || strcmp (basename , "pubspec.yaml" ) == 0 ||
763+ strcmp (basename , "pom.xml" ) == 0 || strcmp (basename , "build.gradle" ) == 0 ||
764+ strcmp (basename , "build.gradle.kts" ) == 0 || strcmp (basename , "mix.exs" ) == 0 ) {
765+ return true;
766+ }
767+ return ends_with (basename , ".gemspec" );
768+ }
769+
770+ /* Recursive filesystem walker that finds and parses package manifest
771+ * files independently of the main discovery filter. The main discovery
772+ * filter intentionally hides package.json / composer.json etc. from
773+ * code indexing (they're config, not source), but pass_pkgmap still
774+ * needs to read them to resolve workspace imports. Skips directories
775+ * matched by the shared cbm_should_skip_dir helper so we don't walk
776+ * node_modules, .git, build, etc. Returns the number of manifests
777+ * parsed, accumulated across the whole walk. */
778+ static int pkgmap_walk_dir (const char * abs_dir , const char * rel_dir ,
779+ cbm_pkg_entries_t * entries ) {
780+ DIR * dir = opendir (abs_dir );
781+ if (!dir ) {
782+ return 0 ;
783+ }
784+ int parsed = 0 ;
785+ struct dirent * entry ;
786+ while ((entry = readdir (dir )) != NULL ) {
787+ const char * name = entry -> d_name ;
788+ if (name [0 ] == '.' && (name [1 ] == '\0' || (name [1 ] == '.' && name [2 ] == '\0' ))) {
789+ continue ;
790+ }
791+ char abs_path [PKGMAP_PATH_BUF ];
792+ char rel_path [PKGMAP_PATH_BUF ];
793+ snprintf (abs_path , sizeof (abs_path ), "%s/%s" , abs_dir , name );
794+ if (rel_dir && rel_dir [0 ]) {
795+ snprintf (rel_path , sizeof (rel_path ), "%s/%s" , rel_dir , name );
796+ } else {
797+ snprintf (rel_path , sizeof (rel_path ), "%s" , name );
798+ }
799+ struct stat st ;
800+ if (lstat (abs_path , & st ) != 0 ) {
801+ continue ;
802+ }
803+ if (S_ISLNK (st .st_mode )) {
804+ continue ;
805+ }
806+ if (S_ISDIR (st .st_mode )) {
807+ if (cbm_should_skip_dir (name , CBM_MODE_FULL )) {
808+ continue ;
809+ }
810+ parsed += pkgmap_walk_dir (abs_path , rel_path , entries );
811+ continue ;
812+ }
813+ if (!S_ISREG (st .st_mode )) {
814+ continue ;
815+ }
816+ if (!is_pkgmap_manifest_basename (name )) {
817+ continue ;
818+ }
819+ int source_len = 0 ;
820+ char * source = pkgmap_read_file (abs_path , & source_len );
821+ if (!source ) {
822+ continue ;
823+ }
824+ if (cbm_pkgmap_try_parse (name , rel_path , source , source_len , entries )) {
825+ parsed ++ ;
826+ }
827+ free (source );
828+ }
829+ closedir (dir );
830+ return parsed ;
831+ }
832+
833+ /* Scan a repository for package manifest files via the filesystem
834+ * walker above. Always-available companion to the parallel path's
835+ * per-worker manifest parsing, which is bound to whatever `files[]`
836+ * the discoverer produces and therefore misses ignored manifests like
837+ * package.json. NULL-safe; returns 0 entries when repo_path is unset. */
838+ int cbm_pkgmap_scan_repo (const char * repo_path , cbm_pkg_entries_t * entries ) {
839+ if (!repo_path || !entries ) {
840+ return 0 ;
841+ }
842+ int parsed = pkgmap_walk_dir (repo_path , "" , entries );
843+ cbm_log_info ("pkgmap.scan_repo" , "manifests" , pkgmap_itoa (parsed ));
844+ return parsed ;
845+ }
846+
750847/* Build pkgmap for sequential path (reads manifest files directly) */
751848CBMHashTable * cbm_pkgmap_build_from_files (const cbm_file_info_t * files , int file_count ,
752849 const char * project_name ) {
@@ -755,15 +852,7 @@ CBMHashTable *cbm_pkgmap_build_from_files(const cbm_file_info_t *files, int file
755852
756853 for (int i = 0 ; i < file_count ; i ++ ) {
757854 const char * basename = path_basename (files [i ].rel_path );
758- /* Quick check: is this a manifest file? */
759- bool is_manifest =
760- (strcmp (basename , "package.json" ) == 0 || strcmp (basename , "go.mod" ) == 0 ||
761- strcmp (basename , "Cargo.toml" ) == 0 || strcmp (basename , "pyproject.toml" ) == 0 ||
762- strcmp (basename , "composer.json" ) == 0 || strcmp (basename , "pubspec.yaml" ) == 0 ||
763- strcmp (basename , "pom.xml" ) == 0 || strcmp (basename , "build.gradle" ) == 0 ||
764- strcmp (basename , "build.gradle.kts" ) == 0 || strcmp (basename , "mix.exs" ) == 0 ||
765- ends_with (basename , ".gemspec" ));
766- if (!is_manifest ) {
855+ if (!is_pkgmap_manifest_basename (basename )) {
767856 continue ;
768857 }
769858
@@ -782,6 +871,44 @@ CBMHashTable *cbm_pkgmap_build_from_files(const cbm_file_info_t *files, int file
782871 return map ;
783872}
784873
874+ /* Variant of cbm_pkgmap_build_from_files that ALSO walks the repo
875+ * filesystem to pick up manifests filtered out by the main discoverer
876+ * (the canonical case: package.json, which is in IGNORED_JSON_FILES).
877+ * Falls back to the files[]-only behaviour if repo_path is NULL. */
878+ CBMHashTable * cbm_pkgmap_build_from_repo (const char * repo_path , const cbm_file_info_t * files ,
879+ int file_count , const char * project_name ) {
880+ cbm_pkg_entries_t entries ;
881+ cbm_pkg_entries_init (& entries );
882+
883+ /* Manifests already visible through discovery (Cargo.toml, go.mod,
884+ * pyproject.toml, ...). package.json typically isn't, but we still
885+ * harvest whatever the discovery filter exposed in case downstream
886+ * filters change. */
887+ int from_files = 0 ;
888+ for (int i = 0 ; i < file_count ; i ++ ) {
889+ const char * basename = path_basename (files [i ].rel_path );
890+ if (!is_pkgmap_manifest_basename (basename )) {
891+ continue ;
892+ }
893+ from_files ++ ;
894+ int source_len = 0 ;
895+ char * source = pkgmap_read_file (files [i ].path , & source_len );
896+ if (!source ) {
897+ continue ;
898+ }
899+ cbm_pkgmap_try_parse (basename , files [i ].rel_path , source , source_len , & entries );
900+ free (source );
901+ }
902+
903+ int from_walk = cbm_pkgmap_scan_repo (repo_path , & entries );
904+ cbm_log_info ("pkgmap.scan" , "manifests_from_files" , pkgmap_itoa (from_files ),
905+ "manifests_from_walk" , pkgmap_itoa (from_walk ),
906+ "entries" , pkgmap_itoa (entries .count ));
907+ CBMHashTable * map = cbm_pkgmap_build (& entries , SKIP_ONE , project_name );
908+ cbm_pkg_entries_free (& entries );
909+ return map ;
910+ }
911+
785912static void pkgmap_free_entry (const char * key , void * value , void * userdata ) {
786913 (void )userdata ;
787914 free ((void * )key );
0 commit comments