aws-cdk icon indicating copy to clipboard operation
aws-cdk copied to clipboard

CDK NPM/Typescript Conflicts

Open sublimemm opened this issue 7 years ago • 12 comments

#434 :question: General Issue

The Question

What is the recommend pattern for managing cdk dependencies? We keep running into issues with many teams wanting to wrap common cdk patterns into reusable node modules. For example team A has created a node module which houses a sub class of CDK's Stack class, which will automatically add tags required by our company. Everyone in our company has to have these tags and so everyone will want to use the node module instead of copy/paste or writing their own.

The problem comes when now you have the CDK specifying it's own dependencies, the node module specifying their dependencies and the end user's project with their own.

The CDK is spitting out new versions constantly (I'm not complaining... keep fixing those bugs :)). So if the node module team locks their versions at a specific minor version, e.g. cdk-core 1.9.0. This is fine, but A) they're out of date intermediately and B) there are dozens of CDK dependencies which will not be limited to 1.9.0. The end users might have cdk-lambda and cdk-gateway at say ^1.10.0. Now typescript freaks out, and the cdk is at conflicting versions of the 900 cdk dependencies... it's quite the mess.

We're really unsure how to specify these things so it works for all parties involved. Any ideas, suggestions, etc would be much appreciated.

Environment

  • CDK CLI Version: Any
  • Module Version: Any
  • OS: Any
  • Language: TypeScript

Other information

sublimemm avatar Oct 08 '19 14:10 sublimemm

I think there are two problems here. First, semver isn't being followed properly, because there are breaking changes on the minor versions. I don't know why so many package maintainers view semver compliance as optional, when it is an intrinsic requirement of npm. Since the whole project is somewhat beta, it should still be in 0.x.y versions, and that way minor increments are treated as breaking.

The related problem is that despite poor compliance with semver, each of the @aws-cdk/* packages specify their dependencies with a ^ constraint rather than a ~ constraint. I think what this means is that when you install say v1.11.0 of a package, its dependencies are listed as ^1.11.0 which will choose the highest matching version (currently v1.12.0) by default. That packages dependencies are now specified as ^1.12.0 which leads to the duplicate versions of packages. This might be yarn-specific, I'm not sure on the differences between yarn and npm on version resolution.

What this means is that when a CDK component is broken (e.g. #4404), it is a total pain to downgrade.

gordonmleigh avatar Oct 09 '19 09:10 gordonmleigh

@gordonmleigh Definitely seeing the same problem with ^ in npm. We tried to lock down the version after 1.5.0 (breaking changes to vpc sub-net resolution). After a solid day of lost productivity, one of our developers managed eventually to lock the versions by specifying every single CDK dependency (even pieces of the CDK we weren't using directly) at a fixed 1.5.0 to get around the multiple version issue.

This has left us in an untenable situation because people want to use higher minor version upgrades, e.g., the entire purpose of the caret ^ but we've been forced to lock everything down or risk our builds breaking the next time AWS puts out a new version.

I feel like this would be solved if the cdk would specify in cdk-core that it depended on all cdk dependencies with a ~ constraint. This would force the cdk itself to have a consistent version across all dependencies and consumers could use cdk-core with a ^ to be able to pull in minor/patch updates. Or even fixed versions, since they're not honoring semver, even ~ is dangerous.

sublimemm avatar Oct 09 '19 12:10 sublimemm

Ok it seems I was a little too quick to judge (sorry). Having looked into it a little deeper, problems in my case are stemming from the fact that Typescript considers private members to be part of the shape of a class and two versions of a class definition (i.e. from two different versions of the same package) cannot be considered equivalent with equal private members.

A good fix that probably requires a lot of effort from the team would be to use interface types instead of classes, but that would require creating interfaces for pretty much every class. FWIW I think classes were an odd choice anyway, because they have side-effects. Constructors with side-effects is pretty much considered an anti-pattern these days, and any time you see a new Class() standing as a statement on its own you should be asking what on earth is going on.

Anyway, to solve the problem, you want to tell yarn to use the highest possible version of each package in order to deduplicate. To do this, keep the version constraints as ^ and use yarn-deduplicate.

I think the equivalent for npm is npm dedupe.

gordonmleigh avatar Oct 16 '19 10:10 gordonmleigh

Wouldnt those tools force you into the highest version? I'm not sure this a good solution either...

sublimemm avatar Oct 17 '19 19:10 sublimemm

Yes but assuming that semver has actually been followed (i.e. I was wrong originally) this isn’t a problem because minor versions are always backward compatible

gordonmleigh avatar Oct 17 '19 20:10 gordonmleigh

Semver isn't actually being followed, as evidenced by the "BREAKING" notes on minor releases.

This issue bites me every time you release a new version. This really isn't sustainable, it's making the project unusable. I don't want to have to be forced to upgrade at some unpredictable time every time you release a new version.

Edit: it is bonkers that you've categorised "don't break peoples builds every time we release a new version" as a feature request.

gordonmleigh avatar Nov 21 '19 14:11 gordonmleigh

@gordonmleigh can you provide a list of issues you encountered? Please bear in mind that we do not follow semver for modules or components that are marked “experimental”, similar to the node.js SDK practices.

eladb avatar Nov 21 '19 14:11 eladb

@eladb always something along the lines of:

TS2345 - Argument of type 'this' is not assignable to parameter of type 'Construct'.
  Type 'Api' is not assignable to type 'Construct'.
    Types of property 'node' are incompatible.
      Type 'import("/Users/folder/packages/api-lambda/node_modules/@aws-cdk/core/lib/construct").ConstructNode' is not assignable to type 'import("/Users/folder/node_modules/@aws-cdk/core/lib/construct").ConstructNode'.
        Types have separate declarations of a private property 'host'.

src/cdk.ts: (29:29)
27: 
28:     // create table
29:     const db = new ddb.Table(this, 'CallbackTable', {
                                 ^

"Experimental" libraries should be version 0.x.y, according to the Semver spec. Minor upgrades are considered breaking by NPM package resolution in this case, meaning ^0.5.0 will only match 0.5.x. CDK is already version 1.x, which means you have effectively declared it stable whether you meant to or not. NPM module versions have to follow Semver, it isn't suitable for package maintainers to suit themselves. If you really must continue to do this, please change all package cross references to a ~ constraint which will mean that minor versions are considered incompatible by NPM.

gordonmleigh avatar Nov 21 '19 14:11 gordonmleigh

A good fix that probably requires a lot of effort from the team would be to use interface types instead of classes,

Actually you can derive the "public" interface from an existing class without rewriting it completely by using Omit

class Vendor {
  public name: string;

  private hide: string | null = null
  constructor(name: string) {
    this.name = name;
  }

  greet() {
    return "Hello, welcome to " + this.name;
  }
}

type VendorPublicShape = Omit<Vendor, 'hide'> 

// Compiles ok
const test: VendorPublicShape = {
    name: 'foo',
    greet() {
        return 'bar'
    }
}

Probably some decorator (or TS AST tranformation) could pick all private member and exclude them from the interface.

Here's a better trick to achieve the same:

https://github.com/microsoft/TypeScript/issues/471#issuecomment-381842426

inakianduaga avatar Dec 19 '19 17:12 inakianduaga

I think we can close this issue, since we have removed caret dependencies from our NPM packages in favor of exact dependencies.

skinny85 avatar Feb 06 '20 23:02 skinny85

sorry, since what version of the cdk you have removed caret dependencies ?

vitodsk avatar Feb 13 '20 12:02 vitodsk

Since 1.13.0: https://github.com/aws/aws-cdk/commit/1d1b8bc22ae9f68f70cb00f1a69d02b5658a3832

skinny85 avatar Feb 13 '20 16:02 skinny85