Preview builds are pre-production versions of packages. Unlike local builds, they are published to NPM (albeit under a separate NPM namespace) and can therefore be treated the same way as production releases in CI. Because of this, we recommend using preview builds for testing.
Generally, when working with preview builds, you will follow this process:
- Create a branch in your clone of this repo
- Work on changes to your package(s)
- Publish preview builds
- Switch to the project and configure it to use the new preview builds
- Repeat steps 2-4 as necessary
To publish preview builds:
- Create a pull request with the changes to your package(s).
- Post a comment on the pull request with the text
@metamaskbot publish-previews. Thepublish-previewGitHub action will kick off to generate and publish preview builds for all packages in the monorepo. - After a few minutes, you will see a new comment that lists the newly published packages along with their versions.
Publishing preview builds as an independent contributor
Note that the steps above will only work if you are a member of the MetaMask engineering team on GitHub. If you are not, you'll need to follow some different steps:
-
First, you'll need access to an NPM organization under which the preview builds will be published. If you have not already done so, you can either create a new organization or convert your existing username into an organization.
-
Open the
package.jsonfor each package that you want to publish, and change the NPM scope in the package's name from@metamaskto reflect your NPM organization. -
Run the following command to create and publish preview builds for all packages in the monorepo (replacing
NPM_ORGas appropriate):yarn prepare-preview-builds "@<NPM_ORG>" "$(git rev-parse --short HEAD)" yarn build yarn publish-previews
You will see a list of the newly published packages along with their versions.
To simulate production as best as possible, there are two different paths to take depending on whether you anticipate making breaking or non-breaking changes to the package you want to test.
If you're in a MetaMask client repo (e.g. metamask-extension or metamask-mobile):
-
Open
package.jsonand locate thepreviewBuildssection. If it doesn't exist, then add it (it should be an object). -
Within
previewBuilds, add an entry that looks like this:"<name-of-package>": { "type": "non-breaking", "previewVersion": "<preview-version>" }For example:
"@metamask/permission-controller": { "type": "non-breaking", "previewVersion": "13.0.2-preview-940c934" } -
Run
yarn install. You're now using the preview build.
Manual steps
-
In the project, open
package.jsonand locate the entry independenciesfor the package. Take note of the major part of the version.-
Note that the dependency may be patched. Patched dependencies look like this:
"@metamask/<PACKAGE_NAME>": "patch:@metamask/<PACKAGE_NAME>@npm:<ESCAPED_VERSION_RANGE>#~/.yarn/patches/<PATCH_NAME>.patch"
where:
PACKAGE_NAMEis the name of the package.ESCAPED_VERSION_RANGEis the version range, but where characters like^are escaped (for instance,^will appear as%3A).PATCH_NAMEis the filename for a patch. It is usually dash-separated, starts with the package name, and ends with the commit ID from which the patch was created.
In this case the major version you are targeting will come from
ESCAPED_VERSION_RANGE.
-
-
Run
yarn why @metamask/<PACKAGE_NAME>, replacingPACKAGE_NAMEas appropriate. You will see output like this:├─ @metamask/parent-controller-1@npm:93.1.0 │ └─ @metamask/my-controller@npm:12.1.1 (via npm:^12.1.1) │ ├─ @metamask/parent-controller-2@npm:94.1.0 │ └─ @metamask/my-controller@npm:12.1.1 (via npm:^12.1.1) │ ├─ @metamask/parent-controller-3@npm:94.1.0 [abc9d] │ └─ @metamask/my-controller@npm:12.1.1 [57677] (via npm:^12.1.1 [15228]) ... │ └─ metamask@workspace:. └─ @metamask/my-controller@npm:12.1.1 [57677] (via npm:^12.1.0 [abc9d])Take note of all the version ranges that match the major version you saw earlier (here, we are looking for all version ranges that start with
^12). -
Back in
package.json, locate theresolutionssection. If it doesn't exist, then add it (it should be an object). -
For each version range, add a new entry that looks like this:
"@metamask/<PACKAGE_NAME>@<VERSION_RANGE>": "npm:@<NPM_ORG>/<PACKAGE_NAME>@<PREVIEW_VERSION>"
where:
PACKAGE_NAMEis the name of your packageVERSION_RANGEis one of the version ranges you noted in step 2NPM_ORGis the NPM scope that the preview build is published under (note: this is not@metamask)PREVIEW_VERSIONis the version string of the preview build (note: this should not start with^)
Note that if the dependency was patched as in step 1, you may have a resolution already. You'll want to replace the existing resolution while keeping the existing patch. Your entry will look more like:
"@metamask/<PACKAGE_NAME>@<VERSION_RANGE>": "patch:@<NPM_ORG>/<PACKAGE_NAME>@npm:<ESCAPED_PREVIEW_VERSION>#~/.yarn/patches/<PATCH_NAME>.patch"
-
Run
yarn installto apply the changes. -
Run
yarn why @metamask/<PACKAGE_NAME>again to confirm that all of the instances of the package you saw when you ran this command earlier are now using your preview build.
Example 1 (non-patched dependency):
- You have non-breaking changes to
@metamask/controller-utilsyou want to test@metamask/controller-utilsis listed at^1.1.4, andyarn whyreveals that^1.0.1and^1.1.3are also being used as version ranges in the dependency tree- You want to use the preview version
1.1.4-preview-e2df9b4In this case, you would go to
resolutionsand add these lines:"@metamask/controller-utils@^1.0.1": "npm:@metamask-previews/controller-utils@1.1.4-preview-e2df9b4", "@metamask/controller-utils@^1.1.3": "npm:@metamask-previews/controller-utils@1.1.4-preview-e2df9b4", "@metamask/controller-utils@^1.1.4": "npm:@metamask-previews/controller-utils@1.1.4-preview-e2df9b4",
Example 2 (patched dependency):
- You have non-breaking changes to
@metamask/controller-utilsyou want to test@metamask/controller-utilsis listed at^1.1.4, andyarn whyreveals that^1.0.1and^1.1.3are also being used as version ranges in the dependency tree- The dependency entry looks like this:
"@metamask/controller-utils": "patch:@metamask/controller-utils@npm%3A1.1.4#~/.yarn/patches/@metamask-controller-utils-npm-1.1.4-cccac388c7.patch"- A resolution entry also exists which looks like this:
"@metamask/controller-utils@npm:^1.1.4": "patch:@metamask/controller-utils@npm%3A1.1.4#~/.yarn/patches/@metamask-controller-utils-npm-1.1.4-cccac388c7.patch"- You want to use the preview version
1.1.4-preview-e2df9b4In this case, you would go to
resolutions, remove the existing entry, and add these new entries:"@metamask/controller-utils@^1.0.1": "patch:@metamask-previews/controller-utils@npm%3A1.1.4-preview-e2df9b4#~/.yarn/patches/@metamask-controller-utils-npm-1.1.4-cccac388c7.patch" "@metamask/controller-utils@^1.1.3": "patch:@metamask-previews/controller-utils@npm%3A1.1.4-preview-e2df9b4#~/.yarn/patches/@metamask-controller-utils-npm-1.1.4-cccac388c7.patch" "@metamask/controller-utils@^1.1.4": "patch:@metamask-previews/controller-utils@npm%3A1.1.4-preview-e2df9b4#~/.yarn/patches/@metamask-controller-utils-npm-1.1.4-cccac388c7.patch"
If you're in a MetaMask client repo (e.g. metamask-extension or metamask-mobile), run:
-
Open
package.jsonand locate thepreviewBuildssection. If it doesn't exist, then add it (it should be an object). -
Within
previewBuilds, add an entry that looks like this:"<name-of-package>": { "type": "breaking", "previewVersion": "<preview-version>" }For example:
"@metamask/permission-controller": { "type": "breaking", "previewVersion": "13.0.2-preview-940c934" } -
Run
yarn install. You're now using the preview build.
Manual steps
-
In the project, open
package.json, and:-
Note the name of the package.
-
Locate the
resolutionssection. If it doesn't exist, then add it (it should be an object). -
Check to see if the dependency you're testing is patched. Patched dependencies look like this:
"@metamask/<PACKAGE_NAME>": "patch:@metamask/<PACKAGE_NAME>@npm:<ESCAPED_VERSION_RANGE>#~/.yarn/patches/<PATCH_NAME>.patch"
where:
PACKAGE_NAMEis the name of the package.ESCAPED_VERSION_RANGEis the version range, but where characters like^are escaped (for instance,^will appear as%3A).PATCH_NAMEis the filename for a patch. It is usually dash-separated, starts with the package name, and ends with the commit ID from which the patch was created.
-
-
Add a new entry to
resolutionsthat looks like this:"<ROOT_PACKAGE_NAME>@workspace:./@metamask/<PACKAGE_NAME>": "npm:@<NPM_ORG>/<PACKAGE_NAME>@<PREVIEW_VERSION>"
where:
ROOT_PACKAGE_NAMEis the value of thenamefield at the top ofpackage.jsonNPM_ORGis the NPM scope that the preview build is published under (note: this is not@metamask)PACKAGE_NAMEis the name of your packagePREVIEW_VERSIONis the version string of the preview build (note: this should not start with^)
Note that if the dependency was patched as in step 1, you may have a resolution already. You'll want to replace the existing resolution while keeping the existing patch. Your entry will look more like:
"@metamask/<PACKAGE_NAME>@<VERSION_RANGE>": "patch:@<NPM_ORG>/<PACKAGE_NAME>@npm:<ESCAPED_PREVIEW_VERSION>#~/.yarn/patches/<PATCH_NAME>.patch"
-
Run
yarn installto apply the changes.
Example 1 (non-patched dependency):
- You have breaking changes to
@metamask/network-controlleryou want to test- The
nameinpackage.jsonis "my-cool-project"@metamask/network-controlleris listed at^12.4.9- You want to use the preview version
12.4.9-preview-e2df9b4In this case, you would go to
resolutionsand add this line:"my-cool-project@workspace:./@metamask/network-controller@^12.4.9": "npm:@metamask-previews/network-controller@12.4.9-preview-e2df9b4",
Example 2 (patched dependency):
- You have breaking changes to
@metamask/network-controlleryou want to test- You're in a MetaMask client repo
- The
nameinpackage.jsonis "metamask"@metamask/network-controlleris listed at^12.4.9- The dependency entry looks like this:
"@metamask/network-controller": "patch:@metamask/network-controller@npm%3A12.4.9#~/.yarn/patches/@metamask-network-controller-npm-12.4.9-cccac388c7.patch"- A resolution entry also exists which looks like this:
"@metamask/network-controller@npm:^12.4.9": "patch:@metamask/network-controller@npm%3A12.4.9#~/.yarn/patches/@metamask-network-controller-npm-12.4.9-cccac388c7.patch"- You want to use the preview version
12.4.9-preview-e2df9b4In this case, you would go to
resolutionsand leave the existing one there, but add a new one:"metamask@workspace:./@metamask/network-controller@^12.4.9": "patch:@metamask-previews/network-controller@npm%3A12.4.9-preview-e2df9b4#~/.yarn/patches/@metamask-network-controller-npm-12.4.9-cccac388c7.patch"