WebExtensions/Contribution Onramp: Difference between revisions

From MozillaWiki
Jump to navigation Jump to search
(Add note about testing as part of Getting started)
(Update section on setting up the development environment. Add more explicit instructions on submitting a patch.)
Line 16: Line 16:


== Setting Up Developer Environment ==  
== Setting Up Developer Environment ==  
* Now, you need to set up your development environment by [https://developer.mozilla.org/en-US/docs/Mozilla/Developer_guide/Build_Instructions/Simple_Firefox_build building Firefox].  There are two ways to do this:  
* Now, you need to set up your development environment by [https://firefox-source-docs.mozilla.org/contributing/contribution_quickref.html#clone-the-sources cloning the source] with mercurial or [https://github.com/glandium/git-cinnabar/wiki/Mozilla:-A-git-workflow-for-Gecko-development with Git].
* Then you need to install dependencies with <code>mach bootstrap</code> for building Firefox.  There are two ways to do this:  
** [recommended] Build Firefox using [https://developer.mozilla.org/en-US/docs/Mozilla/Developer_guide/Build_Instructions/Artifact_builds Firefox Artifact builds]. If the issue only requires changes to privileged JavaScript code, we recommend you use this method. Artifact builds saves time because it downloads a pre-built version of Firefox sources and applies your local changes on it, instead of rebuilding all of the C++ and Rust pieces from scratch.
** [recommended] Build Firefox using [https://developer.mozilla.org/en-US/docs/Mozilla/Developer_guide/Build_Instructions/Artifact_builds Firefox Artifact builds]. If the issue only requires changes to privileged JavaScript code, we recommend you use this method. Artifact builds saves time because it downloads a pre-built version of Firefox sources and applies your local changes on it, instead of rebuilding all of the C++ and Rust pieces from scratch.
*** Build Firefox for Android [https://developer.mozilla.org/en-US/docs/Mozilla/Developer_guide/Build_Instructions/Simple_Firefox_for_Android_build requires some additional setup steps]. For a faster local development experience, we recommend to use the Artifact builds and the Android x86 target (which is especially useful to run the tests locally).
*** To build Firefox for Android, see the [https://firefox-source-docs.mozilla.org/mobile/android/geckoview/contributor/geckoview-quick-start.html GeckoView Contributor guide]. For a faster local development experience, we recommend to use the Artifact builds and the Android x86 target (which is especially useful to run the tests locally).
** [https://developer.mozilla.org/en-US/docs/Mozilla/Developer_guide/Build_Instructions/Simple_Firefox_build Build Firefox from scratch]. Note: doing this can take some time. After the initial build, you can build Firefox using the “mach build” command.  
** [default] Build Firefox from scratch, by running <code>mach build</code> without [https://developer.mozilla.org/en-US/docs/Mozilla/Developer_guide/Build_Instructions/Configuring_Build_Options .mozconfig]. Note: doing this can take some time.


* Next, use [https://searchfox.org Searchfox] to find the code associated with your bug.
* Next, use [https://searchfox.org Searchfox] to find the code associated with your bug.
* Before making any changes, use <code>mach test [path to extension test]</code> to verify that you are able to run unit tests. Being able to run existing tests (and modified or new tests) is necessary, to see that your change works as expected.


==== WebExtensions APIs ====  
==== WebExtensions APIs ====  
Line 81: Line 83:
For an overview of how to submit a patch, please see [https://developer.mozilla.org/en-US/docs/Mozilla/Developer_guide/How_to_Submit_a_Patch this page on MDN].  
For an overview of how to submit a patch, please see [https://developer.mozilla.org/en-US/docs/Mozilla/Developer_guide/How_to_Submit_a_Patch this page on MDN].  


* Create a commit with a meaningful commit message, for example <code>Bug [bug ID here] - [short description of patch here] r=[reviewer name here]</code>.
** Use <code>hg com --amend</code> or <code>git commit --amend</code> to modify the last commit if you want to change the commit title or content.
** If you want to combine multiple commits into one:
*** With mercurial, use <code>hg histedit</code> or <code>hg rebase</code>. For more info, see [https://book.mercurial-scm.org/read/changing-history.html Mercurial - Changing history]
*** With git, use <code>git rebase -i</code>. For more info, see [https://git-scm.com/book/en/v2/Git-Tools-Rewriting-History Git - Rewriting History].
* When you are ready to have your patch reviewed, submit your patch to [https://moz-conduit.readthedocs.io/en/latest/phabricator-user.html Phabricator]:
* When you are ready to have your patch reviewed, submit your patch to [https://moz-conduit.readthedocs.io/en/latest/phabricator-user.html Phabricator]:
** First, [https://moz-conduit.readthedocs.io/en/latest/phabricator-user.html#creating-an-account create an account] and install the [https://phabricator.services.mozilla.com/book/phabricator/article/arcanist_quick_start/ arc tool].
** First, [https://moz-conduit.readthedocs.io/en/latest/phabricator-user.html#creating-an-account create an account] and install the [https://github.com/mozilla-conduit/review/blob/master/README.md#installation moz-phab tool].
*** If your contribution consists of multiple separate patches, then [https://github.com/mozilla-conduit/review#readme moz-phab] may be an easier way to submit all patches at once. <br>If you [https://github.com/glandium/git-cinnabar/wiki/Mozilla:-A-git-workflow-for-Gecko-development use git], then the standalone [https://github.com/mystor/phlay phlay tool] can also be used instead.
** Then [https://github.com/mozilla-conduit/review/blob/master/README.md#execution run moz-phab] to upload your commit. Each commit will become a separe patch, so combine them into one if needed.
** Then, use [https://moz-conduit.readthedocs.io/en/latest/arcanist-user.html these instructions] to submit the patch. Choose your mentor as the reviewer.
** Note that you may go through a few iterations of changes and review before your code is ready to go.  
** Note that you may go through a few iterations before your code is ready to go.  
* If you need to make a change to a patch that has already been submitted to Phabricator, make sure you update the existing revision so the reviewers can check the interdiff and history of changes. <code>moz-phab</code> will automatically modify your commit [https://github.com/mozilla-conduit/review/blob/master/README.md#associating-a-commit-to-an-existing-phabricator-revision to add the relevant link].  
* If you need to make a change to a patch that has already been submitted to Phabricator, make sure you update the existing revision so the reviewers can check the interdiff and history of changes.  
* When working on unrelated changes at the same time, it is useful to work on them into separate hg bookmarks (or entire separate mozilla-central clones if you are not yet feeling confident enough with how the hg bookmarks works) or git branches.
* When working on unrelated changes at the same time, it is useful to work on them into separate hg bookmarks (or entire separate mozilla-central clones if you are not yet feeling confident enough with how the hg bookmarks works)
* When the patch is approved, edit your Phabricator revision and add the “Check-in needed” tag.  
* When the patch is approved, edit your Phabricator revision and add the “Check-in needed” tag.  
* QA will verify your patch after it lands, unless your mentor has added a “qa-not-needed” keyword.
* Once your patch is accepted, you can expect to see your code in an upcoming version of Firefox!
* Once your patch is accepted, you can expect to see your code in an upcoming version of Firefox!



Revision as of 21:48, 4 June 2020

If this is your first contribution to the Firefox codebase, welcome! It can be scary to get started contributing to a large and complex codebase, but we’ve prepared some tips to help you fix a good-first-bug and land a patch in Firefox.

If we haven’t addressed your question in this document, please ask us by leaving a comment in the bug or by joining us on chat.mozilla.org in the #addons room (see Matrix). For a general overview of how to contribute to Firefox, please go here.

General Tips

  • If you see a bug that looks interesting, please leave a comment saying that you would like to work on it, or upload your patch to phabricator and request review. Note: we will only assign the bug to you when a patch is uploaded.
  • It's ok to ask questions in Bugzilla comments! It's best if you ask specific questions, like, "Can you help me find the line of code that needs changing," and not general questions like, "can you help me."
  • It's normal to try things out and get stuck when you're working on your bug. When that happens, we encourage you to try a few different approaches before you ask for help. A good rule of thumb is to try to only needinfo your mentor once a day.
  • It might take mentors a day or two to get back to you. If you haven't heard a response in more than two days, please feel free to ping them on chat.mozilla.org in the #addons room (see Matrix).

Getting Started

  • If you haven't already done so, create an account on https://bugzilla.mozilla.org.
  • Pick a bug you want to work on from the good-first-bugs list.
  • Comment on the bug that you would like to work on it and proceed to set up your developer environment.
  • Run unit tests to verify that your environment works as expected, preferably tests related to the bug that you want to work on.

Setting Up Developer Environment

  • Now, you need to set up your development environment by cloning the source with mercurial or with Git.
  • Then you need to install dependencies with mach bootstrap for building Firefox. There are two ways to do this:
    • [recommended] Build Firefox using Firefox Artifact builds. If the issue only requires changes to privileged JavaScript code, we recommend you use this method. Artifact builds saves time because it downloads a pre-built version of Firefox sources and applies your local changes on it, instead of rebuilding all of the C++ and Rust pieces from scratch.
      • To build Firefox for Android, see the GeckoView Contributor guide. For a faster local development experience, we recommend to use the Artifact builds and the Android x86 target (which is especially useful to run the tests locally).
    • [default] Build Firefox from scratch, by running mach build without .mozconfig. Note: doing this can take some time.
  • Next, use Searchfox to find the code associated with your bug.
  • Before making any changes, use mach test [path to extension test] to verify that you are able to run unit tests. Being able to run existing tests (and modified or new tests) is necessary, to see that your change works as expected.

WebExtensions APIs

You can learn more about how WebExtensions internals are organized and how to work on them by reading this internal API documentation and the WebExtensions Hacking wiki.

Fixing the Bug!

  • You get to drive the actual bug fix. :)
  • We encourage you to check in with the mentor to make sure you are on the right path, or ask for feedback on a patch in progress. You can attach your patch to a comment in Bugzilla and needinfo the mentor for review.

Testing

In order to land code in Firefox, you will need to create tests for your patches. For an overview of the different test systems Firefox uses, please see this overview of automated testing. In the WebExtension API code, most tests are either xpcshell tests or browser chrome tests. Xpcshell tests are preferred because of the lower overhead. Browser tests need to be written when a test interfaces with browser UI, such as tabs or context menus.

The easiest way to get started with tests is to look at existing tests:

  • Browser-chrome tests:
  • Firefox for Android mochitests:

These tests can be run with the “mach test” command. To run a specific test, use “mach test [paths to test files or directories]”. Sometimes "mach test" has bugs (e.g. bug 1498636 and bug 1495311); in that case use "mach xpcshell-test" or "mach mochitest" instead of "mach test" to run the test.

Add your test to an existing file when it is small and fits in that test. Otherwise, create a new test file, and register the test file in one of the .ini files in the same directory as your test, usually browser-common.ini or xpcshell-common.ini.

A test consists of multiple subtasks, which are defined in the test files via add_task(...). Most tests have the following minimal structure:

add_task(async function someShortAndSimpleDescriptionHere() {
  // Define a test extension.
  let extension = ExtensionTestUtils.loadExtension( .... );

  // Loads the actual extension.
  await extension.startup();

  // Tests here, often using extension.sendMessage and/or extension.awaitMessage

  // Unload the extension and remove the temporary files.
  await extension.unload();
});

ExtensionTestUtils.loadExtension allows you to quickly generate a test extension, and its input parameters are documented at: https://searchfox.org/mozilla-central/rev/ef8b3886cb173d5534b954b6fb7eb2d94a9473d0/toolkit/components/extensions/ExtensionTestCommon.jsm#185-212

Another common action in tests is to load a web page or extension page. In xpcshell-tests, use ExtensionTestUtils.loadContentPage.

In browser-chrome tests, the BrowserTestUtils namespace provides many useful test helpers, such as BrowserTestUtils.openNewForegroundTab to open a new tab. BrowserTestUtils is implemented and documented at https://searchfox.org/mozilla-central/source/testing/mochitest/BrowserTestUtils/BrowserTestUtils.jsm

Running the tests (xpcshell tests and Firefox for Android mochitests) on the Android emulator requires some additional setup of the local development environment.

Submitting a Patch

For an overview of how to submit a patch, please see this page on MDN.

  • Create a commit with a meaningful commit message, for example Bug [bug ID here] - [short description of patch here] r=[reviewer name here].
    • Use hg com --amend or git commit --amend to modify the last commit if you want to change the commit title or content.
    • If you want to combine multiple commits into one:
  • When you are ready to have your patch reviewed, submit your patch to Phabricator:
    • First, create an account and install the moz-phab tool.
    • Then run moz-phab to upload your commit. Each commit will become a separe patch, so combine them into one if needed.
    • Note that you may go through a few iterations of changes and review before your code is ready to go.
  • If you need to make a change to a patch that has already been submitted to Phabricator, make sure you update the existing revision so the reviewers can check the interdiff and history of changes. moz-phab will automatically modify your commit to add the relevant link.
  • When working on unrelated changes at the same time, it is useful to work on them into separate hg bookmarks (or entire separate mozilla-central clones if you are not yet feeling confident enough with how the hg bookmarks works) or git branches.
  • When the patch is approved, edit your Phabricator revision and add the “Check-in needed” tag.
  • Once your patch is accepted, you can expect to see your code in an upcoming version of Firefox!

Try server

To verify that the patch works as intended, your mentor may schedule a try job on the Try Server to run tests. Contributors with access to the try server can push try jobs to run tests themselves.

See WebExtensions/Try_Server for more information about using the try server to test WebExtension code.