nix-community / yarn2nix

Generate nix expressions from a yarn.lock file [maintainer=???]
GNU General Public License v3.0
123 stars 61 forks source link

yarn2nix workspace circular dependency #143

Open dmjio opened 4 years ago

dmjio commented 4 years ago

When attempting to build facebook docusaurus, an infinite loop causes the build to hang (and eventually the machine to freeze). Confirmed the build completes successfully when running yarn standalone.

let
  pkgs = import <nixpkgs> {};
  docsrc =
    pkgs.fetchFromGitHub {
      owner = "facebook";
      repo = "docusaurus";
      rev = "368eb4279ff6a1f26d134a2349e2ebd8d9c35644";
      sha256 = "0aqwpxla9mqk1wf6i9yahqj9ymnn953isxk1j4z4zdik9pa4ykwv";
    };

  yarnLockFixed =
      pkgs.stdenv.mkDerivation {
        name = "yarn-lock-fixed";
        src = docsrc;
        buildCommand = ''
          mkdir $out && cp ${docsrc}/yarn.lock $out
          sed -i 's/caniuse-lite-1.0.30001047.tgz/caniuse-lite-1.0.30001047.tgz#bqm3qw12vfgd5gcr6jnl2jpgsm4ncpfv/g' $out/yarn.lock
        '';
      };
  yarn2nix = with pkgs; import (pkgs.fetchFromGitHub {
    owner = "moretea";
    repo = "yarn2nix";
    rev = "9e7279edde2a4e0f5ec04c53f5cd64440a27a1ae";
    sha256 = "0zz2lrwn3y3rb8gzaiwxgz02dvy3s552zc70zvfqc0zh5dhydgn7";
  }) {
    inherit pkgs yarn nodejs;
  };
  gen-docusaurus = name:
    yarn2nix.mkYarnPackage {
      name = "${name}";
      src = "${docsrc}/packages/${name}";
      packageJSON = "${docsrc}/packages/${name}/package.json";
      yarnLock = "${yarnLockFixed}/yarn.lock";
    };
  docusaurus-classic =
    yarn2nix.mkYarnPackage {
      name = "docusaurus-2-classic-template";
      src = "${docsrc}/packages/docusaurus-init/templates/classic";
      packageJSON = "${docsrc}/packages/docusaurus-init/templates/classic/package.json";
      yarnLock = "${yarnLockFixed}/yarn.lock";
    };
  docusaurus-bootstrap =
    yarn2nix.mkYarnPackage {
      name = "docusaurus-2-bootstrap-template";
      src = "${docsrc}/packages/docusaurus-init/templates/bootstrap";
      packageJSON = "${docsrc}/packages/docusaurus-init/templates/bootstrap/package.json";
      yarnLock = "${yarnLockFixed}/yarn.lock";
    };
  website =
    yarn2nix.mkYarnPackage {
      name = "docusaurus-2-website";
      src = "${docsrc}/packages/website";
      packageJSON = "${docsrc}/website/package.json";
      yarnLock = "${yarnLockFixed}/yarn.lock";
    };
  website-1x =
    yarn2nix.mkYarnPackage {
      name = "docusaurus-1-website";
      src = "${docsrc}/packages/website-1.x";
      packageJSON = "${docsrc}/website-1.x/package.json";
      yarnLock = "${yarnLockFixed}/yarn.lock";
    };

  obj = { name = "docu"; version = "2.0.0-alpha.54"; };
  text = builtins.toJSON (obj // builtins.fromJSON (builtins.readFile "${docsrc}/package.json"));
  packageJSON = pkgs.writeTextFile {
    name = "package.json";
    inherit text;
  };
  docu =
    yarn2nix.mkYarnPackage rec {
      src = docsrc;
      name = "docu";
      inherit packageJSON;
      yarnLock = "${yarnLockFixed}/yarn.lock";
      postBuild = ''
        yarn run build --offline
      '';
      distPhase = ''
        rm -f .yarnrc
        cd $out/libexec/${name}/deps/${name}
        mkdir -p $out/tarballs/
        cp ${packageJSON} package.json
        yarn pack --offline --ignore-scripts --filename $out/tarballs/${name}.tgz
      '';
      workspaceDependencies =
        [ (gen-docusaurus "docusaurus-utils")
          (gen-docusaurus "docusaurus-module-type-aliases")
          (gen-docusaurus "docusaurus-types")
          (gen-docusaurus "docusaurus-init")
          # (gen-docusaurus "docusaurus")
          # (gen-docusaurus "docusaurus-1.x")
          # (gen-docusaurus "docusaurus-init-1.x")
          # (gen-docusaurus "docusaurus-mdx-loader")
          # (gen-docusaurus "docusaurus-plugin-content-blog")
          # (gen-docusaurus "docusaurus-plugin-content-docs")
          # (gen-docusaurus "docusaurus-plugin-content-pages")
          # (gen-docusaurus "docusaurus-plugin-google-analytics")
          # (gen-docusaurus "docusaurus-plugin-google-gtag")
          # (gen-docusaurus "docusaurus-plugin-ideal-image")
          # (gen-docusaurus "docusaurus-plugin-sitemap")
          # (gen-docusaurus "docusaurus-preset-bootstrap")
          # (gen-docusaurus "docusaurus-preset-classic")
          # (gen-docusaurus "docusaurus-theme-bootstrap")
          # (gen-docusaurus "docusaurus-theme-classic")
          # (gen-docusaurus "docusaurus-theme-live-codeblock")
          # (gen-docusaurus "docusaurus-theme-search-algolia")
          # (gen-docusaurus "lqip-loader")
          # (gen-docusaurus "stylelint-copyright")

        ];
    };
in
  docu

result

these derivations will be built:
  /nix/store/qjd7qkjmd8wi8r1j9n9ni62d1dg5k0rr-docu.drv
building '/nix/store/qjd7qkjmd8wi8r1j9n9ni62d1dg5k0rr-docu.drv'...
unpacking sources
unpacking source archive /nix/store/swlfp5fc08xlwjpxpnvwxraxbymyr9qv-source
source root is source
patching sources
configuring
building
no Makefile, doing nothing
yarn run v1.22.4
warning Skipping preferred cache folder "/homeless-shelter/Library/Caches/Yarn" because it is not writable.
warning Selected the next writable cache folder in the list, will be "/private/var/folders/tz/gmnrpnw56vl1dwynrkzc78p40000gn/T/nix-build-docu.drv-3/.yarn-cache-501".
$ yarn tsc && yarn build:v2 --offline
warning Cannot find a suitable global folder. Tried these: "/usr/local, /homeless-shelter/.yarn"
warning Skipping preferred cache folder "/homeless-shelter/Library/Caches/Yarn" because it is not writable.
warning Selected the next writable cache folder in the list, will be "/private/var/folders/tz/gmnrpnw56vl1dwynrkzc78p40000gn/T/nix-build-docu.drv-3/.yarn-cache-501".
$ lerna run tsc --no-private
warning Cannot find a suitable global folder. Tried these: "/usr/local, /homeless-shelter/.yarn"
info cli using local version of lerna
lerna notice cli v3.20.2
lerna info Executing command in 8 packages: "yarn run tsc"