Alex Eagle
    • Create new note
    • Create a note from template
      • Sharing URL Link copied
      • /edit
      • View mode
        • Edit mode
        • View mode
        • Book mode
        • Slide mode
        Edit mode View mode Book mode Slide mode
      • Customize slides
      • Note Permission
      • Read
        • Only me
        • Signed-in users
        • Everyone
        Only me Signed-in users Everyone
      • Write
        • Only me
        • Signed-in users
        • Everyone
        Only me Signed-in users Everyone
      • Engagement control Commenting, Suggest edit, Emoji Reply
    • Invite by email
      Invitee
    • Publish Note

      Share your work with the world Congratulations! 🎉 Your note is out in the world Publish Note

      Your note will be visible on your profile and discoverable by anyone.
      Your note is now live.
      This note is visible on your profile and discoverable online.
      Everyone on the web can find and read all notes of this public team.
      See published notes
      Unpublish note
      Please check the box to agree to the Community Guidelines.
      View profile
    • Commenting
      Permission
      Disabled Forbidden Owners Signed-in users Everyone
    • Enable
    • Permission
      • Forbidden
      • Owners
      • Signed-in users
      • Everyone
    • Suggest edit
      Permission
      Disabled Forbidden Owners Signed-in users Everyone
    • Enable
    • Permission
      • Forbidden
      • Owners
      • Signed-in users
    • Emoji Reply
    • Enable
    • Versions and GitHub Sync
    • Note settings
    • Engagement control
    • Transfer ownership
    • Delete this note
    • Save as template
    • Insert from template
    • Import from
      • Dropbox
      • Google Drive
      • Gist
      • Clipboard
    • Export to
      • Dropbox
      • Google Drive
      • Gist
    • Download
      • Markdown
      • HTML
      • Raw HTML
Menu Note settings Versions and GitHub Sync Sharing URL Create Help
Create Create new note Create a note from template
Menu
Options
Engagement control Transfer ownership Delete this note
Import from
Dropbox Google Drive Gist Clipboard
Export to
Dropbox Google Drive Gist
Download
Markdown HTML Raw HTML
Back
Sharing URL Link copied
/edit
View mode
  • Edit mode
  • View mode
  • Book mode
  • Slide mode
Edit mode View mode Book mode Slide mode
Customize slides
Note Permission
Read
Only me
  • Only me
  • Signed-in users
  • Everyone
Only me Signed-in users Everyone
Write
Only me
  • Only me
  • Signed-in users
  • Everyone
Only me Signed-in users Everyone
Engagement control Commenting, Suggest edit, Emoji Reply
  • Invite by email
    Invitee
  • Publish Note

    Share your work with the world Congratulations! 🎉 Your note is out in the world Publish Note

    Your note will be visible on your profile and discoverable by anyone.
    Your note is now live.
    This note is visible on your profile and discoverable online.
    Everyone on the web can find and read all notes of this public team.
    See published notes
    Unpublish note
    Please check the box to agree to the Community Guidelines.
    View profile
    Engagement control
    Commenting
    Permission
    Disabled Forbidden Owners Signed-in users Everyone
    Enable
    Permission
    • Forbidden
    • Owners
    • Signed-in users
    • Everyone
    Suggest edit
    Permission
    Disabled Forbidden Owners Signed-in users Everyone
    Enable
    Permission
    • Forbidden
    • Owners
    • Signed-in users
    Emoji Reply
    Enable
    Import from Dropbox Google Drive Gist Clipboard
       owned this note    owned this note      
    Published Linked with GitHub
    3
    Subscribed
    • Any changes
      Be notified of any changes
    • Mention me
      Be notified of mention me
    • Unsubscribe
    Subscribe
    # RFC: rules_nodejs fetch/install tarballs ## Summary Bazel rulesets must contend with the problem of connecting third-party dependencies from outside the repository. Google itself vendors all dependencies, so this is a novel problem the Bazel community must solve. Rulesets take a variety of approaches, with varying tradeoffs. We can divide the problem into phases: 1. Fetching - What tool does the downloads and caches them? - Are all declared artifacts downloaded, or only those needed for this build? 1. Installing - Are all dependencies installed, or only those needed for this build? - Where are the dependencies installed to? 1. Resolving - How do only the needed dependencies end up as action inputs? - How does the runtime know where the dependencies are installed? ### Fetching dependencies First, the user must specify what dependencies they want. This should involve a lockfile that pins both the direct and transitive dependency versions for reproducibility. It should also include an integrity hash for each artifact so that the downloader can avoid reaching out on the network. **Choice: Bazel downloads** Bazel has a full-featured downloader available to repository rules as `repository_ctx.download()` [1] It uses a local cache separate from the repository cache, so artifacts are not downloaded again even if the repository rule re-runs. However the downloader must be called from starlark code. rules_go uses Bazel to download artifacts, but requires the user transform their `go.sum` file into a `deps.bzl` file containing `go_repository` rules, and downloads them into independent Bazel external repos. This allows each artifact to be referenced like `@com_github_mgechev_revive//:revive` **Choice: Package manager downloads** The alternative is to have the native tooling do the downloads, such as a package manager. It will cache artifacts in its own global cache, which can introduce race conditions if Bazel calls it in parallel and it isn't intended to be used that way (e.g. yarn's [--mutex](https://classic.yarnpkg.com/en/docs/cli/#toc-concurrency-and-mutex) flag) [1]: https://docs.bazel.build/versions/master/skylark/lib/repository_ctx.html#download ### Current state in rules_nodejs npm_install and yarn_install repository rules always install all the dependencies listed into a single external workspace. Actions can then depend on individual packages and their transitive dependencies. This means that the action graph sees O(1000) files for a package like `react-scripts` which has many dependencies, causing long action setup times for sandboxing and remote execution. ## Proposal Proof of concept is at https://github.com/alexeagle/rules_nodejs/tree/npm_install Summary: - new repository rule `npm_fetch_tarballs` - given a `package-lock.json`, download all tarballs to a single external repository, add to an npm cache, and mirror the dependency graph to BUILD files. - new rule `npm_tarball` - has no associated actions. Provides `NpmTarballInfo` which represents one tarball, its package name, and versioned dependencies. - new rule `npm_install_tarballs` - given a list of deps that provide `NpmTarballInfo`, run a single `npm install` command that runs purely offline and produces a `TreeArtifact` called `node_modules`. Also provides `ExternalNpmPackageInfo` - modify existing rules to account for TreeArtifact - Bazel (RBE protocol) doesn't permit a labelled file within a TreeArtifact. So `nodejs_binary`, `npm_package_bin` and others (?) will need new string-typed attributes indicating the entry_point Open questions: - Can we avoid downloading all tarballs, and just download those needed for the build? - We could use `http_file` but it only accepts SHA-256 and package-lock.json doesn't give us that. - We could make the user translate their package-lock.json into a `*.bzl` file like rules_go does. - If we download all tarballs, but then only install the ones needed, does that give the performance boost we're looking for? - How long does download typically take? - How long does install typically take? ## Details ### Lockfile version support Let's start by only allowing package-json.lock files with lockfileVersion=2 which is created by npm 7. We can later try adding support for other lockfile formats. We need two implementations of lockfile reading. The first is in starlark, which is pretty trivial - we just need to parse the "version", "resolved", and "integrity" fields. We can use the new json module in Bazel 4.0, so this feature requires that upgrade. The lockfile can permit this structure to be nested, and starlark doesn't allow recursion. In practice we can just walk a fixed number of levels down the tree using nested loops. The second implementation is in JavaScript where we translate the dependency graph into BUILD files. Ideally we'd use the @npmcli/arborist library to read lockfiles which would let us abstract away the file format. However we would need to vendor that library and its dependencies to avoid the chicken-and-egg of needing to fetch dependencies in the implementation of our dependency fetcher. I tried using rollup/terser to make a self-contained JS file but the result was still 1.3MB which is larger than our whole "built-in" release bundle so it seems too heavy. ### Naming the tarballs We get to choose a name for the files we put on the disk. The "resolved" field contains something like `https://registry.npmjs.org/@istanbuljs/schema/-/schema-0.1.2.tgz` - we cannot just take the `basename` since the package scope is needed for namespacing. We can do something like yarn does for their cache ``` % ls /Users/alex.eagle/Library/Caches/Yarn/v6 | grep rollup npm-@rollup-plugin-commonjs-14.0.0-4285f9ec2db686a31129e5a2b415c94aa1f836f0-integrity ``` Whatever we choose here needs to be written by the starlark download code and then read by the JS code. ### Getting Bazel to understand the package-lock.json integrity hashes I already made a [fix upstream](https://github.com/bazelbuild/bazel/pull/12777) in Bazel to understand SHA-1 which was the only missing hash. This is included in Bazel 4.1 so users must ensure they're on that version (the `.bazelversion` file should be used for this purpose) ### Populating an npm cache Npm can run in an offline mode, which ensures that the `npm_install_tarballs` rule is hermetic and doesn't try to reach out on the network. To make this work, we need to create a cache folder that matches npm semantics. By placing `ENV[npm_config_cache] = <path to external repo>` we can make the npm tool find the cache living in the external repo, which makes good semantics since Bazel will clean that folder at the right time. One way to do this is by running `npm cache add xx.tgz`, with one subprocess for each tarball. However in the rules_nodejs repo as an example, this takes many minutes to complete. To speed this up, we probably want a batched mode to add many tarballs to the cache. We could try to get such a thing upstream, but it will mean users need an even later version of NPM. Another option seems to be to peel back one layer. `npm cache add` just calls through to the `pacote` library here https://github.com/npm/cli/blob/6141de7b9e866979bf51706fd1be10c54235cf08/lib/cache.js#L97-L103 This introduces the possibility of version skew, though. We'd have to ensure that we vendor a version of pacote that creates a cache which works with the version of npm consuming it. ### Bazel extracting the tarballs Sadly this doesn't work because the stripPrefix isn't constant across all npm packages. Most have a top-level directory "package" but some don't, like `@types/rimraf@2.0.3`: `Prefix "package" was given, but not found in the archive. Here are possible prefixes for this archive: "rimraf".` ### Code sharing We already have the `internal/npm_install/generate_build_file.ts` program, which has a lot in common with the build file generation needed for this design. It also writes syntax sugar files, such as `index.bzl` files under packages that have `bin` entries in their `package.json`. So we probably want to write new tests around that script and augment it to run under two modes, either "produce js_library" (the existing mode), or "produce npm_tarball" (the new mode).

    Import from clipboard

    Paste your markdown or webpage here...

    Advanced permission required

    Your current role can only read. Ask the system administrator to acquire write and comment permission.

    This team is disabled

    Sorry, this team is disabled. You can't edit this note.

    This note is locked

    Sorry, only owner can edit this note.

    Reach the limit

    Sorry, you've reached the max length this note can be.
    Please reduce the content or divide it to more notes, thank you!

    Import from Gist

    Import from Snippet

    or

    Export to Snippet

    Are you sure?

    Do you really want to delete this note?
    All users will lose their connection.

    Create a note from template

    Create a note from template

    Oops...
    This template has been removed or transferred.
    Upgrade
    All
    • All
    • Team
    No template.

    Create a template

    Upgrade

    Delete template

    Do you really want to delete this template?
    Turn this template into a regular note and keep its content, versions, and comments.

    This page need refresh

    You have an incompatible client version.
    Refresh to update.
    New version available!
    See releases notes here
    Refresh to enjoy new features.
    Your user state has changed.
    Refresh to load new user state.

    Sign in

    Forgot password

    or

    By clicking below, you agree to our terms of service.

    Sign in via Facebook Sign in via Twitter Sign in via GitHub Sign in via Dropbox Sign in with Wallet
    Wallet ( )
    Connect another wallet

    New to HackMD? Sign up

    Help

    • English
    • 中文
    • Français
    • Deutsch
    • 日本語
    • Español
    • Català
    • Ελληνικά
    • Português
    • italiano
    • Türkçe
    • Русский
    • Nederlands
    • hrvatski jezik
    • język polski
    • Українська
    • हिन्दी
    • svenska
    • Esperanto
    • dansk

    Documents

    Help & Tutorial

    How to use Book mode

    Slide Example

    API Docs

    Edit in VSCode

    Install browser extension

    Contacts

    Feedback

    Discord

    Send us email

    Resources

    Releases

    Pricing

    Blog

    Policy

    Terms

    Privacy

    Cheatsheet

    Syntax Example Reference
    # Header Header 基本排版
    - Unordered List
    • Unordered List
    1. Ordered List
    1. Ordered List
    - [ ] Todo List
    • Todo List
    > Blockquote
    Blockquote
    **Bold font** Bold font
    *Italics font* Italics font
    ~~Strikethrough~~ Strikethrough
    19^th^ 19th
    H~2~O H2O
    ++Inserted text++ Inserted text
    ==Marked text== Marked text
    [link text](https:// "title") Link
    ![image alt](https:// "title") Image
    `Code` Code 在筆記中貼入程式碼
    ```javascript
    var i = 0;
    ```
    var i = 0;
    :smile: :smile: Emoji list
    {%youtube youtube_id %} Externals
    $L^aT_eX$ LaTeX
    :::info
    This is a alert area.
    :::

    This is a alert area.

    Versions and GitHub Sync
    Get Full History Access

    • Edit version name
    • Delete

    revision author avatar     named on  

    More Less

    Note content is identical to the latest version.
    Compare
      Choose a version
      No search result
      Version not found
    Sign in to link this note to GitHub
    Learn more
    This note is not linked with GitHub
     

    Feedback

    Submission failed, please try again

    Thanks for your support.

    On a scale of 0-10, how likely is it that you would recommend HackMD to your friends, family or business associates?

    Please give us some advice and help us improve HackMD.

     

    Thanks for your feedback

    Remove version name

    Do you want to remove this version name and description?

    Transfer ownership

    Transfer to
      Warning: is a public team. If you transfer note to this team, everyone on the web can find and read this note.

        Link with GitHub

        Please authorize HackMD on GitHub
        • Please sign in to GitHub and install the HackMD app on your GitHub repo.
        • HackMD links with GitHub through a GitHub App. You can choose which repo to install our App.
        Learn more  Sign in to GitHub

        Push the note to GitHub Push to GitHub Pull a file from GitHub

          Authorize again
         

        Choose which file to push to

        Select repo
        Refresh Authorize more repos
        Select branch
        Select file
        Select branch
        Choose version(s) to push
        • Save a new version and push
        • Choose from existing versions
        Include title and tags
        Available push count

        Pull from GitHub

         
        File from GitHub
        File from HackMD

        GitHub Link Settings

        File linked

        Linked by
        File path
        Last synced branch
        Available push count

        Danger Zone

        Unlink
        You will no longer receive notification when GitHub file changes after unlink.

        Syncing

        Push failed

        Push successfully