Vendoring Third Party Components

The firefox source tree vendors many third party dependencies. This document describes both the expectations when adding vendored dependencies, and the practical side of how to use our vendoring infrastructure.

Expectations Around Vendoring

When you vendor in new code, ownership expectations are similar to those when you write your own code. We expect you to:

  • check the suitability of the vendored code as compared to available alternatives;

  • check licensing suitability;

  • check the vendored code is maintained in its source location;

  • check for auditing results where the language/vendoring system supports this;

  • commit to keeping it updated (especially but not limited to security updates).

Where vendored packages are depended on by several parts of our codebase and several teams, that responsibility is shared between those teams. In these situations, please proactively organize between the teams to avoid “tragedy of the commons” type situations where nobody takes responsibility for ongoing ownership of the vendored dependency.

To ensure the vendored code stays up-to-date, consider making use of updatebot (detailed below). This will allow you to review automatically created vendoring updates, including running trypushes to verify the updates do not break things.

Otherwise, you will need to set up reminders or a push-based notification for new releases.

For rust crates, we also run cargo deny in automation, which will lead to bugs filed when/where crates are missing critical security updates. This can help you stay on top of security issues but does not cover other updates.

Vendoring Architecture

The build system provides a normalized way to keep track of:

  1. The upstream source license, location and revision

  2. (Optionally) The upstream source modification, including

    1. Mozilla-specific patches

    2. Custom update actions, such as excluding some files, moving files around etc.

This is done through a descriptive moz.yaml file added to the third party sources, and the use of:

./mach vendor [options] ./path/to/moz.yaml

to interact with it.

Template moz.yaml file

---
# Third-Party Library Template
# All fields are mandatory unless otherwise noted

# Version of this schema
schema: 1

bugzilla:
  # Bugzilla product and component for this directory and subdirectories
  product: product name
  component: component name

# Document the source of externally hosted code
origin:

  # Short name of the package/library
  name: name of the package

  description: short (one line) description

  # Full URL for the package's homepage/etc
  # Usually different from repository url
  url: package's homepage url

  # Human-readable identifier for this version/release
  # Generally "version NNN", "tag SSS", "bookmark SSS"
  release: identifier

  # Revision to pull in
  # Must be a long or short commit SHA (long preferred)
  revision: sha

  # The package's license, where possible using the mnemonic from
  # https://spdx.org/licenses/
  # Multiple licenses can be specified (as a YAML list)
  # A "LICENSE" file must exist containing the full license text
  license: MPL-2.0

  # If the package's license is specified in a particular file,
  # this is the name of the file.
  # optional
  license-file: COPYING

  # The name the package is published under on the npm registry, which gives
  # it a pkg:npm package URL in the SBOM.
  # optional
  npm-name: "@scope/name"

  # If there are any mozilla-specific notes you want to put
  # about a library, they can be put here.
  notes: Notes about the library

# Configuration for the automated vendoring system.
# optional
vendoring:

  # Repository URL to vendor from
  # eg. https://github.com/kinetiknz/nestegg
  # Any repository host can be specified here, however initially we'll only
  # support automated vendoring from selected sources.
  url: source url (generally repository clone url)

  # Type of hosting for the upstream repository
  # Valid values are 'gitlab', 'github', googlesource
  source-hosting: gitlab

  # Type of Vendoring
  # This is either 'regular', 'individual-files', or 'rust'
  # If omitted, will default to 'regular'
  flavor: rust

  # Type of git reference (commit, tag) to track updates from.
  # You cannot use tag tracking with the individual-files flavor
  # If omitted, will default to tracking commits.
  tracking: commit

  # When using tag tracking (only on Github currently) use a release artifact
  # for the source code instead of the automatically built git-archive exports.
  # The source repository must build these artifacts with consistent filenames
  # for every tag. This is useful when the Github repository uses submodules
  # since they are not included in the git-archives.
  # Substitution is performed on the filename, {tag} is replaced with the tag name.
  # optional
  release-artifact: "rnp-{tag}.tar.gz"

  # Base directory of the location where the source files will live in-tree.
  # If omitted, will default to the location the moz.yaml file is in.
  vendor-directory: third_party/directory

  # Built-in steps of the vendoring process to skip. These are all of them:
  #   fetch: don't fetch upstream at all. Useful if bringing in upstream is
  #     complicated and done by a script, in a first stage of the library's own,
  #     which must be named 'import'. There is then no 'fetch' stage.
  #   keep, include, exclude, move-contents: the parts of the fetch (see "keep",
  #     "include" and "exclude" below; move-contents copies the unpacked upstream
  #     into the vendor-directory). Skipping one of these along with fetch is an
  #     error.
  #   hg-add: registering added and removed files with version control, after
  #     every stage
  #   update-moz-build: adding and removing files in moz.build files, after every
  #     stage
  #   update-moz-yaml: recording the new revision in this file, at the end of the
  #     first stage
  # optional
  skip-steps:
    - update-moz-build

  # Disable git object validation for clones/fetches of this repository
  # only. Only set this if upstream's history contains an object that
  # fails git's fsck validation (e.g. a very old malformed commit), and
  # you've confirmed the object itself is benign.
  # optional
  tolerate-git-fsck-errors: true

  # List of files that are not removed from the destination directory while vendoring
  # in a new version of the library. Intended for mozilla files not present in upstream.
  # Implicitly contains "moz.yaml", "moz.build", and the patch files of every stage
  # optional
  keep:
    - file
    - path/to/file
    - another/path
    - *.mozilla

  # Files/paths that will not be vendored from the upstream repository
  # Implicitly contains ".git", and ".gitignore"
  # optional
  exclude:
    - file
    - path/to/file
    - another/path
    - docs
    - src/*.test

  # Files/paths that will always be vendored from source repository, even if
  # they would otherwise be excluded by "exclude".
  # optional
  include:
    - file
    - path/to/file
    - another/path
    - docs/LICENSE.*

  # Files that are modified as part of the update process.
  # To avoid creating updates that don't update anything, Updatebot (and the
  # vendor-verify job) check whether vendoring changed any in-tree files. If there
  # are files that are always changed during an update process (e.g. version
  # numbers or source revisions), list them here to avoid having them counted as
  # substantive changes.
  # This field does NOT support directories or globbing
  # optional
  generated:
    - '{yaml_dir}/vcs_version.h'

  # If neither "exclude" or "include" are set, all files will be vendored
  # Files/paths in "include" will always be vendored, even if excluded
  # eg. excluding "docs/" then including "docs/LICENSE" will vendor just the
  #     LICENSE file from the docs directory

  # All three file/path parameters ("keep", "exclude", and "include") support
  # filenames, directory names, and globs/wildcards.

  # The update, as an ordered list of stages. Each stage becomes one commit:
  # `./mach vendor --stage all` commits after each one. Without "stages", a library
  # has a single bare 'fetch' stage (or none, if skip-steps includes fetch).
  #
  # A stage has:
  #   name: required, unique, starting with a lowercase letter and using
  #     lowercase letters, digits and '-'. 'all' can't be used, and 'fetch' only
  #     for the first stage.
  #   patches or actions: exactly one of them, except that the 'fetch' stage can
  #     have neither (a bare fetch).
  #   title: optional, the commit message. By default it is
  #     "Update <name> to <revision>" for the first stage,
  #     "Apply mozilla <stage> patches for <name>" for a stage that applies
  #       patches ("Apply mozilla patches for <name>" for one named 'patches'),
  #     and "Stage <stage> for <name>" otherwise.
  #
  # The first stage must be named 'fetch', or 'import' if skip-steps includes
  # fetch. A
  # 'fetch' stage with actions must include exactly one 'fetch' action, which is
  # where upstream is fetched; the 'fetch' action can't be used anywhere else.
  #
  # After each stage, mach vendor registers the stage's changes with version
  # control (hg-add) and updates moz.build files with the files that stage added
  # and removed (update-moz-build). The first stage then records the new revision
  # in this file (update-moz-yaml). Later stages use the revision recorded there.
  #
  # "patches" is a list of patch files to apply, relative to the directory this
  # file is in. Applied in the order specified, and alphabetically if globbing is
  # used. Patches must apply cleanly before changes are pushed.
  #   - path/to/file
  #   - path/*.patch
  #   - path/**  # Captures all files and subdirectories below path
  #   - path/*   # Captures all files but _not_ subdirectories below path. Equivalent to `path/`
  #
  # "actions" are run in order. The action subfield is required. It must be one of:
  #   - fetch
  #   - apply-patches
  #   - copy-file
  #   - move-file
  #   - move-dir
  #   - replace-in-file
  #   - replace-in-file-regex
  #   - delete-path
  #   - run-script
  #   - run-command
  #   - vcs-add-remove-files
  # Unless otherwise noted, all subfields of action are required.
  #
  # If the action is fetch, it takes no other subfields. It fetches upstream and
  #   unpacks it into the vendor-directory, then applies "keep", "include" and
  #   "exclude".
  #
  # If the action is apply-patches:
  #   patches is a list of patch files, as in a stage's "patches"
  #   directory is the directory to apply them in (optional, defaults to
  #     '{vendor_dir}')
  #
  # If the action is copy-file, move-file, or move-dir:
  #   from is the source file
  #   to is the destination
  #
  # If the action is replace-in-file or replace-in-file-regex:
  #   pattern is what in the file to search for. It is an exact strng match.
  #   with is the string to replace it with. Accepts the special keyword
  #     '{revision}' for the commit we are updating to.
  #   File is the file to replace it in.
  #
  # If the action is vcs-add-remove-files
  #   path is the directory to add all new files to version control, and
  #     delete all removed files.
  #
  # If the action is delete-path
  #   path is the file or directory to recursively delete
  #
  # If the action is run-script:
  #   script is the script to run
  #   cwd is the directory the script should run with as its cwd
  #   args is a list of arguments to pass to the script
  #   Note: 'script', 'cwd', and entries in 'args' support path placeholders
  #     '{vendor_dir}', '{yaml_dir}', '{tmpextractdir}', '{topsrcdir}'.
  #     For 'run-script'/'run-command', '{cwd}' is also supported but must
  #     appear only at the beginning of a path. In 'args', the literal
  #     '{revision}' will be replaced with the target revision string.
  #
  # If the action is run-command:
  #   command is the command to run
  #      Unlike run-script, `command` is _not_ processed to be relative
  #      to the vendor directory, and is passed directly to python's
  #      execution code without any path substitution or manipulation
  #   cwd is the directory the command should run with as its cwd
  #   args is a list of arguments to pass to the command
  #
  # Unless specified otherwise, all files/directories are relative to the
  #     vendor-directory. The following placeholders are supported in most
  #     path fields: '{vendor_dir}', '{yaml_dir}', '{tmpextractdir}', '{topsrcdir}'.
  #     If the vendor-directory differs from the yaml directory, '{yaml_dir}'
  #     makes the path relative to the yaml directory.
  #     For 'run-script'/'run-command', '{cwd}' is additionally supported and
  #     must only be used at the beginning of a path.
  #     '{tmpextractdir}', the unpacked upstream, can only be used in the 'fetch'
  #     stage, after the 'fetch' action.
  #
  # optional
  stages:
    - name: fetch
      actions:
        - action: fetch

        - action: copy-file
          from: include/vcs_version.h.in
          to: '{yaml_dir}/vcs_version.h'

        - action: replace-in-file
          pattern: '@VCS_TAG@'
          with: '{revision}'
          file: '{yaml_dir}/vcs_version.h'

        - action: delete-path
          path: '{yaml_dir}/config'

        - action: run-script
          script: '{cwd}/generate_sources.sh'
          cwd: '{yaml_dir}'

    - name: patches
      patches:
        - patches/*.patch

    - name: regenerate
      title: Regenerate the sources list for library-name
      actions:
        - action: apply-patches
          patches:
            - build-patches/*.patch
        - action: run-script
          script: '{yaml_dir}/post_patch_script.py'
          cwd: '{yaml_dir}'
          args: ['{revision}']


# Configuration for automatic updating system.
# optional
updatebot:

  # TODO: allow multiple users to be specified
  # Phabricator username for a maintainer of the library, used for assigning
  # reviewers. For a review group, preface with #, such as "#build""
  maintainer-phab: tjr

  # Bugzilla email address for a maintainer of the library, used for needinfos
  maintainer-bz: tom@mozilla.com

  # Optional: A preset for ./mach try to use. If present, fuzzy-query and fuzzy-paths will
  # be ignored. If it, fuzzy-query, and fuzzy-path are omitted, ./mach try auto will be used
  try-preset: media

  # Optional: A query string for ./mach try fuzzy. If try-preset, it and fuzzy-paths are omitted
  # then ./mach try auto will be used
  fuzzy-query: media

  # Optional: An array of test paths for ./mach try fuzzy. If try-preset, it and fuzzy-query are
  # omitted then ./mach try auto will be used
  fuzzy-paths: ['media']

  # Optional: Pass --artifact to ./mach try, so the try push uses artifact builds where
  # possible. Only use this if the library's updates don't change compiled Gecko code:
  # artifact builds reuse binaries built from mozilla-central, so compiled changes would
  # not be tested. Defaults to False.
  artifact-build: True

  # The tasks that Updatebot can run. Only one of each task is currently permitted
  # optional
  tasks:
    - type: commit-alert
      branch: upstream-branch-name
      cc: ["bugzilla@email.address", "another@example.com"]
      needinfo: ["bugzilla@email.address", "another@example.com"]
      enabled: True
      filter: security
      frequency: every
      blocking: 1234
      options: ["option1", "option2"]
    - type: vendoring
      branch: master
      enabled: False

      # frequency can be 'every', 'release', 'N weeks', 'N commits'
      # or 'N weeks, M commits' requiring satisfying both constraints.
      frequency: 2 weeks

Adding a New Library

A moz.yaml vendors into its own directory (or its vendor-directory), and mach vendor deletes everything there that keep does not list before unpacking. Each library therefore needs a directory of its own, even when two are built together.

Once the new manifest reproduces the tree you intend to commit:

  • add it to a group’s members in taskcluster/kinds/source-test/vendor-verify.yml; the vendor(coverage) job fails when a vendored manifest is not listed in any group;

  • add the directory to tools/rewriting/ThirdPartyPaths.txt, the list linters and formatters treat as third-party code;

  • declare its license for about:license and the SBOM, as described in Third-Party Licensing and the SBOM;

  • check that re-vendoring at the pinned revision reproduces the committed tree, which is what the vendor-verify job does:

    ./mach python taskcluster/scripts/misc/verify-vendored-library.py path/to/moz.yaml
    

    Run it on a clean checkout: it restores the whole working tree between libraries.

Common Vendoring Operations

Update to the latest upstream revision:

./mach vendor /path/to/moz.yaml

Check for latest revision, returning no output if it is up-to-date, and a version identifier if it needs to be updated:

./mach vendor /path/to/moz.yaml --check-for-update

Vendor a specific revision:

./mach vendor /path/to/moz.yaml -r $REVISION --force

Vendoring Stages

An update is an ordered list of stages, and each stage becomes one commit. Most libraries are just upstream, so they have a single fetch stage and need no stages key at all. Others say what follows the fetch:

vendoring:
  stages:
    - name: fetch
    - name: patches
      patches:
        - patches/*.patch

A library with one stage is vendored with ./mach vendor path/to/moz.yaml. One with several needs --stage:

  • --stage all runs every stage in order and commits after each one, with the stage’s commit message, which you can reword afterwards. It needs a working directory without uncommitted changes. A stage that changes nothing isn’t committed.

  • --stage <name>, or --stage <number> counting from 1, runs just that stage, on top of the working directory, and doesn’t commit. Later stages build on the earlier ones, so run them in order.

Each stage has either a list of patches or a list of actions. After each stage, mach vendor registers its changes with version control and adds the files the stage added (and removes the ones it removed) in moz.build files. The first stage then records the new revision in moz.yaml; later stages use the revision recorded there.

The fetch stage

The fetch stage brings in upstream, so it must come first. It is either bare (- name: fetch), or has actions that include exactly one fetch action, which marks where upstream is fetched and unpacked. Actions before it run on the current tree, and actions after it can use {tmpextractdir}, the unpacked upstream, as well as the vendored files.

This lets patches that must be applied before code generation, or before anything is committed, go in the fetch stage:

  stages:
    - name: fetch
      actions:
        - action: fetch
        - action: apply-patches
          patches:
            - patches/*.patch
        - action: run-script
          script: '{yaml_dir}/generate.py'
          cwd: '{yaml_dir}'

Libraries that bring in upstream with their own script skip the fetch with skip-steps: [fetch], and have no fetch stage. Their first stage, which must be named import, runs the script:

  skip-steps: [fetch]
  stages:
    - name: import
      actions:
        - action: run-script
          script: 'import.py'
          args: ['{revision}']
          cwd: '{yaml_dir}'

Patches

A stage’s patches, and the apply-patches action, apply each patch with patch -p1 in the vendor directory (or the action’s directory). A patch that doesn’t apply makes mach vendor fail with Could not apply <patch> and the rejected hunks. Updatebot uses this to find patches it can rebase, so put patches in patches or apply-patches rather than running patch yourself.

To apply patches among other actions, such as before regenerating files, use an apply-patches action in a stage’s actions.

Commit messages

By default, the first stage’s commit is “Update to ”, a stage that applies patches is “Apply mozilla patches for ” (“Apply mozilla patches for ” for a stage named patches), and any other is “Stage for ”. A stage’s title replaces its message.