Sub topic 6
Companion -meta.xml Files
This chapter explains what the -meta.xml suffix means, which components need a companion metadata file, and how to find and fix a missing or broken one. It assumes you can read XML and have seen the two layouts from earlier chapters: Metadata API format and source format. It also assumes you know what an Apex class and a custom object are.
Two kinds of files per component
Some components are pure configuration. A custom field is an example. Its entire definition is XML: label, type, length. Other components carry content. An Apex class is a body of code. A static resource is a binary or zip file. A Visualforce page is markup.
The Metadata API cannot store both kinds of information in one file for content components. The code must stay as plain code so editors, compilers, and diff tools can read it. So Salesforce adds a second file next to the content file. This companion holds the settings that describe the content, such as the API version and status.
The naming rule is mechanical:
<content file name> + "-meta.xml"| Content file | Companion file |
|---|---|
| `MyClass.cls` | `MyClass.cls-meta.xml` |
| `MyTrigger.trigger` | `MyTrigger.trigger-meta.xml` |
| `Dashboard.page` | `Dashboard.page-meta.xml` |
| `logo.resource` | `logo.resource-meta.xml` |
| `Welcome.email` | `Welcome.email-meta.xml` |
The companion keeps the full content file name, including its extension. A common mistake is to name it MyClass-meta.xml, which the tooling does not recognize.
The same suffix means something different for configuration components
For pure configuration components, the suffix depends on the format. This is the main source of confusion.
- In Metadata API format, a custom object is
objects/Account.object. The file is the component. There is no companion. - In source format, the same component is
objects/Account/Account.object-meta.xml. The-meta.xmlsuffix is added to every metadata file, because the whole file is "metadata about the component", not a content body.
So -meta.xml has two roles:
- In both formats, it marks a companion that describes a content file (
MyClass.cls-meta.xml). - In source format only, it marks a standalone component file (
Rating_Notes__c.field-meta.xml).
The earlier layout comparison showed both roles:
Source format
force-app/main/default/
classes/
MyClass.cls <- content
MyClass.cls-meta.xml <- companion (role 1)
objects/
Account/
Account.object-meta.xml <- standalone component (role 2)
fields/
Rating_Notes__c.field-meta.xmlA quick way to tell them apart: if a file without the -meta.xml suffix exists beside it, the -meta.xml file is a companion. If no such file exists, the -meta.xml file is the component itself.
Which types need a companion
Types whose primary payload is code, markup, or a binary need companions. Types made entirely of XML do not, in Metadata API format.
| Type | Content file | Companion required |
|---|---|---|
| ApexClass | `.cls` | Yes |
| ApexTrigger | `.trigger` | Yes |
| ApexPage (Visualforce) | `.page` | Yes |
| ApexComponent | `.component` | Yes |
| StaticResource | `.resource` | Yes |
| EmailTemplate | `.email` | Yes |
| Document | any file, such as `.png` | Yes |
| CustomObject, CustomField, Layout, Profile | none (the XML is the component) | No |
Lightning bundles (Lightning Web Components and Aura) have a related but different arrangement. They are folders, and the folder holds one companion-style file for settings. The chapter on bundles and static resources covers this in detail. The key point here is that each bundle still has a -meta.xml file that carries the API version and, for LWC, where the component can be used.
Anatomy of a companion file
The root element of a companion is the metadata type name, in the standard metadata namespace. For an Apex class:
<?xml version="1.0" encoding="UTF-8"?>
<ApexClass xmlns="http://soap.sforce.com/2006/04/metadata">
<apiVersion>60.0</apiVersion>
<status>Active</status>
</ApexClass>apiVersionis the API version the code compiles against. It is separate fromsourceApiVersioninsfdx-project.json, which sets the default version for the project and for CLI operations.statusisActiveorInactive. An inactive class or trigger deploys but does not run.
An Apex trigger companion has the same two fields under an ApexTrigger root. A Visualforce page adds a label:
<?xml version="1.0" encoding="UTF-8"?>
<ApexPage xmlns="http://soap.sforce.com/2006/04/metadata">
<apiVersion>60.0</apiVersion>
<label>Dashboard</label>
</ApexPage>A static resource companion describes the file type and caching:
<?xml version="1.0" encoding="UTF-8"?>
<StaticResource xmlns="http://soap.sforce.com/2006/04/metadata">
<cacheControl>Public</cacheControl>
<contentType>application/zip</contentType>
</StaticResource>Note that the root element must match the type. An ApexTrigger root in a .cls-meta.xml file is an error, even though the XML is well formed.
Creating companions the safe way
You rarely write these files by hand. The CLI generates a class and its companion together:
sf apex generate class --name MyClass --output-dir force-app/main/default/classesThis creates MyClass.cls and MyClass.cls-meta.xml, with the API version taken from the project configuration. Retrieving from an org also returns both files. Hand-copying a .cls file into a project is the usual way a companion goes missing.
What happens when a companion is missing or wrong
The failures fall into a few patterns.
- Missing companion. The content file exists, but the
-meta.xmlfile does not. The deploy or convert step reports an error naming the file. The fix is to create the companion, or to regenerate the component with the CLI. - Misnamed companion. The file is
MyClass-meta.xmlormyclass.cls-meta.xml. The names are matched exactly, and on case-sensitive file systems (Linux, and some CI runners) case differences break the match. Copy the content file's name and append the suffix. - Orphan companion. The
-meta.xmlfile exists, but the content file was deleted or renamed. Tooling may report a missing content file or fail to resolve the component. Delete the stray file or restore the content. - Wrong root element or invalid field. The XML is rejected during deploy validation. Check the root element against the type, and check the field names.
- Inactive status by accident. A trigger deploys successfully with
<status>Inactive</status>, but never fires. This is a behavior bug, not a deploy error, so it is easy to miss.
Checking a project for missing companions
Before deploying, you can check for the most common problem with a short script. This one scans a classes directory for .cls files with no matching companion, and for companions with no content file.
import sys
from pathlib import Path
def check(classes_dir: str) -> int:
root = Path(classes_dir)
problems = 0
for cls in sorted(root.glob("*.cls")):
companion = cls.with_name(cls.name + "-meta.xml")
if not companion.exists():
print(f"missing companion: {companion.name}")
problems += 1
for meta in sorted(root.glob("*.cls-meta.xml")):
content = meta.with_name(meta.name[: -len("-meta.xml")])
if not content.exists():
print(f"orphan companion: {meta.name}")
problems += 1
return problems
if __name__ == "__main__":
target = sys.argv[1] if len(sys.argv) > 1 else "force-app/main/default/classes"
count = check(target)
print(f"{count} problem(s) found")
sys.exit(1 if count else 0)Run it from the project root with python check_meta.py. It exits with status 1 when it finds a problem, so you can use it as a CI step. Adapt the .cls pattern for triggers (.trigger) or pages (.page).
Companions during conversion
The conversion commands preserve companions. Converting from source format to Metadata API format and back keeps each content file paired with its settings. For pure configuration components, the conversion changes the naming instead:
Account.object-meta.xml(source) becomesAccount.object(Metadata API format), with the decomposed field files merged into it.MyClass.cls-meta.xmlkeeps its name in both formats.
After a conversion, count the companions on each side. For classes, the number of .cls files should equal the number of .cls-meta.xml files in both the input and output directories. The pitfalls chapter covers broader verification.
Exercise
A project in source format contains force-app/main/default/classes/Billing.cls and force-app/main/default/classes/Billing-meta.xml. A deploy fails. What is the problem, and how do you fix it?
Show answer
The companion is misnamed. The rule is the full content file name plus -meta.xml, so the file must be Billing.cls-meta.xml. Rename it (or regenerate the class with sf apex generate class), confirm the root element is ApexClass, and deploy again.
Exercise
You find objects/Account/fields/Rating_Notes__c.field-meta.xml and classes/MyClass.cls-meta.xml. Which one is a companion of a content file, and which is a standalone component file? How can you tell?
Show answer
MyClass.cls-meta.xml is a companion, because a file named MyClass.cls exists beside it. Rating_Notes__c.field-meta.xml is a standalone component file in source format, because no Rating_Notes__c.field file exists. The field's whole definition is the XML.
Exercise
A trigger deployed without errors but never runs. Which companion setting do you check first?
Show answer
Check <status> in MyTrigger.trigger-meta.xml. If it is Inactive, the trigger is deployed but does not fire. Set it to Active and deploy again.