yarnpnp2nix icon indicating copy to clipboard operation
yarnpnp2nix copied to clipboard

Executing yarn scripts as part of `build` fails

Open niklaskorz opened this issue 1 year ago • 4 comments

When using yarn inside a build script, e.g.

"testb@workspace:packages/testb" = {
  build = ''
    yarn build # defined in package.json as `node build.js`
  '';
};

The following error occurs:

Internal Error: testb@workspace:.: This package doesn't seem to be present in your lockfile; run "yarn install" to update the lockfile

niklaskorz avatar Mar 16 '23 15:03 niklaskorz

I have already tried copying ./yarn.lock to the build environment, i.e.

build = ''
  cp ${workspace/yarn.lock} ../../yarn.lock
  yarn build
'';

but this doesn't seem to change anything

niklaskorz avatar Mar 16 '23 15:03 niklaskorz

Yes this is unsupported for now, but you can run node build.js or whatever your build script is. Any CLI bins provided by dependencies or dev dependencies will be available in the PATH too, so you can run e.g tsc or esbuild

madjam002 avatar Mar 16 '23 16:03 madjam002

Thanks for the information, I am trying to get https://github.com/prisma/prisma/ to work inside yarnpnp2nix. To be precise, prisma generate requires special handling and only works with yarn pnpify. I have tried to work around this by directly adding --require @yarnpkg/pnpify but unfortunately it looks like this also depends on the same behavior. The error message looks a bit different (Assertion failed: Expected the package to have been registered), but it seems to also result from the missing lockfile.

niklaskorz avatar Mar 17 '23 09:03 niklaskorz

I have solved this differently now, I'm generating the prisma client in a separate derivation without yarn / yarnpnp2nix:

prisma-client = pkgs.stdenv.mkDerivation {
  name = "prisma-client";
  buildInputs = with pkgs; [ nodejs nodePackages.prisma ];
  src = pkgs.nix-filter {
    root = packages/prisma;
    include = [
      packages/prisma/prisma/schema.prisma
    ];
  };
  buildPhase = with pkgs; ''
    npm install --cache .npm-cache @prisma/client
    prisma generate
  '';
  installPhase = ''
    mv generated $out
  '';
};

and in the package importing the generated prisma client, I am symlinking the result to the expected location (build.cjs bundles it, otherwise I suppose a copy would be more appropriate):

yarnPackages = pkgs.mkYarnPackagesFromManifest {
  yarnManifest = import ./yarn-manifest.nix;
  packageOverrides = {
    "@alugha/prisma@workspace:packages/prisma" = {
      build = with pkgs; ''
        ln -s ${prisma-client} generated
        node build.cjs
      '';
    };
  };
};

niklaskorz avatar Mar 17 '23 10:03 niklaskorz