Skip to content

Commit 7a68bf4

Browse files
polygraph-snapshot-app[bot]leosvelperez
authored andcommitted
fix(bundling): include tsconfig solution input for webpack (#35477)
## Current Behavior The `@nx/webpack` executor, inferred plugin, and config builder all call `isUsingTsSolutionSetup()` and let its result influence task outputs (e.g. `useTsconfigPaths`). However, the root `tsconfig.json` is not part of the build task's cache inputs — so edits to root `tsconfig.json` (`extends`, `files`, `include`) don't invalidate webpack task caches and stale outputs are reused. The same gap exists in `@nx/node`'s webpack-bundler branch of the application generator: it calls `addBuildTargetDefaults(tree, '@nx/webpack:webpack')` without the tsconfig input, even though the parallel esbuild branch in the same file already passes `TS_SOLUTION_SETUP_TSCONFIG_INPUT`. ## Expected Behavior Matches the rollup fix in #35476: the root `tsconfig.json` is included as a structured input (`{ json: '{workspaceRoot}/tsconfig.json', fields: ['extends', 'files', 'include'] }`) on `@nx/webpack:webpack` task defaults and in the inferred plugin's build target inputs, so changes to the relevant fields invalidate caches. ### Changes - `packages/webpack/src/plugins/plugin.ts` — append `TS_SOLUTION_SETUP_TSCONFIG_INPUT` to the inferred build target's `inputs`. Also gate the targets cache on `NX_CACHE_PROJECT_GRAPH` (mirrors the rollup PR) and update the spec accordingly. - `packages/webpack/src/generators/configuration/configuration.ts` — pass `'build', [TS_SOLUTION_SETUP_TSCONFIG_INPUT]` to `addBuildTargetDefaults`. - `packages/node/src/generators/application/lib/create-project.ts` — same on the webpack branch (the esbuild branch already had it). - `packages/webpack/src/plugins/plugin.spec.ts` — mock spreads `requireActual` so the constant is real; sets/restores `NX_CACHE_PROJECT_GRAPH`; snapshot updated to include the new input. <!-- polygraph-session-start --> --- [View session information ↗](https://snapshot.app.trypolygraph.com/orgs/69cdc268b6aa527e4129c2b4/sessions/73d1eed2) <!-- polygraph-session-end --> Co-authored-by: Leosvel Pérez Espinosa <leosvel.perez.espinosa@gmail.com>
1 parent 8d5c818 commit 7a68bf4

4 files changed

Lines changed: 37 additions & 5 deletions

File tree

‎packages/node/src/generators/application/lib/create-project.ts‎

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -38,7 +38,9 @@ export function addProject(
3838
project.targets.build = getEsBuildConfig(tree, project, options);
3939
} else if (options.bundler === 'webpack') {
4040
if (!hasWebpackPlugin(tree) && options.addPlugin === false) {
41-
addBuildTargetDefaults(tree, `@nx/webpack:webpack`);
41+
addBuildTargetDefaults(tree, `@nx/webpack:webpack`, 'build', [
42+
TS_SOLUTION_SETUP_TSCONFIG_INPUT,
43+
]);
4244
project.targets.build = getWebpackBuildConfig(tree, project, options);
4345
} else if (options.isNest) {
4446
// If we are using Nest that has the webpack plugin we need to override the

‎packages/webpack/src/generators/configuration/configuration.ts‎

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,7 @@ import { ConfigurationGeneratorSchema } from './schema';
1616
import { WebpackExecutorOptions } from '../../executors/webpack/schema';
1717
import { hasPlugin } from '../../utils/has-plugin';
1818
import { addBuildTargetDefaults } from '@nx/devkit/src/generators/target-defaults-utils';
19+
import { TS_SOLUTION_SETUP_TSCONFIG_INPUT } from '@nx/js/src/utils/typescript/ts-solution-setup';
1920
import { ensureDependencies } from '../../utils/ensure-dependencies';
2021

2122
export function configurationGenerator(
@@ -179,7 +180,9 @@ module.exports = composePlugins(withNx(), (config) => {
179180
}
180181

181182
function addBuildTarget(tree: Tree, options: ConfigurationGeneratorSchema) {
182-
addBuildTargetDefaults(tree, '@nx/webpack:webpack');
183+
addBuildTargetDefaults(tree, '@nx/webpack:webpack', 'build', [
184+
TS_SOLUTION_SETUP_TSCONFIG_INPUT,
185+
]);
183186

184187
const project = readProjectConfiguration(tree, options.project);
185188
const buildOptions: WebpackExecutorOptions = {

‎packages/webpack/src/plugins/plugin.spec.ts‎

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,7 @@ jest.mock('@nx/devkit', () => ({
88

99
// Needed so the current environment is not used
1010
jest.mock('@nx/js/src/utils/typescript/ts-solution-setup', () => ({
11+
...jest.requireActual('@nx/js/src/utils/typescript/ts-solution-setup'),
1112
isUsingTsSolutionSetup: jest.fn(() => false),
1213
}));
1314

@@ -20,8 +21,10 @@ describe('@nx/webpack/plugin', () => {
2021
let createNodesFunction = createNodesV2[1];
2122
let context: CreateNodesContextV2;
2223
let tempFs: TempFs;
24+
let originalCacheProjectGraph = process.env.NX_CACHE_PROJECT_GRAPH;
2325

2426
beforeEach(() => {
27+
process.env.NX_CACHE_PROJECT_GRAPH = 'false';
2528
tempFs = new TempFs('webpack-plugin');
2629

2730
context = {
@@ -44,6 +47,11 @@ describe('@nx/webpack/plugin', () => {
4447

4548
afterEach(() => {
4649
jest.resetModules();
50+
if (originalCacheProjectGraph !== undefined) {
51+
process.env.NX_CACHE_PROJECT_GRAPH = originalCacheProjectGraph;
52+
} else {
53+
delete process.env.NX_CACHE_PROJECT_GRAPH;
54+
}
4755
});
4856

4957
it('should create nodes', async () => {
@@ -95,6 +103,14 @@ describe('@nx/webpack/plugin', () => {
95103
"webpack-cli",
96104
],
97105
},
106+
{
107+
"fields": [
108+
"extends",
109+
"files",
110+
"include",
111+
],
112+
"json": "{workspaceRoot}/tsconfig.json",
113+
},
98114
],
99115
"metadata": {
100116
"description": "Runs Webpack build",

‎packages/webpack/src/plugins/plugin.ts‎

Lines changed: 14 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -16,8 +16,11 @@ import {
1616
import { calculateHashForCreateNodes } from '@nx/devkit/src/utils/calculate-hash-for-create-nodes';
1717
import { getNamedInputs } from '@nx/devkit/src/utils/get-named-inputs';
1818
import { getLockFileName, getRootTsConfigPath } from '@nx/js';
19-
import { isUsingTsSolutionSetup } from '@nx/js/src/utils/typescript/ts-solution-setup';
20-
import { existsSync, readdirSync } from 'fs';
19+
import {
20+
isUsingTsSolutionSetup,
21+
TS_SOLUTION_SETUP_TSCONFIG_INPUT,
22+
} from '@nx/js/src/utils/typescript/ts-solution-setup';
23+
import { readdirSync } from 'fs';
2124
import { hashObject } from 'nx/src/hasher/file-hasher';
2225
import { workspaceDataDirectory } from 'nx/src/utils/cache-directory';
2326
import { dirname, isAbsolute, join, relative, resolve } from 'path';
@@ -37,7 +40,13 @@ export interface WebpackPluginOptions {
3740
type WebpackTargets = Pick<ProjectConfiguration, 'targets' | 'metadata'>;
3841

3942
function readTargetsCache(cachePath: string): Record<string, WebpackTargets> {
40-
return existsSync(cachePath) ? readJsonFile(cachePath) : {};
43+
try {
44+
return process.env.NX_CACHE_PROJECT_GRAPH !== 'false'
45+
? readJsonFile(cachePath)
46+
: {};
47+
} catch {
48+
return {};
49+
}
4150
}
4251

4352
function writeTargetsToCache(
@@ -181,13 +190,15 @@ async function createWebpackTargets(
181190
{
182191
externalDependencies: ['webpack-cli'],
183192
},
193+
TS_SOLUTION_SETUP_TSCONFIG_INPUT,
184194
]
185195
: [
186196
'default',
187197
'^default',
188198
{
189199
externalDependencies: ['webpack-cli'],
190200
},
201+
TS_SOLUTION_SETUP_TSCONFIG_INPUT,
191202
],
192203
outputs,
193204
metadata: {

0 commit comments

Comments
 (0)