nix-community / dream2nix

Simplified nix packaging for various programming language ecosystems [maintainer=@DavHau]
https://dream2nix.dev
MIT License
976 stars 122 forks source link

typescript: defined tsconfig.json paths not consistent with reality #158

Open nrdxp opened 2 years ago

nrdxp commented 2 years ago

I have a wip package effort going on here: https://github.com/input-output-hk/cardano-js-sdk/tree/dn2-wip

After working out several other issues during the build, I've reached a point where I keep getting strange type errors during the typescript compiler run. After digging into the node_modules folder, and looking through the various tsconfig.json of various dependencies, I've noticed that they have various path definitions point to non existent locations (inside the source derivation), meaning the compiler cannot find all the type definitions, meaning ultimately that the build fails.

To reproduce one of the errors I'm seeing run: nix build .\#@cardano-sdk/core -L from that branch I linked above.

As a concrete example of a bad path, you can enter the failed build's source derivation and cd into node_modules/@bcoe/v8-coverage/tsconfig.json and see it has the following defintion:

    "typeRoots": [
      "src/lib/custom-typings",
      "node_modules/@types"
    ]

But it's local node_modules folder is completely empty

tgunnoe commented 2 years ago

The error I get is:

src/Cardano/types/TxSubmissionErrors.ts:10:14 - error TS2742: The inferred type of 'TxSubmissionErrors' cannot be named without a reference to '@cardano-sdk/core/node_modules/@cardano-ogmios/schema'. This is likely not portable. A type annotation is necessary.

For what its worth, I had the same problem using hardhat with dream2nix. This was a month or two back for me, so I don't remember precisely. But I added a blank import for the listed reference.

here was my solution: https://github.com/tgunnoe/hardhat-flake/blob/master/flake.nix#L33-L54

I got your package to build with the following updated flake:

  {
    inputs = {
      dream2nix.url = "github:nix-community/dream2nix";
    };

    outputs = {
      self,
      dream2nix,
      nixpkgs,
    } @ inp: let
      inherit (nixpkgs.legacyPackages.x86_64-linux) pkgs;
      inherit (pkgs) lib;

      dream2nix = inp.dream2nix.lib2.init {
        systems = ["x86_64-linux"];
        config.projectRoot = ./.;
      };
    in
      (dream2nix.makeFlakeOutputs {
        source = ./.;
        settings = [
          {subsystemInfo.noDev = false;}
        ];
        packageOverrides =
          builtins.mapAttrs (
            n: _:
              if lib.hasPrefix "@" n
              then {
                add-inputs.buildInputs = old: old ++ [self.packages.x86_64-linux.cardano-sdk.dependencies.type  script];
                add-type-notation = {
                  prePatch = ''
                    echo -e "import _ from \"@cardano-sdk/core/node_modules/@cardano-ogmios/schema\";\n$(cat s  rc/Cardano/types/TxSubmissionErrors.ts)" > src/Cardano/types/TxSubmissionErrors.ts
                  '';
                };
                copy-modules.installMethod = "copy";
                copy-tsconfig.preBuild = ''
                  cp ${./tsconfig.json} $nodeModules/tsconfig.json
                  chmod +w -R node_modules
                  ${pkgs.fd}/bin/fd tsconfig.tsbuildinfo node_modules -x ${pkgs.gnused}/bin/sed -i 's|../../..  /node_modules|../../..|g'
                '';
              }
              else {}
          )
          self.packages.x86_64-linux;

        inject = {
        };

        sourceOverrides = _: {
        };
      })
      // {
        devShell.x86_64-linux = pkgs.mkShell {
          nativeBuildInputs = with pkgs.nodejs-14_x.pkgs; [yarn self.packages.x86_64-linux.cardano-sdk.depende  ncies.typescript pkgs.nodejs-14_x];
        };
      };
  }

I realize this is a work around and there's a missing issue to be addressed in dream2nix.

tgunnoe commented 2 years ago

I think it has something to do with typescript or npm expecting only local references, and it receives references from outside of its local node_modules.

Also, you don't need to add typescript as a buildInput to the packageOverrides if typescript is already defines in the devDependencies of package.json, because dream2nix builds with them in scope.

FYI, I am porting a very similar ecosystem to nix using dream2nix and have mostly completed the prodedure. Working on runtimes and integration tests for it at the moment: https://github.com/bobanetwork/boba/blob/nix/flake.nix

So feel free to message me on matrix!