question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

Tutorial: Monorepo with typescript, how to benefit from tsbuildcache info

See original GitHub issue

We are using github actinos to run our tests, all works fine, except it takes ages to compile everything. Therefore we were looking into caching artifacts and quickly implemented actions/cache (y). But then it turned out, even though files are pre-compiled, nothing has changed and the buildcache info file is valid, typecript still recompiles everything. As it turned out, github checkout (even with fetch-detph: 0) is not restoring last modified time of a file. Therefore typescript always assumes that the buildcache is invalid, even if you just compiled in in a job before that.

Therefore I like to write down how we have solved this: 1.) you need to ensure last modified time is the same 2.) ensure that .tsbuildinfo and build artifacts are included in the cache 3.) ensure that changed files are rebuild, even the buildinfo cache is still “valid”.

What we did: 1.) detect what has changed (we need this later on)

- uses: dorny/paths-filter@v2
        id: filter
        with:
          list-files: 'json'
          filters: |
            packages: // <-- specify all your monorepo locations (usually you probabaly just have one)
              - 'packages/**'
            infrastructure:
              - 'infrastructure/**'
            services:
              - 'services/**'

2.) restore modified time of files via git-restore-mtime

      - name: Restore mtime for git checkout
        run: sudo apt install git-restore-mtime && sudo curl -o /usr/lib/git-core/git-restore-mtime https://raw.githubusercontent.com/MestreLion/git-tools/v2020.09/git-restore-mtime && git restore-mtime

3.) set up cache (include node_modules, build artifacts and tsbuildinfo!)

- name: Setup cache for build and dependencies
        uses: actions/cache@v2
        id: cache
        with:
          path: |
            node_modules
            */*/node_modules
            */*/dist
            */*/.tsbuildinfo
            ~/.npm
          key: ....

4.) ensure buildcache is always in a valid state, therefore remove tsbuildinfo files from directories that have changed.

  - name: Remove buildcache infos
        uses: hokify/remove-buildcache-action@v1
        with:
          changed-files: ${{ steps.filter.outputs.packages_files }}
          tsBuildInfoFile: '.tsbuildinfo'

Hope this helps, it’s just a very plain description, but I was looking for this for ages, and couldn’t find anything.

Issue Analytics

  • State:closed
  • Created 3 years ago
  • Reactions:1
  • Comments:10

github_iconTop GitHub Comments

1reaction
simllllcommented, Mar 24, 2021

our cache key is this:

 with:
          path: |
            node_modules
            */*/node_modules
            */*/dist
            */*/buildcache.tsbuildinfo
            ~/.npm
          key: ${{ runner.os }}-${{ hashFiles('cache_root_version') }}-node-build-${{ hashFiles('packages/*/src/**/*') }}-${{ hashFiles('package-lock.json') }}
          restore-keys: |
            ${{ runner.os }}-${{ hashFiles('cache_root_version') }}-node-build-${{ hashFiles('packages/*/src/**/*') }}-
            ${{ runner.os }}-${{ hashFiles('cache_root_version') }}-node-build-

we also have a monorepo with composite mode, should work with this already 😃

0reactions
phil-lgrcommented, Mar 24, 2021

@simllll thanks for sharing

BTW it’s crazy that we can’t reuse steps on github action see https://github.com/actions/runner/issues/646

TL;DR you can create composite actions, but uses isn’t supported right now, it sucks because we have to copy the steps for each job 😕

Read more comments on GitHub >

github_iconTop Results From Across the Web

The Ultimate Guide to TypeScript Monorepos
I've written a couple of posts about how to set up JavaScript and TypeScript Monorepos over the past three years (#1, #2, #3,...
Read more >
Setting up a monorepo with Lerna for a TypeScript project
You know the benefits of keeping your codebase simple to maintain. Learn how to setup a monorepo with Lerna and configure it for...
Read more >
How to set up a TypeScript monorepo and make Go ... - Medium
This article explains how to make code navigation e.g. Go to definition in an IDE work with a TypeScript monorepo, managed by Lerna...
Read more >
An actual complete guide to typescript monorepos - Rahul Tarak
This guide is really optimized towards typescript monorepos that also contain packages that can be deployed but really should work for any ...
Read more >
The Ultimate Guide to TypeScript Monorepos - Code of Joy
I've written a couple of posts about how to set up JavaScript and TypeScript Monorepos over the past three years (#1, #2, #3,...
Read more >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found