Git Product home page Git Product logo

wireit's Introduction

wireit

Wireit upgrades your npm scripts to make them smarter and more efficient.

Published on npm Build Status Discord

Features

  • πŸ™‚ Use the npm run commands you already know
  • ⛓️ Automatically run dependencies between npm scripts in parallel
  • πŸ‘€ Watch any script and continuously re-run on changes
  • πŸ₯¬ Skip scripts that are already fresh
  • ♻️ Cache output locally and remotely on GitHub Actions for free
  • πŸ› οΈ Works with single packages, npm workspaces, and other monorepos
  • ✏️ VSCode plugin gives suggestions, documentation, and warnings as you develop

Contents

Install

npm i -D wireit

Setup

Wireit works with npm run, it doesn't replace it. To configure an NPM script for Wireit, move the command into a new wireit section of your package.json, and replace the original script with the wireit command.

Before After
{
  "scripts": {
    "build": "tsc"
  }
}
{
  "scripts": {
    "build": "wireit"
  },
  "wireit": {
    "build": {
      "command": "tsc"
    }
  }
}

Now when you run npm run build, Wireit upgrades the script to be smarter and more efficient. Wireit works with yarn (both 1.X "Classic" and its successor "Berry") and pnpm, too.

You should also add .wireit to your .gitignore file. Wireit uses the .wireit directory to store caches and other data for your scripts.

echo .wireit >> .gitignore

VSCode Extension

If you use VSCode, consider installing the google.wireit extension. It adds documentation on hover, autocomplete, can diagnose a number of common mistakes, and even suggest a refactoring to convert an npm script to use wireit.

Install it from the marketplace or on the command line like:

code --install-extension google.wireit

Discord

Join the Wireit Discord to chat with the Wireit community and get support for your project.

Discord

Dependencies

To declare a dependency between two scripts, edit the wireit.<script>.dependencies list:

{
  "scripts": {
    "build": "wireit",
    "bundle": "wireit"
  },
  "wireit": {
    "build": {
      "command": "tsc"
    },
    "bundle": {
      "command": "rollup -c",
      "dependencies": ["build"]
    }
  }
}

Now when you run npm run bundle, the build script will automatically run first.

Vanilla scripts

The scripts you depend on don't need to be configured for Wireit, they can be vanilla npm scripts. This lets you only use Wireit for some of your scripts, or to upgrade incrementally. Scripts that haven't been configured for Wireit are always safe to use as dependencies; they just won't be fully optimized.

Wireit-only scripts

It is valid to define a script in the wireit section that is not in the scripts section, but such scripts can only be used as dependencies from other wireit scripts, and can never be run directly.

Cross-package dependencies

Dependencies can refer to scripts in other npm packages by using a relative path with the syntax <relative-path>:<script-name>. All cross-package dependencies should start with a ".". Cross-package dependencies work well for npm workspaces, as well as in other kinds of monorepos.

{
  "scripts": {
    "build": "wireit"
  },
  "wireit": {
    "build": {
      "command": "tsc",
      "dependencies": ["../other-package:build"]
    }
  }
}

Parallelism

Wireit will run scripts in parallel whenever it is safe to do so according to the dependency graph.

For example, in this diagram, the B and C scripts will run in parallel, while the A script won't start until both B and C finish.

graph TD
  A-->B;
  A-->C;
  subgraph parallel
    B;
    C;
  end

By default, Wireit will run up to 2 scripts in parallel for every logical CPU core detected on your system. To change this default, set the WIREIT_PARALLEL environment variable to a positive integer, or infinity to run without a limit. You may want to lower this number if you experience resource starvation in large builds. For example, to run only one script at a time:

export WIREIT_PARALLEL=1
npm run build

If two or more separate npm run commands are run for the same Wireit script simultaneously, then only one instance will be allowed to run at a time, while the others wait their turn. This prevents coordination problems that can result in incorrect output files being produced. If output is set to an empty array, then this restriction is removed.

Extra arguments

As with plain npm scripts, you can pass extra arguments to a Wireit script by placing a -- double-dash argument in front of them. Any arguments after a -- are sent to the underlying command, instead of being interpreted as arguments to npm or Wireit:

npm run build -- --verbose

Input and output files

The files and output properties of wireit.<script> tell Wireit what your script's input and output files are, respectively. They should be arrays of glob patterns, where paths are interpreted relative to the package directory. They can be set on some, all, or none of your scripts.

Setting these properties allow you to use more features of Wireit:

Requires
files
Requires
output
Dependency graph - -
Watch mode β˜‘οΈ -
Clean build - β˜‘οΈ
Incremental build β˜‘οΈ β˜‘οΈ
Caching β˜‘οΈ β˜‘οΈ

Example configuration

{
  "scripts": {
    "build": "wireit",
    "bundle": "wireit"
  },
  "wireit": {
    "build": {
      "command": "tsc",
      "files": ["src/**/*.ts", "tsconfig.json"],
      "output": ["lib/**"]
    },
    "bundle": {
      "command": "rollup -c",
      "dependencies": ["build"],
      "files": ["rollup.config.json"],
      "output": ["dist/bundle.js"]
    }
  }
}

Default excluded paths

By default, the following folders are excluded from the files and output arrays:

  • .git/
  • .hg/
  • .svn/
  • .wireit/
  • .yarn/
  • CVS/
  • node_modules/

In the highly unusual case that you need to reference a file in one of those folders, set allowUsuallyExcludedPaths: true to remove all default excludes.

Incremental build

Wireit can automatically skip execution of a script if nothing has changed that would cause it to produce different output since the last time it ran. This is called incremental build.

To enable incremental build, configure the input and output files for each script by specifying glob patterns in the wireit.<script>.files and wireit.<script>.output arrays.

ℹ️ If a script doesn't have a files or output list defined at all, then it will always run, because Wireit doesn't know which files to check for changes. To tell Wireit it is safe to skip execution of a script that definitely has no input and/or files, set files and/or output to an empty array (files: [], output: []).

Caching

If a script has previously succeeded with the same configuration and input files, then Wireit can copy the output from a cache, instead of running the command. This can significantly improve build and test time.

To enable caching for a script, ensure you have defined both the files and output arrays.

ℹ️ If a script doesn't produce any output files, it can still be cached by setting output to an empty array ("output": []). Empty output is common for tests, and is useful because it allows you to skip running tests if they previously passed with the exact same inputs.

Local caching

In local mode, Wireit caches output files to the .wireit folder inside each of your packages.

Local caching is enabled by default, unless the CI=true environment variable is detected. To force local caching, set WIREIT_CACHE=local. To disable local caching, set WIREIT_CACHE=none.

⚠️ Wireit does not currently limit the size of local caches. To free up this space, use rm -rf .wireit/*/cache. Automatic cache size limits will be added in an upcoming release, tracked at wireit#71.

GitHub Actions caching

In GitHub Actions mode, Wireit caches output files to the GitHub Actions cache service. This service is available whenever running in GitHub Actions, and is free for all GitHub users.

ℹ️ GitHub Actions cache entries are automatically deleted after 7 days, or if total usage exceeds 10 GB (the least recently used cache entry is deleted first). See the GitHub Actions documentation for more details.

To enable caching on GitHub Actions, add the following uses clause to your workflow. It can appear anywhere before the first npm run or npm test command:

- uses: google/wireit@setup-github-actions-caching/v1

Example workflow

# File: .github/workflows/tests.yml

name: Tests
on: [push, pull_request]
jobs:
  tests:
    runs-on: ubuntu-latest
    steps:
      - uses: actions/checkout@v3
      - uses: actions/setup-node@v3
        with:
          node-version: 18
          cache: npm

      # Set up GitHub Actions caching for Wireit.
      - uses: google/wireit@setup-github-actions-caching/v1

      # Install npm dependencies.
      - run: npm ci

      # Run tests. Wireit will automatically use
      # the GitHub Actions cache whenever possible.
      - run: npm test

Cleaning output

Wireit can automatically delete output files from previous runs before executing a script. This is helpful for ensuring that every build is clean and free from outdated files created in previous runs from source files that have since been removed.

Cleaning is enabled by default as long as the output array is defined. To change this behavior, set the wireit.<script>.clean property to one of these values:

Setting Description
true Clean before every run (the default).
"if-file-deleted" Clean only if an input file has been deleted since the last run.

Use this option for tools that have incremental build support, but do not clean up outdated output when a source file has been deleted, such as tsc --build (see TypeScript for more on this example.)
false Do not clean.

Only use this option if you are certain that the script command itself already takes care of removing outdated files from previous runs.

Watch mode

In watch mode, Wireit monitors all files of a script, and all files of its transitive dependencies, and when there is a change, it re-runs only the affected scripts. To enable watch mode, ensure that the files array is defined, and add the --watch flag:

npm run <script> --watch

The benefit of Wireit's watch mode over built-in watch modes are:

  • Wireit watches the entire dependency graph, so a single watch command replaces many built-in ones.
  • It prevents problems that can occur when running many separate watch commands simultaneously, such as build steps being triggered before all preceding steps have finished.

Environment variables

Use the env setting to either directly set environment variables, or to indicate that an externally-defined environment variable affects the behavior of a script.

Setting environment variables directly

If a property value in the env object is a string, then that environment variable will be set to that value when the script's command runs, overriding any value from the parent process.

Unlike built-in shell environment variable syntaxes, using env to set environment variables works the same in macOS/Linux vs Windows, and in all shells.

Note Setting an environment variable with env does not apply transitively through dependencies. If you need the same environment variable to be set for multiple scripts, you must configure it for each of them.

{
  "wireit": {
    "my-script": {
      "command": "my-command",
      "env": {
        "MY_VARIABLE": "my value"
      }
    }
  }
}

Indicating external environment variables

If an environment variable affects the behavior of a script but is set externally (i.e. it is passed to the wireit parent process), set the env property to {"external": true}. This tells Wireit that if the value of an environment variable changes across executions of a script, then its output should not be re-used. You may also set a default value for the variable to use when none is provided externally.

{
  "wireit": {
    "my-script": {
      "command": "my-command",
      "env": {
        "MY_VARIABLE": {
          "external": true
        },
        "MY_VARIABLE_2": {
          "external": true,
          "default": "foo"
        }
      }
    }
  }
}

Services

By default, Wireit assumes that your scripts will eventually exit by themselves. This is well suited for build and test scripts, but not for long-running processes like servers. To tell Wireit that a process is long-running and not expected to exit by itself, set "service": true.

{
  "scripts": {
    "start": "wireit",
    "build:server": "wireit"
  },
  "wireit": {
    "start": {
      "command": "node my-server.js",
      "service": true,
      "files": ["my-server.js"],
      "dependencies": [
        "build:server",
        {
          "script": "../assets:build",
          "cascade": false
        }
      ]
    },
    "build:server": {
      ...
    }
  }
}

Service lifetime

If a service is run directly (e.g. npm run serve), then it will stay running until the user kills Wireit (e.g. Ctrl-C).

If a service is a dependency of one or more other scripts, then it will start up before any depending script runs, and will shut down after all depending scripts finish.

Service readiness

By default, a service is considered ready as soon as its process spawns, allowing any scripts that depend on that service to start.

However, often times a service needs to perform certain actions before it is safe for dependents to interact with it, such as starting a server and listening on a network interface.

Use service.readyWhen.lineMatches to tell Wireit to monitor the stdout and stderr of the service and defer readiness until a line is printed that matches the given regular expression.

{
  "command": "node my-server.js",
  "service": {
    "readyWhen": {
      "lineMatches": "Server listening on port \\d+"
    }
  }
}

Service restarts

In watch mode, a service will be restarted whenever one of its input files or dependencies change, except for dependencies with cascade set to false.

Service output

Services cannot have output files, because there is no way for Wireit to know when a service has finished writing its output.

If you have a service that produces output, you should define a non-service script that depends on it, and which exits when the service's output is complete.

Execution cascade

By default, a script always needs to run (or restart in the case of services) if any of its dependencies needed to run, regardless of whether the dependency produced new or relevant output.

This automatic cascade of script execution is the default behavior because it ensures that any possible output produced by a dependent script propagates to all other scripts that might depend on it. In other words, Wireit does not assume that the files array completely describes the inputs to a script with dependencies.

Disabling cascade

This execution cascade behavior can be disabled by expanding a dependency into an object, and setting the cascade property to false:

Note What really happens under the hood is that the cascade property simply controls whether the fingerprint of a script includes the fingerprints of its dependencies, which in turn determines whether a script needs to run or restart.

{
  "dependencies": [
    {
      "script": "foo",
      "cascade": false
    }
  ]
}

Reasons to disable cascade

There are two main reasons you might want to set cascade to false:

  1. Your script only consumes a subset of a dependency's output.

    For example, tsc produces both .js files and .d.ts files, but only the .js files might be consumed by rollup. There is no need to re-bundle when a typings-only changed occurred.

    Note In addition to setting cascade to false, the subset of output that does matter (lib/**/*.js) has been added to the files array.

    {
      "scripts": {
        "build": "wireit",
        "bundle": "wireit"
      },
      "wireit": {
        "build": {
          "command": "tsc",
          "files": ["src/**/*.ts", "tsconfig.json"],
          "output": ["lib/**"]
        },
        "bundle": {
          "command": "rollup -c",
          "dependencies": [
            {
              "script": "build",
              "cascade": false
            }
          ],
          "files": ["rollup.config.json", "lib/**/*.js"],
          "output": ["dist/bundle.js"]
        }
      }
    }
  2. Your server doesn't need to restart for certain changes.

    For example, a web server depends on some static assets, but the server reads those assets from disk dynamically on each request. In watch mode, there is no need to restart the server when the assets change.

    Note The build:server dependency uses the default cascade behavior (true), because changing the implementation of the server itself does require the server to be restarted.

    {
      "scripts": {
        "start": "wireit",
        "build:server": "wireit"
      },
      "wireit": {
        "start": {
          "command": "node lib/server.js",
          "service": true,
          "dependencies": [
            "build:server",
            {
              "script": "../assets:build",
              "cascade": false
            }
          ],
          "files": ["lib/**/*.js"]
        },
        "build:server": {
          "command": "tsc",
          "files": ["src/**/*.ts", "tsconfig.json"],
          "output": ["lib/**"]
        }
      }
    }

Failures and errors

By default, when a script fails (meaning it returned with a non-zero exit code), all scripts that are already running are allowed to finish, but new scripts are not started.

In some situations a different behavior may be better suited. There are 2 additional modes, which you can set with the WIREIT_FAILURES environment variable. Note that Wireit always ultimately exits with a non-zero exit code if there was a failure, regardless of the mode.

Continue

When a failure occurs in continue mode, running scripts continue, and new scripts are started as long as the failure did not affect their dependencies. This mode is useful if you want a complete picture of which scripts are succeeding and which are failing.

WIREIT_FAILURES=continue

Kill

When a failure occurs in kill mode, running scripts are immediately killed, and new scripts are not started. This mode is useful if you want to be notified as soon as possible about any failures.

WIREIT_FAILURES=kill

Package locks

By default, Wireit automatically treats package manager lock files as input files (package-lock.json for npm, yarn.lock for yarn, and pnpm-lock.yaml for pnpm). Wireit will look for these lock files in the script's package, and all parent directories.

This is useful because installing or upgrading your dependencies can affect the behavior of your scripts, so it's important to re-run them whenever your dependencies change.

To change the name of the package lock file Wireit should look for, specify it in the wireit.<script>.packageLocks array. You can specify multiple filenames here, if needed.

{
  "scripts": {
    "build": "wireit"
  },
  "wireit": {
    "build": {
      "command": "tsc",
      "files": ["src/**/*.ts", "tsconfig.json"],
      "output": ["lib/**"],
      "packageLocks": ["another-package-manager.lock"]
    }
  }
}

If you're sure that a script isn't affected by dependencies at all, you can turn off this behavior entirely to improve your cache hit rate by setting wireit.<script>.packageLocks to [].

Recipes

This section contains advice about integrating specific build tools with Wireit.

TypeScript

{
  "scripts": {
    "ts": "wireit"
  },
  "wireit": {
    "ts": {
      "command": "tsc --build --pretty",
      "clean": "if-file-deleted",
      "files": ["src/**/*.ts", "tsconfig.json"],
      "output": ["lib/**", ".tsbuildinfo"]
    }
  }
}
  • Set "incremental": true and use --build to enable incremental compilation, which significantly improves performance.
  • Include .tsbuildinfo in output so that it is reset on clean builds. Otherwise tsc will get out of sync and produce incorrect output.
  • Set "clean": "if-file-deleted" so that you get fast incremental compilation when sources are changed/added, but also stale outputs are cleaned up when a source is deleted (tsc does not clean up stale outputs by itself).
  • Include tsconfig.json in files so that changing your configuration re-runs tsc.
  • Use --pretty to get colorful output despite not being attached to a TTY.

ESLint

{
  "scripts": {
    "lint": "wireit"
  },
  "wireit": {
    "lint": {
      "command": "eslint --color --cache --cache-location .eslintcache .",
      "files": ["src/**/*.ts", ".eslintignore", ".eslintrc.cjs"],
      "output": []
    }
  }
}
  • Use --cache so that eslint only lints the files that were added or changed since the last run, which significantly improves performance.
  • Use --color to get colorful output despite not being attached to a TTY.
  • Include config and ignore files in files so that changing your configuration re-runs eslint.

Reference

Configuration

The following properties can be set inside wireit.<script> objects in package.json files:

Property Type Default Description
command string undefined The shell command to run.
dependencies string[] | object[] [] Scripts that must run before this one.
dependencies[i].script string undefined The name of the script, when the dependency is an object..
dependencies[i].cascade boolean true Whether this dependency always causes this script to re-execute.
files string[] undefined Input file glob patterns, used to determine the fingerprint.
output string[] undefined Output file glob patterns, used for caching and cleaning.
clean boolean | "if-file-deleted" true Delete output files before running.
env Record<string, string | object> false Environment variables to set when running this command, or that are external and affect the behavior.
env[i].external true | undefined undefined true if an environment variable is set externally and affects the script's behavior.
env[i].default string | undefined undefined Default value to use when an external environment variable is not provided.
service boolean false Whether this script is long-running, e.g. a server.
packageLocks string[] ['package-lock.json'] Names of package lock files.

Dependency syntax

The following syntaxes can be used in the wireit.<script>.dependencies array:

Example Description
foo Script named "foo" in the same package.
../foo:bar Script named "bar" in the package found at ../foo (details).

Environment variable reference

The following environment variables affect the behavior of Wireit:

Variable Description
WIREIT_FAILURES How to handle script failures.

Options:
  • no-new (default): Allow running scripts to finish, but don't start new ones.
  • continue: Allow running scripts to continue, and start new ones unless any of their dependencies failed.
  • kill: Immediately kill running scripts, and don't start new ones.
WIREIT_PARALLEL Maximum number of scripts to run at one time.

Defaults to 2Γ—logical CPU cores.

Must be a positive integer or infinity.
WIREIT_CACHE Caching mode.

Defaults to local unless CI is true, in which case defaults to none.

Automatically set to github by the google/wireit@setup-github-actions-caching/v1 action.

Options:
  • local: Cache to local disk.
  • github: Cache to GitHub Actions.
  • none: Disable caching.
CI Affects the default value of WIREIT_CACHE.

Automatically set to true by GitHub Actions and most other CI (continuous integration) services.

Must be exactly true. If unset or any other value, interpreted as false.
WIREIT_MAX_OPEN_FILES Limits the number of file descriptors Wireit will have open concurrently. Prevents resource exhaustion when checking large numbers of cached files. Set to a lower number if you hit file descriptor limits.
WIREIT_LOGGER How to present progress and results on the command line.

Options:
  • quiet (default): writes a single dynamically updating line summarizing progress. Only passes along stdout and stderr from commands if there's a failure, or if the command is a service. The planned new default, please try it out.
  • simple (default): A verbose logger that presents clear information about the work that Wireit is doing.
  • metrics: Like simple, but also presents a summary table of results once a command is finished.
  • quiet-ci (default when env.CI or !stdout.isTTY): like quiet but optimized for non-interactive environments, like GitHub Actions runners.

Glob patterns

The following glob syntaxes are supported in the files and output arrays:

Example Description
foo The file named foo, or if foo is a directory, all recursive children of foo.
foo/*.js All files directly in the foo/ directory which end in .js.
foo/**/*.js All files in the foo/ directory, and all recursive subdirectories, which end in .js.
foo.{html,js} Files named foo.html or foo.js.
!foo Exclude the file or directory foo from previous matches.

Also note these details:

  • Paths should always use / (forward-slash) delimiters, even on Windows.
  • Paths are interpreted relative to the current package even if there is a leading / (e.g. /foo is the same as foo).
  • Whenever a directory is matched, all recursive children of that directory are included.
  • files are allowed to reach outside of the current package using e.g. ../foo. output files cannot reference files outside of the current package.
  • Symlinks in input files are followed, so that they are identified by their content.
  • Symlinks in output files are cached as symlinks, so that restoring from cache doesn't create unnecessary copies.
  • The order of !exclude patterns is significant.
  • Hidden/dot files are matched by * and **.
  • Patterns are case-sensitive (if supported by the filesystem).

Fingerprint

The following inputs determine the fingerprint for a script. This value is used to determine whether a script can be skipped for incremental build, and whether its output can be restored from cache.

  • The command setting.
  • The extra arguments set on the command-line.
  • The clean setting.
  • The output glob patterns.
  • The SHA256 content hashes of all files matching files.
  • The SHA256 content hashes of all files matching packageLocks in the current package and all parent directories.
  • The environment variable values configured in env.
  • The system platform (e.g. linux, win32).
  • The system CPU architecture (e.g. x64).
  • The system Node version (e.g. 20.11.1).
  • The fingerprint of all transitive dependencies, unless cascade is set to false.

When using GitHub Actions caching, the following input also affects the fingerprint:

  • The ImageOS environment variable (e.g. ubuntu20, macos11).

Requirements

Wireit is supported on Linux, macOS, and Windows.

Wireit is supported on Node Current (21), Active LTS (20), and Maintenance LTS (18). See Node releases for the schedule.

Wireit is supported on the npm versions that ship with the latest versions of the above supported Node versions (6 and 8), Yarn Classic (1), Yarn Berry (3), and pnpm (7).

Related tools

Wireit shares a number of features with these other great tools, and we highly recommend you check them out too:

Here are some things you might especially like about Wireit:

  • Feels like npm. When you use Wireit, you'll continue typing the same npm commands you already use, like npm run build and npm test. There are no new command-line tools to learn, and there's only one way to run each script. Your script config stays in your package.json, too. Wireit is designed to be the minimal addition to npm needed to get script dependencies and incremental build.

  • Caching with GitHub Actions. Wireit supports caching build artifacts and test results directly through GitHub Actions, without any extra third-party services. Just add a single uses: line to your workflows.

  • Watch any script. Want to automatically re-run your build and tests whenever you make a change? Type npm test --watch. Any script you've configured using Wireit can be watched by typing --watch after it.

  • Great for single packages and monorepos. Wireit has no opinion about how your packages are arranged. It works great with single packages, because you can link together scripts within the same package. It also works great with any kind of monorepo, because you can link together scripts across different packages using relative paths.

  • Complements npm workspaces. We think Wireit could be the missing tool that unlocks the potential for npm workspaces to become the best way to set up monorepos. To use Wireit with npm workspaces, you'll just use standard npm workspace commands like npm run build -ws.

  • Adopt incrementally. Wireit scripts can depend on plain npm scripts, so they can be freely mixed. This means you can use Wireit only for the parts of your build that need it most, or you can try it out on a script-by-script basis without changing too much at the same time.

Contributing

See CONTRIBUTING.md

wireit's People

Contributors

43081j avatar aomarks avatar appsforartists avatar augustjk avatar dependabot[bot] avatar e111077 avatar eltociear avatar jonkoops avatar joshkraft avatar martinnabhan avatar obliviousharmony avatar peschee avatar rictic avatar rivajunior avatar rudy3333 avatar

Stargazers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar

Watchers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar

wireit's Issues

JSON schema for our package.json fields

It looks like a VS Code plugin can contribute json schema files: https://code.visualstudio.com/api/references/contribution-points#contributes.jsonValidation

And those json schema files may be able to augment existing schemas?

https://github.com/runem/lit-analyzer/blob/fc6f2d99b7a61368f21d174dc13bb54c36ca50d2/packages/vscode-lit-plugin/package.json#L632
https://github.com/runem/lit-analyzer/blob/fc6f2d99b7a61368f21d174dc13bb54c36ca50d2/packages/vscode-lit-plugin/schemas/tsconfig.schema.json

I wonder if there's a way to do this without requiring the extension, by having the wireit command automatically add a $schema field to the wireit section

Glob negations don't always work properly in output field

The negation in these glob patterns don't work:

{
  "output": [
    "output",
    "!output/excluded"
  ]
}

Because fast-glob gives us the directory called output, and then excludes any file called output/excluded from the result. However, since it already gave us the output directory, and we do recursive deletes and copies, the exclusion ends up having no effect.

Here's a test case that currently fails, which can go in clean.test.ts:

test(
  'glob negations apply to directory match contents',
  timeout(async ({rig}) => {
    const cmdA = await rig.newCommand();
    await rig.write({
      'package.json': {
        scripts: {
          a: 'wireit',
        },
        wireit: {
          a: {
            command: cmdA.command,
            output: ['output', '!output/excluded'],
          },
        },
      },
      'output/included': '',
      'output/excluded': '',
    });

    const exec = rig.exec('npm run a');
    const inv = await cmdA.nextInvocation();

    assert.not(await rig.exists('output/included'));
    assert.ok(await rig.exists('output/excluded'));

    inv.exit(0);
    const res = await exec.exit;
    assert.equal(res.code, 0);
    assert.equal(cmdA.numInvocations, 1);
  })
);

One solution that doesn't work is to only include files in our glob results, and have the user rewrite the above as:

{
  "output": [
    "output/**",
    "!output/excluded"
  ]
}

but the problem with that is that we then would never be able to delete empty directories.

So I think we actually need to post-process our glob results, detect when a hit is a directory, explicitly recursively expand that directory, and then re-apply the negations?

A way to shorten dependency lists: wildcards or auto-dependencies

I think a lot of monorepos will have scripts that depend on basically all subpackages. Take lit.dev's build script:

  "wireit": {
    "build": {
      "dependencies": [
        "./packages/lit-dev-api:build",
        "./packages/lit-dev-content:build",
        "./packages/lit-dev-server:build",
        "./packages/lit-dev-tests:build",
        "./packages/lit-dev-tools-cjs:build",
        "./packages/lit-dev-tools-esm:build"
      ]
    },

This list is prone to getting out of sync as packages are added. That might be caught quickly when the project doesn't build correctly, but the list is still cumbersome.

Lerna and npm solve this by allowing you to run a script by name in any package that has that script. I could see wireit supporting something similar in a few ways:

  1. Wildcards:
  "wireit": {
    "build": {
      "dependencies": [
        "./packages/**/*:build",
      ]
    },
  1. Wildcard with optional script name
  "wireit": {
    "build": {
      "dependencies": [
        "./packages/**/*", // Uses "build" automatically
      ]
    },
  1. Auto-deps based on "workspaces" field
    This would read the "workspaces" field as a single source of truth for the list of all subpackages and run the build script in each:
  "wireit": {
    "build": {
      "dependencies": "auto"
    },

This version of the idea would really only be useful on the top-level package. To work in workspaces automatically would require some magic around looking at which dependencies are local to the monorepo and running scripts in them. Maybe that's doable.

Since packages would still need their scripts and dependencies specified, even if auto, it seems like this wouldn't require wireit to topo-sort the packages.

binary lookup fails in cross-package dependencies

In hooking up wireit in the lit-analyzer repo, one of the sub-repos – vscode-plugin – depends on vsce, which exposes the vsce command, and one of its scripts package-for-test uses that command.

If I run npm run package-for-test from packages/vscode-plugin then when wireit runs the command, vsce is found.

If I run a script that depends on ./packages/vscode-plugin:package-for-test from the root of the repo, vsce is not found.

Log output:

$ npm run just-package

> [email protected] just-package
> wireit

βœ… [packages/lit-analyzer:build] Already fresh
βœ… [packages/ts-lit-plugin:build] Already fresh
βœ… [packages/vscode-lit-plugin:build] Already fresh
πŸƒ [packages/vscode-lit-plugin:package-for-test] Running command "vsce package -o ./out/packaged.vsix && rm -rf ../../../packaged-extension/ && mkdir ../../../packaged-extension/ && unzip -qq ./out/packaged.vsix -d ../../../packaged-extension/"
/bin/sh: vsce: command not found
❌ [packages/vscode-lit-plugin:package-for-test] Failed with exit status 127

Detect overlapping output

Two scripts should probably not be able to set their output to patterns that could overlap. Especially because when clean is enabled (the default), one script could clobber the output of an other by mistake.

Q: Do all wireit scripts need to also be npm scripts

Let's say I organize one logical script, like build, into several sub-steps, like build:ts and build:graph. Do I need to make the sub-steps into npm script if they're never intended to be called from npm run or as a dependency of another wireit script?

ie, do I need build:ts and build:graphql in scripts?:

  "scripts": {
    "build": "wireit",
    "build:ts": "wireit",
    "build:graphql": "wireit"
  },
  "wireit": {
    "build": {
      "dependencies": [
        "build:ts"
      ]
    },
    "build:ts" {
      "dependencies": [
        "build:graphql"
      ]
    },
    "build:graphql" { ... },
  }

Add ways to automatically set (or reuse) input files

For tsc we generally already have inputs specified in our tsconfig (though I'm not sure if this is complete: tsc may read files outside of the include glob without error if they're imported by path). It'd be nice to be able to reuse that. The same might be true of Rollup configs and other tools.

Could there be a way to read input files from tool-specific configs? Maybe this is best for a worker protocol, or may there's a config plug-in system and/or a built-in set of integrations, that know how to read inputs and outputs from common tools.

For tools whose files are specified on the command line, maybe there's a way to specify that once and place it into the command line with substitution.

Improvements around @actions/cache

Problem

The public API provided by the @actions/cache package doesn't exactly meet our needs, because it automatically uses the file paths that are included in the tarball as part of the cache entry version (see https://github.com/actions/toolkit/blob/7654d97eb6c4a3d564f036a2d4a783ae9105ec07/packages/cache/src/internal/cacheHttpClient.ts#L70), and implements globbing differently.

We want complete control over our cache key, instead of having it be generated automatically based on file paths -- and we want to be sure we are using identical globbing logic to the rest of Wireit.

Current solution

For this reason, we are currently reaching into the internal/ directory of @actions/cache to get more control. This is bad because those modules could change at any time, which is why we currently have a strict ("=") version pin in our package.json.

It's also why we currently have "skipLibCheck": false in our tsconfig.json, and why we have the file types/action-cache-contracts.d.ts -- because the file lib/internal/contracts.d.ts is missing from the published @actions/cache package.

The @actions/cache package is also our largest dependency by far. It's 22MB, and adds 63 transitive dependencies.

Options

  1. We could file an issue or send a PR that provides a way to directly specify the cache key in @actions/cache. This would solve the version pinning problem, the "skipLibCheck": false problem, and would allow us to remove types/action-cache-contracts.d.ts -- but we'd still have the large dependency.

  2. We could potentially move all of this logic into a separate package which is installed only by google/wireit@setup-github-actions-caching/v1 -- instead of the main package. The action would then spin up its own HTTP server, which we would talk to instead with a more minimal API (note that the server we spin up would have direct filesystem access, so it could make the tarballs). This would shrink the main wireit package's dependencies and filesize back down again (though the dependencies would still be installed -- just only in CI, instead of also locally), and would have the added benefit of not requiring us to expose the ACTIONS_CACHE_URL and ACTIONS_RUNTIME_TOKEN variables to all run steps (see actions/toolkit#1053).

  3. The logic we need from @actions/cache could be re-implemented from scratch in a minimal way, such that we could drop the dependency on @actions/cache all together. The main tricky part is the way it handles tarball generation across platforms (https://github.com/actions/toolkit/blob/7654d97eb6c4a3d564f036a2d4a783ae9105ec07/packages/cache/src/internal/tar.ts).

Detect output outside of package dir during analysis instead of execution

As a safety precaution, we refuse to clean output files if they are outside of the current script's package. However, it would be even better to catch this error earlier, in the Analyzer, by looking at the output glob patterns. We just need to be careful that we are parsing and analyzing the glob patterns correctly, accounting for negations, {} groups, and other special glob syntax.

Watch mode optimizations

  • No need to re-analyze or re-create watchers unless a package.json file has changed (and watchers only need to be re-created if a files array changed, and even then only the affected ones do).

  • We can share the CachingPackageJsonReader instance across analysis. File change events tell us when a cached result is stale.

  • We can cache glob results in memory. File change events tell us when a cached result is stale.

  • We can cache file hashes in memory. File change events tell us when a cached result is stale.

Can't run commands in npm workspaces

I'm getting an error when trying to run a basic command in a npm workspace.

my-project/package.json:

{
  "workspaces": [
    "packages/package-a",
  ]
}

my-project/packages/package-a/package.json:

{
  "name": "package-a",
  "scripts": {
    "foo": "wireit",
  },
  "wireit": {
    "foo": {
      "command": "echo FOO!",
    }
  }
}
my-project> npm run foo -w package-a

> [email protected] foo
> wireit

❌ [foo] No script named "foo" was found in /path/to/my-project
npm ERR! Lifecycle script `foo` failed with error: 
npm ERR! Error: command failed 
npm ERR!   in workspace: [email protected] 
npm ERR!   at location: /path/to/my-project/packages/package-a

This also happens if I cd into packages/package-a and run npm run foo

Parent/child delete errors on clean build

If we try to do a clean build, and output paths include a parent and a child, then an error can be thrown if we happen to delete the parent before the child.

Firstly, we should know that we don't need to delete the child directory in the first place, by using optimizeCopies (which we should rename). But we should also not throw if we try to delete a directory whose parent has already been deleted.

A way to specify options for all scripts in a package

Possibility:

{
  "*": {
    "dependencies": [
      "bootstrap"
    ],
    "packageLocks": [
      "yarn.lock"
    ]
  }
}

* is unlikely to collide with a real script name, but we should probably support an escaping scheme anyway, e.g. \* means the script literally called *.

Multiple iterations and flake detection

It could be useful to sometimes run a script multiple times. For example, to detect a flaky test, maybe something like this:

npm test -- --detect-failure-rate --iterations=10

This would run the script 10 times, and then report the % of times it failed at the end.

Globbing doesn't support re-inclusion

Given the following:

foo/**
!foo/bar
foo/bar/baz

The file foo/bar/baz should be included, even though foo/bar was excluded. It looks like fast-glob doesn't care about the order that ! negated patterns appear in. This is different to how .gitignore and the files array in package.json files work, so we should fix it.

Warn if we are cleaning files that are tracked by Git

If automatic output cleaning tries to delete a file that is tracked by Git, then that's a good indication that the output glob could be too broad.

This could be more expensive than we want, since it requires calling out to another process and would block every script's execution. Maybe something to put in a diagnose mode, rather than something we do on every build.

Unexpected errors should be reported with more context

We usually know what script we're running when an unexpected error occurs, so at minimum we couldwrap unexpected errors with the script context and include it when we display it.

There might also be additional context we could add to help narrow down where in the code the error occurred. Especially given the fact that fs errors in Node don't include stack traces (nodejs/node#30944).

Matching directory in files array does not include its contents

When using output, matching a directory implicitly includes all of the contents of that directory (though see #77 for a caveat with how that is currently broken with ! negations). That's because when we clean and cache, we use recursive operations like fs.rm and fs.cp.

However, with files, which is used for generating the cache key, we don't use recursive operations. We just read and hash the files that directly matched the glob.

files should be consistent with output. Matching a directory should implicitly match all of its contents. This is also consistent with the how the package.json files array, and .gitignore files work.

[vscode-extension] Extended validation

Additional diagnostics which aren't expressible as part of the JSON schema:

  • every wireit script needs to be present in the scripts section
  • every wireit script needs to just run "wireit" in the scripts section
  • every dependency needs to resolve to an npm script

Setting to turn off caching for a specific script

Probably "cache": false.

It seems like we might want to control it per-caching implementation too. You might not want local caching (e.g. because it's a really fast script with incremental build so you don't want it during development), but still want it when using GitHub Actions.

Service mode

By default, Wireit assumes that scripts eventually exit by themselves. This works well for things like building or testing. But sometimes a script runs indefinitely, such as a server.

Setting "server": true will tell Wireit that a script runs indefinitely. This has the following effects:

  • It will always run. It will never be skipped or restored from cache.

  • If something depends on the server, Wireit won't wait for the server to exit before the dependent script starts running. Instead, it just waits for the server process to be spawned.

  • If a server script is run directly (e.g. npm run serve), then it will stay running until the user kills Wireit (e.g. Ctrl-C).

  • If a server script is run indirectly (e.g. npm run script-that-depends-on-server), then the server script will stay running until all scripts which transitively depend on it have finished.

  • In watch mode, Wireit will restart the server whenever a dependency changes. If this isn't required for a particular dependency (such as for static assets that the server does not cache), the dependency edge can be annotated with "restart": false.

{
  "scripts": {
    "build": "wireit"
  },
  "wireit": {
    "serve": {
      "command": "node lib/server.js",
      "server": true,
      "dependencies": [
        "build:server",
        {
          "script": "build:assets",
          "restart": false
        }
      ],
      "files": [],
      "output": []
    }
  }
}

Dry run mode

Tells you what scripts would be executed in what order, which existing output files would be deleted, etc.

Option to clean output only when input file deleted

Problem

tsc, even in incremental mode, does not delete the output files corresponding to input files that have been deleted since its last build (see microsoft/TypeScript#30602 (comment)). For example:

  • Write foo.ts
  • Run tsc --build
    • Generates foo.js
  • Rename foo.ts to bar.ts
  • Run tsc --build
    • Generates bar.js, but foo.js still exists

Many build tools behave the same way.

Currently, if you have specified output, by default we delete all output files before running the script. This helps with the above problem, but makes it impossible to use efficient incremental modes, like tsc --build.

You can currently set clean: false to disable deleting before execution, however that means that stale outputs can still exist.

Proposal

Add a new on-delete option to clean which deletes output only if the unique set of matched input files for the script has changed since the last run.

This seems like it would provide a good balance between the two options we currently have; giving you incremental build every time a file is modified, or a new file is added -- but doing a clean build if a file is removed.

So the 3 options would now be:

  • true: Always delete before execution
  • false: Never delete before execution (but do still delete when restoring from cache)
  • "on-delete": Delete only if an input file changed

We could consider making "on-delete" the default, but true still seems like the safer default, because it's also very possible for a stale file to be left around due to a change in a config file (e.g. changing a rollup config to rename the bundle output file).

Example

{
  "build": {
    "command": "tsc --build",
    "files": [
      "src/**/*.ts"
    ],
    "output": [
      "lib/**",
      ".tsbuildinfo"
    ],
    "clean": "on-delete"
  }
}

Garbage collection for cache directory

The .wireit/<script>/cache directory currently can grow indefinitely. We should implement a garbage collection strategy to cap the size of this directory.

An LRU cache with a configurable maximum number of entries seems like what we want.

We will want to make sure we have an efficient way to maintain the cache hit rate data which scales well with the size of the cache. We will probably want some kind of on-disk index file that lets us read/write cache hit rates efficiently, to determine which cache entry needs to be deleted when the cap is hit. A doubly-linked-list implemented in the filesystem itself with symlinks (or just files containing SHAs) could also be an interesting way to do this.

Replay stdout/stderr

If a script is fresh or cached, we should replay the cached stdout/stderr. Help for e.g. test output, since it may not be clear that the tests are in a passing state when nothing is logged.

Pretend to be a TTY

Some tools produce different output when they detect that terminal is not a TTY (interactive). For example, TypeScript produces colorized output by default only in TTY mode. Otherwise the --pretty flag must be specified to force colorized output.

When a process is run via Wireit, the process will think it is not attached to a TTY, because we use the default pipe setting to handle stdio from spawn (https://nodejs.org/api/child_process.html#optionsstdio), so it isn't attached to a TTY directly. We could instead use inherit, but that would not allow us to capture the output, which we need for storing stdio replays.

A downside of this is that it does not match the standard behavior of npm run, which uses inherit. Matching the behavior of npm is one of our goals.

Using a library like https://github.com/microsoft/node-pty may be the only way to trick processes into thinking they are attached to a TTY, while also being able to capture the output for the replay files. We should do a little more research to confirm this, as there is a chance there is a simpler solution. This library is somewhat large, includes a native library, and has a different interface to spawn. Note we would only want to do this when we detect we are running in a TTY.

Concise way to depend on scripts in child and dependency workspaces

[1] Run <script> in all of my workspaces (parent β†’ child).

With Wireit, you can already just do npm run build -ws to run a given script in all workspaces, which is the standard npm workspaces approach. However it's not fully optimal, because npm doesn't parallelize. So we will also support a syntax like this:

{
  "name": "root",
  "scripts": {
    "build": "wireit"
  },
  "wireit": {
    "build": {
      "dependencies": [
        {
          "script": "build",
          "packages": "workspaces"
        }
      ]
    }
  },
  "workspaces": [
    "packages/foo",
    "packages/bar"
  ]
}

Which is equivalent to:

{
  "name": "root",
  "scripts": {
    "build": "wireit"
  },
  "wireit": {
    "build": {
      "dependencies": {
        [
          "./packages/foo:build",
          "./packages/bar:build"
        ]
      }
    }
  }
}

[2] Run <script> in all of my dependencies (child β†’ siblings).

Related, it is often useful to run some script in all of the current packages dependencies (where those dependencies are workspaces contained by the same workspace root).

{
  "name": "foo",
  "scripts": {
    "build": "wireit"
  },
  "wireit": {
    "build": {
      "dependencies": [
        {
          "script": "build",
          "packages": "dependencies"
        }
      ]
    }
  },
  "dependencies": {
    "bar",
    "baz"
  }
}

Which is equivalent to:

{
  "name": "foo",
  "scripts": {
    "build": "wireit"
  },
  "wireit": {
    "build": {
      "dependencies": [
        "../bar:build",
        "../baz:build"
      ]
    }
  }
}

Stdio replayer should preserve stdout/stderr sequence

Context: #68 (comment)

I think the ideal solution requires actually encoding the cross-stream sequence in some way. We could do it with a unified format that can encode the stream, or another idea I had was to have a 3rd file which encodes the sequences as offset/length pairs:

out 0 20
err 0 10
out 20 100

So the replayer would follow these sequences and do something like call fs.read(stdoutFileDescriptor, {offset:0, length: 20}). I think that should have better performance over parsing a big unified format. In the case where there is no mixed streams, we can just stream the file straight through as we do now. Also it's kind of nice right now that you get a file like .wireit/<script>/stdout that the user can do stuff with directly if they want.

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    πŸ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. πŸ“ŠπŸ“ˆπŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❀️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.