Skip to content

Commit 5c51d92

Browse files
jaysooleosvelperez
authored andcommitted
fix(js): infer outputs correctly when both rootDir and outDir are set for tsconfig (#29531)
When the tsconfig has `rootDir` and `outDir` both defined, then the `*.tsbuildinfo` file is not cached. This makes incremental tsc not work through distribution (e.g. agents). For example, given this `tsconfig.lib.json` file: ```json { "compilerOptions": { "outDir": "out-tsc/lib-1", "rootDir": "src" } } ``` The outputs (e.g. `*.d.ts` files) are under `{projectRoot}/out-tsc/lib-1`, but the tsbuild info file is under `{projectRoot}/out-tsc/tsconfig.lib.tsbuildinfo`. ## Current Behavior tsbuildinfo file is not cached ## Expected Behavior tsbuildinfo file is cached ## Related Issue(s) <!-- Please link the issue being fixed so it gets closed when this is merged. --> Fixes # --------- Co-authored-by: Leosvel Pérez Espinosa <[email protected]> (cherry picked from commit df27a97)
1 parent 08f3857 commit 5c51d92

File tree

2 files changed

+140
-8
lines changed

2 files changed

+140
-8
lines changed

packages/js/src/plugins/typescript/plugin.spec.ts

Lines changed: 114 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1194,6 +1194,118 @@ describe(`Plugin: ${PLUGIN_NAME}`, () => {
11941194
`);
11951195
});
11961196

1197+
it('should include tsbuildinfo file when outDir and rootDir at both set', async () => {
1198+
await applyFilesToTempFsAndContext(tempFs, context, {
1199+
'libs/my-lib/tsconfig.json': JSON.stringify({
1200+
files: [],
1201+
references: [{ path: './tsconfig.lib.json' }],
1202+
}),
1203+
'libs/my-lib/tsconfig.lib.json': JSON.stringify({
1204+
compilerOptions: { outDir: 'out-tsc/my-lib', rootDir: 'src' },
1205+
files: ['src/main.ts'],
1206+
}),
1207+
'libs/my-lib/package.json': `{}`,
1208+
});
1209+
1210+
expect(
1211+
await invokeCreateNodesOnMatchingFiles(context, {
1212+
build: {
1213+
configName: 'tsconfig.lib.json',
1214+
},
1215+
})
1216+
).toMatchInlineSnapshot(`
1217+
{
1218+
"projects": {
1219+
"libs/my-lib": {
1220+
"projectType": "library",
1221+
"targets": {
1222+
"build": {
1223+
"cache": true,
1224+
"command": "tsc --build tsconfig.lib.json --pretty --verbose",
1225+
"dependsOn": [
1226+
"^build",
1227+
],
1228+
"inputs": [
1229+
"production",
1230+
"^production",
1231+
{
1232+
"externalDependencies": [
1233+
"typescript",
1234+
],
1235+
},
1236+
],
1237+
"metadata": {
1238+
"description": "Builds the project with \`tsc\`.",
1239+
"help": {
1240+
"command": "npx tsc --build --help",
1241+
"example": {
1242+
"args": [
1243+
"--force",
1244+
],
1245+
},
1246+
},
1247+
"technologies": [
1248+
"typescript",
1249+
],
1250+
},
1251+
"options": {
1252+
"cwd": "libs/my-lib",
1253+
},
1254+
"outputs": [
1255+
"{projectRoot}/out-tsc/my-lib",
1256+
"{projectRoot}/out-tsc/*.tsbuildinfo",
1257+
],
1258+
"syncGenerators": [
1259+
"@nx/js:typescript-sync",
1260+
],
1261+
},
1262+
"typecheck": {
1263+
"cache": true,
1264+
"command": "tsc --build --emitDeclarationOnly --pretty --verbose",
1265+
"dependsOn": [
1266+
"^typecheck",
1267+
],
1268+
"inputs": [
1269+
"production",
1270+
"^production",
1271+
{
1272+
"externalDependencies": [
1273+
"typescript",
1274+
],
1275+
},
1276+
],
1277+
"metadata": {
1278+
"description": "Runs type-checking for the project.",
1279+
"help": {
1280+
"command": "npx tsc --build --help",
1281+
"example": {
1282+
"args": [
1283+
"--force",
1284+
],
1285+
},
1286+
},
1287+
"technologies": [
1288+
"typescript",
1289+
],
1290+
},
1291+
"options": {
1292+
"cwd": "libs/my-lib",
1293+
},
1294+
"outputs": [
1295+
"{projectRoot}/out-tsc/my-lib",
1296+
"{projectRoot}/out-tsc/*.tsbuildinfo",
1297+
],
1298+
"syncGenerators": [
1299+
"@nx/js:typescript-sync",
1300+
],
1301+
},
1302+
},
1303+
},
1304+
},
1305+
}
1306+
`);
1307+
});
1308+
11971309
it('should add the inline output files when `outDir` is not defined', async () => {
11981310
await applyFilesToTempFsAndContext(tempFs, context, {
11991311
'libs/my-lib/tsconfig.json': JSON.stringify({
@@ -2771,6 +2883,7 @@ describe(`Plugin: ${PLUGIN_NAME}`, () => {
27712883
},
27722884
"outputs": [
27732885
"{workspaceRoot}/dist/libs/my-lib",
2886+
"{workspaceRoot}/dist/libs/*.tsbuildinfo",
27742887
],
27752888
"syncGenerators": [
27762889
"@nx/js:typescript-sync",
@@ -2933,6 +3046,7 @@ describe(`Plugin: ${PLUGIN_NAME}`, () => {
29333046
"{workspaceRoot}/dist/libs/my-lib/lib.d.ts.map",
29343047
"{workspaceRoot}/dist/libs/my-lib/lib.tsbuildinfo",
29353048
"{workspaceRoot}/dist/libs/my-lib/other",
3049+
"{workspaceRoot}/dist/libs/my-lib/*.tsbuildinfo",
29363050
],
29373051
"syncGenerators": [
29383052
"@nx/js:typescript-sync",

packages/js/src/plugins/typescript/plugin.ts

Lines changed: 26 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -549,16 +549,34 @@ function getOutputs(
549549
pathToInputOrOutput(config.options.outDir, workspaceRoot, projectRoot)
550550
);
551551

552-
if (
553-
config.options.tsBuildInfoFile &&
554-
!normalize(config.options.tsBuildInfoFile).startsWith(
555-
`${normalize(config.options.outDir)}${sep}`
556-
)
557-
) {
558-
// https://www.typescriptlang.org/tsconfig#tsBuildInfoFile
552+
if (config.options.tsBuildInfoFile) {
553+
if (
554+
!normalize(config.options.tsBuildInfoFile).startsWith(
555+
`${normalize(config.options.outDir)}${sep}`
556+
)
557+
) {
558+
// https://www.typescriptlang.org/tsconfig#tsBuildInfoFile
559+
outputs.add(
560+
pathToInputOrOutput(
561+
config.options.tsBuildInfoFile,
562+
workspaceRoot,
563+
projectRoot
564+
)
565+
);
566+
}
567+
} else if (config.options.rootDir && config.options.rootDir !== '.') {
568+
// If rootDir is set, then the tsbuildinfo file will be outside the outDir so we need to add it.
569+
const relativeRootDir = relative(
570+
config.options.rootDir,
571+
join(workspaceRoot, projectRoot)
572+
);
559573
outputs.add(
560574
pathToInputOrOutput(
561-
config.options.tsBuildInfoFile,
575+
joinPathFragments(
576+
config.options.outDir,
577+
relativeRootDir,
578+
`*.tsbuildinfo`
579+
),
562580
workspaceRoot,
563581
projectRoot
564582
)

0 commit comments

Comments
 (0)