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

fix(js): infer outputs correctly when both rootDir and outDir are set for tsconfig #29531

Merged
merged 2 commits into from
Jan 7, 2025
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
114 changes: 114 additions & 0 deletions packages/js/src/plugins/typescript/plugin.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -1194,6 +1194,118 @@ describe(`Plugin: ${PLUGIN_NAME}`, () => {
`);
});

it('should include tsbuildinfo file when outDir and rootDir at both set', async () => {
await applyFilesToTempFsAndContext(tempFs, context, {
'libs/my-lib/tsconfig.json': JSON.stringify({
files: [],
references: [{ path: './tsconfig.lib.json' }],
}),
'libs/my-lib/tsconfig.lib.json': JSON.stringify({
compilerOptions: { outDir: 'out-tsc/my-lib', rootDir: 'src' },
files: ['src/main.ts'],
}),
'libs/my-lib/package.json': `{}`,
});

expect(
await invokeCreateNodesOnMatchingFiles(context, {
build: {
configName: 'tsconfig.lib.json',
},
})
).toMatchInlineSnapshot(`
{
"projects": {
"libs/my-lib": {
"projectType": "library",
"targets": {
"build": {
"cache": true,
"command": "tsc --build tsconfig.lib.json --pretty --verbose",
"dependsOn": [
"^build",
],
"inputs": [
"production",
"^production",
{
"externalDependencies": [
"typescript",
],
},
],
"metadata": {
"description": "Builds the project with \`tsc\`.",
"help": {
"command": "npx tsc --build --help",
"example": {
"args": [
"--force",
],
},
},
"technologies": [
"typescript",
],
},
"options": {
"cwd": "libs/my-lib",
},
"outputs": [
"{projectRoot}/out-tsc/my-lib",
"{projectRoot}/out-tsc/*.tsbuildinfo",
],
"syncGenerators": [
"@nx/js:typescript-sync",
],
},
"typecheck": {
"cache": true,
"command": "tsc --build --emitDeclarationOnly --pretty --verbose",
"dependsOn": [
"^typecheck",
],
"inputs": [
"production",
"^production",
{
"externalDependencies": [
"typescript",
],
},
],
"metadata": {
"description": "Runs type-checking for the project.",
"help": {
"command": "npx tsc --build --help",
"example": {
"args": [
"--force",
],
},
},
"technologies": [
"typescript",
],
},
"options": {
"cwd": "libs/my-lib",
},
"outputs": [
"{projectRoot}/out-tsc/my-lib",
"{projectRoot}/out-tsc/*.tsbuildinfo",
],
"syncGenerators": [
"@nx/js:typescript-sync",
],
},
},
},
},
}
`);
});

it('should add the inline output files when `outDir` is not defined', async () => {
await applyFilesToTempFsAndContext(tempFs, context, {
'libs/my-lib/tsconfig.json': JSON.stringify({
Expand Down Expand Up @@ -2771,6 +2883,7 @@ describe(`Plugin: ${PLUGIN_NAME}`, () => {
},
"outputs": [
"{workspaceRoot}/dist/libs/my-lib",
"{workspaceRoot}/dist/libs/*.tsbuildinfo",
],
"syncGenerators": [
"@nx/js:typescript-sync",
Expand Down Expand Up @@ -2933,6 +3046,7 @@ describe(`Plugin: ${PLUGIN_NAME}`, () => {
"{workspaceRoot}/dist/libs/my-lib/lib.d.ts.map",
"{workspaceRoot}/dist/libs/my-lib/lib.tsbuildinfo",
"{workspaceRoot}/dist/libs/my-lib/other",
"{workspaceRoot}/dist/libs/my-lib/*.tsbuildinfo",
],
"syncGenerators": [
"@nx/js:typescript-sync",
Expand Down
34 changes: 26 additions & 8 deletions packages/js/src/plugins/typescript/plugin.ts
Original file line number Diff line number Diff line change
Expand Up @@ -549,16 +549,34 @@ function getOutputs(
pathToInputOrOutput(config.options.outDir, workspaceRoot, projectRoot)
);

if (
config.options.tsBuildInfoFile &&
!normalize(config.options.tsBuildInfoFile).startsWith(
`${normalize(config.options.outDir)}${sep}`
)
) {
// https://www.typescriptlang.org/tsconfig#tsBuildInfoFile
if (config.options.tsBuildInfoFile) {
if (
!normalize(config.options.tsBuildInfoFile).startsWith(
`${normalize(config.options.outDir)}${sep}`
)
) {
// https://www.typescriptlang.org/tsconfig#tsBuildInfoFile
outputs.add(
pathToInputOrOutput(
config.options.tsBuildInfoFile,
workspaceRoot,
projectRoot
)
);
}
} else if (config.options.rootDir && config.options.rootDir !== '.') {
// If rootDir is set, then the tsbuildinfo file will be outside the outDir so we need to add it.
const relativeRootDir = relative(
config.options.rootDir,
join(workspaceRoot, projectRoot)
);
outputs.add(
pathToInputOrOutput(
config.options.tsBuildInfoFile,
joinPathFragments(
config.options.outDir,
relativeRootDir,
`*.tsbuildinfo`
),
workspaceRoot,
projectRoot
)
Expand Down
Loading