Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

An error occured while creating pruned lockfile due to an issue with getParentPackage function #15835

Closed
joshuawwright opened this issue Mar 22, 2023 · 9 comments
Assignees
Labels

Comments

@joshuawwright
Copy link

joshuawwright commented Mar 22, 2023

Current Behavior

I am currently serving a NestJS application using the command nx serve api. However, I encounter an error message stating, "An error occurred while creating the pruned lockfile." Despite this error, the application compiles and runs successfully, and it also generates package.json and package-lock.json files in the dist directory. As a result, it is unclear what impact this error has on the process.

The error seems to originate from the getPackageParent function, which is utilized for lock-file generation. To provide a clearer understanding of the current issue, I will outline how this function operates:

  1. The function takes in a path (a string containing a file path or a package path) and a packages object containing package information.
  2. It begins by splitting the path using the delimiter /node_modules/; the split() function returns an array of strings.
  3. The first and last elements of the array are removed using slice(1, -1).
  4. If no elements remain in the array, the package is deemed parentless, and the packages object is returned.
  5. If elements persist in the array, the function attempts to identify the parent package. It retrieves the first element of the array using segments.shift() and searches for the package in the packages object.
  6. Should the package lack a dependencies property, an empty object is created for its dependencies.
  7. The function iterates through the remaining elements in the segments array. For each segment, it locates the corresponding dependency in the parent package and designates it as the new parent package.
  8. If the new parent package is missing a dependencies property, an empty object is created for its dependencies.
  9. Once no elements remain in the segments array, the function has located the parent package and returns the parent package's dependencies object.

At present, the function encounters a failure when consecutive segments return a parent with an undefined dependencies key, leading to the "undefined" error displayed in the failure log section. In the steps to reproduce, I have included a unit test that demonstrates this issue.

Expected Behavior

I would expect no error message.

GitHub Repo

See steps to reproduce below:

Steps to Reproduce

Here is a unit test to demonstrate the issue:

describe('get-package-parent', () => {
    it('should get parent package', () => {
        const fn = () => getPackageParent(PATH, PACKAGES);

        expect(fn).not.toThrow();
    });
});

function getPackageParent(path: any, packages: any) {

    const segments = path.split(/\/?node_modules\//).slice(1, -1);

    if (!segments.length) {
        return packages;
    }

    let parent = packages[segments.shift()];

    if (!parent.dependencies) {
        parent.dependencies = {};
    }

    while (segments.length) {
        parent = parent.dependencies[segments.shift()];

        if (!parent?.dependencies) {
            parent.dependencies = {};
        }
    }

    return parent.dependencies;
}

const PATH = 'node_modules/mailparser/node_modules/html-to-text/node_modules/htmlparser2/node_modules/domhandler';

const PACKAGES = {
    mailparser: {
        version: '3.6.4',
        resolved: 'https://registry.npmjs.org/mailparser/-/mailparser-3.6.4.tgz',
        integrity: 'sha512-4bDgbLdlcBKX8jtVskfn/G93nZo3lf7pyuLbAQ031SHQLihEqxtRwHrb9SXMTqiTkEGlOdpDrZE5uH18O+2A+A==',
        requires: {
            'encoding-japanese': '2.0.0',
            he: '1.2.0',
            'html-to-text': '9.0.4',
            'iconv-lite': '0.6.3',
            libmime: '5.2.1',
            'linkify-it': '4.0.1',
            mailsplit: '5.4.0',
            nodemailer: '6.9.1',
            tlds: '1.236.0',
        },
        dependencies: {
            '@selderee/plugin-htmlparser2': [Object],
            'dom-serializer': [Object],
            domhandler: [Object],
            domutils: [Object],
            entities: [Object],
            'html-to-text': [Object],
        },
    },
    'html-to-text': {
        version: '8.2.1',
        resolved: 'https://registry.npmjs.org/html-to-text/-/html-to-text-8.2.1.tgz',
        integrity: 'sha512-aN/3JvAk8qFsWVeE9InWAWueLXrbkoVZy0TkzaGhoRBC2gCFEeRLDDJN3/ijIGHohy6H+SZzUQWN/hcYtaPK8w==',
        requires: {
            '@selderee/plugin-htmlparser2': '^0.6.0',
            deepmerge: '^4.2.2',
            he: '^1.2.0',
            htmlparser2: '^6.1.0',
            minimist: '^1.2.6',
            selderee: '^0.6.0',
        },
    },
    htmlparser2: {
        version: '6.1.0',
        resolved: 'https://registry.npmjs.org/htmlparser2/-/htmlparser2-6.1.0.tgz',
        integrity: 'sha512-gyyPk6rgonLFEDGoeRgQNaEUvdJ4ktTmmUh/h2t7s+M8oPpIPxgNACWa+6ESR57kXstwqPiCut0V8NRpcwgU7A==',
        requires: {
            domelementtype: '^2.0.1',
            domhandler: '^4.0.0',
            domutils: '^2.5.2',
            entities: '^2.0.0',
        },
    },
};

Nx Report

>  NX   Ran target test for project shared-api-util-wrappers (6s)
 
         With additional flags:
           --silent=false
 
    ✖    1/1 failed
    ✔    0/1 succeeded [0 read from cache]
 
josh@mli-GL553VW:~/WebStormProjects/mcclelland-labs$ nx report

 >  NX   Report complete - copy this into the issue template

   Node : 16.19.0
   OS   : linux x64
   npm  : 8.19.3
   
   nx                      : 15.8.7
   @nrwl/js                : 15.8.7
   @nrwl/jest              : 15.8.7
   @nrwl/linter            : 15.8.7
   @nrwl/workspace         : 15.8.7
   @nrwl/angular           : 15.8.7
   @nrwl/cli               : 15.8.7
   @nrwl/cypress           : 15.8.7
   @nrwl/devkit            : 15.8.7
   @nrwl/eslint-plugin-nx  : 15.8.7
   @nrwl/nest              : 15.8.7
   @nrwl/node              : 15.8.7
   @nrwl/nx-plugin         : 15.8.7
   @nrwl/tao               : 15.8.7
   @nrwl/webpack           : 15.8.7
   typescript              : 4.9.5
   ---------------------------------------
   Local workspace plugins:
         @mcclelland-labs/create-manifest-file
         @mcclelland-labs/make-certificate

Failure Logs

>  NX   An error occured while creating pruned lockfile

   Please open an issue at `https://github.com/nrwl/nx/issues/new?template=1-bug.yml` and provide a reproduction.
   To prevent the build from breaking we are returning the root lock file.
   If you run `npm install --package-lock-only` in your output folder it will regenerate the correct pruned lockfile.

Original error: Cannot set properties of undefined (setting 'dependencies')


   TypeError: Cannot set properties of undefined (setting 'dependencies')
    at getPackageParent (/home/josh/WebStormProjects/mcclelland-labs/node_modules/nx/src/lock-file/npm-parser.js:252:33)
    at /home/josh/WebStormProjects/mcclelland-labs/node_modules/nx/src/lock-file/npm-parser.js:231:9
    at Array.forEach (<anonymous>)
    at mapV1Snapshots (/home/josh/WebStormProjects/mcclelland-labs/node_modules/nx/src/lock-file/npm-parser.js:230:20)
    at stringifyNpmLockfile (/home/josh/WebStormProjects/mcclelland-labs/node_modules/nx/src/lock-file/npm-parser.js:212:31)
    at createLockFile (/home/josh/WebStormProjects/mcclelland-labs/node_modules/nx/src/lock-file/lock-file.js:136:58)
    at /home/josh/WebStormProjects/mcclelland-labs/node_modules/@nrwl/webpack/src/plugins/generate-package-json-plugin.js:42:131
    at fn (/home/josh/WebStormProjects/mcclelland-labs/node_modules/webpack/lib/Compilation.js:478:10)
    at _next0 (eval at create (/home/josh/WebStormProjects/mcclelland-labs/node_modules/tapable/lib/HookCodeFactory.js:33:10), <anonymous>:21:1)
    at eval (eval at create (/home/josh/WebStormProjects/mcclelland-labs/node_modules/tapable/lib/HookCodeFactory.js:33:10), <anonymous>:34:1)

Additional Information

No response

@joshuawwright joshuawwright changed the title An error occured while creating pruned lockfile An error occured while creating pruned lockfile due to an issue withh getParentPackage function Mar 22, 2023
@joshuawwright joshuawwright changed the title An error occured while creating pruned lockfile due to an issue withh getParentPackage function An error occured while creating pruned lockfile due to an issue with getParentPackage function Mar 22, 2023
@natrixofficial
Copy link

Hi, we are facing with the same issue. But we use AWS SDK and createPackageJson is turned on webpack builder.
Because the root package lock is copied to the build directory, our CI pipline is failing!

@orgaizer
Copy link

We are facing the same issue

@JumBay
Copy link

JumBay commented Mar 30, 2023

Same here

@meeroslav
Copy link
Contributor

This issue is fixed by #15833 and will be rolled out in the upcoming 15.9.0

@JumBay
Copy link

JumBay commented Mar 31, 2023

I still have it on 15.9.1:

 >  NX   An error occured while creating pruned lockfile
   Please open an issue at `https://github.com/nrwl/nx/issues/new?template=1-bug.yml` and provide a reproduction.
   To prevent the build from breaking we are returning the root lock file.

Original error: Cannot read properties of undefined (reading 'data')
   TypeError: Cannot read properties of undefined (reading 'data')
    at findNodeMatchingVersion (/myproject/node_modules/.pnpm/[email protected]_@[email protected]_@[email protected]/node_modules/nx/src/plugins/js/lock-file/project-graph-pruning.js:55:75)
    at /myproject/node_modules/.pnpm/[email protected]_@[email protected]_@[email protected]/node_modules/nx/src/plugins/js/lock-file/project-graph-pruning.js:35:22
    at Array.forEach (<anonymous>)
    at normalizeDependencies (/myproject/node_modules/.pnpm/[email protected]_@[email protected]_@[email protected]/node_modules/nx/src/plugins/js/lock-file/project-graph-pruning.js:26:42)
    at pruneProjectGraph (/myproject/node_modules/.pnpm/[email protected]_@[email protected]_@[email protected]/node_modules/nx/src/plugins/js/lock-file/project-graph-pruning.js:13:34)
    at createLockFile (/myproject/node_modules/.pnpm/[email protected]_@[email protected]_@[email protected]/node_modules/nx/src/plugins/js/lock-file/lock-file.js:137:79)
    at /myproject/node_modules/.pnpm/@[email protected]_@[email protected]_@[email protected]_@[email protected]_@types+node_okv4irf3a47yr4t4ihkpcphlx4/node_modules/@nrwl/webpack/src/plugins/generate-package-json-plugin.js:43:131
    at fn (/myproject/node_modules/.pnpm/[email protected]_@[email protected][email protected]/node_modules/webpack/lib/Compilation.js:478:10)
    at Hook.eval [as callAsync] (eval at create (/myproject/node_modules/.pnpm/[email protected]/node_modules/tapable/lib/HookCodeFactory.js:33:10), <anonymous>:20:1)
    at Hook.CALL_ASYNC_DELEGATE [as _callAsync] (/myproject/node_modules/.pnpm/[email protected]/node_modules/tapable/lib/Hook.js:18:14)
$ nx report

 >  NX   Report complete - copy this into the issue template

   Node : 18.12.1
   OS   : darwin x64
   pnpm : 8.1.0

   nx                      : 15.9.1
   @nrwl/js                : 15.9.1
   @nrwl/jest              : 15.9.1
   @nrwl/linter            : 15.9.1
   @nrwl/workspace         : 15.9.1
   @nrwl/angular           : 15.9.1
   @nrwl/cli               : 15.9.1
   @nrwl/cypress           : 15.9.1
   @nrwl/devkit            : 15.9.1
   @nrwl/eslint-plugin-nx  : 15.9.1
   @nrwl/next              : 15.9.1
   @nrwl/node              : 15.9.1
   @nrwl/nx-plugin         : 15.9.1
   @nrwl/react             : 15.9.1
   @nrwl/tao               : 15.9.1
   @nrwl/web               : 15.9.1
   @nrwl/webpack           : 15.9.1
   typescript              : 4.9.5
   ---------------------------------------
   Community plugins:
   @fortawesome/angular-fontawesome : 0.12.1
   @nguniversal/express-engine      : 15.2.0
   ng2-charts                       : 4.1.1
   @nguniversal/builders            : 15.2.0

@meeroslav
Copy link
Contributor

@JumBay can you open another issue and provide repo where this can be reproduced?

@JumBay
Copy link

JumBay commented Apr 13, 2023

I think this one #16097 is the same issue

@LPCmedia
Copy link

LPCmedia commented May 11, 2023

still hitting this in 16.1.4

in my case its :

Original error: Pruned lock file creation failed. The following package was not found in the root lock file: @nestjs/[email protected] ...but I have @nestjs/[email protected] installed.

@github-actions
Copy link

This issue has been closed for more than 30 days. If this issue is still occuring, please open a new issue with more recent context.

@github-actions github-actions bot locked as resolved and limited conversation to collaborators Jun 11, 2023
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
Projects
None yet
Development

No branches or pull requests

6 participants