Skip to content

Commit

Permalink
fix(js): handle potentially missing lockfile in @nx/js/typescript p…
Browse files Browse the repository at this point in the history
…lugin
  • Loading branch information
leosvelperez committed Feb 18, 2025
1 parent a99cee9 commit 6d9ad6b
Show file tree
Hide file tree
Showing 2 changed files with 22 additions and 11 deletions.
17 changes: 14 additions & 3 deletions packages/js/src/plugins/typescript/plugin.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@ describe(`Plugin: ${PLUGIN_NAME}`, () => {
let cwd = process.cwd();
let tempFs: TempFs;
let originalCacheProjectGraph: string | undefined;
let lockFileName: string;

beforeEach(() => {
mkdirSync('tmp/project-graph-cache', { recursive: true });
Expand All @@ -34,9 +35,7 @@ describe(`Plugin: ${PLUGIN_NAME}`, () => {
process.chdir(tempFs.tempDir);
originalCacheProjectGraph = process.env.NX_CACHE_PROJECT_GRAPH;
process.env.NX_CACHE_PROJECT_GRAPH = 'false';
const lockFileName = getLockFileName(
detectPackageManager(context.workspaceRoot)
);
lockFileName = getLockFileName(detectPackageManager(context.workspaceRoot));
applyFilesToTempFsAndContext(tempFs, context, { [lockFileName]: '' });
});

Expand All @@ -48,6 +47,18 @@ describe(`Plugin: ${PLUGIN_NAME}`, () => {
rmdirSync('tmp/project-graph-cache', { recursive: true });
});

it('should handle missing lock file', async () => {
await applyFilesToTempFsAndContext(tempFs, context, {
'libs/my-lib/tsconfig.json': JSON.stringify({ files: [] }),
'libs/my-lib/package.json': `{}`,
});
tempFs.removeFileSync(lockFileName);

await expect(
invokeCreateNodesOnMatchingFiles(context, {})
).resolves.not.toThrow();
});

it('should not create nodes for root tsconfig.json files', async () => {
await applyFilesToTempFsAndContext(tempFs, context, {
'package.json': `{}`,
Expand Down
16 changes: 8 additions & 8 deletions packages/js/src/plugins/typescript/plugin.ts
Original file line number Diff line number Diff line change
Expand Up @@ -133,12 +133,6 @@ export const createNodesV2: CreateNodesV2<TscPluginOptions> = [
initializeTsConfigCache(configFilePaths, context.workspaceRoot);

const normalizedOptions = normalizePluginOptions(options);
const lockFileHash = hashFile(
join(
context.workspaceRoot,
getLockFileName(detectPackageManager(context.workspaceRoot))
)
);

const {
configFilePaths: validConfigFilePaths,
Expand All @@ -147,7 +141,6 @@ export const createNodesV2: CreateNodesV2<TscPluginOptions> = [
} = await resolveValidConfigFilesAndHashes(
configFilePaths,
optionsHash,
lockFileHash,
context
);

Expand Down Expand Up @@ -234,13 +227,20 @@ export const createNodes: CreateNodes<TscPluginOptions> = [
async function resolveValidConfigFilesAndHashes(
configFilePaths: readonly string[],
optionsHash: string,
lockFileHash: string,
context: CreateNodesContext | CreateNodesContextV2
): Promise<{
configFilePaths: string[];
hashes: string[];
projectRoots: string[];
}> {
const lockFileHash =
hashFile(
join(
context.workspaceRoot,
getLockFileName(detectPackageManager(context.workspaceRoot))
)
) ?? '';

const validConfigFilePaths: string[] = [];
const hashes: string[] = [];
const projectRoots: string[] = [];
Expand Down

0 comments on commit 6d9ad6b

Please sign in to comment.