Skip to content

unsafe eval() and relationship to ncc instead of package.json bin field #945

Closed

Description

@LongLiveCHIEF

I noticed some eval's in the code as I was looking at the download.js files to experiment with some of the environment options I proposed in #938 (and eliminate the need for --unsafe-perm or anything that would be disallowed in an enterprise environment (such as a code analysis tool catching eval in a module and failing a build as a result.

My question is why are we using ncc instead of just the bin field in package.json? I'm not arguing against it, just want to make sure I understand the reasons it is there so I can be confident in any PR contributions.

Also, instead of using eval, did you happen to try returning the value instead like so:

// probably better to move this up here anyways
const path = require('path)
const binaryPath = function(){
  return path.join(__dirname, '../')
}

const version = (pkg && pkg.prisma && pkg.prisma.version) || 'latest'

download({
  binaries: {
    'query-engine': binaryPath(),
    'migration-engine': binaryPath(),
  },
  showProgress: true,
  version,
})

One of the reasons I'm asking about ncc is so I can understand the usage of the compiled binary during development of the cli itself. (what test would I perform if I wanted to test out replacing eval with returns?

Edit: forgot to remove the require in the return statement. Fixed that.

Activity

  1. LongLiveCHIEF commented on Nov 15, 2019

    @LongLiveCHIEF
    Author

    and yes, I did the see the issue referenced in the original commit, which is why i was wondering if the return approach had been tried before using eval.

  2. timsuchanek commented on Nov 16, 2019

    @timsuchanek
    Contributor

    Thanks @LongLiveCHIEF for asking this, that's indeed a good question I'm happy to answer.
    We use ncc in 3 places:

    1. For the prisma2 cli - We rely on a significant amount of packages and want them to be safely packaged together and not rely on yarn/npm resolving the correct dependencies. In Prisma 1, we used to have normal npm dependencies in our CLI, te installation time was 60-120s. Here, with Prisma 2 the installation time is now cut down to a fraction of that.
    2. We have a generator architecture, you can read more about it here https://github.com/prisma/specs/tree/master/generators
      Photon.js is one generator, but there could be many more. The idea is, that a generator is a standalone executable, which can be executed by the prisma2 cli.
      Again, in order to save pulling tons of dependencies in, we chose to have this as a ncc'ed build.
    3. The runtime for Photon is also ncc'ed. This comes from the decision, that Photon can be generated into any folder, e.g. ./my-photon. That folder should include all the dependencies needed, so that it can just be copied around in a CI system if needed.

    In regards to binaries, ncc has nothing to do with how binaries are fetched or used. The same mechanism of binary fetching could be used without ncc.

    In regards to eval statements: For the use cases 1) and 2), ncc is obviously an optimization. The main reason there is speed.
    For 3) - we need some system to package things.

    So what is now on the horizon?

    1. We could work with the ncc team to have a proper solution not including the eval hacks.
    2. Parcel released TS bundling support Add support for generating TypeScript declarations, and named pipelines parcel-bundler/parcel#3613

    We will look into both options and I 100% agree, that we need to get rid of the eval statements.

    Now back to your question, if the

    const binaryPath = function() {
      return path.join(__dirname, '../')
    }

    Would work with ncc, the answer is no. You can simply try it out by running ncc over a test file with ncc build: https://github.com/zeit/ncc

    Or to see the whole thing running, you can use https://github.com/prisma/prisma2-development-environment

    Then you go into prisma2/cli/prisma2, do yarn build and yarn download to see if it works. The problem that eval solves, is that ncc tries to be smart, and everything that includes path.join(__dirname, ...) or path.resolve(__dirname, ...) will be rewritten by it.

    This is the tracking issue regarding eval and ncc vercel/ncc#390

  3. self-assigned this
    on Nov 16, 2019
  4. LongLiveCHIEF commented on Nov 16, 2019

    @LongLiveCHIEF
    Author

    Thanks for the response, that helps a bunch. I'm hoping I can become a contributor to the prisma2 ecosystem, so this kind of background summary is much appreciated!

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

Labels

No labels
No labels

Type

No type

Projects

No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions