Skip to content

Commit

Permalink
fix(js): normalize paths correctly when creating temporary tsconfig f…
Browse files Browse the repository at this point in the history
…ile for incremental builds
  • Loading branch information
leosvelperez committed Nov 29, 2024
1 parent ec5a5e6 commit d0c542d
Show file tree
Hide file tree
Showing 2 changed files with 64 additions and 12 deletions.
48 changes: 47 additions & 1 deletion packages/js/src/utils/buildable-libs-utils.spec.ts
Original file line number Diff line number Diff line change
@@ -1,10 +1,14 @@
import { DependencyType, ProjectGraph, TaskGraph } from '@nx/devkit';
import { TempFs } from '@nx/devkit/internal-testing-utils';
import { readFileSync } from 'fs';
import {
calculateProjectDependencies,
calculateDependenciesFromTaskGraph,
calculateProjectDependencies,
createTmpTsConfig,
DependentBuildableProjectNode,
updatePaths,
} from './buildable-libs-utils';
import { join } from 'path';

describe('updatePaths', () => {
const deps: DependentBuildableProjectNode[] = [
Expand Down Expand Up @@ -768,3 +772,45 @@ describe('missingDependencies', () => {
).toThrow();
});
});

describe('createTmpTsConfig', () => {
it('should create a temporary tsconfig file extending the provided tsconfig', () => {
const fs = new TempFs('buildable-libs-utils#createTmpTsConfig');
const tsconfigPath = 'packages/foo/tsconfig.json';
fs.createFileSync(tsconfigPath, '{}');

const tmpTsConfigPath = createTmpTsConfig(
tsconfigPath,
fs.tempDir,
'packages/foo',
[]
);

const tmpTsConfig = readFileSync(tmpTsConfigPath, 'utf8');
// would be generated at <workspaceRoot>/tmp/packages/foo/build/tsconfig.generated.json
// while the extended tsconfig path is <workspaceRoot>/packages/foo/tsconfig.json
expect(JSON.parse(tmpTsConfig).extends).toBe(
'../../../../packages/foo/tsconfig.json'
);
});

it('should also work when the provided tsconfig is an absolute path', () => {
const fs = new TempFs('buildable-libs-utils#createTmpTsConfig');
const tsconfigPath = join(fs.tempDir, 'packages/foo/tsconfig.json');
fs.createFileSync(tsconfigPath, '{}');

const tmpTsConfigPath = createTmpTsConfig(
tsconfigPath,
fs.tempDir,
'packages/foo',
[]
);

const tmpTsConfig = readFileSync(tmpTsConfigPath, 'utf8');
// would be generated at <workspaceRoot>/tmp/packages/foo/build/tsconfig.generated.json
// while the extended tsconfig path is <workspaceRoot>/packages/foo/tsconfig.json
expect(JSON.parse(tmpTsConfig).extends).toBe(
'../../../../packages/foo/tsconfig.json'
);
});
});
28 changes: 17 additions & 11 deletions packages/js/src/utils/buildable-libs-utils.ts
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@ import { unlinkSync } from 'fs';
import { isNpmProject } from 'nx/src/project-graph/operators';
import { directoryExists, fileExists } from 'nx/src/utils/fileutils';
import { output } from 'nx/src/utils/output';
import { dirname, join, relative, isAbsolute, extname } from 'path';
import { dirname, join, relative, extname, resolve } from 'path';
import type * as ts from 'typescript';
import { readTsConfigPaths } from './typescript/ts-config';

Expand Down Expand Up @@ -194,18 +194,23 @@ function collectDependencies(
}

function readTsConfigWithRemappedPaths(
tsConfig: string,
generatedTsConfigPath: string,
dependencies: DependentBuildableProjectNode[]
originalTsconfigPath: string,
generatedTsconfigPath: string,
dependencies: DependentBuildableProjectNode[],
workspaceRoot: string
) {
const generatedTsConfig: any = { compilerOptions: {} };
const dirnameTsConfig = dirname(generatedTsConfigPath);
const relativeTsconfig = isAbsolute(dirnameTsConfig)
? relative(workspaceRoot, dirnameTsConfig)
: dirnameTsConfig;
generatedTsConfig.extends = relative(relativeTsconfig, tsConfig);
const normalizedTsConfig = resolve(workspaceRoot, originalTsconfigPath);
const normalizedGeneratedTsConfigDir = resolve(
workspaceRoot,
dirname(generatedTsconfigPath)
);
generatedTsConfig.extends = relative(
normalizedGeneratedTsConfigDir,
normalizedTsConfig
);
generatedTsConfig.compilerOptions.paths = computeCompilerOptionsPaths(
tsConfig,
originalTsconfigPath,
dependencies
);

Expand Down Expand Up @@ -443,7 +448,8 @@ export function createTmpTsConfig(
const parsedTSConfig = readTsConfigWithRemappedPaths(
tsconfigPath,
tmpTsConfigPath,
dependencies
dependencies,
workspaceRoot
);
process.on('exit', () => cleanupTmpTsConfigFile(tmpTsConfigPath));

Expand Down

0 comments on commit d0c542d

Please sign in to comment.