Skip to content

Commit ce185bf

Browse files
[9.3] fix stray .d.ts files from type_check in git worktrees (#269960) (#269998)
# Backport This will backport the following commits from `main` to `9.3`: - [fix stray .d.ts files from type_check in git worktrees (#269960)](#269960) <!--- Backport version: 9.6.6 --> ### Questions ? Please refer to the [Backport tool documentation](https://github.com/sorenlouv/backport) <!--BACKPORT [{"author":{"name":"Tyler Smalley","email":"tyler.smalley@elastic.co"},"sourceCommit":{"committedDate":"2026-05-19T15:54:02Z","message":"fix stray .d.ts files from type_check in git worktrees (#269960)\n\nWorktrees share `node_modules `with the primary checkout, so\n`@kbn/repo-info` always resolved `REPO_ROOT` to the primary tree. This\ncaused tsc to emit target/types artifacts there instead of the active\nworktree.\n\nFix: prefer `process.cwd()` when it resolves to a different kibana root\nthan `__dirname`. Also throw a clear error when `--project` matches no\nknown project rather than silently falling back to checking every\nproject.\n\nCloses #269739","sha":"05ba1523a7940a890f002387797bfbb0e66a2369","branchLabelMapping":{"^v9.5.0$":"main","^v(\\d+).(\\d+).\\d+$":"$1.$2"}},"sourcePullRequest":{"labels":["release_note:skip","backport:all-open","v9.5.0"],"title":"fix stray .d.ts files from type_check in git worktrees","number":269960,"url":"https://github.com/elastic/kibana/pull/269960","mergeCommit":{"message":"fix stray .d.ts files from type_check in git worktrees (#269960)\n\nWorktrees share `node_modules `with the primary checkout, so\n`@kbn/repo-info` always resolved `REPO_ROOT` to the primary tree. This\ncaused tsc to emit target/types artifacts there instead of the active\nworktree.\n\nFix: prefer `process.cwd()` when it resolves to a different kibana root\nthan `__dirname`. Also throw a clear error when `--project` matches no\nknown project rather than silently falling back to checking every\nproject.\n\nCloses #269739","sha":"05ba1523a7940a890f002387797bfbb0e66a2369"}},"sourceBranch":"main","suggestedTargetBranches":[],"targetPullRequestStates":[{"branch":"main","label":"v9.5.0","branchLabelMappingKey":"^v9.5.0$","isSourceBranch":true,"state":"MERGED","url":"https://github.com/elastic/kibana/pull/269960","number":269960,"mergeCommit":{"message":"fix stray .d.ts files from type_check in git worktrees (#269960)\n\nWorktrees share `node_modules `with the primary checkout, so\n`@kbn/repo-info` always resolved `REPO_ROOT` to the primary tree. This\ncaused tsc to emit target/types artifacts there instead of the active\nworktree.\n\nFix: prefer `process.cwd()` when it resolves to a different kibana root\nthan `__dirname`. Also throw a clear error when `--project` matches no\nknown project rather than silently falling back to checking every\nproject.\n\nCloses #269739","sha":"05ba1523a7940a890f002387797bfbb0e66a2369"}}]}] BACKPORT--> Co-authored-by: Tyler Smalley <tyler.smalley@elastic.co>
1 parent 065f5dd commit ce185bf

2 files changed

Lines changed: 29 additions & 6 deletions

File tree

packages/kbn-ts-type-check-cli/run_type_check_legacy_cli.ts

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -63,6 +63,10 @@ export const runLegacyTypeCheckCli = () => {
6363
!project.isTypeCheckDisabled() && (!projectFilter || project.path === projectFilter)
6464
);
6565

66+
if (projectFilter && projects.length === 0) {
67+
throw createFailError(`Could not find a TypeScript project at '${projectFilter}'.`);
68+
}
69+
6670
const createdConfigs = await createTypeCheckConfigs(log, projects, TS_PROJECTS);
6771
let tscFailed = false;
6872
try {

src/platform/packages/shared/kbn-repo-info/index.js

Lines changed: 25 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -31,11 +31,11 @@ const readKibanaPkgJson = (path) => {
3131
}
3232
};
3333

34-
const findKibanaPackageJson = () => {
35-
// search for the kibana directory, since this file is moved around it might
36-
// not be where we think but should always be a relatively close parent
37-
// of this directory
38-
const startDir = __dirname;
34+
/**
35+
* @param {string} startDir
36+
* @returns {{ kibanaDir: string, kibanaPkgJson: KibanaPackageJson } | undefined}
37+
*/
38+
const searchForKibanaPackageJson = (startDir) => {
3939
const { root: rootDir } = Path.parse(startDir);
4040
let cursor = startDir;
4141
while (true) {
@@ -53,12 +53,31 @@ const findKibanaPackageJson = () => {
5353

5454
const parent = Path.dirname(cursor);
5555
if (parent === rootDir) {
56-
throw new Error(`unable to find kibana directory from ${startDir}`);
56+
return undefined;
5757
}
5858
cursor = parent;
5959
}
6060
};
6161

62+
const findKibanaPackageJson = () => {
63+
// In a git worktree, node_modules are shared with the primary worktree, so __dirname
64+
// always resolves into the primary worktree's tree. Check process.cwd() first: if it
65+
// is inside a *different* kibana root, prefer that root so REPO_ROOT matches the active
66+
// working tree rather than the primary checkout.
67+
const cwdResult = searchForKibanaPackageJson(process.cwd());
68+
const dirnameResult = searchForKibanaPackageJson(__dirname);
69+
70+
if (!dirnameResult) {
71+
throw new Error(`unable to find kibana directory from ${__dirname}`);
72+
}
73+
74+
if (cwdResult && cwdResult.kibanaDir !== dirnameResult.kibanaDir) {
75+
return cwdResult;
76+
}
77+
78+
return dirnameResult;
79+
};
80+
6281
const { kibanaDir, kibanaPkgJson } = findKibanaPackageJson();
6382

6483
const REPO_ROOT = kibanaDir;

0 commit comments

Comments
 (0)