nrwl / nx

Smart Monorepos · Fast CI
https://nx.dev
MIT License
23.28k stars 2.32k forks source link

Build fails with @nx/webpack:webpack executor when setting generatePackageJson to true #22040

Closed martialanouman closed 4 months ago

martialanouman commented 6 months ago

Current Behavior

An error is raised when trying to build app with @nx/webpack:webpack executor while the generatePackageJson option is set to true

Expected Behavior

Successful build

GitHub Repo

No response

Steps to Reproduce

  1. Create a workspace
  2. Generate a nestjs app
  3. Update build task executor in app project.json: set build task executor to @nx/webpack:webpack and set options accordingly
  4. Run nx build <app_name>

Nx Report

NX   Report complete - copy this into the issue template

Node   : 20.11.1
OS     : darwin-x64
pnpm   : 8.15.3

nx                 : 18.0.5
@nx/js             : 18.0.5
@nx/jest           : 18.0.5
@nx/linter         : 18.0.5
@nx/eslint         : 18.0.5
@nx/workspace      : 18.0.5
@nx/devkit         : 18.0.5
@nx/eslint-plugin  : 18.0.5
@nx/nest           : 18.0.5
@nx/node           : 18.0.5
@nx/playwright     : 18.0.5
@nx/react          : 18.0.5
@nx/remix          : 18.0.5
@nrwl/tao          : 18.0.5
@nx/vite           : 18.0.5
@nx/web            : 18.0.5
@nx/webpack        : 18.0.5
typescript         : 5.3.3
---------------------------------------
Community plugins:
@nx-tools/nx-container : 5.2.0

Failure Logs

NX   The "path" argument must be of type string or an instance of Buffer or URL. Received an instance of Object

Pass --verbose to see the stacktrace.

Package Manager Version

pnpm@8.15.3

Operating System

Additional Information

// webpack.config.js

const { NxWebpackPlugin } = require('@nx/webpack')
const { join } = require('path')

module.exports = {
    output: {
        path: join(__dirname, '../../dist/apps/gateway'),
    },
    plugins: [
        new NxWebpackPlugin({
            target: 'node',
            compiler: 'tsc',
            main: './src/main.ts',
            tsConfig: './tsconfig.app.json',
            assets: ['./src/assets'],
            optimization: false,
            outputHashing: 'none',
            generatePackageJson: true,
        }),
    ],
}

Target config in project.json

"build": {
   "executor": "@nx/webpack:webpack",
   "outputs": ["{options.outputPath}"],
   "options": {
     "webpackConfig": "apps/gateway/webpack.config.js",
     "outputPath": "dist/apps/gateway"
   }
 }
martialanouman commented 6 months ago

Hi, Any update on this ?

SirPhemmiey commented 6 months ago

Hello, any update on this, please?

dan-cooke commented 6 months ago

I cannot reproduce this, I am using generate package successfully in my project. Check you guys are setting out path correctly

SirPhemmiey commented 6 months ago

@dan-cooke can you provide the github project for us to compare? and what do you mean by "setting out path correctly">

dan-cooke commented 6 months ago

@SirPhemmiey i cannot provide the repo as it’s private.

But I had previously experienced this error message because I was not setting the webpack plugins outputPath in my project json

I can give you an example of a working config later

SirPhemmiey commented 6 months ago

@dan-cooke i would highly appreciate it, thank you.

martialanouman commented 5 months ago

Hi @dan-cooke , could you provide the configuration example you mentioned in your last comment?

dan-cooke commented 5 months ago
{
  "name": "api",
  "$schema": "../../node_modules/nx/schemas/project-schema.json",
  "sourceRoot": "apps/api/src",
  "projectType": "application",
  "targets": {
    "build": {
      "executor": "@nx/webpack:webpack",
      "options": {
        "webpackConfig": "apps/api/webpack.config.js",
        "outputPath": "dist/apps/api"
      }
    }
}
const { NxWebpackPlugin } = require('@nx/webpack');
const { join } = require('path');

module.exports = {
  output: {
    path: join(__dirname, '../../dist/apps/api'),
  },
  plugins: [
    new NxWebpackPlugin({
      target: 'node',
      compiler: 'tsc',
      main: './src/main.ts',
      tsConfig: './tsconfig.app.json',
      generatePackageJson: true,
      transformers: [
        {
          name: '@nestjs/swagger/plugin',
          options: {
            introspectComments: true,
          },
        },
      ],
      assets: ['./src/assets'],
      optimization: false,
      outputHashing: 'none',
    }),
  ],
};

Works for me

I can post a min repro someday soon, but have you made sure you are not running with the nx cache? I always run my builds with --skipNxCache because i've found it to be stale most of the time

Edit: Just realised im missing outputPath, I added that in and it still works

Node   : 20.9.0
OS     : linux-x64
yarn   : 1.22.22

nx (global)        : 18.0.8
nx                 : 18.0.6
@nx/js             : 18.0.6
@nx/jest           : 18.0.6
@nx/linter         : 18.0.6
@nx/eslint         : 18.0.6
@nx/workspace      : 18.0.6
@nx/cypress        : 18.0.6
@nx/devkit         : 18.0.6
@nx/esbuild        : 18.0.6
@nx/eslint-plugin  : 18.0.6
@nx/nest           : 18.0.6
@nx/node           : 18.0.6
@nx/react          : 18.0.6
@nrwl/tao          : 18.0.6
@nx/vite           : 18.0.6
@nx/web            : 18.0.6
@nx/webpack        : 18.0.6
typescript         : 5.3.3
---------------------------------------
Community plugins:
@nx-extend/shadcn-ui : 1.0.0
martialanouman commented 5 months ago

Hi @dan-cooke, Thanks. Testing it...

pmwisdom commented 4 months ago

I'm not sure if this helps but I ran into this issue with an application that was using TSC that was importing a SWC library with a custom executor I wrote to generate the application's package json since it couldn't use esbuild or webpack.

My executor looked like this

   // The call to getHelperDependenciesFromProjectGraph was failing
    const helperDependencies = getHelperDependenciesFromProjectGraph(
        context.root,
        context.projectName!,
        context.projectGraph!
    );

    const packageJson = createPackageJson(context.projectName!, context.projectGraph!, {
        target: context.targetName,
        root: context.root,
        isProduction: true,
        helperDependencies: helperDependencies.map((dep) => dep.target),
    });

Removing the function call getHelperDependenciesFromProjectGraph and passing in an empty array for helperDependencies alleviated the issue for that application

Coly010 commented 4 months ago

This issue has been resolved and therefore I'm going to close it.

Thanks everyone for weighing in!

github-actions[bot] commented 3 months ago

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.