Skip to content

Conversation

gribnoysup
Copy link
Collaborator

@gribnoysup gribnoysup commented Sep 25, 2025

Currently in Compass the default tsconfig for packages is the build tsconfig that excludes anything that is not the source files. This has a few downsides: IDE integrations pick it up by default and disable all the meaningful typescript intellisense stuff for those files, we also use this default file sometimes for typechecks, which means that some type issues in test files are missed at the moment.

This patch swaps the default and lint tsconfigs making it so that the default tsconfig is always the one that covers all the files in the package

There were a few packages where we either were missing a typecheck script completely or it was using the build config. Some of them are very broken, so to avoid making this change too big I'm switching the typecheck command to use the build config instead and adding a TODO in the code. I opened https://jira.mongodb.org/browse/COMPASS-9897 to follow-up and address those

Script I used to do the "migration"
import child_process from 'child_process';
import path from 'path';
import fs from 'fs';

/**
 * @type {{name: string, location: string}[]}
 */
const workspaces = JSON.parse(
  child_process.execSync('npx lerna list --json --all').toString()
);

const hasTypeScript = workspaces.filter((ws) => {
  return fs.existsSync(path.join(ws.location, 'tsconfig.json'));
});

for (const ws of hasTypeScript) {
  const files = fs.readdirSync(ws.location);
  const wsPath = (...args) => path.join(ws.location, ...args);
  const packageJson = JSON.parse(
    fs.readFileSync(wsPath('package.json'), 'utf8')
  );
  if (packageJson.scripts.compile?.includes('tsconfig.json')) {
    try {
      const { extends: buildExtends, ...buildConfig } = JSON.parse(
        fs.readFileSync(wsPath('tsconfig.json'), 'utf-8')
      );
      const { extends: lintExtends, ...lintConfig } = JSON.parse(
        fs.readFileSync(wsPath('tsconfig-lint.json'), 'utf-8')
      );
      fs.rmSync(wsPath('tsconfig.json'));
      fs.rmSync(wsPath('tsconfig-lint.json'));
      fs.writeFileSync(
        wsPath('tsconfig.json'),
        JSON.stringify(
          {
            extends: buildExtends,
            ...buildConfig,
            include: lintConfig.include,
            exclude: lintConfig.exclude,
          },
          null,
          2
        ) + '\n'
      );
      fs.writeFileSync(
        wsPath('tsconfig-build.json'),
        JSON.stringify(
          {
            extends: './tsconfig.json',
            ...lintConfig,
            include: buildConfig.include,
            exclude: buildConfig.exclude,
          },
          null,
          2
        ) + '\n'
      );
    } catch (e) {
      console.log(ws.name, e.message);
      throw e;
    }
    packageJson.scripts.compile = packageJson.scripts.compile.replaceAll(
      'tsconfig.json',
      'tsconfig-build.json'
    );
  }
  if (!packageJson.scripts.check.includes('typecheck')) {
    packageJson.scripts.check = `npm run typecheck && ${packageJson.scripts.check.trim()}`;
  }
  if (files.includes('.eslintrc.js')) {
    fs.writeFileSync(
      wsPath('.eslintrc.js'),
      fs
        .readFileSync(wsPath('.eslintrc.js'), 'utf-8')
        .replaceAll('tsconfig-lint', 'tsconfig')
    );
  }
  packageJson.scripts.typecheck = 'tsc -p tsconfig.json --noEmit';
  fs.writeFileSync(
    path.join(ws.location, 'package.json'),
    JSON.stringify(packageJson, null, 2) + '\n'
  );
}

@gribnoysup gribnoysup requested a review from a team as a code owner September 25, 2025 14:14
@gribnoysup gribnoysup added the no-title-validation Skips validation of PR titles (conventional commit adherence + JIRA ticket inclusion) label Sep 25, 2025
Copy link
Member

@Anemy Anemy left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Do we need to update the create-workspace script as well?

@gribnoysup
Copy link
Collaborator Author

@Anemy yesss, good catch, forgot about that, I'll open a follow up

@gribnoysup gribnoysup merged commit 20d740f into main Sep 26, 2025
122 of 131 checks passed
@gribnoysup gribnoysup deleted the swap-tsconfig-build-with-tsconfig-lint branch September 26, 2025 07:43
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

no-title-validation Skips validation of PR titles (conventional commit adherence + JIRA ticket inclusion)

Projects

None yet

Development

Successfully merging this pull request may close these issues.

3 participants