Skip to content

apex import rule: re.IGNORECASE neutralises the [A-Z] guard, so every qualified reference (foo.bar) counts as an import #2671

Closed

Description

@squid-protocol

Found by the Batch A regen in keyword-rosetta (gitgalaxy#2669 A.5), while attributing a new red cell: apex import read 6 against a cross-language median of 3.

The defect

LANGUAGE_DEFINITIONS["apex"]["rules"]["import"] is:

\bType\.forName\b|(?!(?:System|Database|Schema|Auth|Cache|Chatter|EventBus|Limits|Messaging|RestContext|Test)\b)\b[a-zA-Z_]\w*\.[A-Z]\w*\b

compiled with re.IGNORECASE. The second alternative is clearly meant to catch a type reference — lowercase receiver, capitalised member — but re.IGNORECASE applies to the whole pattern, so [A-Z] matches any letter and the alternative degrades to "any word.word". Every method call in an Apex file is therefore counted as an import.

Micro-repro (live registry, engine 3ac9182):

foo.bar          -> ["foo.bar"]      # should not match
conn.clear       -> ["conn.clear"]   # should not match
Logger.info      -> ["Logger.info"]  # should not match
System.debug     -> []               # correctly excluded by the lookahead
Database.query   -> []               # correctly excluded by the lookahead

Recompiling the identical pattern without re.IGNORECASE returns [] for all three of the first group, which isolates the flag as the whole cause.

Scanned every language: apex is the only rule that combines re.IGNORECASE with an [A-Z] guard, so the blast radius is apex alone.

Why it surfaced now

The keyword-rosetta corpus expresses the SPEC dependency chain main->a->b->c as Type.forName(...), planted in #2669 A.3 (apex previously had no chain at all). That is +1 legitimate import per planting file. The corpus also contains ordinary Apex code — Logger.info, Log.error in b.cls, conn.clear in c.cls — and those three incidental method calls are counted as imports too:

file matches
main.cls Type.forName
a.cls Type.forName
b.cls Type.forName, Logger.info, Log.error
c.cls conn.clear

= 6 against a planted intent of 3. Before A.3 the count coincidentally read 3 (the three incidental hits with no chain), which is why nothing caught it earlier — the number was right for the wrong reason.

Fix shape

Keep re.IGNORECASE — Apex is a case-insensitive language and Type.forName genuinely needs it — but scope the capital-letter guard, e.g. (?-i:[A-Z]) on the member segment, or split the alternation so only the Type.forName arm is case-insensitive. Strict tests should cover:

  • positive: Type.forName(...), TYPE.FORNAME(...), a real type reference Account.SObjectType
  • negative: foo.bar, conn.clear, Logger.info, System.debug, Database.query

Same family as #2544 (swift open counted from prose) — a rule whose guard does not actually guard.

Corpus side

Not worked around: Logger.info/Log.error/conn.clear are ordinary Apex and deleting them would hide the defect. The red cell is ledgered in keyword-rosetta as apex-import-ignorecase-type-guard so the Batch A bless has an explanation, and the manifest keeps the observed 6. Once this lands, apex re-blesses to import 3 (the chain alone).

Collected by #2669 B.1/B.3.

Activity

  1. added
    bug
    metrics
    core-engine
    on Sep 2, 2026
  2. squid-protocol commented on Sep 2, 2026

    @squid-protocol
    OwnerAuthor

    Not the same rule as #2672 (that is the doc marker/tag double-count family), but the same one-regex/one-strict-test shape and the same apex definition file — #2672 recommends B.3 carry both in one PR.

  3. squid-protocol commented on Sep 3, 2026

    @squid-protocol
    OwnerAuthor

    Closing: verified fixed on main. The apex import rule's type guard holds again — account.name and myvar.field are no longer counted as imports, while Type.forName(x) still is. Landed in #2681 (Batch B.3 wave 2 of #2669). Worth noting for anyone re-checking: the rule still compiles with re.IGNORECASE, so a flag check alone reads as unfixed; the guard was restructured instead, and only a behavioural test shows it.

  4. added 2 commits that reference this issue on Sep 6, 2026
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Labels

    bugcore-enginemetrics

    Type

    No type

    Projects

    No projects

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions