Packages and Namespaces
aer loads a managed package from a .pkg file passed with --package or found in a --package-dir directory. This page describes how aer decides which namespace a package and your own source belong to, and how to load several packages that share a namespace. To create .pkg files, see Load First-Party Packages and Load Third-Party Packages.
A package’s namespace comes from its file name
A package file stores its components without a namespace prefix. aer applies the namespace when it loads the file, and takes the namespace from the file name: dlrs.pkg loads in the dlrs namespace. Renaming the file changes the namespace of everything in it, so the same package file works for namespaced and non-namespaced source.
The namespace must start with a letter and contain only letters, digits and underscores. aer rejects a file whose name does not give a valid namespace, such as dlrs-patched.pkg, when it loads or writes the file.
When type checking finds objects or fields whose namespace prefix no loaded package registers, the error ends with a tip that lists those namespaces and the registered ones.
A .pkg file passed as a source path, instead of with --package, loads as an unmanaged package with no namespace. See Load Third-Party Packages.
Run your source in a namespace
By default, your source code has no namespace. To run it as the code of a managed package, give the namespace with --default-namespace:
aer test force-app/main/default --default-namespace dlrsTo put only some source directories in a namespace, add @<namespace> to the path. Here the package’s source runs in the dlrs namespace and the unpackaged directory runs without one:
aer test force-app/main/default@dlrs unpackagedA package developer can run the same tests with and without the namespace, from the same source, to find code that works in one and fails in the other, such as code that builds SOQL queries or describe calls from field and object names:
aer test force-app/main/default
aer test force-app/main/default --default-namespace dlrsSeveral packages in one namespace
In Salesforce, several second-generation packages can share a namespace. aer models this with a package name in addition to the namespace.
| What you load | How to name the package | Example |
|---|---|---|
| Package file | <package>@<namespace>.pkg | core@fflib.pkg |
| Source directories | --default-namespace <namespace>:<package> | --default-namespace fflib:tests |
| One source directory | <path>@<namespace>:<package> | force-app@fflib:tests |
Without a package name, the package name is the namespace. A file named fflib.pkg and source loaded with --default-namespace fflib both belong to the package fflib, so they are treated as one package.
The objects and fields of packages that share a namespace are combined under that namespace.
Visibility between packages
A package’s Apex can use the Apex of another package according to these rules:
| Where the code is | What it can use |
|---|---|
| Same package | Members visible under the usual public, protected and private rules |
| Another package in the same namespace | Members that are global or annotated @NamespaceAccessible |
| Another namespace | Members that are global |
A public member without @NamespaceAccessible is visible only inside its own package, even to other packages in the same namespace. A method reached through a global interface or parent class that the caller can use can be called regardless of the implementing method’s own access modifier.
Build a package against another package in the same namespace
To build a package whose code uses another package in the same namespace, pass the first package with --package:
aer package create \
--output packages/tests@fflib.pkg \
--package packages/core@fflib.pkg \
path/to/fflib-testsaer package create type checks the new package against the classes in core@fflib.pkg. Then load both packages:
aer test force-app/main/default \
--package packages/core@fflib.pkg \
--package packages/tests@fflib.pkg