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

@nx/node webpack executor type checker does not honor buildLibsFromSource flag #30160

Open
1 of 4 tasks
JoshuaCWebDeveloper opened this issue Feb 24, 2025 · 1 comment
Open
1 of 4 tasks
Assignees
Labels
scope: bundlers Issues related to webpack, rollup type: bug

Comments

@JoshuaCWebDeveloper
Copy link

Current Behavior

A common usage of incremental builds is to be able to have different typescript settings between a library and the app that imports it. I've seen this request in a few places, and buildLibsFromSource: false is the most popular solution. However, the Webpack type checker does not take into account the buildLibsFromSource flag and instead type checks the source files of every imported lib even though Webpack correctly bundles the build artifacts of each library.

Expected Behavior

When buildLibsFromSource is false, Webpack should not type check the source files of imported libraries, so that libraries can use different tsconfig compiler options than the app that imports them.

GitHub Repo

https://github.com/JoshuaCWebDeveloper/debug-nx-incremental-build-typechecking

Steps to Reproduce

$ npx create-nx-workspace@latest
$ npx nx g @nx/js:library util-lib
  1. Update util-lib/src/lib/util-lib.ts:
export function utilLib(arg1): string {
  return arg1 ? arg1 : 'util-lib';
}
  1. Disable noImplicitAny in packages/util-lib/tsconfig.json:
"noImplicitAny": false
$ npx nx g @nx/node:application webpack-app --bundler webpack
  1. Add ajv@8 dependency to package.json and run npm install:
"ajv": "^8.12.0",
  1. Update webpack-app/src/main.ts:
import { utilLib } from '@debug-nx-incremental-build-typechecking/util-lib';

console.log('Hello World');
console.log(utilLib('Hello Lib'));
  1. Update webpack-app/tsconfig.json:
"strict": true
$ npx nx serve webpack-app

Nx Report

NX   Report complete - copy this into the issue template

Node           : 23.3.0
OS             : linux-x64
Native Target  : x86_64-linux
npm            : 10.9.0

nx                 : 20.4.6
@nx/js             : 20.4.6
@nx/jest           : 20.4.6
@nx/eslint         : 20.4.6
@nx/workspace      : 20.4.6
@nx/devkit         : 20.4.6
@nx/esbuild        : 20.4.6
@nx/eslint-plugin  : 20.4.6
@nx/node           : 20.4.6
@nx/web            : 20.4.6
@nx/webpack        : 20.4.6
typescript         : 5.7.3
---------------------------------------
Registered Plugins:
@nx/eslint/plugin
@nx/jest/plugin
@nx/webpack/plugin

Failure Logs

> nx run webpack-app:build

> webpack-cli build --node-env=production

chunk (runtime: main) main.js (main) 616 bytes [entry] [rendered]

ERROR in ../util-lib/src/lib/util-lib.ts:1:25
TS7006: Parameter 'arg1' implicitly has an 'any' type.
  > 1 | export function utilLib(arg1): string {
      |                         ^^^^
    2 |   return arg1 ? arg1 : 'util-lib';
    3 | }
    4 |

webpack compiled with 1 error (2ee899ebd804bfc9)
Warning: command "webpack-cli build --node-env=production" exited with non-zero status code

Package Manager Version

No response

Operating System

  • macOS
  • Linux
  • Windows
  • Other (Please specify)

Additional Information

This seems to be the same issue for the Vite executor: #21844.

I haven't looked into this deeply, so I don't know whether the implementation problems and solution discussed in that issue are relevant here or not.

@JoshuaCWebDeveloper
Copy link
Author

This issue can be worked around by defining a custom webpack plugin in your app's webpack.config.js file that instructs the ts checker plugin to ignore any errors originating from files outside the project root:

const { NxAppWebpackPlugin } = require("@nx/webpack/app-plugin");
const path = require("path");

class TypeCheckerHooksPlugin {
  constructor(options) {
    this.options = options;
  }

  apply(compiler) {
    const ForkTsCheckerWebpackPlugin = require("fork-ts-checker-webpack-plugin");
    if (!ForkTsCheckerWebpackPlugin) {
      return;
    }

    const logger = compiler.getInfrastructureLogger(
      TypeCheckerHooksPlugin.name,
    );
    const hooks = ForkTsCheckerWebpackPlugin.getCompilerHooks(compiler);

    logger.debug("hooks: ", hooks);

    hooks.issues.tap(TypeCheckerHooksPlugin.name, (issues) => {
      const projectRoot = path.join(
        this.options.root,
        this.options.projectRoot,
      );

      logger.debug("projectRoot: ", projectRoot);

      return issues.filter((issue) => {
        const relativePath = path.relative(projectRoot, issue.file);

        logger.debug("relativePath: ", relativePath);

        return (
          relativePath &&
          !relativePath.startsWith("..") &&
          !path.isAbsolute(relativePath)
        );
      });
    });
  }
}

const nxAppWebpackInstance = new NxAppWebpackPlugin({
  target: "node",
  compiler: "tsc",
  main: "./src/index.ts",
  tsConfig: "./tsconfig.app.json",
  optimization: false,
  outputHashing: "none",
  generatePackageJson: true,
  buildLibsFromSource: false,
});

module.exports = {
  output: {
    path: path.join(__dirname, "../../dist/packages/rest-api"),
  },
  plugins: [
    nxAppWebpackInstance,
    new TypeCheckerHooksPlugin(nxAppWebpackInstance.options),
  ],
};

@FrozenPandaz FrozenPandaz added the scope: bundlers Issues related to webpack, rollup label Feb 24, 2025
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
scope: bundlers Issues related to webpack, rollup type: bug
Projects
None yet
Development

No branches or pull requests

3 participants