Skip to content

[BUG]: --code-coverage crashes with "Cannot read properties of null (reading 'Id')" when an ApexCodeCoverageAggregate row's class/trigger was deleted #3695

Backlog

Description

@tom-gangemi

Before You Submit

  • I'm using the latest version of Salesforce CLI
  • I've searched the existing issues
  • I've run sf doctor to diagnose common issues

Related Issue

#3389 (same message, different cause: that one is a null ApexClass on ApexTestResult in asyncTests.js; this one is in codeCoverage.js)

Command

sf apex run test --code-coverage and sf apex get test --code-coverage

Org Type

Sandbox

Did this work before?

Not sure

Last Working Version

No response

Summary

With Store Only Aggregate Code Coverage turned on, any --code-coverage run fails with TypeError: Cannot read properties of null (reading 'Id') if the org has an ApexCodeCoverageAggregate row whose class or trigger no longer exists. All tests pass; the crash happens while the CLI builds the coverage summary, so no results are reported or written.

Repository to Reproduce

No response. It depends on org data, not on project source.

Steps to reproduce

  1. In a sandbox, turn on Store Only Aggregate Code Coverage (Setup → Apex Test Execution → Options).
  2. Have an ApexCodeCoverageAggregate row whose ApexClassOrTrigger is null. Ours points at a deleted trigger (01q prefix); it came over with the sandbox copy. Check with:
    sf data query --use-tooling-api -q "SELECT Id, ApexClassOrTriggerId FROM ApexCodeCoverageAggregate WHERE ApexClassOrTrigger.Name = null"
    
    This returns 1 row out of about 1,540. The row can't be deleted: a Tooling API delete returns INSUFFICIENT_ACCESS_OR_READONLY for a user with Modify All Data and Author Apex.
  3. Run tests with coverage. Ours:
    sf apex run test --test-level RunSpecifiedTests --class-names <~350 classes> --code-coverage --detailed-coverage --result-format junit --output-dir test-results/apex --wait 180
    
  4. Fetching the finished run gives the same error:
    sf apex get test -i <testRunId> --code-coverage --json
    
    The same command without --code-coverage succeeds: Passed, 2884 run, 2884 passing, 0 failing.

Expected Result

Aggregate rows with a null ApexClassOrTrigger (class or trigger no longer exists) are skipped, and the command reports results and writes the output files.

Actual Result

The command exits with code 10 and TypeError: Cannot read properties of null (reading 'Id'). With --output-dir, no result files are written (no JUnit), so CI sees no test results even though every test passed.

Error Message

TypeError: Cannot read properties of null (reading 'Id')
    at <sf>/node_modules/@salesforce/apex-node/lib/src/tests/codeCoverage.js:137:57
    at Array.map (<anonymous>)
    at <sf>/node_modules/@salesforce/apex-node/lib/src/tests/codeCoverage.js:131:59
    at Array.forEach (<anonymous>)
    at CodeCoverage.getAggregateCodeCoverage (<sf>/node_modules/@salesforce/apex-node/lib/src/tests/codeCoverage.js:130:36)
    at process.processTicksAndRejections (node:internal/process/task_queues:104:5)
    at async calculateCodeCoverage (<sf>/node_modules/@salesforce/apex-node/lib/src/tests/utils.js:170:67)
    at async AsyncTests.formatAsyncResults (<sf>/node_modules/@salesforce/apex-node/lib/src/tests/asyncTests.js:350:13)
    at async AsyncTests.reportAsyncResults (<sf>/node_modules/@salesforce/apex-node/lib/src/tests/asyncTests.js:235:38)
    at async TestService.reportAsyncResults (<sf>/node_modules/@salesforce/apex-node/lib/src/tests/testService.js:287:20)

The stack is from sf apex get test on 2.157.8; sf apex run test on 2.159.7 (system information below) fails with the same message.

System Information

{
  "architecture": "linux-x64",
  "cliVersion": "@salesforce/cli/2.159.7",
  "nodeVersion": "node-v24.22.3",
  "osVersion": "Linux 4.18.0-553.182.1.el8_10.x86_64",
  "rootPath": "/usr/lib/node_modules/@salesforce/cli",
  "shell": "bash",
  "pluginVersions": [
    "@oclif/plugin-autocomplete 4.0.6 (core)",
    "@oclif/plugin-commands 5.0.12 (core)",
    "@oclif/plugin-help 7.0.7 (core)",
    "@oclif/plugin-not-found 4.0.7 (core)",
    "@oclif/plugin-plugins 7.0.16 (core)",
    "@oclif/plugin-search 2.0.12 (core)",
    "@oclif/plugin-update 5.0.10 (core)",
    "@oclif/plugin-version 3.0.10 (core)",
    "@oclif/plugin-warn-if-update-available 4.0.5 (core)",
    "@oclif/plugin-which 4.0.10 (core)",
    "@salesforce/cli 2.159.7 (core)",
    "agent 2.3.0 (core)",
    "apex 4.4.7 (core)",
    "api 2.0.29 (core)",
    "auth 6.0.5 (core)",
    "data 5.1.28 (core)",
    "deploy-retrieve 4.3.3 (user) published 16 days ago (Mon Nov 09 2026)",
    "info 4.0.26 (core)",
    "limits 4.0.6 (core)",
    "marketplace 2.0.28 (core)",
    "org 7.0.15 (core)",
    "packaging 3.0.17 (user) published 30 days ago (Mon Oct 26 2026)",
    "schema 4.0.17 (core)",
    "settings 3.0.25 (core)",
    "sobject 2.0.26 (core)",
    "telemetry 4.2.4 (core)",
    "templates 57.6.1 (core)",
    "trust 4.1.4 (core)",
    "user 6.0.13 (core)",
    "sfdmu 5.8.0 (user) published 145 days ago (Fri Jul 03 2026)",
    "sfdx-browserforce-plugin 7.2.2 (user) published 7 days ago (Wed Nov 18 2026)",
    "sfdx-git-delta 6.45.1 (user) published 153 days ago (Fri Jun 26 2026)",
    "sfdx-hardis 8.27.0 (user) published 3 days ago (Mon Nov 23 2026)"
  ]
}

Additional Information

Cause. getAggregateCodeCoverage maps every aggregate record and reads item.ApexClassOrTrigger.Id without a null check (codeCoverage.ts L137-L152). With Store Only Aggregate Code Coverage on, ApexCodeCoverage is empty, so the covered-class ID set is empty, and queryAggregateCodeCov queries the whole ApexCodeCoverageAggregate table (L191). One orphan row anywhere in the org then fails every coverage run. The unfiltered query came in with forcedotcom/salesforcedx-apex#370; before that, an empty set returned no aggregate rows.

Suggested fix. Skip null rows before mapping in getAggregateCodeCoverage, and add the same guard in getPerClassCodeCoverage:

chunk.records
  .filter((item) => item.ApexClassOrTrigger)
  .map((item) => { /* ... */ })

Workaround. Run without --code-coverage (confirmed). Turning Store Only Aggregate Code Coverage off should also avoid the unfiltered query (not tested).

Activity

  1. added
    bug
    on Nov 30, 2026
  2. git2gus commented on Nov 30, 2026

    @git2gus

    This issue has been linked to a new work item: W-25077966

  3. jfeingold35 commented on Nov 30, 2026

    @jfeingold35

    @tom-gangemi , thanks for logging this. We've created a work item, and we'll provide updates as they become available.

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

    bug

    Type

    No type

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions