NixOS / nixpkgs

Nix Packages collection & NixOS
MIT License
16.67k stars 13.12k forks source link

aws-cdk: Can not initialize project #236151

Open dschrempf opened 1 year ago

dschrempf commented 1 year ago

Describe the bug

When initializing a project with aws-cdk, I get the following error:

nix run nixpkgs#nodePackages.aws-cdk -- init app --language typescript
Applying project template app for typescript

Cannot read properties of undefined (reading 'replace')

Expected behavior

The project is correctly initialized.

Additional context

I can initialize the project using pkgs.nodejs and npx aws-cdk init app --language typescript, which is a bit weird.

Notify maintainers

No maintainers are listed.

Metadata

[user@system:~]$ nix-shell -p nix-info --run "nix-info -m"
 - system: `"x86_64-linux"`
 - host os: `Linux 6.1.31, NixOS, 23.11 (Tapir), 23.11.20230605.7409480`
 - multi-user?: `yes`
 - sandbox: `yes`
 - version: `nix-env (Nix) 2.16.0`
 - nixpkgs: `/nix/store/my1b33f1dm7srg8ngcmk769ahkslgkaz-source`

Thanks!

EDIT: When running the failing command with --verbose, I get the following stack trace:

[22:56:19] Determining if we're on an EC2 instance.
[22:56:19] Does not look like an EC2 instance.
[22:56:19] Reading cached notices from /home/dominik/.cdk/cache/notices.json
[22:56:19] Unable to determine AWS region from environment or AWS configuration (profile: "default"), defaulting to 'us-east-1'
[22:56:19] Toolkit stack: CDKToolkit
Applying project template app for typescript
[22:56:19] Reading cached notices from /home/dominik/.cdk/cache/notices.json

Cannot read properties of undefined (reading 'replace')
[22:56:20] TypeError: Cannot read properties of undefined (reading 'replace')
    at InitTemplate.expand (/nix/store/npl84qpcnya069k8389rd81l7jjsll53-aws-cdk-2.82.0/lib/node_modules/aws-cdk/lib/index.js:402:27303)
    at InitTemplate.installFiles (/nix/store/npl84qpcnya069k8389rd81l7jjsll53-aws-cdk-2.82.0/lib/node_modules/aws-cdk/lib/index.js:402:26436)
    at async InitTemplate.install (/nix/store/npl84qpcnya069k8389rd81l7jjsll53-aws-cdk-2.82.0/lib/node_modules/aws-cdk/lib/index.js:402:25683)
    at async initializeProject (/nix/store/npl84qpcnya069k8389rd81l7jjsll53-aws-cdk-2.82.0/lib/node_modules/aws-cdk/lib/index.js:402:21259)
    at async cliInit (/nix/store/npl84qpcnya069k8389rd81l7jjsll53-aws-cdk-2.82.0/lib/node_modules/aws-cdk/lib/index.js:402:19478)
    at async exec4 (/nix/store/npl84qpcnya069k8389rd81l7jjsll53-aws-cdk-2.82.0/lib/node_modules/aws-cdk/lib/index.js:455:51970)
rohanshukla94 commented 1 year ago

Facing similar issue on Nixos 23.05 and Nodejs 18.16.3

The CDK package available on nixpkgs is 2.80

Affected CDK CLI Versions on Node 18+ are cli: >=2.80.0 <2.83.1

Fix: Fix is to use cli version 2.87 which isn't available it seems

Or use node js 16

buckley310 commented 1 year ago

Using nodejs 16 does not resolve the issue on my end. Using npx as mentioned above does work around the issue.

buckley310 commented 1 year ago

ref: https://github.com/NixOS/nixpkgs/pull/242263 aws-cdk is now at 2.87, and it does not solve the issue.

rohanshukla94 commented 9 months ago

Facing similar issue

simon-andrews commented 9 months ago

I am experiencing this same issue on macOS.

$ cdk --version
2.99.1 (build b2a895e)
$ nix-info -m
 - system: `"x86_64-darwin"`
 - host os: `Darwin 22.6.0, macOS 10.16`
 - multi-user?: `yes`
 - sandbox: `no`
 - version: `nix-env (Nix) 2.17.0`
 - channels(root): `"home-manager, nixpkgs"`
 - nixpkgs: `/nix/var/nix/profiles/per-user/root/channels/nixpkgs`
schlarpc commented 7 months ago

This is caused by this line in aws-cdk, which I believe is attempting to introspect on its package manifest: https://github.com/aws/aws-cdk/blob/b865320674f97bd7dd7cfcc69235d3521e6aa0d4/packages/aws-cdk/lib/init.ts#L163

When running as packaged in nixpkgs, this manifest variable doesn't contain anything in devDependencies:

{
  name: 'aws-cdk',
  description: 'CDK Toolkit, the command line tool for CDK apps',
  version: '2.90.0',
  // snip
  devDependencies: {},
}

But it is defined in the package.json inside the src fetched in node-packages.nix:

{
  "name": "aws-cdk",
  "description": "CDK Toolkit, the command line tool for CDK apps",
  "version": "2.105.0",
  // snip
  "devDependencies": {
    "constructs": "^10.0.0",
    // snip
  },
}

I'm guessing this might be because aws-cdk is defined in node-packages.nix with production = true;, so devDependencies get omitted?

  aws-cdk = nodeEnv.buildNodePackage {
    name = "aws-cdk";
    packageName = "aws-cdk";
    version = "2.105.0";
    # snip
    production = true;
  };

Attempting to override this with nodePackages.aws-cdk.override { production = false; } gets me an ENOTCACHED error which I assume means the dev dependencies aren't captured in the Nix derivation:

npm ERR! code ENOTCACHEDidealTree buildDeps
npm ERR! request to https://registry.npmjs.org/@aws-cdk%2fcdk-build-tools failed: cache mode is 'only-if-cached' but no cached response is available

That's about all the patience I have for digging into this tonight.

createchange commented 6 months ago

Adding my voice as someone who would like to see this fixed. CDK has heavy utilization at workplace.

AleXoundOS commented 5 months ago

Somewhat related: https://github.com/svanderburg/node2nix/issues/149.

AleXoundOS commented 5 months ago

@schlarpc, reset of devDependencies seems to happen here: https://github.com/NixOS/nixpkgs/blob/fa15b53dbea5028db38d6e09b4cef6eba42aeebb/pkgs/development/node-packages/node-env.nix#L169.

AleXoundOS commented 5 months ago

Here comes another issue: https://github.com/aws/aws-cdk/blob/1fd548588274a83810a55090dcc3a3a152c5116d/packages/aws-cdk/lib/init.ts#L149. Templates for cdk init are copied from /nix/store with read-only permissions.

AleXoundOS commented 5 months ago

I wrote a very dirty workaround for these issues. I return devDependencies array back into package.json and cdk gets satisfied it seems. flake.nix:

{
  description = "AWS CDK";
  inputs.nixpkgs.url = github:nixos/nixpkgs;
  outputs = { self, nixpkgs }:
    let
      system = "x86_64-linux";
      pkgs = nixpkgs.legacyPackages.${system};
    in
    {
      devShells.${system}.default = pkgs.mkShell {
        packages = [
          self.packages.${system}.default
          (pkgs.python3.withPackages (ps: [ ps.pip ])) # if you use CDK in python
          pkgs.nodejs
        ];
      };
      packages.${system}.default = pkgs.nodePackages.aws-cdk.overrideAttrs (_: {
        preRebuild = ''
          substituteInPlace lib/index.js \
          --replace 'await fs27.copy(fromFile,toFile)' 'await fs27.copy(fromFile, toFile); await fs27.chmod(toFile, 0o644);'
          tar --to-stdout -xf $src package/package.json \
          | ${pkgs.jq}/bin/jq '{"devDependencies"}' > /build/devDependencies.json
        '';
        postInstall = ''
          FIXED_PACKAGE_JSON="$(${pkgs.jq}/bin/jq -s '.[0] * .[1]' package.json /build/devDependencies.json)"
          printf "%s\n" "$FIXED_PACKAGE_JSON" > package.json
        '';
      });
    };
}

flake.lock:

{"nodes": {"nixpkgs": {"locked": {"lastModified": 1708396807, "narHash": "sha256-PRNwdCEknI5tqpdW7HrDzBd7986BDpwaw13C8NLHi9c=", "owner": "nixos", "repo": "nixpkgs", "rev": "a41cc628170e0c3d42f250c7a8be4d0d41a78c82", "type": "github"}, "original": {"owner": "nixos", "repo": "nixpkgs", "type": "github"}}, "root": {"inputs": {"nixpkgs": "nixpkgs"}}}, "root": "root", "version": 7}

I do not understand how to properly patch source code of this TypeScript package. It is already compiled into index.js when fetched from NPM. However, index.js contents are altered somehow during the buildNodePackage and fs29.copy becomes fs27.copy.

AndrewCloete commented 2 weeks ago

Workaround if you are in a hurry is to use npx. This does defeat the purpose of using nix though

npx aws-cdk init app --language python