Skip to content

Objective-C .h files fail to parse unless a .m file preloads the grammar #1628

Description

@Juddd

Summary

An Objective-C header can fail with Failed to get parser for language: objc when a project contains no .m/.mm file.

CodeGraph initially classifies .h files as C and preloads the C and C++ grammars. After the worker pool has been initialized, content-aware detection correctly reclassifies the header as Objective-C, but the worker has no Objective-C grammar loaded.

This was originally observed in OpenCV's opencv2/videoio/cap_ios.h, but the reproducer below has no dependencies or imports.

Environment

  • CodeGraph 1.6.0
  • Linux 5.15 x86_64

Minimal reproduction

Create an otherwise empty directory containing exactly this file:

repro.h

@interface CGRepro
- (void)run;
@end

Run:

$ codegraph init --yes

Actual result:

Indexing failed — all 1 files had errors

1 parser initialization failures

.codegraph/errors.log:

1 files with errors

repro.h: Failed to get parser for language: objc

codegraph status --json reports fileCount: 1, nodeCount: 0, languages: ["objc"], and state: "failed".

The same failure reproduces with a subsequent codegraph index.

Expected result

repro.h should be parsed as Objective-C and its class/method should be indexed, without requiring a sibling .m or .mm file.

Control experiment

Adding this second file causes both files to index successfully:

grammar_seed.m

void codegraph_objc_grammar_seed(void) {}

With that file present, codegraph index reports:

Indexed 2 files
4 nodes, 2 edges

Removing grammar_seed.m and rebuilding restores the parser initialization failure.

Diagnosis

The failure is deterministic:

path-only detection:       repro.h -> c
preloaded grammars:        c, cpp
content-aware detection:  repro.h -> objc
getParser("objc"):        null

Loading the Objective-C grammar explicitly changes getParser("objc") from null to a valid parser, so the shipped tree-sitter-objc.wasm is present and loadable.

The preload logic currently derives neededLanguages without file contents and adds cpp when c is present, but not objc:

https://github.com/colbymchenry/codegraph/blob/main/src/extraction/index.ts#L1665-L1669

Later, parsing performs content-aware detection and sends the resulting objc language to the already initialized worker.

Possible fixes

  • When .h/C is present, preload both cpp and objc; or
  • lazily load the final content-detected grammar in the worker.

Either approach should preserve the current content-aware .h detection while ensuring the selected parser is available.

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions